4#include "Stroika/Foundation/StroikaPreComp.h"
10#include "Stroika/Foundation/Common/GUID.h"
20using namespace Characters;
21using namespace Containers;
23using namespace DataExchange;
24using namespace Database;
25using namespace Database::Document::TrivialDocumentDB;
26using namespace Execution;
53 const shared_ptr<MemoryDatabaseRep_> fConnectionRep_;
56 MyCollectionRep_ (
const shared_ptr<MemoryDatabaseRep_>& connectionRep,
const String& collectionName)
57 : fConnectionRep_{connectionRep}
58 , fTableName_{collectionName}
63#if USE_NOISY_TRACE_IN_THIS_MODULE_
64 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::Add()"};
66 auto rwLock = fConnectionRep_->fCollections_.rwget ();
67 CollectionRep_ collection = rwLock.cref ().LookupValue (fTableName_);
69 collection.Add (
id, v);
70 rwLock.rwref ().Add (fTableName_, collection);
71 return id.ToString ();
73 virtual optional<Document::Document> GetOne (
const IDType&
id,
const optional<Projection>& projection)
override
75#if USE_NOISY_TRACE_IN_THIS_MODULE_
76 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::GetOne()"};
78 auto r = fConnectionRep_->fCollections_->LookupValue (fTableName_).Lookup (
GUID{
id});
80 r->Add (Document::kID,
id);
82 if (projection && r) {
83 r = projection->Apply (*r);
89#if USE_NOISY_TRACE_IN_THIS_MODULE_
90 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::GetAll()",
"filter={}, projection={}"_f,
93 return fConnectionRep_->fCollections_->LookupValue (fTableName_)
96 d.
Add (Document::kID, kvp.fKey.ToString ());
97 if (filter and not filter->Matches (d)) {
102 d = projection->
Apply (d);
110#if USE_NOISY_TRACE_IN_THIS_MODULE_
111 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::Update()"};
114 if (onlyTheseFields) {
119 auto rwLock = fConnectionRep_->fCollections_.rwget ();
120 CollectionRep_ collection = rwLock.cref ().LookupChecked (fTableName_, kExcept1_);
123 if (onlyTheseFields) {
124 d2Update.
AddAll (uploadDoc);
128 collection.Add (
id, d2Update);
129 rwLock.rwref ().Add (fTableName_, collection);
131 virtual void Remove (
const IDType&
id)
override
133#if USE_NOISY_TRACE_IN_THIS_MODULE_
134 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::Remove()"};
136 auto rwLock = fConnectionRep_->fCollections_.rwget ();
137 if (optional<CollectionRep_> oc = rwLock.cref ().Lookup (fTableName_)) {
138 CollectionRep_ c = *oc;
139 if (c.RemoveIf (
id)) {
140 rwLock.rwref ().Add (fTableName_, c);
148 virtual void Commit ()
override
152 virtual void Rollback ()
override
156 virtual Disposition GetDisposition ()
const override
158 return Disposition::eCompleted;
162 MemoryDatabaseRep_ ([[maybe_unused]]
const Options& options)
166 virtual shared_ptr<const EngineProperties> GetEngineProperties ()
const override
169 virtual String GetEngineName ()
const override
171 return "TrivialDocumentDB"sv;
174 static const shared_ptr<const EngineProperties> kProps_ = Memory::MakeSharedPtr<const MyEngineProperties_> ();
181 virtual void CreateCollection (
const String& name)
override
183 auto rwLock = fCollections_.
rwget ();
184 if (not rwLock.cref ().Lookup (name)) {
185 rwLock.rwref ().Add (name, {});
188 virtual void DropCollection (
const String& name)
override
190 auto rwLock = fCollections_.
rwget ();
191 rwLock.rwref ().RemoveIf (name);
196 Memory::MakeSharedPtr<MyCollectionRep_> (Debug::UncheckedDynamicPointerCast<MemoryDatabaseRep_> (shared_from_this ()), name)};
205 using FilesystemDatabaseRep_ = MemoryDatabaseRep_;
213auto Document::TrivialDocumentDB::New (
const Options& options) ->
Ptr
215 if (get_if<Options::MemoryStorage> (&options.fStorage)) {
216 return Ptr{Memory::MakeSharedPtr<MemoryDatabaseRep_> (options)};
218 else if (get_if<Options::FilesystemStorage> (&options.fStorage)) {
219 return Ptr{Memory::MakeSharedPtr<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