~ubuntu-branches/ubuntu/raring/ceph/raring

« back to all changes in this revision

Viewing changes to src/os/LevelDBStore.cc

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-08 15:54:37 UTC
  • mfrom: (1.1.8) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120608155437-gy3j9k6wzv7w4gn9
Tags: 0.44.1-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - d/control: Switch from libcryptopp to libnss as libcryptopp
    is not seeded.
  - d/control,d/rules: Move from python-support to dh_python2.
  - d/patches/manpage_updates*.patch: cherry picked upstream manpage
    updates warning about lack of encryption, per MIR review.
  - d/rules,d/control: Drop radosgw since libfcgi is not in main and
    the code may not be suitable for LTS.
  - d/rules,d/control: Drop tcmalloc since google perftools is not
    in main yet.
  - d/rules,d/control: Drop ceph-fuse entirely per MIR review
    recommendation.
* d/patches/fix-radosgw-tests.patch: Cherry picked patch from upstream
  VCS to fixup tests to conditionally use radosgw if enabled.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
 
2
 
 
3
#include "LevelDBStore.h"
 
4
 
 
5
#include <set>
 
6
#include <map>
 
7
#include <string>
 
8
#include <tr1/memory>
 
9
#include "leveldb/include/leveldb/db.h"
 
10
#include "leveldb/include/leveldb/write_batch.h"
 
11
#include "leveldb/include/leveldb/slice.h"
 
12
#include <errno.h>
 
13
using std::string;
 
14
 
 
15
int LevelDBStore::init(ostream &out)
 
16
{
 
17
  leveldb::Options options;
 
18
  options.create_if_missing = true;
 
19
  leveldb::DB *_db;
 
20
  leveldb::Status status = leveldb::DB::Open(options, path, &_db);
 
21
  db.reset(_db);
 
22
  if (!status.ok()) {
 
23
    out << status.ToString() << std::endl;
 
24
    return -EINVAL;
 
25
  } else
 
26
    return 0;
 
27
}
 
28
 
 
29
void LevelDBStore::LevelDBTransactionImpl::set(
 
30
  const string &prefix,
 
31
  const std::map<string, bufferlist> &to_set)
 
32
{
 
33
  for (std::map<string, bufferlist>::const_iterator i = to_set.begin();
 
34
       i != to_set.end();
 
35
       ++i) {
 
36
    buffers.push_back(i->second);
 
37
    buffers.rbegin()->rebuild();
 
38
    bufferlist &bl = *(buffers.rbegin());
 
39
    string key = combine_strings(prefix, i->first);
 
40
    keys.push_back(key);
 
41
    bat.Delete(leveldb::Slice(*(keys.rbegin())));
 
42
    bat.Put(leveldb::Slice(*(keys.rbegin())),
 
43
            leveldb::Slice(bl.c_str(), bl.length()));
 
44
  }
 
45
}
 
46
void LevelDBStore::LevelDBTransactionImpl::rmkeys(const string &prefix,
 
47
                                                  const std::set<string> &to_rm)
 
48
{
 
49
  for (std::set<string>::const_iterator i = to_rm.begin();
 
50
       i != to_rm.end();
 
51
       ++i) {
 
52
    string key = combine_strings(prefix, *i);
 
53
    keys.push_back(key);
 
54
    bat.Delete(leveldb::Slice(*(keys.rbegin())));
 
55
  }
 
56
}
 
57
 
 
58
void LevelDBStore::LevelDBTransactionImpl::rmkeys_by_prefix(const string &prefix)
 
59
{
 
60
  KeyValueDB::Iterator it = db->get_iterator(prefix);
 
61
  for (it->seek_to_first();
 
62
       it->valid();
 
63
       it->next()) {
 
64
    string key = combine_strings(prefix, it->key());
 
65
    keys.push_back(key);
 
66
    bat.Delete(*(keys.rbegin()));
 
67
  }
 
68
}
 
69
 
 
70
int LevelDBStore::get(
 
71
    const string &prefix,
 
72
    const std::set<string> &keys,
 
73
    std::map<string, bufferlist> *out)
 
74
{
 
75
  KeyValueDB::Iterator it = get_iterator(prefix);
 
76
  for (std::set<string>::const_iterator i = keys.begin();
 
77
       i != keys.end();
 
78
       ++i) {
 
79
    it->lower_bound(*i);
 
80
    if (it->valid() && it->key() == *i) {
 
81
      out->insert(make_pair(*i, it->value()));
 
82
    } else if (!it->valid())
 
83
      break;
 
84
  }
 
85
  return 0;
 
86
}
 
87
 
 
88
string LevelDBStore::combine_strings(const string &prefix, const string &value)
 
89
{
 
90
  string out = prefix;
 
91
  out.push_back(0);
 
92
  out.append(value);
 
93
  return out;
 
94
}
 
95
 
 
96
bufferlist LevelDBStore::to_bufferlist(leveldb::Slice in)
 
97
{
 
98
  bufferlist bl;
 
99
  bl.append(bufferptr(in.data(), in.size()));
 
100
  return bl;
 
101
}
 
102
 
 
103
int LevelDBStore::split_key(leveldb::Slice in, string *prefix, string *key)
 
104
{
 
105
  string in_prefix = in.ToString();
 
106
  size_t prefix_len = in_prefix.find('\0');
 
107
  if (prefix_len >= in_prefix.size())
 
108
    return -EINVAL;
 
109
 
 
110
  if (prefix)
 
111
    *prefix = string(in_prefix, 0, prefix_len);
 
112
  if (key)
 
113
    *key= string(in_prefix, prefix_len + 1);
 
114
  return 0;
 
115
}