Couchbase Lite C++
Couchbase Lite C++ API
Loading...
Searching...
No Matches
Document.hh
Go to the documentation of this file.
1//
2// Document.hh
3//
4// Copyright (c) 2019 Couchbase, Inc All rights reserved.
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17//
18
19#pragma once
20#include "cbl++/Collection.hh"
21#include "cbl++/Database.hh"
22#include "cbl/CBLDocument.h"
23#include "fleece/Mutable.hh"
24#include <string>
25
26
28
29namespace cbl {
30 class MutableDocument;
31
33 class Document : protected RefCounted {
34 public:
35 // Metadata:
36
38 std::string id() const {return internal::asString(CBLDocument_ID(ref()));}
39
42 std::string revisionID() const {return internal::asString(CBLDocument_RevisionID(ref()));}
43
45 uint64_t timestamp() const {return CBLDocument_Timestamp(ref());}
46
51 uint64_t sequence() const {return CBLDocument_Sequence(ref());}
52
55
56 // Properties:
57
59 fleece::Dict properties() const {return CBLDocument_Properties(ref());}
60
63
65 fleece::Value operator[] (std::string_view key) const {return properties()[slice(key)];}
66
67 // Operations:
68
72 inline MutableDocument mutableCopy() const;
73
74 protected:
75 friend class Collection;
76 friend class Database;
77 friend class Replicator;
78
79 Document(CBLRefCounted* r) :RefCounted(r) { }
80
81 static Document adopt(const CBLDocument* _cbl_nullable d, CBLError *error) {
82 if (!d && error->code != 0)
83 internal::check(false, *error);
84 Document doc;
85 doc._ref = (CBLRefCounted*)d;
86 return doc;
87 }
88
89 static bool checkSave(bool saveResult, CBLError &error) {
90 if (saveResult)
91 return true;
92 else {
93 bool permittedError = (error.code == kCBLErrorConflict && error.domain == kCBLDomain);
94 internal::check(permittedError, error); // throw if not permittedError
95 return false;
96 }
97 }
98
100 };
101
102
104 class MutableDocument : public Document {
105 public:
108 explicit MutableDocument(std::nullptr_t) {_ref = (CBLRefCounted*)CBLDocument_CreateWithID(fleece::nullslice);}
109
116 explicit MutableDocument(std::string_view docID) {
118 }
119
124 fleece::MutableDict properties() {return CBLDocument_MutableProperties(ref());}
125
128 template <typename V>
129 void set(std::string_view key, const V &val) {properties().set(slice(key), val);}
130
133 template <typename K, typename V>
134 void set(const K &key, const V &val) {properties().set(key, val);}
135
138 fleece::keyref<fleece::MutableDict,fleece::slice> operator[] (std::string_view key)
139 {return properties()[slice(key)];}
140
144 void setProperties(fleece::MutableDict properties) {
146 }
147
151 void setProperties(fleece::Dict properties) {
152 CBLDocument_SetProperties(ref(), properties.mutableCopy());
153 }
154
158 void setPropertiesAsJSON(std::string_view json) {
159 CBLError error;
160 internal::check(CBLDocument_SetJSON(ref(), slice(json), &error), error);
161 }
162
163 protected:
165 internal::check(d != nullptr || error->code == 0, *error);
166 MutableDocument doc;
167 doc._ref = (CBLRefCounted*)d;
168 return doc;
169 }
170
171 friend class Collection;
172 friend class Database;
173 friend class Document;
175 };
176
177 // Document method bodies:
178
180 MutableDocument doc;
182 return doc;
183 }
184
185 // Collection method bodies:
186
187 inline Document Collection::getDocument(std::string_view id) const {
188 CBLError error;
189 return Document::adopt(CBLCollection_GetDocument(ref(), slice(id), &error), &error);
190 }
191
192 inline MutableDocument Collection::getMutableDocument(std::string_view id) const {
193 CBLError error;
195 }
196
200
206
208 CBLConflictHandler cHandler = [](void *context, CBLDocument *myDoc,
209 const CBLDocument *otherDoc) -> bool {
210 return (*(CollectionConflictHandler*)context)(MutableDocument(myDoc),
211 Document(otherDoc));
212 };
213 CBLError error;
214 return Document::checkSave(
216 &conflictHandler, &error), error);
217 }
218
222
224 CBLError error;
225 return Document::checkSave(
226 CBLCollection_DeleteDocumentWithConcurrencyControl(ref(), doc.ref(), cc, &error), error);
227 }
228
230 CBLError error{};
231 internal::check(CBLCollection_PurgeDocument(ref(), doc.ref(), &error), error);
232 }
233}
234
#define CBL_REFCOUNTED_BOILERPLATE(CLASS, SUPER, C_TYPE)
Definition Base.hh:160
#define _cbl_nullable
#define CBL_ASSUME_NONNULL_END
#define CBL_ASSUME_NONNULL_BEGIN
MutableDocument getMutableDocument(std::string_view docID) const
Reads a document from the collection in mutable form that can be updated and saved.
Definition Document.hh:192
CBLCollection *_Nullable ref() const
Returns a pointer to the underlying C object (CBLCollection), or NULL if this is a null reference.
Definition Collection.hh:373
void saveDocument(MutableDocument &doc)
Saves a (mutable) document to the collection.
Definition Document.hh:197
void purgeDocument(Document &doc)
Purges a document from the collection.
Definition Document.hh:229
Document getDocument(std::string_view docID) const
Reads a document from the collection in an immutable form.
Definition Document.hh:187
void deleteDocument(Document &doc)
Deletes a document from the collection.
Definition Document.hh:219
friend class Document
Definition Collection.hh:370
std::string id() const
A document's ID.
Definition Document.hh:38
Collection collection() const
A document's collection or NULL for the new document that hasn't been saved.
Definition Document.hh:54
const CBLDocument *_Nullable ref() const
Returns a pointer to the underlying C object (const CBLDocument), or NULL if this is a null reference...
Definition Document.hh:99
static bool checkSave(bool saveResult, CBLError &error)
Definition Document.hh:89
fleece::Value operator[](std::string_view key) const
A subscript operator to access a document's property value by key.
Definition Document.hh:65
Document(CBLRefCounted *r)
Definition Document.hh:79
uint64_t sequence() const
A document's current sequence in the local database.
Definition Document.hh:51
friend class Collection
Definition Document.hh:75
friend class Database
Definition Document.hh:76
std::string revisionID() const
A document's revision ID, which is a short opaque string that's guaranteed to be unique to every chan...
Definition Document.hh:42
friend class Replicator
Definition Document.hh:77
static Document adopt(const CBLDocument *_Nullable d, CBLError *error)
Definition Document.hh:81
uint64_t timestamp() const
The hybrid logical timestamp in nanoseconds since epoch that the revision was created.
Definition Document.hh:45
fleece::Dict properties() const
A document's properties as an immutable dictionary.
Definition Document.hh:59
MutableDocument mutableCopy() const
Creates a new mutable Document instance that refers to the same document as the original.
Definition Document.hh:179
alloc_slice propertiesAsJSON() const
A document's properties as JSON.
Definition Document.hh:62
A mutable document, whose properties can be modified and saved to a collection.
Definition Document.hh:104
static MutableDocument adopt(CBLDocument *_Nullable d, CBLError *error)
Definition Document.hh:164
void setProperties(fleece::MutableDict properties)
Sets a mutable document's properties.
Definition Document.hh:144
void set(std::string_view key, const V &val)
Sets a property key and value.
Definition Document.hh:129
friend class Collection
Definition Document.hh:171
friend class Database
Definition Document.hh:172
void setPropertiesAsJSON(std::string_view json)
Sets a mutable document's properties from a JSON Dictionary string.
Definition Document.hh:158
friend class Document
Definition Document.hh:173
fleece::keyref< fleece::MutableDict, fleece::slice > operator[](std::string_view key)
A subscript operator to access a document's property value by key for either getting or setting the v...
Definition Document.hh:138
MutableDocument(std::nullptr_t)
Creates a new, empty document in memory, with a randomly-generated unique ID.
Definition Document.hh:108
MutableDocument(std::string_view docID)
Creates a new, empty document in memory, with the given ID.
Definition Document.hh:116
CBLDocument *_Nullable ref() const
Returns a pointer to the underlying C object (CBLDocument), or NULL if this is a null reference.
Definition Document.hh:174
fleece::MutableDict properties()
Returns a mutable document's properties as a mutable dictionary.
Definition Document.hh:124
void setProperties(fleece::Dict properties)
Sets a mutable document's properties.
Definition Document.hh:151
void set(const K &key, const V &val)
Sets a property key and value.
Definition Document.hh:134
bool CBLCollection_DeleteDocumentWithConcurrencyControl(CBLCollection *collection, const CBLDocument *document, CBLConcurrencyControl concurrency, CBLError *_cbl_nullable outError)
bool CBLCollection_PurgeDocument(CBLCollection *collection, const CBLDocument *document, CBLError *_cbl_nullable outError)
_cbl_warn_unused const CBLDocument *_cbl_nullable CBLCollection_GetDocument(const CBLCollection *collection, FLString docID, CBLError *_cbl_nullable outError)
_cbl_warn_unused CBLDocument *_cbl_nullable CBLCollection_GetMutableDocument(CBLCollection *collection, FLString docID, CBLError *_cbl_nullable outError)
bool CBLCollection_SaveDocumentWithConflictHandler(CBLCollection *collection, CBLDocument *doc, CBLConflictHandler conflictHandler, void *_cbl_nullable context, CBLError *_cbl_nullable outError)
bool CBLCollection_SaveDocumentWithConcurrencyControl(CBLCollection *collection, CBLDocument *doc, CBLConcurrencyControl concurrency, CBLError *_cbl_nullable outError)
_cbl_warn_unused FLSliceResult CBLDocument_CreateJSON(const CBLDocument *)
_cbl_warn_unused CBLDocument * CBLDocument_MutableCopy(const CBLDocument *original)
CBLConcurrencyControl
FLString CBLDocument_ID(const CBLDocument *)
bool CBLDocument_SetJSON(CBLDocument *, FLSlice json, CBLError *_cbl_nullable outError)
CBLCollection *_cbl_nullable CBLDocument_Collection(const CBLDocument *)
uint64_t CBLDocument_Timestamp(const CBLDocument *)
bool(* CBLConflictHandler)(void *_cbl_nullable context, CBLDocument *_cbl_nullable documentBeingSaved, const CBLDocument *_cbl_nullable conflictingDocument)
struct CBLDocument CBLDocument
uint64_t CBLDocument_Sequence(const CBLDocument *)
FLString CBLDocument_RevisionID(const CBLDocument *)
FLMutableDict CBLDocument_MutableProperties(CBLDocument *)
FLDict CBLDocument_Properties(const CBLDocument *)
_cbl_warn_unused CBLDocument * CBLDocument_CreateWithID(FLString docID)
void CBLDocument_SetProperties(CBLDocument *, FLMutableDict properties)
kCBLConcurrencyControlLastWriteWins
kCBLDomain
kCBLErrorConflict
struct CBLRefCounted CBLRefCounted
Definition Base.hh:46
fleece::slice slice
Convenience alias for fleece::slice, a non-owning view of a byte range.
Definition Base.hh:49
std::function< bool(MutableDocument documentBeingSaved, Document conflictingDocument)> CollectionConflictHandler
Conflict handler used when saving a document.
Definition Collection.hh:41
fleece::alloc_slice alloc_slice
Convenience alias for fleece::alloc_slice, an owning byte buffer.
Definition Base.hh:51
CBLErrorDomain domain