summaryrefslogtreecommitdiff
path: root/chromium/content/browser/indexed_db/leveldb/leveldb_transaction.h
blob: 83b4763edc1dcb1abf0db458d3fa07983c430606 (plain)
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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
#define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_

#include <set>
#include <string>
#include <vector>

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_piece.h"
#include "content/browser/indexed_db/leveldb/avltree.h"
#include "content/browser/indexed_db/leveldb/leveldb_comparator.h"
#include "content/browser/indexed_db/leveldb/leveldb_database.h"
#include "content/browser/indexed_db/leveldb/leveldb_iterator.h"

namespace content {

class LevelDBWriteBatch;

class CONTENT_EXPORT LevelDBTransaction
    : public base::RefCounted<LevelDBTransaction> {
 public:
  explicit LevelDBTransaction(LevelDBDatabase* db);

  void Put(const base::StringPiece& key, std::string* value);
  void Remove(const base::StringPiece& key);
  bool Get(const base::StringPiece& key, std::string* value, bool* found);
  bool Commit();
  void Rollback();

  scoped_ptr<LevelDBIterator> CreateIterator();

 private:
  virtual ~LevelDBTransaction();
  friend class base::RefCounted<LevelDBTransaction>;

  struct AVLTreeNode {
    AVLTreeNode();
    ~AVLTreeNode();
    std::string key;
    std::string value;
    bool deleted;

    AVLTreeNode* less;
    AVLTreeNode* greater;
    int balance_factor;
    DISALLOW_COPY_AND_ASSIGN(AVLTreeNode);
  };

  struct AVLTreeAbstractor {
    typedef AVLTreeNode* handle;
    typedef size_t size;
    typedef base::StringPiece key;

    handle GetLess(handle h) { return h->less; }
    void SetLess(handle h, handle less) { h->less = less; }
    handle GetGreater(handle h) { return h->greater; }
    void SetGreater(handle h, handle greater) { h->greater = greater; }

    int GetBalanceFactor(handle h) { return h->balance_factor; }
    void SetBalanceFactor(handle h, int bf) { h->balance_factor = bf; }

    int CompareKeyKey(const key& ka, const key& kb) {
      return comparator_->Compare(ka, kb);
    }
    int CompareKeyNode(const key& k, handle h) {
      return CompareKeyKey(k, h->key);
    }
    int CompareNodeNode(handle ha, handle hb) {
      return CompareKeyKey(ha->key, hb->key);
    }

    static handle Null() { return 0; }

    const LevelDBComparator* comparator_;
  };

  typedef AVLTree<AVLTreeAbstractor> TreeType;

  class TreeIterator : public LevelDBIterator {
   public:
    static scoped_ptr<TreeIterator> Create(LevelDBTransaction* transaction);
    virtual ~TreeIterator();

    virtual bool IsValid() const OVERRIDE;
    virtual void SeekToLast() OVERRIDE;
    virtual void Seek(const base::StringPiece& slice) OVERRIDE;
    virtual void Next() OVERRIDE;
    virtual void Prev() OVERRIDE;
    virtual base::StringPiece Key() const OVERRIDE;
    virtual base::StringPiece Value() const OVERRIDE;
    bool IsDeleted() const;
    void Reset();

   private:
    explicit TreeIterator(LevelDBTransaction* transaction);
    mutable TreeType::Iterator iterator_;  // Dereferencing this is non-const.
    TreeType* tree_;
    LevelDBTransaction* transaction_;
    std::string key_;
  };

  class TransactionIterator : public LevelDBIterator {
   public:
    virtual ~TransactionIterator();
    static scoped_ptr<TransactionIterator> Create(
        scoped_refptr<LevelDBTransaction> transaction);

    virtual bool IsValid() const OVERRIDE;
    virtual void SeekToLast() OVERRIDE;
    virtual void Seek(const base::StringPiece& target) OVERRIDE;
    virtual void Next() OVERRIDE;
    virtual void Prev() OVERRIDE;
    virtual base::StringPiece Key() const OVERRIDE;
    virtual base::StringPiece Value() const OVERRIDE;
    void TreeChanged();

   private:
    explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction);
    void HandleConflictsAndDeletes();
    void SetCurrentIteratorToSmallestKey();
    void SetCurrentIteratorToLargestKey();
    void RefreshTreeIterator() const;
    bool TreeIteratorIsLower() const;
    bool TreeIteratorIsHigher() const;

    scoped_refptr<LevelDBTransaction> transaction_;
    const LevelDBComparator* comparator_;
    mutable scoped_ptr<TreeIterator> tree_iterator_;
    scoped_ptr<LevelDBIterator> db_iterator_;
    LevelDBIterator* current_;

    enum Direction {
      FORWARD,
      REVERSE
    };
    Direction direction_;
    mutable bool tree_changed_;
  };

  void Set(const base::StringPiece& key, std::string* value, bool deleted);
  void ClearTree();
  void RegisterIterator(TransactionIterator* iterator);
  void UnregisterIterator(TransactionIterator* iterator);
  void NotifyIteratorsOfTreeChange();

  LevelDBDatabase* db_;
  const LevelDBSnapshot snapshot_;
  const LevelDBComparator* comparator_;
  TreeType tree_;
  bool finished_;
  std::set<TransactionIterator*> iterators_;
};

class LevelDBWriteOnlyTransaction {
 public:
  static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db);

  ~LevelDBWriteOnlyTransaction();
  void Remove(const base::StringPiece& key);
  bool Commit();

 private:
  explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db);

  LevelDBDatabase* db_;
  scoped_ptr<LevelDBWriteBatch> write_batch_;
  bool finished_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_