OLD | NEW |
| (Empty) |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SYNC_SYNCABLE_ENTRY_KERNEL_H_ | |
6 #define SYNC_SYNCABLE_ENTRY_KERNEL_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <algorithm> | |
11 #include <map> | |
12 #include <set> | |
13 #include <string> | |
14 | |
15 #include "base/time/time.h" | |
16 #include "base/values.h" | |
17 #include "sync/base/sync_export.h" | |
18 #include "sync/internal_api/public/base/model_type.h" | |
19 #include "sync/internal_api/public/base/unique_position.h" | |
20 #include "sync/internal_api/public/util/immutable.h" | |
21 #include "sync/internal_api/public/util/proto_value_ptr.h" | |
22 #include "sync/protocol/attachments.pb.h" | |
23 #include "sync/protocol/sync.pb.h" | |
24 #include "sync/syncable/metahandle_set.h" | |
25 #include "sync/syncable/syncable_id.h" | |
26 #include "sync/util/time.h" | |
27 | |
28 namespace syncer { | |
29 | |
30 class Cryptographer; | |
31 | |
32 namespace syncable { | |
33 | |
34 // Things you need to update if you change any of the fields below: | |
35 // - EntryKernel struct in this file | |
36 // - syncable_columns.h | |
37 // - syncable_enum_conversions{.h,.cc,_unittest.cc} | |
38 // - EntryKernel::EntryKernel(), EntryKernel::ToValue() in entry_kernel.cc | |
39 // - operator<< in Entry.cc | |
40 // - BindFields() and UnpackEntry() in directory_backing_store.cc | |
41 // - kCurrentDBVersion, DirectoryBackingStore::InitializeTables in | |
42 // directory_backing_store.cc | |
43 // - TestSimpleFieldsPreservedDuringSaveChanges in syncable_unittest.cc | |
44 | |
45 static const int64_t kInvalidMetaHandle = 0; | |
46 | |
47 enum { | |
48 BEGIN_FIELDS = 0, | |
49 INT64_FIELDS_BEGIN = BEGIN_FIELDS | |
50 }; | |
51 | |
52 enum MetahandleField { | |
53 // Primary key into the table. Keep this as a handle to the meta entry | |
54 // across transactions. | |
55 META_HANDLE = INT64_FIELDS_BEGIN | |
56 }; | |
57 | |
58 enum BaseVersion { | |
59 // After initial upload, the version is controlled by the server, and is | |
60 // increased whenever the data or metadata changes on the server. | |
61 BASE_VERSION = META_HANDLE + 1, | |
62 }; | |
63 | |
64 enum Int64Field { | |
65 SERVER_VERSION = BASE_VERSION + 1, | |
66 LOCAL_EXTERNAL_ID, // ID of an item in the external local storage that this | |
67 // entry is associated with. (such as bookmarks.js) | |
68 TRANSACTION_VERSION, | |
69 INT64_FIELDS_END | |
70 }; | |
71 | |
72 enum { | |
73 INT64_FIELDS_COUNT = INT64_FIELDS_END - INT64_FIELDS_BEGIN, | |
74 TIME_FIELDS_BEGIN = INT64_FIELDS_END, | |
75 }; | |
76 | |
77 enum TimeField { | |
78 MTIME = TIME_FIELDS_BEGIN, | |
79 SERVER_MTIME, | |
80 CTIME, | |
81 SERVER_CTIME, | |
82 TIME_FIELDS_END, | |
83 }; | |
84 | |
85 enum { | |
86 TIME_FIELDS_COUNT = TIME_FIELDS_END - TIME_FIELDS_BEGIN, | |
87 ID_FIELDS_BEGIN = TIME_FIELDS_END, | |
88 }; | |
89 | |
90 enum IdField { | |
91 // Code in InitializeTables relies on ID being the first IdField value. | |
92 ID = ID_FIELDS_BEGIN, | |
93 PARENT_ID, | |
94 SERVER_PARENT_ID, | |
95 ID_FIELDS_END | |
96 }; | |
97 | |
98 enum { | |
99 ID_FIELDS_COUNT = ID_FIELDS_END - ID_FIELDS_BEGIN, | |
100 BIT_FIELDS_BEGIN = ID_FIELDS_END | |
101 }; | |
102 | |
103 enum IndexedBitField { | |
104 IS_UNSYNCED = BIT_FIELDS_BEGIN, | |
105 IS_UNAPPLIED_UPDATE, | |
106 INDEXED_BIT_FIELDS_END, | |
107 }; | |
108 | |
109 enum IsDelField { | |
110 IS_DEL = INDEXED_BIT_FIELDS_END, | |
111 }; | |
112 | |
113 enum BitField { | |
114 IS_DIR = IS_DEL + 1, | |
115 SERVER_IS_DIR, | |
116 SERVER_IS_DEL, | |
117 BIT_FIELDS_END | |
118 }; | |
119 | |
120 enum { | |
121 BIT_FIELDS_COUNT = BIT_FIELDS_END - BIT_FIELDS_BEGIN, | |
122 STRING_FIELDS_BEGIN = BIT_FIELDS_END | |
123 }; | |
124 | |
125 enum StringField { | |
126 // Name, will be truncated by server. Can be duplicated in a folder. | |
127 NON_UNIQUE_NAME = STRING_FIELDS_BEGIN, | |
128 // The server version of |NON_UNIQUE_NAME|. | |
129 SERVER_NON_UNIQUE_NAME, | |
130 | |
131 // A tag string which identifies this node as a particular top-level | |
132 // permanent object. The tag can be thought of as a unique key that | |
133 // identifies a singleton instance. | |
134 UNIQUE_SERVER_TAG, // Tagged by the server | |
135 UNIQUE_CLIENT_TAG, // Tagged by the client | |
136 UNIQUE_BOOKMARK_TAG, // Client tags for bookmark items | |
137 STRING_FIELDS_END, | |
138 }; | |
139 | |
140 enum { | |
141 STRING_FIELDS_COUNT = STRING_FIELDS_END - STRING_FIELDS_BEGIN, | |
142 PROTO_FIELDS_BEGIN = STRING_FIELDS_END | |
143 }; | |
144 | |
145 // From looking at the sqlite3 docs, it's not directly stated, but it | |
146 // seems the overhead for storing a NULL blob is very small. | |
147 enum ProtoField { | |
148 SPECIFICS = PROTO_FIELDS_BEGIN, | |
149 SERVER_SPECIFICS, | |
150 BASE_SERVER_SPECIFICS, | |
151 PROTO_FIELDS_END, | |
152 }; | |
153 | |
154 enum { | |
155 PROTO_FIELDS_COUNT = PROTO_FIELDS_END - PROTO_FIELDS_BEGIN, | |
156 UNIQUE_POSITION_FIELDS_BEGIN = PROTO_FIELDS_END | |
157 }; | |
158 | |
159 enum UniquePositionField { | |
160 SERVER_UNIQUE_POSITION = UNIQUE_POSITION_FIELDS_BEGIN, | |
161 UNIQUE_POSITION, | |
162 UNIQUE_POSITION_FIELDS_END | |
163 }; | |
164 | |
165 enum { | |
166 UNIQUE_POSITION_FIELDS_COUNT = | |
167 UNIQUE_POSITION_FIELDS_END - UNIQUE_POSITION_FIELDS_BEGIN, | |
168 ATTACHMENT_METADATA_FIELDS_BEGIN = UNIQUE_POSITION_FIELDS_END | |
169 }; | |
170 | |
171 enum AttachmentMetadataField { | |
172 ATTACHMENT_METADATA = ATTACHMENT_METADATA_FIELDS_BEGIN, | |
173 SERVER_ATTACHMENT_METADATA, | |
174 ATTACHMENT_METADATA_FIELDS_END | |
175 }; | |
176 | |
177 enum { | |
178 ATTACHMENT_METADATA_FIELDS_COUNT = | |
179 ATTACHMENT_METADATA_FIELDS_END - ATTACHMENT_METADATA_FIELDS_BEGIN, | |
180 FIELD_COUNT = ATTACHMENT_METADATA_FIELDS_END - BEGIN_FIELDS, | |
181 // Past this point we have temporaries, stored in memory only. | |
182 BEGIN_TEMPS = ATTACHMENT_METADATA_FIELDS_END, | |
183 BIT_TEMPS_BEGIN = BEGIN_TEMPS, | |
184 }; | |
185 | |
186 enum BitTemp { | |
187 // Whether a server commit operation was started and has not yet completed | |
188 // for this entity. | |
189 SYNCING = BIT_TEMPS_BEGIN, | |
190 // Whether a local change was made to an entity that had SYNCING set to true, | |
191 // and was therefore in the middle of a commit operation. | |
192 // Note: must only be set if SYNCING is true. | |
193 DIRTY_SYNC, | |
194 BIT_TEMPS_END, | |
195 }; | |
196 | |
197 enum { | |
198 BIT_TEMPS_COUNT = BIT_TEMPS_END - BIT_TEMPS_BEGIN | |
199 }; | |
200 | |
201 struct SYNC_EXPORT EntryKernel { | |
202 private: | |
203 typedef syncer::ProtoValuePtr<sync_pb::EntitySpecifics> EntitySpecificsPtr; | |
204 typedef syncer::ProtoValuePtr<sync_pb::AttachmentMetadata> | |
205 AttachmentMetadataPtr; | |
206 | |
207 std::string string_fields[STRING_FIELDS_COUNT]; | |
208 EntitySpecificsPtr specifics_fields[PROTO_FIELDS_COUNT]; | |
209 int64_t int64_fields[INT64_FIELDS_COUNT]; | |
210 base::Time time_fields[TIME_FIELDS_COUNT]; | |
211 Id id_fields[ID_FIELDS_COUNT]; | |
212 UniquePosition unique_position_fields[UNIQUE_POSITION_FIELDS_COUNT]; | |
213 AttachmentMetadataPtr | |
214 attachment_metadata_fields[ATTACHMENT_METADATA_FIELDS_COUNT]; | |
215 std::bitset<BIT_FIELDS_COUNT> bit_fields; | |
216 std::bitset<BIT_TEMPS_COUNT> bit_temps; | |
217 | |
218 friend std::ostream& operator<<(std::ostream& s, const EntryKernel& e); | |
219 | |
220 public: | |
221 EntryKernel(); | |
222 EntryKernel(const EntryKernel& other); | |
223 ~EntryKernel(); | |
224 | |
225 // Set the dirty bit, and optionally add this entry's metahandle to | |
226 // a provided index on dirty bits in |dirty_index|. Parameter may be null, | |
227 // and will result only in setting the dirty bit of this entry. | |
228 inline void mark_dirty(syncable::MetahandleSet* dirty_index) { | |
229 if (!dirty_ && dirty_index) { | |
230 DCHECK_NE(0, ref(META_HANDLE)); | |
231 dirty_index->insert(ref(META_HANDLE)); | |
232 } | |
233 dirty_ = true; | |
234 } | |
235 | |
236 // Clear the dirty bit, and optionally remove this entry's metahandle from | |
237 // a provided index on dirty bits in |dirty_index|. Parameter may be null, | |
238 // and will result only in clearing dirty bit of this entry. | |
239 inline void clear_dirty(syncable::MetahandleSet* dirty_index) { | |
240 if (dirty_ && dirty_index) { | |
241 DCHECK_NE(0, ref(META_HANDLE)); | |
242 dirty_index->erase(ref(META_HANDLE)); | |
243 } | |
244 dirty_ = false; | |
245 } | |
246 | |
247 inline bool is_dirty() const { | |
248 return dirty_; | |
249 } | |
250 | |
251 // Setters. | |
252 inline void put(MetahandleField field, int64_t value) { | |
253 int64_fields[field - INT64_FIELDS_BEGIN] = value; | |
254 } | |
255 inline void put(Int64Field field, int64_t value) { | |
256 int64_fields[field - INT64_FIELDS_BEGIN] = value; | |
257 } | |
258 inline void put(TimeField field, const base::Time& value) { | |
259 // Round-trip to proto time format and back so that we have | |
260 // consistent time resolutions (ms). | |
261 time_fields[field - TIME_FIELDS_BEGIN] = | |
262 ProtoTimeToTime(TimeToProtoTime(value)); | |
263 } | |
264 inline void put(IdField field, const Id& value) { | |
265 id_fields[field - ID_FIELDS_BEGIN] = value; | |
266 } | |
267 inline void put(BaseVersion field, int64_t value) { | |
268 int64_fields[field - INT64_FIELDS_BEGIN] = value; | |
269 } | |
270 inline void put(IndexedBitField field, bool value) { | |
271 bit_fields[field - BIT_FIELDS_BEGIN] = value; | |
272 } | |
273 inline void put(IsDelField field, bool value) { | |
274 bit_fields[field - BIT_FIELDS_BEGIN] = value; | |
275 } | |
276 inline void put(BitField field, bool value) { | |
277 bit_fields[field - BIT_FIELDS_BEGIN] = value; | |
278 } | |
279 inline void put(StringField field, const std::string& value) { | |
280 string_fields[field - STRING_FIELDS_BEGIN] = value; | |
281 } | |
282 inline void put(ProtoField field, const sync_pb::EntitySpecifics& value) { | |
283 specifics_fields[field - PROTO_FIELDS_BEGIN].set_value(value); | |
284 } | |
285 inline void put(UniquePositionField field, const UniquePosition& value) { | |
286 unique_position_fields[field - UNIQUE_POSITION_FIELDS_BEGIN] = value; | |
287 } | |
288 inline void put(AttachmentMetadataField field, | |
289 const sync_pb::AttachmentMetadata& value) { | |
290 attachment_metadata_fields[field - ATTACHMENT_METADATA_FIELDS_BEGIN] | |
291 .set_value(value); | |
292 } | |
293 inline void put(BitTemp field, bool value) { | |
294 bit_temps[field - BIT_TEMPS_BEGIN] = value; | |
295 } | |
296 | |
297 // Const ref getters. | |
298 inline int64_t ref(MetahandleField field) const { | |
299 return int64_fields[field - INT64_FIELDS_BEGIN]; | |
300 } | |
301 inline int64_t ref(Int64Field field) const { | |
302 return int64_fields[field - INT64_FIELDS_BEGIN]; | |
303 } | |
304 inline const base::Time& ref(TimeField field) const { | |
305 return time_fields[field - TIME_FIELDS_BEGIN]; | |
306 } | |
307 inline const Id& ref(IdField field) const { | |
308 return id_fields[field - ID_FIELDS_BEGIN]; | |
309 } | |
310 inline int64_t ref(BaseVersion field) const { | |
311 return int64_fields[field - INT64_FIELDS_BEGIN]; | |
312 } | |
313 inline bool ref(IndexedBitField field) const { | |
314 return bit_fields[field - BIT_FIELDS_BEGIN]; | |
315 } | |
316 inline bool ref(IsDelField field) const { | |
317 return bit_fields[field - BIT_FIELDS_BEGIN]; | |
318 } | |
319 inline bool ref(BitField field) const { | |
320 return bit_fields[field - BIT_FIELDS_BEGIN]; | |
321 } | |
322 inline const std::string& ref(StringField field) const { | |
323 return string_fields[field - STRING_FIELDS_BEGIN]; | |
324 } | |
325 inline const sync_pb::EntitySpecifics& ref(ProtoField field) const { | |
326 return specifics_fields[field - PROTO_FIELDS_BEGIN].value(); | |
327 } | |
328 inline const UniquePosition& ref(UniquePositionField field) const { | |
329 return unique_position_fields[field - UNIQUE_POSITION_FIELDS_BEGIN]; | |
330 } | |
331 inline const sync_pb::AttachmentMetadata& ref( | |
332 AttachmentMetadataField field) const { | |
333 return attachment_metadata_fields[field - ATTACHMENT_METADATA_FIELDS_BEGIN] | |
334 .value(); | |
335 } | |
336 inline bool ref(BitTemp field) const { | |
337 return bit_temps[field - BIT_TEMPS_BEGIN]; | |
338 } | |
339 | |
340 // Non-const, mutable ref getters for object types only. | |
341 inline std::string& mutable_ref(StringField field) { | |
342 return string_fields[field - STRING_FIELDS_BEGIN]; | |
343 } | |
344 inline Id& mutable_ref(IdField field) { | |
345 return id_fields[field - ID_FIELDS_BEGIN]; | |
346 } | |
347 inline UniquePosition& mutable_ref(UniquePositionField field) { | |
348 return unique_position_fields[field - UNIQUE_POSITION_FIELDS_BEGIN]; | |
349 } | |
350 | |
351 // Deserialization methods for ::google::protobuf::MessageLite derived types. | |
352 inline void load(ProtoField field, const void* blob, int length) { | |
353 specifics_fields[field - PROTO_FIELDS_BEGIN].load(blob, length); | |
354 } | |
355 | |
356 inline void load(AttachmentMetadataField field, | |
357 const void* blob, | |
358 int length) { | |
359 attachment_metadata_fields[field - ATTACHMENT_METADATA_FIELDS_BEGIN].load( | |
360 blob, length); | |
361 } | |
362 | |
363 // Sharing data methods for ::google::protobuf::MessageLite derived types. | |
364 inline void copy(ProtoField src, ProtoField dest) { | |
365 DCHECK_NE(src, dest); | |
366 specifics_fields[dest - PROTO_FIELDS_BEGIN] = | |
367 specifics_fields[src - PROTO_FIELDS_BEGIN]; | |
368 } | |
369 | |
370 inline void copy(AttachmentMetadataField src, AttachmentMetadataField dest) { | |
371 DCHECK_NE(src, dest); | |
372 attachment_metadata_fields[dest - ATTACHMENT_METADATA_FIELDS_BEGIN] = | |
373 attachment_metadata_fields[src - ATTACHMENT_METADATA_FIELDS_BEGIN]; | |
374 } | |
375 | |
376 ModelType GetModelType() const; | |
377 ModelType GetServerModelType() const; | |
378 bool ShouldMaintainPosition() const; | |
379 bool ShouldMaintainHierarchy() const; | |
380 | |
381 // Dumps all kernel info into a DictionaryValue and returns it. | |
382 // Transfers ownership of the DictionaryValue to the caller. | |
383 // Note: |cryptographer| is an optional parameter for use in decrypting | |
384 // encrypted specifics. If it is NULL or the specifics are not decryptsble, | |
385 // they will be serialized as empty proto's. | |
386 base::DictionaryValue* ToValue(Cryptographer* cryptographer) const; | |
387 | |
388 private: | |
389 // Tracks whether this entry needs to be saved to the database. | |
390 bool dirty_; | |
391 }; | |
392 | |
393 class EntryKernelLessByMetaHandle { | |
394 public: | |
395 inline bool operator()(const EntryKernel* a, | |
396 const EntryKernel* b) const { | |
397 return a->ref(META_HANDLE) < b->ref(META_HANDLE); | |
398 } | |
399 }; | |
400 | |
401 typedef std::set<const EntryKernel*, EntryKernelLessByMetaHandle> | |
402 EntryKernelSet; | |
403 | |
404 struct EntryKernelMutation { | |
405 EntryKernel original, mutated; | |
406 }; | |
407 | |
408 typedef std::map<int64_t, EntryKernelMutation> EntryKernelMutationMap; | |
409 | |
410 typedef Immutable<EntryKernelMutationMap> ImmutableEntryKernelMutationMap; | |
411 | |
412 // Caller owns the return value. | |
413 base::DictionaryValue* EntryKernelMutationToValue( | |
414 const EntryKernelMutation& mutation); | |
415 | |
416 // Caller owns the return value. | |
417 base::ListValue* EntryKernelMutationMapToValue( | |
418 const EntryKernelMutationMap& mutations); | |
419 | |
420 std::ostream& operator<<(std::ostream& os, const EntryKernel& entry_kernel); | |
421 | |
422 } // namespace syncable | |
423 } // namespace syncer | |
424 | |
425 #endif // SYNC_SYNCABLE_ENTRY_KERNEL_H_ | |
OLD | NEW |