-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDocument.hh
More file actions
286 lines (233 loc) · 12 KB
/
Document.hh
File metadata and controls
286 lines (233 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// Document.hh
//
// Copyright (c) 2019 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#pragma once
#include "cbl++/Collection.hh"
#include "cbl++/Database.hh"
#include "cbl/CBLDocument.h"
#include "fleece/Mutable.hh"
#include <string>
// VOLATILE API: Couchbase Lite C++ API is not finalized, and may change in
// future releases.
CBL_ASSUME_NONNULL_BEGIN
namespace cbl {
class MutableDocument;
/** Immutable Document. */
class Document : protected RefCounted {
public:
// Metadata:
/** A document's ID */
std::string id() const {return asString(CBLDocument_ID(ref()));}
/** A document's revision ID, which is a short opaque string that's guaranteed to be unique to every change made to
the document. If the document doesn't exist yet, this function returns an empty string. */
std::string revisionID() const {return asString(CBLDocument_RevisionID(ref()));}
/** A document's current sequence in the local database.
This number increases every time the document is saved, and a more recently saved document
will have a greater sequence number than one saved earlier, so sequences may be used as an
abstract 'clock' to tell relative modification times. */
uint64_t sequence() const {return CBLDocument_Sequence(ref());}
/** A document's collection or NULL for the new document that hasn't been saved. */
Collection collection() const {return Collection(CBLDocument_Collection(ref()));}
// Properties:
/** A document's properties as an immutable dictionary. */
fleece::Dict properties() const {return CBLDocument_Properties(ref());}
/** A document's properties as JSON. */
alloc_slice propertiesAsJSON() const {return alloc_slice(CBLDocument_CreateJSON(ref()));}
/** A subscript operator to access a document's property value by key. */
fleece::Value operator[] (slice key) const {return properties()[key];}
// Operations:
/** Creates a new mutable Document instance that refers to the same document as the original.
If the original document has unsaved changes, the new one will also start out with the same
changes; but mutating one document thereafter will not affect the other. */
inline MutableDocument mutableCopy() const;
protected:
Document(CBLRefCounted* r) :RefCounted(r) { }
static Document adopt(const CBLDocument* _cbl_nullable d, CBLError *error) {
if (!d && error->code != 0)
throw *error;
Document doc;
doc._ref = (CBLRefCounted*)d;
return doc;
}
static bool checkSave(bool saveResult, CBLError &error) {
if (saveResult)
return true;
else if (error.code == kCBLErrorConflict && error.domain == kCBLDomain)
return false;
else
throw error;
}
friend class Collection;
friend class Database;
friend class Replicator;
CBL_REFCOUNTED_BOILERPLATE(Document, RefCounted, const CBLDocument)
};
/** Mutable Document. */
class MutableDocument : public Document {
public:
/** Creates a new, empty document in memory, with a randomly-generated unique ID.
It will not be added to a database until saved. */
explicit MutableDocument(nullptr_t) {_ref = (CBLRefCounted*)CBLDocument_CreateWithID(fleece::nullslice);}
/** Creates a new, empty document in memory, with the given ID.
It will not be added to a database until saved.
@note If the given ID conflicts with a document already in the database, that will not
be apparent until this document is saved. At that time, the result depends on the
conflict handling mode used when saving; see the save functions for details.
@param docID The ID of the new document, or NULL to assign a new unique ID. */
explicit MutableDocument(slice docID) {_ref = (CBLRefCounted*)CBLDocument_CreateWithID(docID);}
/** Returns a mutable document's properties as a mutable dictionary.
You may modify this dictionary and then call \ref Collection::saveDocument(MutableDocument &doc) to persist the changes.
@note When accessing nested collections inside the properties as a mutable collection
for modification, use \ref MutableDict::getMutableDict() or \ref MutableDict::getMutableArray() */
fleece::MutableDict properties() {return CBLDocument_MutableProperties(ref());}
/** Sets a property key and value.
Call \ref Collection::saveDocument(MutableDocument &doc) to persist the changes. */
template <typename V>
void set(slice key, const V &val) {properties().set(key, val);}
/** Sets a property key and value.
Call \ref Collection::saveDocument(MutableDocument &doc) to persist the changes. */
template <typename K, typename V>
void set(const K &key, const V &val) {properties().set(key, val);}
/** A subscript operator to access a document's property value by key for either getting or setting the value.
Call \ref Collection::saveDocument(MutableDocument &doc) to persist the changes. */
fleece::keyref<fleece::MutableDict,fleece::slice> operator[] (slice key)
{return properties()[key];}
/** Sets a mutable document's properties.
Call \ref Collection::saveDocument(MutableDocument &doc) to persist the changes.
@param properties The document properties. */
void setProperties(fleece::MutableDict properties) {
CBLDocument_SetProperties(ref(), properties);
}
/** Sets a mutable document's properties.
Call \ref Collection::saveDocument(MutableDocument &doc) to persist the changes.
@param properties The document properties. */
void setProperties(fleece::Dict properties) {
CBLDocument_SetProperties(ref(), properties.mutableCopy());
}
/** Sets a mutable document's properties from a JSON Dictionary string.
Call \ref Collection::saveDocument(MutableDocument &doc) to persist the changes.
@param json A JSON Dictionaryt string */
void setPropertiesAsJSON(slice json) {
CBLError error;
if (!CBLDocument_SetJSON(ref(), json, &error))
throw error;
}
protected:
static MutableDocument adopt(CBLDocument* _cbl_nullable d, CBLError *error) {
if (!d && error->code != 0)
throw *error;
MutableDocument doc;
doc._ref = (CBLRefCounted*)d;
return doc;
}
friend class Collection;
friend class Database;
friend class Document;
CBL_REFCOUNTED_BOILERPLATE(MutableDocument, Document, CBLDocument)
};
// Document method bodies:
inline MutableDocument Document::mutableCopy() const {
MutableDocument doc;
doc._ref = (CBLRefCounted*) CBLDocument_MutableCopy(ref());
return doc;
}
// Collection method bodies:
inline Document Collection::getDocument(slice id) const {
CBLError error;
return Document::adopt(CBLCollection_GetDocument(ref(), id, &error), &error);
}
inline MutableDocument Collection::getMutableDocument(slice id) const {
CBLError error;
return MutableDocument::adopt(CBLCollection_GetMutableDocument(ref(), id, &error), &error);
}
inline void Collection::saveDocument(MutableDocument &doc) {
(void) saveDocument(doc, kCBLConcurrencyControlLastWriteWins);
}
inline bool Collection::saveDocument(MutableDocument &doc, CBLConcurrencyControl c) {
CBLError error;
return Document::checkSave(
CBLCollection_SaveDocumentWithConcurrencyControl(ref(), doc.ref(), c, &error), error);
}
inline bool Collection::saveDocument(MutableDocument &doc, CollectionConflictHandler conflictHandler) {
CBLConflictHandler cHandler = [](void *context, CBLDocument *myDoc,
const CBLDocument *otherDoc) -> bool {
return (*(CollectionConflictHandler*)context)(MutableDocument(myDoc),
Document(otherDoc));
};
CBLError error;
return Document::checkSave(
CBLCollection_SaveDocumentWithConflictHandler(ref(), doc.ref(), cHandler,
&conflictHandler, &error), error);
}
inline void Collection::deleteDocument(Document &doc) {
(void) deleteDocument(doc, kCBLConcurrencyControlLastWriteWins);
}
inline bool Collection::deleteDocument(Document &doc, CBLConcurrencyControl cc) {
CBLError error;
return Document::checkSave(
CBLCollection_DeleteDocumentWithConcurrencyControl(ref(), doc.ref(), cc, &error), error);
}
inline void Collection::purgeDocument(Document &doc) {
CBLError error;
check(CBLCollection_PurgeDocument(ref(), doc.ref(), &error), error);
}
// Database method bodies:
inline Document Database::getDocument(slice id) const {
CBLError error;
return Document::adopt(CBLDatabase_GetDocument(ref(), id, &error), &error);
}
inline MutableDocument Database::getMutableDocument(slice id) const {
CBLError error;
return MutableDocument::adopt(CBLDatabase_GetMutableDocument(ref(), id, &error), &error);
}
inline void Database::saveDocument(MutableDocument &doc) {
(void) saveDocument(doc, kCBLConcurrencyControlLastWriteWins);
}
inline bool Database::saveDocument(MutableDocument &doc, CBLConcurrencyControl c) {
CBLError error;
return Document::checkSave(
CBLDatabase_SaveDocumentWithConcurrencyControl(ref(), doc.ref(), c, &error),
error);
}
inline bool Database::saveDocument(MutableDocument &doc,
ConflictHandler conflictHandler)
{
CBLConflictHandler cHandler = [](void *context, CBLDocument *myDoc,
const CBLDocument *otherDoc) -> bool {
return (*(ConflictHandler*)context)(MutableDocument(myDoc),
Document(otherDoc));
};
CBLError error;
return Document::checkSave(
CBLDatabase_SaveDocumentWithConflictHandler(ref(), doc.ref(), cHandler, &conflictHandler, &error),
error);
}
inline void Database::deleteDocument(Document &doc) {
(void) deleteDocument(doc, kCBLConcurrencyControlLastWriteWins);
}
inline bool Database::deleteDocument(Document &doc, CBLConcurrencyControl cc) {
CBLError error;
return Document::checkSave(CBLDatabase_DeleteDocumentWithConcurrencyControl(
ref(), doc.ref(), cc, &error),
error);
}
inline void Database::purgeDocument(Document &doc) {
CBLError error;
check(CBLDatabase_PurgeDocument(ref(), doc.ref(), &error), error);
}
}
CBL_ASSUME_NONNULL_END