4#include "Stroika/Foundation/StroikaPreComp.h"
10#include "Stroika/Foundation/Common/GUID.h"
19using namespace Characters;
20using namespace Containers;
22using namespace DataExchange;
23using namespace Database;
24using namespace Database::Document::TrivialDocumentDB;
25using namespace Execution;
52 const shared_ptr<MemoryDatabaseRep_> fConnectionRep_;
55 MyCollectionRep_ (
const shared_ptr<MemoryDatabaseRep_>& connectionRep,
const String& collectionName)
56 : fConnectionRep_{connectionRep}
57 , fTableName_{collectionName}
62#if USE_NOISY_TRACE_IN_THIS_MODULE_
63 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::Add()"};
65 auto rwLock = fConnectionRep_->fCollections_.rwget ();
66 CollectionRep_ collection = rwLock.cref ().LookupValue (fTableName_);
68 collection.Add (
id, v);
69 rwLock.rwref ().Add (fTableName_, collection);
70 return id.ToString ();
72 virtual optional<Document::Document> GetOne (
const IDType&
id,
const optional<Projection>& projection)
override
74#if USE_NOISY_TRACE_IN_THIS_MODULE_
75 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::GetOne()"};
77 auto r = fConnectionRep_->fCollections_->LookupValue (fTableName_).Lookup (
GUID{
id});
79 r->Add (Document::kID,
id);
81 if (projection && r) {
82 r = projection->Apply (*r);
88#if USE_NOISY_TRACE_IN_THIS_MODULE_
89 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::GetAll()",
"filter={}, projection={}"_f,
92 return fConnectionRep_->fCollections_->LookupValue (fTableName_)
95 d.
Add (Document::kID, kvp.fKey.ToString ());
96 if (filter and not filter->Matches (d)) {
101 d = projection->
Apply (d);
109#if USE_NOISY_TRACE_IN_THIS_MODULE_
110 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::Update()"};
113 if (onlyTheseFields) {
118 auto rwLock = fConnectionRep_->fCollections_.rwget ();
119 CollectionRep_ collection = rwLock.cref ().LookupChecked (fTableName_, kExcept1_);
122 if (onlyTheseFields) {
123 d2Update.
AddAll (uploadDoc);
127 collection.Add (
id, d2Update);
128 rwLock.rwref ().Add (fTableName_, collection);
130 virtual void Remove (
const IDType&
id)
override
132#if USE_NOISY_TRACE_IN_THIS_MODULE_
133 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::Remove()"};
135 auto rwLock = fConnectionRep_->fCollections_.rwget ();
136 if (optional<CollectionRep_> oc = rwLock.cref ().Lookup (fTableName_)) {
137 CollectionRep_ c = *oc;
138 if (c.RemoveIf (
id)) {
139 rwLock.rwref ().Add (fTableName_, c);
147 virtual void Commit ()
override
151 virtual void Rollback ()
override
155 virtual Disposition GetDisposition ()
const override
157 return Disposition::eCompleted;
161 MemoryDatabaseRep_ ([[maybe_unused]]
const Options& options)
165 virtual shared_ptr<const EngineProperties> GetEngineProperties ()
const override
168 virtual String GetEngineName ()
const override
170 return "TrivialDocumentDB"sv;
173 static const shared_ptr<const EngineProperties> kProps_ = make_shared<const MyEngineProperties_> ();
180 virtual void CreateCollection (
const String& name)
override
182 auto rwLock = fCollections_.
rwget ();
183 if (not rwLock.cref ().Lookup (name)) {
184 rwLock.rwref ().Add (name, {});
187 virtual void DropCollection (
const String& name)
override
189 auto rwLock = fCollections_.
rwget ();
190 rwLock.rwref ().RemoveIf (name);
195 make_shared<MyCollectionRep_> (Debug::UncheckedDynamicPointerCast<MemoryDatabaseRep_> (shared_from_this ()), name)};
204 using FilesystemDatabaseRep_ = MemoryDatabaseRep_;
212auto Document::TrivialDocumentDB::New (
const Options& options) ->
Ptr
214 if (get_if<Options::MemoryStorage> (&options.fStorage)) {
215 return Ptr{make_shared<MemoryDatabaseRep_> (options)};
217 else if (get_if<Options::FilesystemStorage> (&options.fStorage)) {
218 return Ptr{make_shared<FilesystemDatabaseRep_> (options)};
#define AssertNotImplemented()
String is like std::u32string, except it is much easier to use, often much more space efficient,...
nonvirtual bool Add(ArgByValueType< key_type > key, ArgByValueType< mapped_type > newElt, AddReplaceMode addReplaceMode=AddReplaceMode::eAddReplaces)
nonvirtual mapped_type LookupChecked(ArgByValueType< key_type > key, const THROW_IF_MISSING &throwIfMissing) const
nonvirtual unsigned int AddAll(ITERABLE_OF_ADDABLE &&items, AddReplaceMode addReplaceMode=AddReplaceMode::eAddReplaces)
nonvirtual void RemoveAll()
RemoveAll removes all, or all matching (predicate, iterator range, equals comparer or whatever) items...
nonvirtual Iterable< key_type > Keys() const
nonvirtual void RetainAll(const ITERABLE_OF_KEY_TYPE &items)
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.
EngineProperties captures the features associated with a given database engine (being talked to throu...
define a (simple) projection on a document, subsetting the fields of that document.
Wrap any object with Synchronized<> and it can be used similarly to the base type,...
nonvirtual WritableReference rwget()
get a read-write smart pointer to the underlying Synchronized<> object, holding the full lock the who...
nonvirtual T load() const
nonvirtual void Apply(const function< void(ArgByValueType< T > item)> &doToElement, Execution::SequencePolicy seq=Execution::SequencePolicy::eDEFAULT) const
Run the argument function (or lambda) on each element of the container.
static GUID GenerateNew() noexcept