10#include <unordered_map>
41 template<
typename CollectionType>
43 : impl_(std::make_unique<CollectionModel<CollectionType>>(std::move(collection)))
89 std::vector<std::string>
find(
const std::function<
bool(
const Document&)>& predicate)
const;
133 template<
typename CollectionType>
136 auto derived =
dynamic_cast<CollectionModel<CollectionType>*
>(impl_.get());
139 throw std::bad_cast();
141 return derived->collection_;
152 template<
typename CollectionType>
153 const CollectionType&
as()
const
155 auto derived =
dynamic_cast<CollectionModel<CollectionType>*
>(impl_.get());
158 throw std::bad_cast();
160 return derived->collection_;
165 struct CollectionConcept
167 virtual ~CollectionConcept() =
default;
168 virtual Document& doc(
const std::string&
id) = 0;
169 virtual const Document& doc(
const std::string&
id)
const = 0;
170 virtual std::vector<std::string> find(
const std::function<
bool(
const Document&)>& predicate
173 virtual std::string
type()
const = 0;
174 virtual std::string
name()
const = 0;
179 template<
typename CollectionType>
180 struct CollectionModel : CollectionConcept
182 CollectionModel(CollectionType collection) : collection_(std::move(collection)) {}
184 Document& doc(
const std::string&
id)
override {
return collection_.doc(
id); }
186 const Document& doc(
const std::string&
id)
const override {
return collection_.doc(
id); }
188 std::vector<std::string> find(
const std::function<
bool(
const Document&)>& predicate
191 return collection_.find(predicate);
194 sizeType size()
const override {
return collection_.size(); }
196 std::string
type()
const override {
return collection_.type(); }
198 std::string
name()
const override {
return collection_.name(); }
200 Database&
db()
override {
return collection_.db(); }
202 const Database&
db()
const override {
return collection_.db(); }
204 CollectionType collection_;
207 std::unique_ptr<CollectionConcept> impl_;
216template<
typename DocumentType>
272 std::vector<std::string>
find(
const std::function<
bool(
const Document&)>& predicate)
const
274 std::vector<std::string> result;
275 for (
const auto& [key,
doc] :
docs_)
277 if (predicate(
doc.doc()))
279 result.push_back(
doc.id());
318 std::string
type()
const {
return DocumentType::typeName(); }
327 auto k =
docs_ | std::views::keys;
328 std::vector<std::string> result(k.begin(), k.end());
329 std::ranges::sort(result);
335 std::unordered_map<std::string, DocumentType>
docs_;
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.
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
sizeType size() const
Gets the number of documents in the collection.
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.
sizeType 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.
Integer types used throughout NeoN.