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

feat: prefix_extractor option #168

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <rocksdb/env.h>
#include <rocksdb/options.h>
#include <rocksdb/table.h>
#include "rocksdb/slice_transform.h"
#include "rocksdb/slice.h"

namespace leveldb = rocksdb;

Expand Down Expand Up @@ -210,6 +212,38 @@ static std::string StringProperty (napi_env env, napi_value obj, const char* key
return "";
}

static const rocksdb::SliceTransform* PrefixExtractor(napi_env env, napi_value options){
const char* PREFIX_EXTRACTOR = "prefix_extractor";
const char* PREFIX_FIXED = "fixed";
const char* PREFIX_CAPPED = "capped";

if(!HasProperty(env, options, PREFIX_EXTRACTOR)) {
return nullptr;
}

napi_value prefix = GetProperty(env, options, PREFIX_EXTRACTOR);

if(!IsObject(env, prefix)) {
return nullptr;
}

if(HasProperty(env, prefix, PREFIX_FIXED)){
int length = Int32Property(env, prefix,PREFIX_FIXED, -1);
if(length >= 0){
return rocksdb::NewFixedPrefixTransform(length);
}
}

if(HasProperty(env, prefix, PREFIX_CAPPED)){
int length = Int32Property(env, prefix, PREFIX_CAPPED, -1);
if(length >= 0){
return rocksdb::NewCappedPrefixTransform(length);
}
}

return nullptr;
}

static void DisposeSliceBuffer (leveldb::Slice slice) {
if (!slice.empty()) delete [] slice.data();
}
Expand Down Expand Up @@ -749,7 +783,8 @@ struct OpenWorker final : public BaseWorker {
uint32_t maxFileSize,
uint32_t cacheSize,
const std::string& infoLogLevel,
bool readOnly)
bool readOnly,
const rocksdb::SliceTransform* prefix_extractor)
: BaseWorker(env, database, callback, "leveldown.db.open"),
readOnly_(readOnly),
location_(location) {
Expand All @@ -763,6 +798,10 @@ struct OpenWorker final : public BaseWorker {
options_.max_log_file_size = maxFileSize;
options_.paranoid_checks = false;

if(prefix_extractor != nullptr){
options_.prefix_extractor.reset(prefix_extractor);
}

if (infoLogLevel.size() > 0) {
rocksdb::InfoLogLevel lvl;

Expand Down Expand Up @@ -834,13 +873,15 @@ NAPI_METHOD(db_open) {
"blockRestartInterval", 16);
uint32_t maxFileSize = Uint32Property(env, options, "maxFileSize", 2 << 20);

const rocksdb::SliceTransform* prefix_extractor = PrefixExtractor(env, options);

napi_value callback = argv[3];
OpenWorker* worker = new OpenWorker(env, database, callback, location,
createIfMissing, errorIfExists,
compression, writeBufferSize, blockSize,
maxOpenFiles, blockRestartInterval,
maxFileSize, cacheSize,
infoLogLevel, readOnly);
infoLogLevel, readOnly, prefix_extractor);
worker->Queue();
delete [] location;

Expand Down
33 changes: 33 additions & 0 deletions test/prefix_extractor-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const test = require('tape')
const testCommon = require('./common')

test('prefix_extractor', function (t) {
const db = testCommon.factory()

t.test('setup', function (t) {
db.open({ prefix_extractor: { fixed: 3 } }, function (err) {
t.ifError(err, 'no open error')

db.batch()
.put('fooA', 'A')
.put('fooB', 'B')
.put('fooC', 'C')
.write(t.end.bind(t))
})
})

t.test('get keys using prefix_extractor', function (t) {
t.plan(2)

db.get('fooA', function (err, value) {
t.ifError(err, 'failed to get key')
t.is(value.toString(), 'A', 'got key value')
})
})

t.test('teardown', function (t) {
db.close(t.end.bind(t))
})

t.end()
})