Skip to content

Commit

Permalink
use slice instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
Yichao 'Peak' Ji committed Nov 28, 2016
1 parent 4c24d74 commit 26a297c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Iterator::Iterator (
options->snapshot = database->NewSnapshot();
dbIterator = NULL;
count = 0;
target = NULL;
seeking = false;
nexting = false;
ended = false;
Expand All @@ -64,6 +65,7 @@ Iterator::Iterator (

Iterator::~Iterator () {
delete options;
ReleaseTarget();
if (start != NULL) {
// Special case for `start` option: it won't be
// freed up by any of the delete calls below.
Expand Down Expand Up @@ -205,7 +207,19 @@ void Iterator::Release () {
database->ReleaseIterator(id);
}

void Iterator::ReleaseTarget () {
if (target != NULL) {

if (!target->empty())
delete[] target->data();

delete target;

This comment has been minimized.

Copy link
@ralphtheninja

ralphtheninja Dec 19, 2016

Member

Isn't delete target; enough? Assuming leveldb::Slice::~Slice() cleans up its internal buffer.

This comment has been minimized.

Copy link
@peakji

peakji Dec 20, 2016

Member

I wrote this after browsing slice.h. I think its ok? Lets just leave it this way 🤔

target = NULL;
}
}

void checkEndCallback (Iterator* iterator) {
iterator->ReleaseTarget();
iterator->nexting = false;
if (iterator->endWorker != NULL) {
Nan::AsyncQueueWorker(iterator->endWorker);
Expand All @@ -216,26 +230,26 @@ void checkEndCallback (Iterator* iterator) {
NAN_METHOD(Iterator::Seek) {
Iterator* iterator = Nan::ObjectWrap::Unwrap<Iterator>(info.This());

iterator->ReleaseTarget();

if (!node::Buffer::HasInstance(info[0]) && !info[0]->IsString())
return Nan::ThrowError("seek() requires a string or buffer key");

if (StringOrBufferLength(info[0]) == 0)
return Nan::ThrowError("cannot seek() to an empty key");

std::string* key = NULL;
v8::Local<v8::Value> keyBuffer = info[0].As<v8::Value>();
LD_STRING_OR_BUFFER_TO_COPY(_key, keyBuffer, key);
key = new std::string(_keyCh_, _keySz_);
delete[] _keyCh_;
v8::Local<v8::Value> targetBuffer = info[0].As<v8::Value>();
LD_STRING_OR_BUFFER_TO_COPY(_target, targetBuffer, target);
iterator->target = new leveldb::Slice(_targetCh_, _targetSz_);

iterator->GetIterator();
leveldb::Iterator* dbIterator = iterator->dbIterator;

dbIterator->Seek(*key);
dbIterator->Seek(*iterator->target);
iterator->seeking = true;

if (dbIterator->Valid()) {
int cmp = dbIterator->key().compare(*key);
int cmp = dbIterator->key().compare(*iterator->target);
if (cmp > 0 && iterator->reverse) {
dbIterator->Prev();
} else if (cmp < 0 && !iterator->reverse) {
Expand All @@ -248,7 +262,7 @@ NAN_METHOD(Iterator::Seek) {
dbIterator->SeekToFirst();
}
if (dbIterator->Valid()) {
int cmp = dbIterator->key().compare(*key);
int cmp = dbIterator->key().compare(*iterator->target);
if (cmp > 0 && iterator->reverse) {
dbIterator->SeekToFirst();
dbIterator->Prev();
Expand Down
2 changes: 2 additions & 0 deletions src/iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ class Iterator : public Nan::ObjectWrap {
leveldb::Status IteratorStatus ();
void IteratorEnd ();
void Release ();
void ReleaseTarget ();

private:
Database* database;
uint32_t id;
leveldb::Iterator* dbIterator;
leveldb::ReadOptions* options;
leveldb::Slice* start;
leveldb::Slice* target;
std::string* end;
bool seeking;
bool reverse;
Expand Down

0 comments on commit 26a297c

Please sign in to comment.