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()"};
92 return fConnectionRep_->fCollections_->LookupValue (fTableName_)
95 d.
Add (Document::kID, kvp.fKey.ToString ());
96 if (not filter.has_value () or filter->Matches (d)) {
98 d = projection->
Apply (d);
107#if USE_NOISY_TRACE_IN_THIS_MODULE_
108 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::Update()"};
111 if (onlyTheseFields) {
116 auto rwLock = fConnectionRep_->fCollections_.rwget ();
117 CollectionRep_ collection = rwLock.cref ().LookupChecked (fTableName_, kExcept1_);
120 if (onlyTheseFields) {
121 d2Update.
AddAll (uploadDoc);
125 collection.Add (
id, d2Update);
126 rwLock.rwref ().Add (fTableName_, collection);
128 virtual void Remove (
const IDType&
id)
override
130#if USE_NOISY_TRACE_IN_THIS_MODULE_
131 TraceContextBumper ctx{
"TrivialDocumentDB::MemoryDatabaseRep_::MyCollectionRep_::Remove()"};
133 auto rwLock = fConnectionRep_->fCollections_.rwget ();
134 if (optional<CollectionRep_> oc = rwLock.cref ().Lookup (fTableName_)) {
135 CollectionRep_ c = *oc;
136 if (c.RemoveIf (
id)) {
137 rwLock.rwref ().Add (fTableName_, c);
145 virtual void Commit ()
override
149 virtual void Rollback ()
override
153 virtual Disposition GetDisposition ()
const override
155 return Disposition::eCompleted;
159 MemoryDatabaseRep_ ([[maybe_unused]]
const Options& options)
163 virtual shared_ptr<const EngineProperties> GetEngineProperties ()
const override
166 virtual String GetEngineName ()
const override
168 return "TrivialDocumentDB"sv;
171 static const shared_ptr<const EngineProperties> kProps_ = make_shared<const MyEngineProperties_> ();
178 virtual void CreateCollection (
const String& name)
override
180 auto rwLock = fCollections_.
rwget ();
181 if (not rwLock.cref ().Lookup (name)) {
182 rwLock.rwref ().Add (name, {});
185 virtual void DropCollection (
const String& name)
override
187 auto rwLock = fCollections_.
rwget ();
188 rwLock.rwref ().RemoveIf (name);
193 make_shared<MyCollectionRep_> (Debug::UncheckedDynamicPointerCast<MemoryDatabaseRep_> (shared_from_this ()), name)};
202 using FilesystemDatabaseRep_ = MemoryDatabaseRep_;
210auto Document::TrivialDocumentDB::New (
const Options& options) ->
Ptr
212 if (get_if<Options::MemoryStorage> (&options.fStorage)) {
213 return Ptr{make_shared<MemoryDatabaseRep_> (options)};
215 else if (get_if<Options::FilesystemStorage> (&options.fStorage)) {
216 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