Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kv: implement value_as_ptr() and use it in .get() #7052

Merged
merged 2 commits into from Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/kv/KeyValueDB.h
Expand Up @@ -163,6 +163,14 @@ class KeyValueDB {
virtual std::pair<std::string,std::string> raw_key() = 0;
virtual bool raw_key_is_prefixed(const std::string &prefix) = 0;
virtual bufferlist value() = 0;
virtual bufferptr value_as_ptr() {
bufferlist bl = value();
if (bl.length()) {
return *bl.buffers().begin();
} else {
return bufferptr();
}
}
virtual int status() = 0;
virtual ~WholeSpaceIteratorImpl() { }
};
Expand Down Expand Up @@ -223,6 +231,9 @@ class KeyValueDB {
bufferlist value() {
return generic_iter->value();
}
bufferptr value_as_ptr() {
return generic_iter->value_as_ptr();
}
int status() {
return generic_iter->status();
}
Expand Down
3 changes: 2 additions & 1 deletion src/kv/LevelDBStore.cc
Expand Up @@ -267,12 +267,13 @@ int LevelDBStore::get(const string &prefix,
const string &key,
bufferlist *value)
{
assert(value && (value->length() == 0));
utime_t start = ceph_clock_now(g_ceph_context);
int r = 0;
KeyValueDB::Iterator it = get_iterator(prefix);
it->lower_bound(key);
if (it->valid() && it->key() == key) {
*value = it->value();
value->append(it->value_as_ptr());
} else {
r = -ENOENT;
}
Expand Down
6 changes: 6 additions & 0 deletions src/kv/LevelDBStore.h
Expand Up @@ -298,6 +298,12 @@ class LevelDBStore : public KeyValueDB {
bufferlist value() {
return to_bufferlist(dbiter->value());
}

bufferptr value_as_ptr() {
leveldb::Slice data = dbiter->value();
return bufferptr(data.data(), data.size());
}

int status() {
return dbiter->status().ok() ? 0 : -1;
}
Expand Down
11 changes: 9 additions & 2 deletions src/kv/RocksDBStore.cc
Expand Up @@ -366,12 +366,13 @@ int RocksDBStore::get(
const string &key,
bufferlist *out)
{
assert(out && (out->length() == 0));
utime_t start = ceph_clock_now(g_ceph_context);
int r = 0;
KeyValueDB::Iterator it = get_iterator(prefix);
it->lower_bound(key);
if (it->valid() && it->key() == key) {
*out = it->value();
out->append(it->value_as_ptr());
} else {
r = -ENOENT;
}
Expand Down Expand Up @@ -589,6 +590,13 @@ bufferlist RocksDBStore::RocksDBWholeSpaceIteratorImpl::value()
{
return to_bufferlist(dbiter->value());
}

bufferptr RocksDBStore::RocksDBWholeSpaceIteratorImpl::value_as_ptr()
{
rocksdb::Slice val = dbiter->value();
return bufferptr(val.data(), val.size());
}

int RocksDBStore::RocksDBWholeSpaceIteratorImpl::status()
{
return dbiter->status().ok() ? 0 : -1;
Expand All @@ -601,7 +609,6 @@ string RocksDBStore::past_prefix(const string &prefix)
return limit;
}


RocksDBStore::WholeSpaceIterator RocksDBStore::_get_iterator()
{
return std::shared_ptr<KeyValueDB::WholeSpaceIteratorImpl>(
Expand Down
1 change: 1 addition & 0 deletions src/kv/RocksDBStore.h
Expand Up @@ -192,6 +192,7 @@ class RocksDBStore : public KeyValueDB {
pair<string,string> raw_key();
bool raw_key_is_prefixed(const string &prefix);
bufferlist value();
bufferptr value_as_ptr();
int status();
};

Expand Down
8 changes: 2 additions & 6 deletions src/mon/MonitorDBStore.h
Expand Up @@ -507,12 +507,8 @@ class MonitorDBStore
}

int get(const string& prefix, const string& key, bufferlist& bl) {
bufferlist outbl;
int r = db->get(prefix, key, &outbl);
if (!r) {
bl.append(outbl);
}
return r;
assert(bl.length() == 0);
return db->get(prefix, key, &bl);
}

int get(const string& prefix, const version_t ver, bufferlist& bl) {
Expand Down