OLD | NEW |
| (Empty) |
1 // Copyright (c) 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 CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | |
6 #define CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | |
7 #pragma once | |
8 | |
9 #include <algorithm> | |
10 #include <bitset> | |
11 #include <cstddef> | |
12 #include <iosfwd> | |
13 #include <limits> | |
14 #include <map> | |
15 #include <set> | |
16 #include <string> | |
17 #include <vector> | |
18 | |
19 #include "base/atomicops.h" | |
20 #include "base/basictypes.h" | |
21 #include "base/compiler_specific.h" | |
22 #include "base/file_path.h" | |
23 #include "base/gtest_prod_util.h" | |
24 #include "base/location.h" | |
25 #include "base/logging.h" | |
26 #include "base/memory/ref_counted.h" | |
27 #include "base/synchronization/lock.h" | |
28 #include "base/time.h" | |
29 #include "chrome/browser/sync/internal_api/includes/report_unrecoverable_error_f
unction.h" | |
30 #include "chrome/browser/sync/internal_api/includes/unrecoverable_error_handler.
h" | |
31 #include "chrome/browser/sync/syncable/blob.h" | |
32 #include "chrome/browser/sync/syncable/dir_open_result.h" | |
33 #include "chrome/browser/sync/syncable/model_type.h" | |
34 #include "chrome/browser/sync/syncable/syncable_id.h" | |
35 #include "chrome/browser/sync/util/cryptographer.h" | |
36 #include "chrome/browser/sync/util/immutable.h" | |
37 #include "chrome/browser/sync/util/time.h" | |
38 #include "chrome/browser/sync/util/weak_handle.h" | |
39 #include "sync/protocol/sync.pb.h" | |
40 | |
41 namespace base { | |
42 class DictionaryValue; | |
43 class ListValue; | |
44 } | |
45 | |
46 namespace browser_sync { | |
47 class Encryptor; | |
48 } // namespace browser_sync | |
49 | |
50 namespace sync_api { | |
51 class ReadTransaction; | |
52 class WriteNode; | |
53 class ReadNode; | |
54 } // sync_api | |
55 | |
56 namespace syncable { | |
57 class DirectoryChangeDelegate; | |
58 class TransactionObserver; | |
59 class Entry; | |
60 | |
61 std::ostream& operator<<(std::ostream& s, const Entry& e); | |
62 | |
63 class DirectoryBackingStore; | |
64 | |
65 static const int64 kInvalidMetaHandle = 0; | |
66 | |
67 // Things you need to update if you change any of the fields below: | |
68 // - EntryKernel struct in syncable.h (this file) | |
69 // - syncable_columns.h | |
70 // - syncable_enum_conversions{.h,.cc,_unittest.cc} | |
71 // - EntryKernel::EntryKernel(), EntryKernel::ToValue(), operator<< | |
72 // for Entry in syncable.cc | |
73 // - BindFields() and UnpackEntry() in directory_backing_store.cc | |
74 // - TestSimpleFieldsPreservedDuringSaveChanges in syncable_unittest.cc | |
75 | |
76 enum { | |
77 BEGIN_FIELDS = 0, | |
78 INT64_FIELDS_BEGIN = BEGIN_FIELDS | |
79 }; | |
80 | |
81 enum MetahandleField { | |
82 // Primary key into the table. Keep this as a handle to the meta entry | |
83 // across transactions. | |
84 META_HANDLE = INT64_FIELDS_BEGIN | |
85 }; | |
86 | |
87 enum BaseVersion { | |
88 // After initial upload, the version is controlled by the server, and is | |
89 // increased whenever the data or metadata changes on the server. | |
90 BASE_VERSION = META_HANDLE + 1, | |
91 }; | |
92 | |
93 enum Int64Field { | |
94 SERVER_VERSION = BASE_VERSION + 1, | |
95 | |
96 // A numeric position value that indicates the relative ordering of | |
97 // this object among its siblings. | |
98 SERVER_POSITION_IN_PARENT, | |
99 | |
100 LOCAL_EXTERNAL_ID, // ID of an item in the external local storage that this | |
101 // entry is associated with. (such as bookmarks.js) | |
102 | |
103 INT64_FIELDS_END | |
104 }; | |
105 | |
106 enum { | |
107 INT64_FIELDS_COUNT = INT64_FIELDS_END - INT64_FIELDS_BEGIN, | |
108 TIME_FIELDS_BEGIN = INT64_FIELDS_END, | |
109 }; | |
110 | |
111 enum TimeField { | |
112 MTIME = TIME_FIELDS_BEGIN, | |
113 SERVER_MTIME, | |
114 CTIME, | |
115 SERVER_CTIME, | |
116 TIME_FIELDS_END, | |
117 }; | |
118 | |
119 enum { | |
120 TIME_FIELDS_COUNT = TIME_FIELDS_END - TIME_FIELDS_BEGIN, | |
121 ID_FIELDS_BEGIN = TIME_FIELDS_END, | |
122 }; | |
123 | |
124 enum IdField { | |
125 // Code in InitializeTables relies on ID being the first IdField value. | |
126 ID = ID_FIELDS_BEGIN, | |
127 PARENT_ID, | |
128 SERVER_PARENT_ID, | |
129 | |
130 PREV_ID, | |
131 NEXT_ID, | |
132 ID_FIELDS_END | |
133 }; | |
134 | |
135 enum { | |
136 ID_FIELDS_COUNT = ID_FIELDS_END - ID_FIELDS_BEGIN, | |
137 BIT_FIELDS_BEGIN = ID_FIELDS_END | |
138 }; | |
139 | |
140 enum IndexedBitField { | |
141 IS_UNSYNCED = BIT_FIELDS_BEGIN, | |
142 IS_UNAPPLIED_UPDATE, | |
143 INDEXED_BIT_FIELDS_END, | |
144 }; | |
145 | |
146 enum IsDelField { | |
147 IS_DEL = INDEXED_BIT_FIELDS_END, | |
148 }; | |
149 | |
150 enum BitField { | |
151 IS_DIR = IS_DEL + 1, | |
152 SERVER_IS_DIR, | |
153 SERVER_IS_DEL, | |
154 BIT_FIELDS_END | |
155 }; | |
156 | |
157 enum { | |
158 BIT_FIELDS_COUNT = BIT_FIELDS_END - BIT_FIELDS_BEGIN, | |
159 STRING_FIELDS_BEGIN = BIT_FIELDS_END | |
160 }; | |
161 | |
162 enum StringField { | |
163 // Name, will be truncated by server. Can be duplicated in a folder. | |
164 NON_UNIQUE_NAME = STRING_FIELDS_BEGIN, | |
165 // The server version of |NON_UNIQUE_NAME|. | |
166 SERVER_NON_UNIQUE_NAME, | |
167 | |
168 // A tag string which identifies this node as a particular top-level | |
169 // permanent object. The tag can be thought of as a unique key that | |
170 // identifies a singleton instance. | |
171 UNIQUE_SERVER_TAG, // Tagged by the server | |
172 UNIQUE_CLIENT_TAG, // Tagged by the client | |
173 STRING_FIELDS_END, | |
174 }; | |
175 | |
176 enum { | |
177 STRING_FIELDS_COUNT = STRING_FIELDS_END - STRING_FIELDS_BEGIN, | |
178 PROTO_FIELDS_BEGIN = STRING_FIELDS_END | |
179 }; | |
180 | |
181 // From looking at the sqlite3 docs, it's not directly stated, but it | |
182 // seems the overhead for storing a NULL blob is very small. | |
183 enum ProtoField { | |
184 SPECIFICS = PROTO_FIELDS_BEGIN, | |
185 SERVER_SPECIFICS, | |
186 BASE_SERVER_SPECIFICS, | |
187 PROTO_FIELDS_END, | |
188 }; | |
189 | |
190 enum { | |
191 PROTO_FIELDS_COUNT = PROTO_FIELDS_END - PROTO_FIELDS_BEGIN | |
192 }; | |
193 | |
194 enum { | |
195 FIELD_COUNT = PROTO_FIELDS_END, | |
196 // Past this point we have temporaries, stored in memory only. | |
197 BEGIN_TEMPS = PROTO_FIELDS_END, | |
198 BIT_TEMPS_BEGIN = BEGIN_TEMPS, | |
199 }; | |
200 | |
201 enum BitTemp { | |
202 SYNCING = BIT_TEMPS_BEGIN, | |
203 BIT_TEMPS_END, | |
204 }; | |
205 | |
206 enum { | |
207 BIT_TEMPS_COUNT = BIT_TEMPS_END - BIT_TEMPS_BEGIN | |
208 }; | |
209 | |
210 class BaseTransaction; | |
211 class WriteTransaction; | |
212 class ReadTransaction; | |
213 class Directory; | |
214 | |
215 // Instead of: | |
216 // Entry e = transaction.GetById(id); | |
217 // use: | |
218 // Entry e(transaction, GET_BY_ID, id); | |
219 // | |
220 // Why? The former would require a copy constructor, and it would be difficult | |
221 // to enforce that an entry never outlived its transaction if there were a copy | |
222 // constructor. | |
223 enum GetById { | |
224 GET_BY_ID | |
225 }; | |
226 | |
227 enum GetByClientTag { | |
228 GET_BY_CLIENT_TAG | |
229 }; | |
230 | |
231 enum GetByServerTag { | |
232 GET_BY_SERVER_TAG | |
233 }; | |
234 | |
235 enum GetByHandle { | |
236 GET_BY_HANDLE | |
237 }; | |
238 | |
239 enum Create { | |
240 CREATE | |
241 }; | |
242 | |
243 enum CreateNewUpdateItem { | |
244 CREATE_NEW_UPDATE_ITEM | |
245 }; | |
246 | |
247 typedef std::set<int64> MetahandleSet; | |
248 | |
249 // TODO(akalin): Move EntryKernel and related into its own header file. | |
250 | |
251 // Why the singular enums? So the code compile-time dispatches instead of | |
252 // runtime dispatches as it would with a single enum and an if() statement. | |
253 | |
254 // The EntryKernel class contains the actual data for an entry. | |
255 struct EntryKernel { | |
256 private: | |
257 std::string string_fields[STRING_FIELDS_COUNT]; | |
258 sync_pb::EntitySpecifics specifics_fields[PROTO_FIELDS_COUNT]; | |
259 int64 int64_fields[INT64_FIELDS_COUNT]; | |
260 base::Time time_fields[TIME_FIELDS_COUNT]; | |
261 Id id_fields[ID_FIELDS_COUNT]; | |
262 std::bitset<BIT_FIELDS_COUNT> bit_fields; | |
263 std::bitset<BIT_TEMPS_COUNT> bit_temps; | |
264 | |
265 public: | |
266 EntryKernel(); | |
267 ~EntryKernel(); | |
268 | |
269 // Set the dirty bit, and optionally add this entry's metahandle to | |
270 // a provided index on dirty bits in |dirty_index|. Parameter may be null, | |
271 // and will result only in setting the dirty bit of this entry. | |
272 inline void mark_dirty(syncable::MetahandleSet* dirty_index) { | |
273 if (!dirty_ && dirty_index) { | |
274 DCHECK_NE(0, ref(META_HANDLE)); | |
275 dirty_index->insert(ref(META_HANDLE)); | |
276 } | |
277 dirty_ = true; | |
278 } | |
279 | |
280 // Clear the dirty bit, and optionally remove this entry's metahandle from | |
281 // a provided index on dirty bits in |dirty_index|. Parameter may be null, | |
282 // and will result only in clearing dirty bit of this entry. | |
283 inline void clear_dirty(syncable::MetahandleSet* dirty_index) { | |
284 if (dirty_ && dirty_index) { | |
285 DCHECK_NE(0, ref(META_HANDLE)); | |
286 dirty_index->erase(ref(META_HANDLE)); | |
287 } | |
288 dirty_ = false; | |
289 } | |
290 | |
291 inline bool is_dirty() const { | |
292 return dirty_; | |
293 } | |
294 | |
295 // Setters. | |
296 inline void put(MetahandleField field, int64 value) { | |
297 int64_fields[field - INT64_FIELDS_BEGIN] = value; | |
298 } | |
299 inline void put(Int64Field field, int64 value) { | |
300 int64_fields[field - INT64_FIELDS_BEGIN] = value; | |
301 } | |
302 inline void put(TimeField field, const base::Time& value) { | |
303 // Round-trip to proto time format and back so that we have | |
304 // consistent time resolutions (ms). | |
305 time_fields[field - TIME_FIELDS_BEGIN] = | |
306 browser_sync::ProtoTimeToTime( | |
307 browser_sync::TimeToProtoTime(value)); | |
308 } | |
309 inline void put(IdField field, const Id& value) { | |
310 id_fields[field - ID_FIELDS_BEGIN] = value; | |
311 } | |
312 inline void put(BaseVersion field, int64 value) { | |
313 int64_fields[field - INT64_FIELDS_BEGIN] = value; | |
314 } | |
315 inline void put(IndexedBitField field, bool value) { | |
316 bit_fields[field - BIT_FIELDS_BEGIN] = value; | |
317 } | |
318 inline void put(IsDelField field, bool value) { | |
319 bit_fields[field - BIT_FIELDS_BEGIN] = value; | |
320 } | |
321 inline void put(BitField field, bool value) { | |
322 bit_fields[field - BIT_FIELDS_BEGIN] = value; | |
323 } | |
324 inline void put(StringField field, const std::string& value) { | |
325 string_fields[field - STRING_FIELDS_BEGIN] = value; | |
326 } | |
327 inline void put(ProtoField field, const sync_pb::EntitySpecifics& value) { | |
328 specifics_fields[field - PROTO_FIELDS_BEGIN].CopyFrom(value); | |
329 } | |
330 inline void put(BitTemp field, bool value) { | |
331 bit_temps[field - BIT_TEMPS_BEGIN] = value; | |
332 } | |
333 | |
334 // Const ref getters. | |
335 inline int64 ref(MetahandleField field) const { | |
336 return int64_fields[field - INT64_FIELDS_BEGIN]; | |
337 } | |
338 inline int64 ref(Int64Field field) const { | |
339 return int64_fields[field - INT64_FIELDS_BEGIN]; | |
340 } | |
341 inline const base::Time& ref(TimeField field) const { | |
342 return time_fields[field - TIME_FIELDS_BEGIN]; | |
343 } | |
344 inline const Id& ref(IdField field) const { | |
345 return id_fields[field - ID_FIELDS_BEGIN]; | |
346 } | |
347 inline int64 ref(BaseVersion field) const { | |
348 return int64_fields[field - INT64_FIELDS_BEGIN]; | |
349 } | |
350 inline bool ref(IndexedBitField field) const { | |
351 return bit_fields[field - BIT_FIELDS_BEGIN]; | |
352 } | |
353 inline bool ref(IsDelField field) const { | |
354 return bit_fields[field - BIT_FIELDS_BEGIN]; | |
355 } | |
356 inline bool ref(BitField field) const { | |
357 return bit_fields[field - BIT_FIELDS_BEGIN]; | |
358 } | |
359 inline const std::string& ref(StringField field) const { | |
360 return string_fields[field - STRING_FIELDS_BEGIN]; | |
361 } | |
362 inline const sync_pb::EntitySpecifics& ref(ProtoField field) const { | |
363 return specifics_fields[field - PROTO_FIELDS_BEGIN]; | |
364 } | |
365 inline bool ref(BitTemp field) const { | |
366 return bit_temps[field - BIT_TEMPS_BEGIN]; | |
367 } | |
368 | |
369 // Non-const, mutable ref getters for object types only. | |
370 inline std::string& mutable_ref(StringField field) { | |
371 return string_fields[field - STRING_FIELDS_BEGIN]; | |
372 } | |
373 inline sync_pb::EntitySpecifics& mutable_ref(ProtoField field) { | |
374 return specifics_fields[field - PROTO_FIELDS_BEGIN]; | |
375 } | |
376 inline Id& mutable_ref(IdField field) { | |
377 return id_fields[field - ID_FIELDS_BEGIN]; | |
378 } | |
379 | |
380 syncable::ModelType GetServerModelType() const; | |
381 | |
382 // Does a case in-sensitive search for a given string, which must be | |
383 // lower case. | |
384 bool ContainsString(const std::string& lowercase_query) const; | |
385 | |
386 // Dumps all kernel info into a DictionaryValue and returns it. | |
387 // Transfers ownership of the DictionaryValue to the caller. | |
388 base::DictionaryValue* ToValue() const; | |
389 | |
390 private: | |
391 // Tracks whether this entry needs to be saved to the database. | |
392 bool dirty_; | |
393 }; | |
394 | |
395 // A read-only meta entry. | |
396 class Entry { | |
397 friend class Directory; | |
398 friend std::ostream& operator << (std::ostream& s, const Entry& e); | |
399 | |
400 public: | |
401 // After constructing, you must check good() to test whether the Get | |
402 // succeeded. | |
403 Entry(BaseTransaction* trans, GetByHandle, int64 handle); | |
404 Entry(BaseTransaction* trans, GetById, const Id& id); | |
405 Entry(BaseTransaction* trans, GetByServerTag, const std::string& tag); | |
406 Entry(BaseTransaction* trans, GetByClientTag, const std::string& tag); | |
407 | |
408 bool good() const { return 0 != kernel_; } | |
409 | |
410 BaseTransaction* trans() const { return basetrans_; } | |
411 | |
412 // Field accessors. | |
413 inline int64 Get(MetahandleField field) const { | |
414 DCHECK(kernel_); | |
415 return kernel_->ref(field); | |
416 } | |
417 inline Id Get(IdField field) const { | |
418 DCHECK(kernel_); | |
419 return kernel_->ref(field); | |
420 } | |
421 inline int64 Get(Int64Field field) const { | |
422 DCHECK(kernel_); | |
423 return kernel_->ref(field); | |
424 } | |
425 inline const base::Time& Get(TimeField field) const { | |
426 DCHECK(kernel_); | |
427 return kernel_->ref(field); | |
428 } | |
429 inline int64 Get(BaseVersion field) const { | |
430 DCHECK(kernel_); | |
431 return kernel_->ref(field); | |
432 } | |
433 inline bool Get(IndexedBitField field) const { | |
434 DCHECK(kernel_); | |
435 return kernel_->ref(field); | |
436 } | |
437 inline bool Get(IsDelField field) const { | |
438 DCHECK(kernel_); | |
439 return kernel_->ref(field); | |
440 } | |
441 inline bool Get(BitField field) const { | |
442 DCHECK(kernel_); | |
443 return kernel_->ref(field); | |
444 } | |
445 const std::string& Get(StringField field) const; | |
446 inline const sync_pb::EntitySpecifics& Get(ProtoField field) const { | |
447 DCHECK(kernel_); | |
448 return kernel_->ref(field); | |
449 } | |
450 inline bool Get(BitTemp field) const { | |
451 DCHECK(kernel_); | |
452 return kernel_->ref(field); | |
453 } | |
454 | |
455 ModelType GetServerModelType() const; | |
456 ModelType GetModelType() const; | |
457 | |
458 inline bool ExistsOnClientBecauseNameIsNonEmpty() const { | |
459 DCHECK(kernel_); | |
460 return !kernel_->ref(NON_UNIQUE_NAME).empty(); | |
461 } | |
462 | |
463 inline bool IsRoot() const { | |
464 DCHECK(kernel_); | |
465 return kernel_->ref(ID).IsRoot(); | |
466 } | |
467 | |
468 Directory* dir() const; | |
469 | |
470 const EntryKernel GetKernelCopy() const { | |
471 return *kernel_; | |
472 } | |
473 | |
474 // Compute a local predecessor position for |update_item|, based on its | |
475 // absolute server position. The returned ID will be a valid predecessor | |
476 // under SERVER_PARENT_ID that is consistent with the | |
477 // SERVER_POSITION_IN_PARENT ordering. | |
478 Id ComputePrevIdFromServerPosition(const Id& parent_id) const; | |
479 | |
480 // Dumps all entry info into a DictionaryValue and returns it. | |
481 // Transfers ownership of the DictionaryValue to the caller. | |
482 base::DictionaryValue* ToValue() const; | |
483 | |
484 protected: // Don't allow creation on heap, except by sync API wrappers. | |
485 friend class sync_api::ReadNode; | |
486 void* operator new(size_t size) { return (::operator new)(size); } | |
487 | |
488 inline explicit Entry(BaseTransaction* trans) | |
489 : basetrans_(trans), | |
490 kernel_(NULL) { } | |
491 | |
492 protected: | |
493 BaseTransaction* const basetrans_; | |
494 | |
495 EntryKernel* kernel_; | |
496 | |
497 private: | |
498 DISALLOW_COPY_AND_ASSIGN(Entry); | |
499 }; | |
500 | |
501 // A mutable meta entry. Changes get committed to the database when the | |
502 // WriteTransaction is destroyed. | |
503 class MutableEntry : public Entry { | |
504 friend class WriteTransaction; | |
505 friend class Directory; | |
506 void Init(WriteTransaction* trans, const Id& parent_id, | |
507 const std::string& name); | |
508 | |
509 public: | |
510 MutableEntry(WriteTransaction* trans, Create, const Id& parent_id, | |
511 const std::string& name); | |
512 MutableEntry(WriteTransaction* trans, CreateNewUpdateItem, const Id& id); | |
513 MutableEntry(WriteTransaction* trans, GetByHandle, int64); | |
514 MutableEntry(WriteTransaction* trans, GetById, const Id&); | |
515 MutableEntry(WriteTransaction* trans, GetByClientTag, const std::string& tag); | |
516 MutableEntry(WriteTransaction* trans, GetByServerTag, const std::string& tag); | |
517 | |
518 inline WriteTransaction* write_transaction() const { | |
519 return write_transaction_; | |
520 } | |
521 | |
522 // Field Accessors. Some of them trigger the re-indexing of the entry. | |
523 // Return true on success, return false on failure, which means | |
524 // that putting the value would have caused a duplicate in the index. | |
525 // TODO(chron): Remove some of these unecessary return values. | |
526 bool Put(Int64Field field, const int64& value); | |
527 bool Put(TimeField field, const base::Time& value); | |
528 bool Put(IdField field, const Id& value); | |
529 | |
530 // Do a simple property-only update if the PARENT_ID field. Use with caution. | |
531 // | |
532 // The normal Put(IS_PARENT) call will move the item to the front of the | |
533 // sibling order to maintain the linked list invariants when the parent | |
534 // changes. That's usually what you want to do, but it's inappropriate | |
535 // when the caller is trying to change the parent ID of a the whole set | |
536 // of children (e.g. because the ID changed during a commit). For those | |
537 // cases, there's this function. It will corrupt the sibling ordering | |
538 // if you're not careful. | |
539 void PutParentIdPropertyOnly(const Id& parent_id); | |
540 | |
541 bool Put(StringField field, const std::string& value); | |
542 bool Put(BaseVersion field, int64 value); | |
543 | |
544 bool Put(ProtoField field, const sync_pb::EntitySpecifics& value); | |
545 bool Put(BitField field, bool value); | |
546 inline bool Put(IsDelField field, bool value) { | |
547 return PutIsDel(value); | |
548 } | |
549 bool Put(IndexedBitField field, bool value); | |
550 | |
551 // Sets the position of this item, and updates the entry kernels of the | |
552 // adjacent siblings so that list invariants are maintained. Returns false | |
553 // and fails if |predecessor_id| does not identify a sibling. Pass the root | |
554 // ID to put the node in first position. | |
555 bool PutPredecessor(const Id& predecessor_id); | |
556 | |
557 bool Put(BitTemp field, bool value); | |
558 | |
559 protected: | |
560 syncable::MetahandleSet* GetDirtyIndexHelper(); | |
561 | |
562 bool PutIsDel(bool value); | |
563 | |
564 private: // Don't allow creation on heap, except by sync API wrappers. | |
565 friend class sync_api::WriteNode; | |
566 void* operator new(size_t size) { return (::operator new)(size); } | |
567 | |
568 bool PutImpl(StringField field, const std::string& value); | |
569 bool PutUniqueClientTag(const std::string& value); | |
570 | |
571 // Adjusts the successor and predecessor entries so that they no longer | |
572 // refer to this entry. | |
573 bool UnlinkFromOrder(); | |
574 | |
575 // Kind of redundant. We should reduce the number of pointers | |
576 // floating around if at all possible. Could we store this in Directory? | |
577 // Scope: Set on construction, never changed after that. | |
578 WriteTransaction* const write_transaction_; | |
579 | |
580 protected: | |
581 MutableEntry(); | |
582 | |
583 DISALLOW_COPY_AND_ASSIGN(MutableEntry); | |
584 }; | |
585 | |
586 template <typename FieldType, FieldType field_index> class LessField; | |
587 | |
588 class EntryKernelLessByMetaHandle { | |
589 public: | |
590 inline bool operator()(const EntryKernel& a, | |
591 const EntryKernel& b) const { | |
592 return a.ref(META_HANDLE) < b.ref(META_HANDLE); | |
593 } | |
594 }; | |
595 typedef std::set<EntryKernel, EntryKernelLessByMetaHandle> EntryKernelSet; | |
596 | |
597 struct EntryKernelMutation { | |
598 EntryKernel original, mutated; | |
599 }; | |
600 typedef std::map<int64, EntryKernelMutation> EntryKernelMutationMap; | |
601 | |
602 typedef browser_sync::Immutable<EntryKernelMutationMap> | |
603 ImmutableEntryKernelMutationMap; | |
604 | |
605 // A WriteTransaction has a writer tag describing which body of code is doing | |
606 // the write. This is defined up here since WriteTransactionInfo also contains | |
607 // one. | |
608 enum WriterTag { | |
609 INVALID, | |
610 SYNCER, | |
611 AUTHWATCHER, | |
612 UNITTEST, | |
613 VACUUM_AFTER_SAVE, | |
614 PURGE_ENTRIES, | |
615 SYNCAPI | |
616 }; | |
617 | |
618 // Make sure to update this if you update WriterTag. | |
619 std::string WriterTagToString(WriterTag writer_tag); | |
620 | |
621 struct WriteTransactionInfo { | |
622 WriteTransactionInfo(int64 id, | |
623 tracked_objects::Location location, | |
624 WriterTag writer, | |
625 ImmutableEntryKernelMutationMap mutations); | |
626 WriteTransactionInfo(); | |
627 ~WriteTransactionInfo(); | |
628 | |
629 // Caller owns the return value. | |
630 base::DictionaryValue* ToValue(size_t max_mutations_size) const; | |
631 | |
632 int64 id; | |
633 // If tracked_objects::Location becomes assignable, we can use that | |
634 // instead. | |
635 std::string location_string; | |
636 WriterTag writer; | |
637 ImmutableEntryKernelMutationMap mutations; | |
638 }; | |
639 | |
640 typedef | |
641 browser_sync::Immutable<WriteTransactionInfo> | |
642 ImmutableWriteTransactionInfo; | |
643 | |
644 // Caller owns the return value. | |
645 base::DictionaryValue* EntryKernelMutationToValue( | |
646 const EntryKernelMutation& mutation); | |
647 | |
648 // Caller owns the return value. | |
649 base::ListValue* EntryKernelMutationMapToValue( | |
650 const EntryKernelMutationMap& mutations); | |
651 | |
652 // How syncable indices & Indexers work. | |
653 // | |
654 // The syncable Directory maintains several indices on the Entries it tracks. | |
655 // The indices follow a common pattern: | |
656 // (a) The index allows efficient lookup of an Entry* with particular | |
657 // field values. This is done by use of a std::set<> and a custom | |
658 // comparator. | |
659 // (b) There may be conditions for inclusion in the index -- for example, | |
660 // deleted items might not be indexed. | |
661 // (c) Because the index set contains only Entry*, one must be careful | |
662 // to remove Entries from the set before updating the value of | |
663 // an indexed field. | |
664 // The traits of an index are a Comparator (to define the set ordering) and a | |
665 // ShouldInclude function (to define the conditions for inclusion). For each | |
666 // index, the traits are grouped into a class called an Indexer which | |
667 // can be used as a template type parameter. | |
668 | |
669 // Traits type for metahandle index. | |
670 struct MetahandleIndexer { | |
671 // This index is of the metahandle field values. | |
672 typedef LessField<MetahandleField, META_HANDLE> Comparator; | |
673 | |
674 // This index includes all entries. | |
675 inline static bool ShouldInclude(const EntryKernel* a) { | |
676 return true; | |
677 } | |
678 }; | |
679 | |
680 // Traits type for ID field index. | |
681 struct IdIndexer { | |
682 // This index is of the ID field values. | |
683 typedef LessField<IdField, ID> Comparator; | |
684 | |
685 // This index includes all entries. | |
686 inline static bool ShouldInclude(const EntryKernel* a) { | |
687 return true; | |
688 } | |
689 }; | |
690 | |
691 // Traits type for unique client tag index. | |
692 struct ClientTagIndexer { | |
693 // This index is of the client-tag values. | |
694 typedef LessField<StringField, UNIQUE_CLIENT_TAG> Comparator; | |
695 | |
696 // Items are only in this index if they have a non-empty client tag value. | |
697 static bool ShouldInclude(const EntryKernel* a); | |
698 }; | |
699 | |
700 // This index contains EntryKernels ordered by parent ID and metahandle. | |
701 // It allows efficient lookup of the children of a given parent. | |
702 struct ParentIdAndHandleIndexer { | |
703 // This index is of the parent ID and metahandle. We use a custom | |
704 // comparator. | |
705 class Comparator { | |
706 public: | |
707 bool operator() (const syncable::EntryKernel* a, | |
708 const syncable::EntryKernel* b) const; | |
709 }; | |
710 | |
711 // This index does not include deleted items. | |
712 static bool ShouldInclude(const EntryKernel* a); | |
713 }; | |
714 | |
715 // Given an Indexer providing the semantics of an index, defines the | |
716 // set type used to actually contain the index. | |
717 template <typename Indexer> | |
718 struct Index { | |
719 typedef std::set<EntryKernel*, typename Indexer::Comparator> Set; | |
720 }; | |
721 | |
722 // The name Directory in this case means the entire directory | |
723 // structure within a single user account. | |
724 // | |
725 // The db is protected against concurrent modification by a reader/ | |
726 // writer lock, negotiated by the ReadTransaction and WriteTransaction | |
727 // friend classes. The in-memory indices are protected against | |
728 // concurrent modification by the kernel lock. | |
729 // | |
730 // All methods which require the reader/writer lock to be held either | |
731 // are protected and only called from friends in a transaction | |
732 // or are public and take a Transaction* argument. | |
733 // | |
734 // All methods which require the kernel lock to be already held take a | |
735 // ScopeKernelLock* argument. | |
736 // | |
737 // To prevent deadlock, the reader writer transaction lock must always | |
738 // be held before acquiring the kernel lock. | |
739 class ScopedKernelLock; | |
740 class IdFilter; | |
741 | |
742 class Directory { | |
743 friend class BaseTransaction; | |
744 friend class Entry; | |
745 friend class MutableEntry; | |
746 friend class ReadTransaction; | |
747 friend class ReadTransactionWithoutDB; | |
748 friend class ScopedKernelLock; | |
749 friend class ScopedKernelUnlock; | |
750 friend class WriteTransaction; | |
751 friend class SyncableDirectoryTest; | |
752 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, | |
753 TakeSnapshotGetsAllDirtyHandlesTest); | |
754 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, | |
755 TakeSnapshotGetsOnlyDirtyHandlesTest); | |
756 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, | |
757 TakeSnapshotGetsMetahandlesToPurge); | |
758 | |
759 public: | |
760 static const FilePath::CharType kSyncDatabaseFilename[]; | |
761 | |
762 // Various data that the Directory::Kernel we are backing (persisting data | |
763 // for) needs saved across runs of the application. | |
764 struct PersistedKernelInfo { | |
765 PersistedKernelInfo(); | |
766 ~PersistedKernelInfo(); | |
767 | |
768 // Set the |download_progress| entry for the given model to a | |
769 // "first sync" start point. When such a value is sent to the server, | |
770 // a full download of all objects of the model will be initiated. | |
771 void reset_download_progress(ModelType model_type); | |
772 | |
773 // Last sync timestamp fetched from the server. | |
774 sync_pb::DataTypeProgressMarker download_progress[MODEL_TYPE_COUNT]; | |
775 // true iff we ever reached the end of the changelog. | |
776 ModelTypeSet initial_sync_ended; | |
777 // The store birthday we were given by the server. Contents are opaque to | |
778 // the client. | |
779 std::string store_birthday; | |
780 // The next local ID that has not been used with this cache-GUID. | |
781 int64 next_id; | |
782 // The persisted notification state. | |
783 std::string notification_state; | |
784 }; | |
785 | |
786 // What the Directory needs on initialization to create itself and its Kernel. | |
787 // Filled by DirectoryBackingStore::Load. | |
788 struct KernelLoadInfo { | |
789 PersistedKernelInfo kernel_info; | |
790 std::string cache_guid; // Created on first initialization, never changes. | |
791 int64 max_metahandle; // Computed (using sql MAX aggregate) on init. | |
792 KernelLoadInfo() : max_metahandle(0) { | |
793 } | |
794 }; | |
795 | |
796 // The dirty/clean state of kernel fields backed by the share_info table. | |
797 // This is public so it can be used in SaveChangesSnapshot for persistence. | |
798 enum KernelShareInfoStatus { | |
799 KERNEL_SHARE_INFO_INVALID, | |
800 KERNEL_SHARE_INFO_VALID, | |
801 KERNEL_SHARE_INFO_DIRTY | |
802 }; | |
803 | |
804 // When the Directory is told to SaveChanges, a SaveChangesSnapshot is | |
805 // constructed and forms a consistent snapshot of what needs to be sent to | |
806 // the backing store. | |
807 struct SaveChangesSnapshot { | |
808 SaveChangesSnapshot(); | |
809 ~SaveChangesSnapshot(); | |
810 | |
811 KernelShareInfoStatus kernel_info_status; | |
812 PersistedKernelInfo kernel_info; | |
813 EntryKernelSet dirty_metas; | |
814 MetahandleSet metahandles_to_purge; | |
815 }; | |
816 | |
817 // Does not take ownership of |encryptor|. | |
818 // |report_unrecoverable_error_function| may be NULL. | |
819 Directory( | |
820 browser_sync::Encryptor* encryptor, | |
821 browser_sync::UnrecoverableErrorHandler* unrecoverable_error_handler, | |
822 browser_sync::ReportUnrecoverableErrorFunction | |
823 report_unrecoverable_error_function); | |
824 virtual ~Directory(); | |
825 | |
826 // Does not take ownership of |delegate|, which must not be NULL. | |
827 // Starts sending events to |delegate| if the returned result is | |
828 // OPENED. Note that events to |delegate| may be sent from *any* | |
829 // thread. |transaction_observer| must be initialized. | |
830 DirOpenResult Open(const FilePath& file_path, const std::string& name, | |
831 DirectoryChangeDelegate* delegate, | |
832 const browser_sync::WeakHandle<TransactionObserver>& | |
833 transaction_observer); | |
834 | |
835 // Same as above, but does not create a file to persist the database. This is | |
836 // useful for tests where we were not planning to persist this data and don't | |
837 // want to pay the performance penalty of using a real database. | |
838 DirOpenResult OpenInMemoryForTest( | |
839 const std::string& name, DirectoryChangeDelegate* delegate, | |
840 const browser_sync::WeakHandle<TransactionObserver>& | |
841 transaction_observer); | |
842 | |
843 // Stops sending events to the delegate and the transaction | |
844 // observer. | |
845 void Close(); | |
846 | |
847 int64 NextMetahandle(); | |
848 // Always returns a negative id. Positive client ids are generated | |
849 // by the server only. | |
850 Id NextId(); | |
851 | |
852 bool good() const { return NULL != store_; } | |
853 | |
854 // The download progress is an opaque token provided by the sync server | |
855 // to indicate the continuation state of the next GetUpdates operation. | |
856 void GetDownloadProgress( | |
857 ModelType type, | |
858 sync_pb::DataTypeProgressMarker* value_out) const; | |
859 void GetDownloadProgressAsString( | |
860 ModelType type, | |
861 std::string* value_out) const; | |
862 size_t GetEntriesCount() const; | |
863 void SetDownloadProgress( | |
864 ModelType type, | |
865 const sync_pb::DataTypeProgressMarker& value); | |
866 | |
867 bool initial_sync_ended_for_type(ModelType type) const; | |
868 void set_initial_sync_ended_for_type(ModelType type, bool value); | |
869 | |
870 const std::string& name() const { return kernel_->name; } | |
871 | |
872 // (Account) Store birthday is opaque to the client, so we keep it in the | |
873 // format it is in the proto buffer in case we switch to a binary birthday | |
874 // later. | |
875 std::string store_birthday() const; | |
876 void set_store_birthday(const std::string& store_birthday); | |
877 | |
878 std::string GetNotificationState() const; | |
879 void SetNotificationState(const std::string& notification_state); | |
880 | |
881 // Unique to each account / client pair. | |
882 std::string cache_guid() const; | |
883 | |
884 // Returns a pointer to our cryptographer. Does not transfer ownership. The | |
885 // cryptographer is not thread safe; it should not be accessed after the | |
886 // transaction has been released. | |
887 browser_sync::Cryptographer* GetCryptographer(const BaseTransaction* trans); | |
888 | |
889 // Returns true if the directory had encountered an unrecoverable error. | |
890 // Note: Any function in |Directory| that can be called without holding a | |
891 // transaction need to check if the Directory already has an unrecoverable | |
892 // error on it. | |
893 bool unrecoverable_error_set(const BaseTransaction* trans) const; | |
894 | |
895 // Called to immediately report an unrecoverable error (but don't | |
896 // propagate it up). | |
897 void ReportUnrecoverableError() { | |
898 if (report_unrecoverable_error_function_) { | |
899 report_unrecoverable_error_function_(); | |
900 } | |
901 } | |
902 | |
903 // Called to set the unrecoverable error on the directory and to propagate | |
904 // the error to upper layers. | |
905 void OnUnrecoverableError(const BaseTransaction* trans, | |
906 const tracked_objects::Location& location, | |
907 const std::string & message); | |
908 | |
909 protected: // for friends, mainly used by Entry constructors | |
910 virtual EntryKernel* GetEntryByHandle(int64 handle); | |
911 virtual EntryKernel* GetEntryByHandle(int64 metahandle, | |
912 ScopedKernelLock* lock); | |
913 virtual EntryKernel* GetEntryById(const Id& id); | |
914 EntryKernel* GetEntryByServerTag(const std::string& tag); | |
915 virtual EntryKernel* GetEntryByClientTag(const std::string& tag); | |
916 EntryKernel* GetRootEntry(); | |
917 bool ReindexId(WriteTransaction* trans, EntryKernel* const entry, | |
918 const Id& new_id); | |
919 bool ReindexParentId(WriteTransaction* trans, EntryKernel* const entry, | |
920 const Id& new_parent_id); | |
921 void ClearDirtyMetahandles(); | |
922 | |
923 // These don't do semantic checking. | |
924 // The semantic checking is implemented higher up. | |
925 bool UnlinkEntryFromOrder(EntryKernel* entry, | |
926 WriteTransaction* trans, | |
927 ScopedKernelLock* lock); | |
928 | |
929 DirOpenResult OpenImpl( | |
930 DirectoryBackingStore* store, const std::string& name, | |
931 DirectoryChangeDelegate* delegate, | |
932 const browser_sync::WeakHandle<TransactionObserver>& | |
933 transaction_observer); | |
934 | |
935 private: | |
936 // These private versions expect the kernel lock to already be held | |
937 // before calling. | |
938 EntryKernel* GetEntryById(const Id& id, ScopedKernelLock* const lock); | |
939 | |
940 template <class T> void TestAndSet(T* kernel_data, const T* data_to_set); | |
941 | |
942 public: | |
943 typedef std::vector<int64> ChildHandles; | |
944 | |
945 // Returns the child meta handles (even those for deleted/unlinked | |
946 // nodes) for given parent id. Clears |result| if there are no | |
947 // children. | |
948 bool GetChildHandlesById(BaseTransaction*, const Id& parent_id, | |
949 ChildHandles* result); | |
950 | |
951 // Returns the child meta handles (even those for deleted/unlinked | |
952 // nodes) for given meta handle. Clears |result| if there are no | |
953 // children. | |
954 bool GetChildHandlesByHandle(BaseTransaction*, int64 handle, | |
955 ChildHandles* result); | |
956 | |
957 // Returns true iff |id| has children. | |
958 bool HasChildren(BaseTransaction* trans, const Id& id); | |
959 | |
960 // Find the first child in the positional ordering under a parent, | |
961 // and fill in |*first_child_id| with its id. Fills in a root Id if | |
962 // parent has no children. Returns true if the first child was | |
963 // successfully found, or false if an error was encountered. | |
964 bool GetFirstChildId(BaseTransaction* trans, const Id& parent_id, | |
965 Id* first_child_id) WARN_UNUSED_RESULT; | |
966 | |
967 // Find the last child in the positional ordering under a parent, | |
968 // and fill in |*first_child_id| with its id. Fills in a root Id if | |
969 // parent has no children. Returns true if the first child was | |
970 // successfully found, or false if an error was encountered. | |
971 bool GetLastChildIdForTest(BaseTransaction* trans, const Id& parent_id, | |
972 Id* last_child_id) WARN_UNUSED_RESULT; | |
973 | |
974 // Compute a local predecessor position for |update_item|. The position | |
975 // is determined by the SERVER_POSITION_IN_PARENT value of |update_item|, | |
976 // as well as the SERVER_POSITION_IN_PARENT values of any up-to-date | |
977 // children of |parent_id|. | |
978 Id ComputePrevIdFromServerPosition( | |
979 const EntryKernel* update_item, | |
980 const syncable::Id& parent_id); | |
981 | |
982 // SaveChanges works by taking a consistent snapshot of the current Directory | |
983 // state and indices (by deep copy) under a ReadTransaction, passing this | |
984 // snapshot to the backing store under no transaction, and finally cleaning | |
985 // up by either purging entries no longer needed (this part done under a | |
986 // WriteTransaction) or rolling back the dirty bits. It also uses | |
987 // internal locking to enforce SaveChanges operations are mutually exclusive. | |
988 // | |
989 // WARNING: THIS METHOD PERFORMS SYNCHRONOUS I/O VIA SQLITE. | |
990 bool SaveChanges(); | |
991 | |
992 // Fill in |result| with all entry kernels. | |
993 void GetAllEntryKernels(BaseTransaction* trans, | |
994 std::vector<const EntryKernel*>* result); | |
995 | |
996 // Returns the number of entities with the unsynced bit set. | |
997 int64 unsynced_entity_count() const; | |
998 | |
999 // Get GetUnsyncedMetaHandles should only be called after SaveChanges and | |
1000 // before any new entries have been created. The intention is that the | |
1001 // syncer should call it from its PerformSyncQueries member. | |
1002 typedef std::vector<int64> UnsyncedMetaHandles; | |
1003 void GetUnsyncedMetaHandles(BaseTransaction* trans, | |
1004 UnsyncedMetaHandles* result); | |
1005 | |
1006 // Returns all server types with unapplied updates. A subset of | |
1007 // those types can then be passed into | |
1008 // GetUnappliedUpdateMetaHandles() below. | |
1009 FullModelTypeSet GetServerTypesWithUnappliedUpdates( | |
1010 BaseTransaction* trans) const; | |
1011 | |
1012 // Get all the metahandles for unapplied updates for a given set of | |
1013 // server types. | |
1014 typedef std::vector<int64> UnappliedUpdateMetaHandles; | |
1015 void GetUnappliedUpdateMetaHandles(BaseTransaction* trans, | |
1016 FullModelTypeSet server_types, | |
1017 UnappliedUpdateMetaHandles* result); | |
1018 | |
1019 // Checks tree metadata consistency. | |
1020 // If full_scan is false, the function will avoid pulling any entries from the | |
1021 // db and scan entries currently in ram. | |
1022 // If full_scan is true, all entries will be pulled from the database. | |
1023 // No return value, CHECKs will be triggered if we're given bad | |
1024 // information. | |
1025 bool CheckTreeInvariants(syncable::BaseTransaction* trans, | |
1026 bool full_scan); | |
1027 | |
1028 bool CheckTreeInvariants(syncable::BaseTransaction* trans, | |
1029 const EntryKernelMutationMap& mutations); | |
1030 | |
1031 bool CheckTreeInvariants(syncable::BaseTransaction* trans, | |
1032 const MetahandleSet& handles, | |
1033 const IdFilter& idfilter); | |
1034 | |
1035 // Purges all data associated with any entries whose ModelType or | |
1036 // ServerModelType is found in |types|, from _both_ memory and disk. | |
1037 // Only valid, "real" model types are allowed in |types| (see model_type.h | |
1038 // for definitions). "Purge" is just meant to distinguish from "deleting" | |
1039 // entries, which means something different in the syncable namespace. | |
1040 // WARNING! This can be real slow, as it iterates over all entries. | |
1041 // WARNING! Performs synchronous I/O. | |
1042 virtual void PurgeEntriesWithTypeIn(ModelTypeSet types); | |
1043 | |
1044 private: | |
1045 // Helper to prime ids_index, parent_id_and_names_index, unsynced_metahandles | |
1046 // and unapplied_metahandles from metahandles_index. | |
1047 void InitializeIndices(); | |
1048 | |
1049 // Constructs a consistent snapshot of the current Directory state and | |
1050 // indices (by deep copy) under a ReadTransaction for use in |snapshot|. | |
1051 // See SaveChanges() for more information. | |
1052 void TakeSnapshotForSaveChanges(SaveChangesSnapshot* snapshot); | |
1053 | |
1054 // Purges from memory any unused, safe to remove entries that were | |
1055 // successfully deleted on disk as a result of the SaveChanges that processed | |
1056 // |snapshot|. See SaveChanges() for more information. | |
1057 bool VacuumAfterSaveChanges(const SaveChangesSnapshot& snapshot); | |
1058 | |
1059 // Rolls back dirty bits in the event that the SaveChanges that | |
1060 // processed |snapshot| failed, for example, due to no disk space. | |
1061 void HandleSaveChangesFailure(const SaveChangesSnapshot& snapshot); | |
1062 | |
1063 // For new entry creation only | |
1064 bool InsertEntry(WriteTransaction* trans, | |
1065 EntryKernel* entry, ScopedKernelLock* lock); | |
1066 bool InsertEntry(WriteTransaction* trans, EntryKernel* entry); | |
1067 | |
1068 // Used by CheckTreeInvariants | |
1069 void GetAllMetaHandles(BaseTransaction* trans, MetahandleSet* result); | |
1070 bool SafeToPurgeFromMemory(WriteTransaction* trans, | |
1071 const EntryKernel* const entry) const; | |
1072 | |
1073 // Internal setters that do not acquire a lock internally. These are unsafe | |
1074 // on their own; caller must guarantee exclusive access manually by holding | |
1075 // a ScopedKernelLock. | |
1076 void set_initial_sync_ended_for_type_unsafe(ModelType type, bool x); | |
1077 void SetNotificationStateUnsafe(const std::string& notification_state); | |
1078 | |
1079 Directory& operator = (const Directory&); | |
1080 | |
1081 public: | |
1082 typedef Index<MetahandleIndexer>::Set MetahandlesIndex; | |
1083 typedef Index<IdIndexer>::Set IdsIndex; | |
1084 // All entries in memory must be in both the MetahandlesIndex and | |
1085 // the IdsIndex, but only non-deleted entries will be the | |
1086 // ParentIdChildIndex. | |
1087 typedef Index<ParentIdAndHandleIndexer>::Set ParentIdChildIndex; | |
1088 | |
1089 // Contains both deleted and existing entries with tags. | |
1090 // We can't store only existing tags because the client would create | |
1091 // items that had a duplicated ID in the end, resulting in a DB key | |
1092 // violation. ID reassociation would fail after an attempted commit. | |
1093 typedef Index<ClientTagIndexer>::Set ClientTagIndex; | |
1094 | |
1095 protected: | |
1096 // Used by tests. |delegate| must not be NULL. | |
1097 // |transaction_observer| must be initialized. | |
1098 void InitKernelForTest( | |
1099 const std::string& name, | |
1100 DirectoryChangeDelegate* delegate, | |
1101 const browser_sync::WeakHandle<TransactionObserver>& | |
1102 transaction_observer); | |
1103 | |
1104 private: | |
1105 struct Kernel { | |
1106 // |delegate| must not be NULL. |transaction_observer| must be | |
1107 // initialized. | |
1108 Kernel(const std::string& name, const KernelLoadInfo& info, | |
1109 DirectoryChangeDelegate* delegate, | |
1110 const browser_sync::WeakHandle<TransactionObserver>& | |
1111 transaction_observer); | |
1112 | |
1113 ~Kernel(); | |
1114 | |
1115 void AddRef(); // For convenience. | |
1116 void Release(); | |
1117 | |
1118 // TODO(timsteele): audit use of the member and remove if possible | |
1119 volatile base::subtle::AtomicWord refcount; | |
1120 | |
1121 // Implements ReadTransaction / WriteTransaction using a simple lock. | |
1122 base::Lock transaction_mutex; | |
1123 | |
1124 // Protected by transaction_mutex. Used by WriteTransactions. | |
1125 int64 next_write_transaction_id; | |
1126 | |
1127 // The name of this directory. | |
1128 std::string const name; | |
1129 | |
1130 // Protects all members below. | |
1131 // The mutex effectively protects all the indices, but not the | |
1132 // entries themselves. So once a pointer to an entry is pulled | |
1133 // from the index, the mutex can be unlocked and entry read or written. | |
1134 // | |
1135 // Never hold the mutex and do anything with the database or any | |
1136 // other buffered IO. Violating this rule will result in deadlock. | |
1137 base::Lock mutex; | |
1138 // Entries indexed by metahandle | |
1139 MetahandlesIndex* metahandles_index; | |
1140 // Entries indexed by id | |
1141 IdsIndex* ids_index; | |
1142 ParentIdChildIndex* parent_id_child_index; | |
1143 ClientTagIndex* client_tag_index; | |
1144 // So we don't have to create an EntryKernel every time we want to | |
1145 // look something up in an index. Needle in haystack metaphor. | |
1146 EntryKernel needle; | |
1147 | |
1148 // 3 in-memory indices on bits used extremely frequently by the syncer. | |
1149 // |unapplied_update_metahandles| is keyed by the server model type. | |
1150 MetahandleSet unapplied_update_metahandles[MODEL_TYPE_COUNT]; | |
1151 MetahandleSet* const unsynced_metahandles; | |
1152 // Contains metahandles that are most likely dirty (though not | |
1153 // necessarily). Dirtyness is confirmed in TakeSnapshotForSaveChanges(). | |
1154 MetahandleSet* const dirty_metahandles; | |
1155 | |
1156 // When a purge takes place, we remove items from all our indices and stash | |
1157 // them in here so that SaveChanges can persist their permanent deletion. | |
1158 MetahandleSet* const metahandles_to_purge; | |
1159 | |
1160 KernelShareInfoStatus info_status; | |
1161 | |
1162 // These 3 members are backed in the share_info table, and | |
1163 // their state is marked by the flag above. | |
1164 | |
1165 // A structure containing the Directory state that is written back into the | |
1166 // database on SaveChanges. | |
1167 PersistedKernelInfo persisted_info; | |
1168 | |
1169 // A unique identifier for this account's cache db, used to generate | |
1170 // unique server IDs. No need to lock, only written at init time. | |
1171 const std::string cache_guid; | |
1172 | |
1173 // It doesn't make sense for two threads to run SaveChanges at the same | |
1174 // time; this mutex protects that activity. | |
1175 base::Lock save_changes_mutex; | |
1176 | |
1177 // The next metahandle is protected by kernel mutex. | |
1178 int64 next_metahandle; | |
1179 | |
1180 // The delegate for directory change events. Must not be NULL. | |
1181 DirectoryChangeDelegate* const delegate; | |
1182 | |
1183 // The transaction observer. | |
1184 const browser_sync::WeakHandle<TransactionObserver> transaction_observer; | |
1185 }; | |
1186 | |
1187 // Helper method used to do searches on |parent_id_child_index|. | |
1188 ParentIdChildIndex::iterator LocateInParentChildIndex( | |
1189 const ScopedKernelLock& lock, | |
1190 const Id& parent_id, | |
1191 int64 position_in_parent, | |
1192 const Id& item_id_for_tiebreaking); | |
1193 | |
1194 // Return an iterator to the beginning of the range of the children of | |
1195 // |parent_id| in the kernel's parent_id_child_index. | |
1196 ParentIdChildIndex::iterator GetParentChildIndexLowerBound( | |
1197 const ScopedKernelLock& lock, | |
1198 const Id& parent_id); | |
1199 | |
1200 // Return an iterator to just past the end of the range of the | |
1201 // children of |parent_id| in the kernel's parent_id_child_index. | |
1202 ParentIdChildIndex::iterator GetParentChildIndexUpperBound( | |
1203 const ScopedKernelLock& lock, | |
1204 const Id& parent_id); | |
1205 | |
1206 // Append the handles of the children of |parent_id| to |result|. | |
1207 void AppendChildHandles( | |
1208 const ScopedKernelLock& lock, | |
1209 const Id& parent_id, Directory::ChildHandles* result); | |
1210 | |
1211 // Return a pointer to what is probably (but not certainly) the | |
1212 // first child of |parent_id|, or NULL if |parent_id| definitely has | |
1213 // no children. | |
1214 EntryKernel* GetPossibleFirstChild( | |
1215 const ScopedKernelLock& lock, const Id& parent_id); | |
1216 | |
1217 // Return a pointer to what is probably (but not certainly) the last | |
1218 // child of |parent_id|, or NULL if |parent_id| definitely has no | |
1219 // children. | |
1220 EntryKernel* GetPossibleLastChildForTest( | |
1221 const ScopedKernelLock& lock, const Id& parent_id); | |
1222 | |
1223 browser_sync::Cryptographer cryptographer_; | |
1224 | |
1225 Kernel* kernel_; | |
1226 | |
1227 DirectoryBackingStore* store_; | |
1228 | |
1229 browser_sync::UnrecoverableErrorHandler* const unrecoverable_error_handler_; | |
1230 const browser_sync::ReportUnrecoverableErrorFunction | |
1231 report_unrecoverable_error_function_; | |
1232 bool unrecoverable_error_set_; | |
1233 }; | |
1234 | |
1235 class ScopedKernelLock { | |
1236 public: | |
1237 explicit ScopedKernelLock(const Directory*); | |
1238 ~ScopedKernelLock() {} | |
1239 | |
1240 base::AutoLock scoped_lock_; | |
1241 Directory* const dir_; | |
1242 DISALLOW_COPY_AND_ASSIGN(ScopedKernelLock); | |
1243 }; | |
1244 | |
1245 // Transactions are now processed FIFO with a straight lock | |
1246 class BaseTransaction { | |
1247 friend class Entry; | |
1248 public: | |
1249 inline Directory* directory() const { return directory_; } | |
1250 inline Id root_id() const { return Id(); } | |
1251 | |
1252 virtual ~BaseTransaction(); | |
1253 | |
1254 // This should be called when a database corruption is detected and there is | |
1255 // no way for us to recover short of wiping the database clean. When this is | |
1256 // called we set a bool in the transaction. The caller has to unwind the | |
1257 // stack. When the destructor for the transaction is called it acts upon the | |
1258 // bool and calls the Directory to handle the unrecoverable error. | |
1259 void OnUnrecoverableError(const tracked_objects::Location& location, | |
1260 const std::string& message); | |
1261 | |
1262 bool unrecoverable_error_set() const; | |
1263 | |
1264 protected: | |
1265 BaseTransaction(const tracked_objects::Location& from_here, | |
1266 const char* name, | |
1267 WriterTag writer, | |
1268 Directory* directory); | |
1269 | |
1270 void Lock(); | |
1271 void Unlock(); | |
1272 | |
1273 // This should be called before unlocking because it calls the Direcotry's | |
1274 // OnUnrecoverableError method which is not protected by locks and could | |
1275 // be called from any thread. Holding the transaction lock ensures only one | |
1276 // thread could call the method at a time. | |
1277 void HandleUnrecoverableErrorIfSet(); | |
1278 | |
1279 const tracked_objects::Location from_here_; | |
1280 const char* const name_; | |
1281 WriterTag writer_; | |
1282 Directory* const directory_; | |
1283 Directory::Kernel* const dirkernel_; // for brevity | |
1284 | |
1285 // Error information. | |
1286 bool unrecoverable_error_set_; | |
1287 tracked_objects::Location unrecoverable_error_location_; | |
1288 std::string unrecoverable_error_msg_; | |
1289 | |
1290 private: | |
1291 DISALLOW_COPY_AND_ASSIGN(BaseTransaction); | |
1292 }; | |
1293 | |
1294 // Locks db in constructor, unlocks in destructor. | |
1295 class ReadTransaction : public BaseTransaction { | |
1296 public: | |
1297 ReadTransaction(const tracked_objects::Location& from_here, | |
1298 Directory* directory); | |
1299 | |
1300 virtual ~ReadTransaction(); | |
1301 | |
1302 protected: // Don't allow creation on heap, except by sync API wrapper. | |
1303 friend class sync_api::ReadTransaction; | |
1304 void* operator new(size_t size) { return (::operator new)(size); } | |
1305 | |
1306 DISALLOW_COPY_AND_ASSIGN(ReadTransaction); | |
1307 }; | |
1308 | |
1309 // Locks db in constructor, unlocks in destructor. | |
1310 class WriteTransaction : public BaseTransaction { | |
1311 friend class MutableEntry; | |
1312 public: | |
1313 WriteTransaction(const tracked_objects::Location& from_here, | |
1314 WriterTag writer, Directory* directory); | |
1315 | |
1316 virtual ~WriteTransaction(); | |
1317 | |
1318 void SaveOriginal(const EntryKernel* entry); | |
1319 | |
1320 protected: | |
1321 // Overridden by tests. | |
1322 virtual void NotifyTransactionComplete(ModelTypeSet models_with_changes); | |
1323 | |
1324 private: | |
1325 // Clears |mutations_|. | |
1326 ImmutableEntryKernelMutationMap RecordMutations(); | |
1327 | |
1328 void UnlockAndNotify(const ImmutableEntryKernelMutationMap& mutations); | |
1329 | |
1330 ModelTypeSet NotifyTransactionChangingAndEnding( | |
1331 const ImmutableEntryKernelMutationMap& mutations); | |
1332 | |
1333 // Only the original fields are filled in until |RecordMutations()|. | |
1334 // We use a mutation map instead of a kernel set to avoid copying. | |
1335 EntryKernelMutationMap mutations_; | |
1336 | |
1337 DISALLOW_COPY_AND_ASSIGN(WriteTransaction); | |
1338 }; | |
1339 | |
1340 bool IsLegalNewParent(BaseTransaction* trans, const Id& id, const Id& parentid); | |
1341 | |
1342 // This function sets only the flags needed to get this entry to sync. | |
1343 bool MarkForSyncing(syncable::MutableEntry* e); | |
1344 | |
1345 } // namespace syncable | |
1346 | |
1347 std::ostream& operator <<(std::ostream&, const syncable::Blob&); | |
1348 | |
1349 #endif // CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | |
OLD | NEW |