summaryrefslogtreecommitdiffstats
path: root/src/kv/MemDB.h
blob: 117b6aee88d62ebdb918bedef41980c90601e159 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * In-memory crash non-safe keyvalue db
 * Author: Ramesh Chander, Ramesh.Chander@sandisk.com
 */

#ifndef CEPH_OS_BLUESTORE_MEMDB_H
#define CEPH_OS_BLUESTORE_MEMDB_H

#include "include/buffer.h"
#include <ostream>
#include <set>
#include <map>
#include <string>
#include <memory>
#include <boost/scoped_ptr.hpp>
#include "include/encoding.h"
#include "include/btree_map.h"
#include "KeyValueDB.h"
#include "osd/osd_types.h"

using std::string;
#define KEY_DELIM '\0' 

class PerfCounters;

enum {
  l_memdb_first = 34440,
  l_memdb_gets,
  l_memdb_txns,
  l_memdb_get_latency,
  l_memdb_submit_latency,
  l_memdb_last,
};

class MemDB : public KeyValueDB
{
  typedef std::pair<std::pair<std::string, std::string>, bufferlist> ms_op_t;
  std::mutex m_lock;
  uint64_t m_total_bytes;
  uint64_t m_allocated_bytes;

  typedef std::map<std::string, bufferptr> mdb_map_t;
  typedef mdb_map_t::iterator mdb_iter_t;
  bool m_using_btree;

  mdb_map_t m_map;

  CephContext *m_cct;
  PerfCounters *logger;
  void* m_priv;
  string m_options;
  string m_db_path;

  int transaction_rollback(KeyValueDB::Transaction t);
  int _open(ostream &out);
  void close() override;
  bool _get(const string &prefix, const string &k, bufferlist *out);
  bool _get_locked(const string &prefix, const string &k, bufferlist *out);
  std::string _get_data_fn();
  void _encode(mdb_iter_t iter, bufferlist &bl);
  void _save();
  int _load();
  uint64_t iterator_seq_no;

public:
  MemDB(CephContext *c, const string &path, void *p) :
    m_total_bytes(0), m_allocated_bytes(0), m_using_btree(false),
    m_cct(c), logger(NULL), m_priv(p), m_db_path(path), iterator_seq_no(1)
  {
    //Nothing as of now
  }

  ~MemDB() override;
  int set_merge_operator(const std::string& prefix,
         std::shared_ptr<MergeOperator> mop) override;

  std::shared_ptr<MergeOperator> _find_merge_op(const std::string &prefix);

  static
  int _test_init(const string& dir) { return 0; };

  class MDBTransactionImpl : public KeyValueDB::TransactionImpl {
    public:
      enum op_type { WRITE = 1, MERGE = 2, DELETE = 3};
    private:

      std::vector<std::pair<op_type, ms_op_t>> ops;
      MemDB *m_db;

      bool key_is_prefixed(const string &prefix, const string& full_key);
    public:
      const std::vector<std::pair<op_type, ms_op_t>>&
        get_ops() { return ops; };

    void set(const std::string &prefix, const std::string &key,
      const bufferlist &val) override;
    using KeyValueDB::TransactionImpl::set;
    void rmkey(const std::string &prefix, const std::string &k) override;
    using KeyValueDB::TransactionImpl::rmkey;
    void rmkeys_by_prefix(const std::string &prefix) override;
    void rm_range_keys(
        const string &prefix,
        const string &start,
        const string &end) override;

    void merge(const std::string &prefix, const std::string &key, const bufferlist  &value) override;
    void clear() {
      ops.clear();
    }
    explicit MDBTransactionImpl(MemDB* _db) :m_db(_db)
    {
      ops.clear();
    }
    ~MDBTransactionImpl() override {};
  };

private:

  /*
   * Transaction states.
   */
  int _merge(const std::string &k, bufferptr &bl);
  int _merge(ms_op_t &op);
  int _setkey(ms_op_t &op);
  int _rmkey(ms_op_t &op);

public:

  int init(string option_str="") override { m_options = option_str; return 0; }
  int _init(bool format);

  int do_open(ostream &out, bool create);
  int open(ostream &out, const std::vector<ColumnFamily>&) override;
  int create_and_open(ostream &out, const std::vector<ColumnFamily>&) override;
  using KeyValueDB::create_and_open;

  KeyValueDB::Transaction get_transaction() override {
    return std::shared_ptr<MDBTransactionImpl>(new MDBTransactionImpl(this));
  }

  int submit_transaction(Transaction) override;
  int submit_transaction_sync(Transaction) override;

  int get(const std::string &prefix, const std::set<std::string> &key,
    std::map<std::string, bufferlist> *out) override;

  int get(const std::string &prefix, const std::string &key,
          bufferlist *out) override;

  using KeyValueDB::get;

  class MDBWholeSpaceIteratorImpl : public KeyValueDB::WholeSpaceIteratorImpl {

      mdb_iter_t m_iter;
      std::pair<string, bufferlist> m_key_value;
      mdb_map_t *m_map_p;
      std::mutex *m_map_lock_p;
      uint64_t *global_seq_no;
      uint64_t this_seq_no;
      bool m_using_btree;

  public:
    MDBWholeSpaceIteratorImpl(mdb_map_t *btree_p, std::mutex *btree_lock_p,
                              uint64_t *iterator_seq_no, bool using_btree) {
      m_map_p = btree_p;
      m_map_lock_p = btree_lock_p;
      std::lock_guard<std::mutex> l(*m_map_lock_p);
      global_seq_no = iterator_seq_no;
      this_seq_no = *iterator_seq_no;
      m_using_btree = using_btree;
    }

    void fill_current();
    void free_last();


    int seek_to_first(const std::string &k) override;
    int seek_to_last(const std::string &k) override;

    int seek_to_first() override { return seek_to_first(std::string()); };
    int seek_to_last() override { return seek_to_last(std::string()); };

    int upper_bound(const std::string &prefix, const std::string &after) override;
    int lower_bound(const std::string &prefix, const std::string &to) override;
    bool valid() override;
    bool iterator_validate();

    int next() override;
    int prev() override;
    int status() override { return 0; };

    std::string key() override;
    std::pair<std::string,std::string> raw_key() override;
    bool raw_key_is_prefixed(const std::string &prefix) override;
    bufferlist value() override;
    ~MDBWholeSpaceIteratorImpl() override;
  };

  uint64_t get_estimated_size(std::map<std::string,uint64_t> &extra) override {
      std::lock_guard<std::mutex> l(m_lock);
      return m_allocated_bytes;
  };

  int get_statfs(struct store_statfs_t *buf) override {
    std::lock_guard<std::mutex> l(m_lock);
    buf->reset();
    buf->total = m_total_bytes;
    buf->allocated = m_allocated_bytes;
    buf->data_stored = m_total_bytes;
    return 0;
  }

  WholeSpaceIterator get_wholespace_iterator() override {
    return std::shared_ptr<KeyValueDB::WholeSpaceIteratorImpl>(
      new MDBWholeSpaceIteratorImpl(&m_map, &m_lock, &iterator_seq_no, m_using_btree));
  }
};

#endif