[Pkg-javascript-commits] [node-leveldown] 147/492: feature: Support setting size of LRU-cache

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:53 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.

commit 92371117a4a2a481c1c6679cc4908301262b8c47
Author: David Björklund <david.bjorklund at gmail.com>
Date:   Thu Jan 3 11:57:28 2013 +0100

    feature: Support setting size of LRU-cache
---
 README.md            | 6 ++++++
 src/database.cc      | 4 ++++
 src/database.h       | 2 ++
 src/database_async.h | 5 +++++
 src/iterator.cc      | 3 ++-
 src/iterator.h       | 3 +++
 src/levelup.h        | 2 ++
 7 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 22f87e2..45798a4 100644
--- a/README.md
+++ b/README.md
@@ -116,6 +116,8 @@ db.get('foo', function (err, value) {
 
 * `'compression'` *(boolean, default: `true`)*: If `true`, all *compressible* data will be run through the Snappy compression algorithm before being stored. Snappy is very fast and shouldn't gain much speed by disabling so leave this on unless you have good reason to turn it off.
 
+* `'cacheSize'` *(number, default: `8 * 1024 * 1024`)*: The size (in bytes) of the in-memory [LRU](http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used) cache with frequently used uncompressed block contents. 
+
 * `'encoding'` *(string, default: `'utf8'`)*: The encoding of the keys and values passed through Node.js' `Buffer` implementation (see [Buffer#toString()](http://nodejs.org/docs/latest/api/buffer.html#buffer_buf_tostring_encoding_start_end))
   <p><code>'utf8'</code> is the default encoding for both keys and values so you can simply pass in strings and expect strings from your <code>get()</code> operations. You can also pass <code>Buffer</code> objects as keys and/or values and conversion will be performed.</p>
   <p>Supported encodings are: hex, utf8, ascii, binary, base64, ucs2, utf16le.</p>
@@ -161,6 +163,8 @@ If you provide a `'sync'` value of `true` in your `options` object, LevelDB will
 
 Encoding of the `key` objects will adhere to `encoding` option(s) provided to <a href="#ctor"><code>levelup()</code></a>, although you can provide alternative encoding settings in the options for `get()` (it's recommended that you stay consistent in your encoding of keys and values in a single store).
 
+LevelDB will by default fill the in-memory LRU Cache with data from a call to get. Disabling this is done by setting `fillCache` to `false`. 
+
 --------------------------------------------------------
 <a name="del"></a>
 ### db.del(key[, options][, callback])
@@ -269,6 +273,8 @@ Additionally, you can supply an options object as the first parameter to `readSt
 
 * `'limit'` *(number, default: `-1`)*: limit the number of results collected by this stream. This number represents a *maximum* number of results and may not be reached if you get to the end of the store or your `'end'` value first. A value of `-1` means there is no limit.
 
+* `'fillCache'` *(boolean, default: `false`)*: wheather LevelDB's LRU-cache should be filled with data read.
+
 --------------------------------------------------------
 <a name="keyStream"></a>
 ### db.keyStream([options])
diff --git a/src/database.cc b/src/database.cc
index 1f6c7ea..dded13a 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -125,6 +125,7 @@ Handle<Value> Database::Open (const Arguments& args) {
   BOOLEAN_OPTION_VALUE(optionsObj, createIfMissing)
   BOOLEAN_OPTION_VALUE(optionsObj, errorIfExists)
   BOOLEAN_OPTION_VALUE(optionsObj, compression)
+  UINT32_OPTION_VALUE(optionsObj, cacheSize, 8 << 20)
   Persistent<Function> callback = Persistent<Function>::New(Local<Function>::Cast(args[2]));
 
   OpenWorker* worker = new OpenWorker(
@@ -134,6 +135,7 @@ Handle<Value> Database::Open (const Arguments& args) {
     , createIfMissing
     , errorIfExists
     , compression
+    , cacheSize
   );
   AsyncQueueWorker(worker);
 
@@ -196,12 +198,14 @@ Handle<Value> Database::Get (const Arguments& args) {
   STRING_OR_BUFFER_TO_SLICE(key, keyBuffer)
   Local<Object> optionsObj = Local<Object>::Cast(args[1]);
   BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, asBuffer)
+  BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, fillCache)
 
   ReadWorker* worker = new ReadWorker(
       database
     , callback
     , key
     , asBuffer
+    , fillCache
     , keyBuffer
   );
   AsyncQueueWorker(worker);
diff --git a/src/database.h b/src/database.h
index cdfcfb2..4ca0890 100644
--- a/src/database.h
+++ b/src/database.h
@@ -17,8 +17,10 @@ using namespace leveldb;
 LU_OPTION ( createIfMissing ); // for open()
 LU_OPTION ( errorIfExists   ); // for open()
 LU_OPTION ( compression     ); // for open()
+LU_OPTION ( cacheSize       ); // for open() 
 LU_OPTION ( sync            ); // for put() and delete()
 LU_OPTION ( asBuffer        ); // for get()
+LU_OPTION ( fillCache       ); // for get() and readStream()
 LU_STR    ( key   );
 LU_STR    ( value );
 LU_STR    ( type  );
diff --git a/src/database_async.h b/src/database_async.h
index 50d2213..2ad7b49 100644
--- a/src/database_async.h
+++ b/src/database_async.h
@@ -8,6 +8,7 @@
 #include <node.h>
 #include "async.h"
 #include "batch.h"
+#include "leveldb/cache.h"
 
 using namespace std;
 using namespace v8;
@@ -22,6 +23,7 @@ public:
     , bool createIfMissing
     , bool errorIfExists
     , bool compression
+    , uint32_t cacheSize
   ) : AsyncWorker(database, callback)
     , location(location)
   {
@@ -29,6 +31,7 @@ public:
     options->create_if_missing = createIfMissing;
     options->error_if_exists = errorIfExists;
     options->compression = compression ? kSnappyCompression : kNoCompression;
+    options->block_cache = NewLRUCache(cacheSize);
   };
 
   virtual ~OpenWorker ();
@@ -79,11 +82,13 @@ public:
     , Persistent<Function> callback
     , Slice key
     , bool asBuffer
+    , bool fillCache
     , Persistent<Value> keyPtr
   ) : IOWorker(database, callback, key, keyPtr)
     , asBuffer(asBuffer)
   {
     options = new ReadOptions();
+    options->fill_cache = fillCache;
   };
 
   virtual ~ReadWorker ();
diff --git a/src/iterator.cc b/src/iterator.cc
index ac939e4..d056c4f 100644
--- a/src/iterator.cc
+++ b/src/iterator.cc
@@ -139,11 +139,12 @@ Handle<Value> levelup::Iterator::New (const Arguments& args) {
   BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, values)
   BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, keyAsBuffer)
   BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, valueAsBuffer)
+  BOOLEAN_OPTION_VALUE(optionsObj, fillCache)
   int limit = -1;
   if (args[1]->ToObject()->Has(option_limit)) {
     limit = Local<Integer>::Cast(args[1]->ToObject()->Get(option_limit))->Value();
   }
-  Iterator* iterator = new Iterator(database, start, end, reverse, keys, values, limit, keyAsBuffer, valueAsBuffer);
+  Iterator* iterator = new Iterator(database, start, end, reverse, keys, values, limit, fillCache, keyAsBuffer, valueAsBuffer);
   iterator->Wrap(args.This());
 
   return args.This();
diff --git a/src/iterator.h b/src/iterator.h
index 3e48fd1..78c4d24 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -23,6 +23,7 @@ LU_OPTION ( keys          );
 LU_OPTION ( values        );
 LU_OPTION ( keyAsBuffer   );
 LU_OPTION ( valueAsBuffer );
+LU_OPTION ( fillCache     );
 
 Handle<Value> CreateIterator (const Arguments& args);
 
@@ -43,6 +44,7 @@ public:
     , bool keys
     , bool values
     , int limit
+    , bool fillCache
     , bool keyAsBuffer
     , bool valueAsBuffer
   ) : database(database)
@@ -56,6 +58,7 @@ public:
     , valueAsBuffer(valueAsBuffer)
   {
     options = new ReadOptions();
+    options->fill_cache = fillCache;
     dbIterator = NULL;
     count = 0;
   };
diff --git a/src/levelup.h b/src/levelup.h
index 4706e53..5250208 100644
--- a/src/levelup.h
+++ b/src/levelup.h
@@ -39,6 +39,8 @@
   bool opt = optionsObj->Has(option_ ## opt) && optionsObj->Get(option_ ## opt)->BooleanValue();
 #define BOOLEAN_OPTION_VALUE_DEFTRUE(optionsObj, opt) \
   bool opt = !optionsObj->Has(option_ ## opt) || optionsObj->Get(option_ ## opt)->BooleanValue();
+#define UINT32_OPTION_VALUE(optionsObj, opt, default) \
+  uint32_t opt = optionsObj->Has(option_ ## opt) ? optionsObj->Get(option_ ## opt)->Uint32Value() : default;
 
 void RunCallback (v8::Persistent<v8::Function> callback, v8::Local<v8::Value> argv[], int length);
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git



More information about the Pkg-javascript-commits mailing list