leveldb_wrapper.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright 2014 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_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_
6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_
7
8#include <map>
9#include <string>
10
11#include "base/macros.h"
12#include "base/memory/scoped_ptr.h"
13#include "third_party/leveldatabase/src/include/leveldb/slice.h"
14
15namespace leveldb {
16class DB;
17class Iterator;
18class Slice;
19class Status;
20class WriteBatch;
21}
22
23namespace sync_file_system {
24namespace drive_backend {
25
26class SliceComparator {
27 public:
28  bool operator()(const leveldb::Slice& a, const leveldb::Slice& b) const {
29    return a.compare(b) < 0;
30  }
31};
32
33// LevelDBWrapper class wraps leveldb::DB and leveldb::WriteBatch to provide
34// transparent access to pre-commit data.
35// Put() and Delete() update data on memory, and Get() can access to those data
36// and also data on disk.  Those data on memory are written down on disk when
37// Commit() is called.
38class LevelDBWrapper {
39 private:
40  enum Operation {
41    PUT_OPERATION,
42    DELETE_OPERATION,
43  };
44  typedef std::pair<Operation, std::string> Transaction;
45  typedef std::map<std::string, Transaction, SliceComparator>
46      PendingOperationMap;
47
48 public:
49  class Iterator {
50   public:
51    explicit Iterator(LevelDBWrapper* db);
52    ~Iterator();
53
54    bool Valid();
55    void Seek(const std::string& target);
56    void SeekToFirst();
57    void SeekToLast();
58    void Next();
59    leveldb::Slice key();
60    leveldb::Slice value();
61
62   private:
63    // Advances internal iterators to be valid.
64    void AdvanceIterators();
65
66    LevelDBWrapper* db_;  // do not own
67    scoped_ptr<leveldb::Iterator> db_iterator_;
68    PendingOperationMap::iterator map_iterator_;
69
70    DISALLOW_COPY_AND_ASSIGN(Iterator);
71  };
72
73  explicit LevelDBWrapper(scoped_ptr<leveldb::DB> db);
74  ~LevelDBWrapper();
75
76  // Wrapping methods of leveldb::WriteBatch
77  void Put(const std::string& key, const std::string& value);
78  void Delete(const std::string& key);
79
80  // Wrapping methods of leveldb::DB
81  leveldb::Status Get(const std::string& key, std::string* value);
82  scoped_ptr<Iterator> NewIterator();
83
84  // Commits pending transactions to |db_| and clears cached transactions.
85  // Returns true if the commitment succeeds.
86  leveldb::Status Commit();
87
88  // Clears pending transactions.
89  void Clear();
90
91  // TODO(peria): Rename this method to GetLevelDBForTesting, after removing
92  // usages of drive_backend::MigrateDatabaseFromVxToVy() under
93  // drive_backend_v1/.
94  leveldb::DB* GetLevelDB();
95
96 private:
97  scoped_ptr<leveldb::DB> db_;
98
99  PendingOperationMap pending_;
100
101  DISALLOW_COPY_AND_ASSIGN(LevelDBWrapper);
102};
103
104}  // namespace drive_backend
105}  // namespace sync_file_system
106
107#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_
108