node-lmdb icon indicating copy to clipboard operation
node-lmdb copied to clipboard

Support for mdb_cursor_put()

Open degifted opened this issue 8 years ago • 4 comments

Any plans to add support for mdb_cursor_put()? It should be more efficient along with MDB_CURRENT option in case when you need to perform read-modify-update operation.

degifted avatar May 12 '17 15:05 degifted

@degifted Sure, can you give me an example use case?

Venemo avatar May 13 '17 14:05 Venemo

Imagine that you have to modify some values in a big table. Currently you have to iterate over the table and alter desired values by putString() method. But this would require additional unnecessary lookup which can be quite costly. Having cursor.setCurrentString() would eliminate that lookup.

degifted avatar May 16 '17 17:05 degifted

@degifted Taking a look at mdb_cursor_put it should be quite easy to wrap it. I'll take a look and have a go at it next week.

Venemo avatar May 17 '17 08:05 Venemo

@Venemo In the meanwhile I implemented quick and dirty method which addresses my needs

NAN_METHOD(CursorWrap::putUInt32) {
    Nan::HandleScope scope;
    CursorWrap *cw = Nan::ObjectWrap::Unwrap<CursorWrap>(info.This());
    uint32_t value = Nan::To<v8::Number>(info[0]).ToLocalChecked()->Value();
    MDB_val data;
    data.mv_size = sizeof(uint32_t);
    data.mv_data = &value;
    unsigned int flags = MDB_CURRENT;
    int rc = mdb_cursor_put(cw->cursor, &(cw->key), &data, flags);
    if (rc == MDB_NOTFOUND) {
        return info.GetReturnValue().Set(Nan::Null());
    }
    else if (rc != 0) {
        return Nan::ThrowError(mdb_strerror(rc));
    }
}

degifted avatar Jun 17 '17 20:06 degifted