mutable_entry.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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_MUTABLE_ENTRY_H_
6#define SYNC_SYNCABLE_MUTABLE_ENTRY_H_
7
8#include "sync/base/sync_export.h"
9#include "sync/internal_api/public/base/model_type.h"
10#include "sync/syncable/entry.h"
11#include "sync/syncable/metahandle_set.h"
12
13namespace syncer {
14class WriteNode;
15
16namespace syncable {
17
18class WriteTransaction;
19
20enum Create {
21  CREATE
22};
23
24enum CreateNewUpdateItem {
25  CREATE_NEW_UPDATE_ITEM
26};
27
28// A mutable meta entry.  Changes get committed to the database when the
29// WriteTransaction is destroyed.
30class SYNC_EXPORT_PRIVATE MutableEntry : public Entry {
31  void Init(WriteTransaction* trans, ModelType model_type,
32            const Id& parent_id, const std::string& name);
33
34 public:
35  MutableEntry(WriteTransaction* trans, Create, ModelType model_type,
36               const Id& parent_id, const std::string& name);
37  MutableEntry(WriteTransaction* trans, CreateNewUpdateItem, const Id& id);
38  MutableEntry(WriteTransaction* trans, GetByHandle, int64);
39  MutableEntry(WriteTransaction* trans, GetById, const Id&);
40  MutableEntry(WriteTransaction* trans, GetByClientTag, const std::string& tag);
41  MutableEntry(WriteTransaction* trans, GetByServerTag, const std::string& tag);
42
43  inline WriteTransaction* write_transaction() const {
44    return write_transaction_;
45  }
46
47  // Field Accessors.  Some of them trigger the re-indexing of the entry.
48  // Return true on success, return false on failure, which means
49  // that putting the value would have caused a duplicate in the index.
50  // TODO(chron): Remove some of these unecessary return values.
51  bool Put(Int64Field field, const int64& value);
52  bool Put(TimeField field, const base::Time& value);
53  bool Put(IdField field, const Id& value);
54  bool Put(UniquePositionField field, const UniquePosition& value);
55
56  // Do a simple property-only update if the PARENT_ID field.  Use with caution.
57  //
58  // The normal Put(IS_PARENT) call will move the item to the front of the
59  // sibling order to maintain the linked list invariants when the parent
60  // changes.  That's usually what you want to do, but it's inappropriate
61  // when the caller is trying to change the parent ID of a the whole set
62  // of children (e.g. because the ID changed during a commit).  For those
63  // cases, there's this function.  It will corrupt the sibling ordering
64  // if you're not careful.
65  void PutParentIdPropertyOnly(const Id& parent_id);
66
67  bool Put(StringField field, const std::string& value);
68  bool Put(BaseVersion field, int64 value);
69
70  bool Put(ProtoField field, const sync_pb::EntitySpecifics& value);
71  bool Put(BitField field, bool value);
72  inline bool Put(IsDelField field, bool value) {
73    return PutIsDel(value);
74  }
75  bool Put(IndexedBitField field, bool value);
76
77  void PutUniqueBookmarkTag(const std::string& tag);
78
79  // Sets the position of this item, and updates the entry kernels of the
80  // adjacent siblings so that list invariants are maintained.  Returns false
81  // and fails if |predecessor_id| does not identify a sibling.  Pass the root
82  // ID to put the node in first position.
83  bool PutPredecessor(const Id& predecessor_id);
84
85  bool Put(BitTemp field, bool value);
86
87 protected:
88  syncable::MetahandleSet* GetDirtyIndexHelper();
89
90  bool PutIsDel(bool value);
91
92 private:
93  friend class Directory;
94  friend class WriteTransaction;
95  friend class syncer::WriteNode;
96
97  // Don't allow creation on heap, except by sync API wrappers.
98  void* operator new(size_t size) { return (::operator new)(size); }
99
100  bool PutUniqueClientTag(const std::string& value);
101
102  // Adjusts the successor and predecessor entries so that they no longer
103  // refer to this entry.
104  bool UnlinkFromOrder();
105
106  // Kind of redundant. We should reduce the number of pointers
107  // floating around if at all possible. Could we store this in Directory?
108  // Scope: Set on construction, never changed after that.
109  WriteTransaction* const write_transaction_;
110
111 protected:
112  MutableEntry();
113
114  DISALLOW_COPY_AND_ASSIGN(MutableEntry);
115};
116
117// This function sets only the flags needed to get this entry to sync.
118bool MarkForSyncing(syncable::MutableEntry* e);
119
120}  // namespace syncable
121}  // namespace syncer
122
123#endif  // SYNC_SYNCABLE_MUTABLE_ENTRY_H_
124