summaryrefslogtreecommitdiffstats
path: root/components/leveldb/leveldb_database_impl.h
blob: 29ff697eebb2d0a7cb58ea73a449db3bc075ca1f (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
// Copyright 2016 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 COMPONENTS_LEVELDB_LEVELDB_DATABASE_IMPL_H_
#define COMPONENTS_LEVELDB_LEVELDB_DATABASE_IMPL_H_

#include "base/memory/scoped_ptr.h"
#include "components/leveldb/public/interfaces/leveldb.mojom.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "third_party/leveldatabase/src/include/leveldb/db.h"

namespace leveldb {

class MojoEnv;

// The backing to a database object that we pass to our called.
class LevelDBDatabaseImpl : public LevelDBDatabase {
 public:
  LevelDBDatabaseImpl(leveldb::LevelDBDatabaseRequest request,
                      scoped_ptr<MojoEnv> environment,
                      scoped_ptr<leveldb::DB> db);
  ~LevelDBDatabaseImpl() override;

  // Overridden from LevelDBDatabase:
  void Put(mojo::Array<uint8_t> key,
           mojo::Array<uint8_t> value,
           const PutCallback& callback) override;
  void Delete(mojo::Array<uint8_t> key,
              const DeleteCallback& callback) override;
  void Write(mojo::Array<BatchedOperationPtr> operations,
             const WriteCallback& callback) override;
  void Get(mojo::Array<uint8_t> key, const GetCallback& callback) override;
  void GetSnapshot(const GetSnapshotCallback& callback) override;
  void ReleaseSnapshot(uint64_t snapshot_id) override;
  void GetFromSnapshot(uint64_t snapshot_id,
                       mojo::Array<uint8_t> key,
                       const GetCallback& callback) override;

 private:
  mojo::StrongBinding<LevelDBDatabase> binding_;
  scoped_ptr<MojoEnv> environment_;
  scoped_ptr<leveldb::DB> db_;

  std::map<uint64_t, const Snapshot*> snapshot_map_;

  // TODO(erg): If we have an existing iterator which depends on a snapshot,
  // and delete the snapshot from the client side, that shouldn't delete the
  // snapshot maybe? At worse it's a DDoS if there's multiple users of the
  // system, but this maybe should be fixed...

  std::map<uint64_t, Iterator*> iterator_map_;

  DISALLOW_COPY_AND_ASSIGN(LevelDBDatabaseImpl);
};

}  // namespace leveldb

#endif  // COMPONENTS_LEVELDB_LEVELDB_DATABASE_IMPL_H_