File: leveldb_wrapper.h

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (122 lines) | stat: -rw-r--r-- 3,356 bytes parent folder | download | duplicates (5)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_
#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_

#include <stdint.h>

#include <map>
#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "third_party/leveldatabase/src/include/leveldb/slice.h"

namespace leveldb {
class DB;
class Iterator;
class Slice;
class Status;
class WriteBatch;
}

namespace sync_file_system {
namespace drive_backend {

class SliceComparator {
 public:
  bool operator()(const leveldb::Slice& a, const leveldb::Slice& b) const {
    return a.compare(b) < 0;
  }
};

// LevelDBWrapper class wraps leveldb::DB and leveldb::WriteBatch to provide
// transparent access to pre-commit data.
// Put() and Delete() update data on memory, and Get() can access to those data
// and also data on disk.  Those data on memory are written down on disk when
// Commit() is called.
class LevelDBWrapper {
 private:
  enum Operation {
    PUT_OPERATION,
    DELETE_OPERATION,
  };
  typedef std::pair<Operation, std::string> Transaction;
  typedef std::map<std::string, Transaction, SliceComparator>
      PendingOperationMap;

 public:
  class Iterator {
   public:
    explicit Iterator(LevelDBWrapper* db);

    Iterator(const Iterator&) = delete;
    Iterator& operator=(const Iterator&) = delete;

    ~Iterator();

    bool Valid();
    void Seek(const std::string& target);
    void SeekToFirst();
    void SeekToLast();
    void Next();
    void Delete();
    leveldb::Slice key();
    leveldb::Slice value();

   private:
    // Advances internal iterators to be valid.
    void AdvanceIterators();

    raw_ptr<LevelDBWrapper> db_;  // do not own
    std::unique_ptr<leveldb::Iterator> db_iterator_;
    PendingOperationMap::iterator map_iterator_;
  };

  explicit LevelDBWrapper(std::unique_ptr<leveldb::DB> db);

  LevelDBWrapper(const LevelDBWrapper&) = delete;
  LevelDBWrapper& operator=(const LevelDBWrapper&) = delete;

  ~LevelDBWrapper();

  // Wrapping methods of leveldb::WriteBatch
  void Put(const std::string& key, const std::string& value);
  void Delete(const std::string& key);

  // Wrapping methods of leveldb::DB
  leveldb::Status Get(const std::string& key, std::string* value);
  std::unique_ptr<Iterator> NewIterator();

  // Commits pending transactions to |db_| and clears cached transactions.
  // Returns true if the commitment succeeds.
  leveldb::Status Commit();

  // Clears pending transactions.
  void Clear();

  // Returns the number of pending PUT/DELETE operations.
  // Each counter counts operations independently, so operations on a key
  // may be counted more than once.
  int64_t num_puts() { return num_puts_; }
  int64_t num_deletes() { return num_deletes_; }

  // TODO(peria): Rename this method to GetLevelDBForTesting, after removing
  // usages of drive_backend::MigrateDatabaseFromVxToVy() under
  // drive_backend_v1/.
  leveldb::DB* GetLevelDB();

 private:
  std::unique_ptr<leveldb::DB> db_;

  PendingOperationMap pending_;
  int64_t num_puts_;
  int64_t num_deletes_;
};

}  // namespace drive_backend
}  // namespace sync_file_system

#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_