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: add flatten option #18

Draft
wants to merge 1 commit into
base: main
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
40 changes: 31 additions & 9 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ struct Iterator final : public BaseIterator {
std::string* gt,
std::string* gte,
const bool fillCache,
const bool flatten,
const bool keyAsBuffer,
const bool valueAsBuffer,
const uint32_t highWaterMarkBytes)
Expand All @@ -839,6 +840,7 @@ struct Iterator final : public BaseIterator {
values_(values),
keyAsBuffer_(keyAsBuffer),
valueAsBuffer_(valueAsBuffer),
flatten_(flatten),
highWaterMarkBytes_(highWaterMarkBytes),
first_(true),
nexting_(false),
Expand Down Expand Up @@ -899,6 +901,7 @@ struct Iterator final : public BaseIterator {
const bool values_;
const bool keyAsBuffer_;
const bool valueAsBuffer_;
const bool flatten_;
const uint32_t highWaterMarkBytes_;
bool first_;
bool nexting_;
Expand Down Expand Up @@ -1630,6 +1633,7 @@ NAPI_METHOD(iterator_init) {
const bool keys = BooleanProperty(env, options, "keys", true);
const bool values = BooleanProperty(env, options, "values", true);
const bool fillCache = BooleanProperty(env, options, "fillCache", false);
const bool flatten = BooleanProperty(env, options, "flatten", false);
const bool keyAsBuffer = EncodingIsBuffer(env, options, "keyEncoding");
const bool valueAsBuffer = EncodingIsBuffer(env, options, "valueEncoding");
const int limit = Int32Property(env, options, "limit", -1);
Expand All @@ -1642,7 +1646,7 @@ NAPI_METHOD(iterator_init) {

const uint32_t id = database->currentIteratorId_++;
Iterator* iterator = new Iterator(database, id, reverse, keys,
values, limit, lt, lte, gt, gte, fillCache,
values, limit, lt, lte, gt, gte, fillCache, flatten,
keyAsBuffer, valueAsBuffer, highWaterMarkBytes);
napi_value result;

Expand Down Expand Up @@ -1757,17 +1761,35 @@ struct NextWorker final : public BaseWorker {
}

void HandleOKCallback (napi_env env, napi_value callback) override {
size_t size = iterator_->cache_.size();
napi_value jsArray;
napi_create_array_with_length(env, size, &jsArray);

const bool kab = iterator_->keyAsBuffer_;
const bool vab = iterator_->valueAsBuffer_;
const bool flatten = iterator_->flatten_;

for (uint32_t idx = 0; idx < size; idx++) {
napi_value element;
iterator_->cache_[idx].ConvertByMode(env, Mode::entries, kab, vab, &element);
napi_set_element(env, jsArray, idx, element);
napi_value jsArray;

if (flatten) {
const auto size = iterator_->cache_.size() * 2;
napi_create_array_with_length(env, size, &jsArray);

for (uint32_t idx = 0; idx < size; idx += 2) {
napi_value key;
napi_value value;

iterator_->cache_[idx].ConvertByMode(env, Mode::keys, kab, vab, &key);
iterator_->cache_[idx].ConvertByMode(env, Mode::values, kab, vab, &value);

napi_set_element(env, jsArray, idx + 0, key);
napi_set_element(env, jsArray, idx + 1, value);
}
} else {
const auto size = iterator_->cache_.size();
napi_create_array_with_length(env, size, &jsArray);

for (uint32_t idx = 0; idx < size; idx++) {
napi_value element;
iterator_->cache_[idx].ConvertByMode(env, Mode::entries, kab, vab, &element);
napi_set_element(env, jsArray, idx, element);
}
}

napi_value argv[3];
Expand Down
14 changes: 14 additions & 0 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ make('iterator optimized for seek', function (db, t, done) {
})
})
})

make('iterator flatten', function (db, t, done) {
const batch = db.batch()
batch.put('a', 1)
batch.put('b', 1)
batch.write(function (err) {
t.ifError(err, 'no error from batch()')
const ite = db.iterator({ flatten: true }).all((err, arr) => {
t.ifError(err, 'no error from iterator')
t.same(arr, ['a', 1, 'b', 1])
done()
})
})
})