7#include <unordered_map>
39 template<
typename CollectionType>
41 : impl_(std::make_unique<CollectionModel<CollectionType>>(std::move(collection)))
87 std::vector<std::string>
find(
const std::function<
bool(
const Document&)>& predicate)
const;
131 template<
typename CollectionType>
134 auto derived =
dynamic_cast<CollectionModel<CollectionType>*
>(impl_.get());
137 throw std::bad_cast();
139 return derived->collection_;
150 template<
typename CollectionType>
151 const CollectionType&
as()
const
153 auto derived =
dynamic_cast<CollectionModel<CollectionType>*
>(impl_.get());
156 throw std::bad_cast();
158 return derived->collection_;
163 struct CollectionConcept
165 virtual ~CollectionConcept() =
default;
166 virtual Document& doc(
const std::string&
id) = 0;
167 virtual const Document& doc(
const std::string&
id)
const = 0;
168 virtual std::vector<std::string> find(
const std::function<
bool(
const Document&)>& predicate
170 virtual size_t size()
const = 0;
171 virtual std::string type()
const = 0;
172 virtual std::string
name()
const = 0;
174 virtual const Database& db()
const = 0;
177 template<
typename CollectionType>
178 struct CollectionModel : CollectionConcept
180 CollectionModel(CollectionType collection) : collection_(std::move(collection)) {}
182 Document& doc(
const std::string&
id)
override {
return collection_.doc(
id); }
184 const Document& doc(
const std::string&
id)
const override {
return collection_.doc(
id); }
186 std::vector<std::string> find(
const std::function<
bool(
const Document&)>& predicate
189 return collection_.find(predicate);
192 size_t size()
const override {
return collection_.size(); }
194 std::string type()
const override {
return collection_.type(); }
196 std::string
name()
const override {
return collection_.name(); }
198 Database& db()
override {
return collection_.db(); }
200 const Database& db()
const override {
return collection_.db(); }
202 CollectionType collection_;
205 std::unique_ptr<CollectionConcept> impl_;
214template<
typename DocumentType>
270 std::vector<std::string>
find(
const std::function<
bool(
const Document&)>& predicate)
const
272 std::vector<std::string> result;
273 for (
const auto& [key,
doc] :
docs_)
275 if (predicate(
doc.doc()))
277 result.push_back(
doc.
id());
316 std::string
type()
const {
return DocumentType::typeName(); }
325 std::vector<std::string> result;
326 for (
const auto& [key,
doc] :
docs_)
328 result.push_back(key);
330 std::sort(result.begin(), result.end());
336 std::unordered_map<std::string, DocumentType>
docs_;
A mixin class for collection of documents in a database to simplify the implementation of common oper...
std::string name_
The name of the collection.
CollectionMixin & operator=(CollectionMixin &&)=delete
NeoN::Database & db()
Gets a reference to the database.
const std::string & name() const
Gets the name of the collection.
const Document & doc(const std::string &id) const
Retrieves a document by its ID (const version).
CollectionMixin(NeoN::Database &db, std::string name)
Constructs a CollectionMixin with the given database and collection name.
std::vector< std::string > find(const std::function< bool(const Document &)> &predicate) const
Finds documents that match a given predicate.
std::size_t size() const
Gets the number of documents in the collection.
CollectionMixin(CollectionMixin &&)=default
const NeoN::Database & db() const
Gets a const reference to the database.
std::string type() const
Gets the type name of the documents in the collection.
std::unordered_map< std::string, DocumentType > docs_
The map of document IDs to documents.
NeoN::Database & db_
The reference to the database.
CollectionMixin & operator=(const CollectionMixin &)=delete
CollectionMixin(const CollectionMixin &)=delete
std::vector< std::string > sortedKeys() const
Gets the sorted keys of the documents in the collection.
Document & doc(const std::string &id)
Retrieves a document by its ID.
A type-erased interface collection types.
size_t size() const
Returns the number of documents in the collection.
CollectionType & as()
Casts the collection to a specific collection type.
std::string type() const
Returns the type of the collection.
Collection & operator=(Collection &&other)=default
A Collection is moveable, but not copyable.
Collection(const Collection &other)=delete
A Collection is not copyable, only moveable.
const CollectionType & as() const
Casts the collection to a specific collection type (const version).
const Document & doc(const std::string &id) const
Retrieves a document by its ID (const version).
std::vector< std::string > find(const std::function< bool(const Document &)> &predicate) const
Finds documents that match a given predicate.
Collection(CollectionType collection)
Constructs a Collection from a specific collection type.
Document & doc(const std::string &id)
Retrieves a document by its ID.
const Database & db() const
Returns a const reference to the database containing the collection.
Collection & operator=(const Collection &other)=delete
A Collection is not copyable, only moveable.
std::string name() const
Returns the name of the collection.
Collection(Collection &&other)=default
A Collection is moveable, but not copyable.
Database & db()
Returns a reference to the database containing the collection.
A class representing a document in a database.
std::string id() const
Retrieves the ID of the Document.
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.