44#include < node_api.h>
55#include < assert.h>
66
7- #include < leveldb/db.h>
8- #include < leveldb/write_batch.h>
9- #include < leveldb/cache.h>
10- #include < leveldb/filter_policy.h>
7+ #include < rocksdb/db.h>
8+ #include < rocksdb/write_batch.h>
9+ #include < rocksdb/cache.h>
10+ #include < rocksdb/filter_policy.h>
11+ #include < rocksdb/utilities/leveldb_options.h>
12+ #include < rocksdb/cache.h>
13+ #include < rocksdb/comparator.h>
14+ #include < rocksdb/env.h>
15+ #include < rocksdb/options.h>
16+ #include < rocksdb/table.h>
17+
18+ namespace leveldb = rocksdb;
1119
1220#include < map>
1321#include < vector>
@@ -337,8 +345,6 @@ struct Database {
337345 Database (napi_env env)
338346 : env_(env),
339347 db_ (NULL ),
340- blockCache_(NULL ),
341- filterPolicy_(leveldb::NewBloomFilterPolicy(10 )),
342348 currentIteratorId_(0 ),
343349 pendingCloseWorker_(NULL ),
344350 priorityWork_(0 ) {}
@@ -351,17 +357,18 @@ struct Database {
351357 }
352358
353359 leveldb::Status Open (const leveldb::Options& options,
360+ bool readOnly,
354361 const char * location) {
355- return leveldb::DB::Open (options, location, &db_);
362+ if (readOnly) {
363+ return rocksdb::DB::OpenForReadOnly (options, location, &db_);
364+ } else {
365+ return leveldb::DB::Open (options, location, &db_);
366+ }
356367 }
357368
358369 void CloseDatabase () {
359370 delete db_;
360371 db_ = NULL ;
361- if (blockCache_) {
362- delete blockCache_;
363- blockCache_ = NULL ;
364- }
365372 }
366373
367374 leveldb::Status Put (const leveldb::WriteOptions& options,
@@ -394,7 +401,8 @@ struct Database {
394401
395402 void CompactRange (const leveldb::Slice* start,
396403 const leveldb::Slice* end) {
397- db_->CompactRange (start, end);
404+ rocksdb::CompactRangeOptions options;
405+ db_->CompactRange (options, start, end);
398406 }
399407
400408 void GetProperty (const leveldb::Slice& property, std::string* value) {
@@ -440,8 +448,6 @@ struct Database {
440448
441449 napi_env env_;
442450 leveldb::DB* db_;
443- leveldb::Cache* blockCache_;
444- const leveldb::FilterPolicy* filterPolicy_;
445451 uint32_t currentIteratorId_;
446452 BaseWorker *pendingCloseWorker_;
447453 std::map< uint32_t , Iterator * > iterators_;
@@ -521,6 +527,7 @@ struct Iterator {
521527 ref_(NULL ) {
522528 options_ = new leveldb::ReadOptions ();
523529 options_->fill_cache = fillCache;
530+ options_->verify_checksums = false ;
524531 options_->snapshot = database->NewSnapshot ();
525532 }
526533
@@ -768,30 +775,48 @@ struct OpenWorker final : public BaseWorker {
768775 uint32_t blockSize,
769776 uint32_t maxOpenFiles,
770777 uint32_t blockRestartInterval,
771- uint32_t maxFileSize)
778+ uint32_t maxFileSize,
779+ uint32_t cacheSize,
780+ bool readOnly)
772781 : BaseWorker(env, database, callback, " leveldown.db.open" ),
782+ readOnly_ (readOnly),
773783 location_(location) {
774- options_.block_cache = database->blockCache_ ;
775- options_.filter_policy = database->filterPolicy_ ;
776784 options_.create_if_missing = createIfMissing;
777785 options_.error_if_exists = errorIfExists;
778786 options_.compression = compression
779787 ? leveldb::kSnappyCompression
780788 : leveldb::kNoCompression ;
781789 options_.write_buffer_size = writeBufferSize;
782- options_.block_size = blockSize;
783790 options_.max_open_files = maxOpenFiles;
784- options_.block_restart_interval = blockRestartInterval;
785- options_.max_file_size = maxFileSize;
791+ options_.max_log_file_size = maxFileSize;
792+ options_.paranoid_checks = false ;
793+ options_.info_log_level = rocksdb::InfoLogLevel::WARN_LEVEL;
794+
795+ rocksdb::BlockBasedTableOptions tableOptions;
796+
797+ if (cacheSize) {
798+ tableOptions.block_cache = rocksdb::NewLRUCache (cacheSize);
799+ } else {
800+ tableOptions.no_block_cache = true ;
801+ }
802+
803+ tableOptions.block_size = blockSize;
804+ tableOptions.block_restart_interval = blockRestartInterval;
805+ tableOptions.filter_policy .reset (rocksdb::NewBloomFilterPolicy (10 ));
806+
807+ options_.table_factory .reset (
808+ rocksdb::NewBlockBasedTableFactory (tableOptions)
809+ );
786810 }
787811
788812 ~OpenWorker () {}
789813
790814 void DoExecute () override {
791- SetStatus (database_->Open (options_, location_.c_str ()));
815+ SetStatus (database_->Open (options_, readOnly_, location_.c_str ()));
792816 }
793817
794818 leveldb::Options options_;
819+ bool readOnly_;
795820 std::string location_;
796821};
797822
@@ -807,6 +832,7 @@ NAPI_METHOD(db_open) {
807832 bool createIfMissing = BooleanProperty (env, options, " createIfMissing" , true );
808833 bool errorIfExists = BooleanProperty (env, options, " errorIfExists" , false );
809834 bool compression = BooleanProperty (env, options, " compression" , true );
835+ bool readOnly = BooleanProperty (env, options, " readOnly" , false );
810836
811837 uint32_t cacheSize = Uint32Property (env, options, " cacheSize" , 8 << 20 );
812838 uint32_t writeBufferSize = Uint32Property (env, options , " writeBufferSize" , 4 << 20 );
@@ -816,14 +842,12 @@ NAPI_METHOD(db_open) {
816842 " blockRestartInterval" , 16 );
817843 uint32_t maxFileSize = Uint32Property (env, options, " maxFileSize" , 2 << 20 );
818844
819- database->blockCache_ = leveldb::NewLRUCache (cacheSize);
820-
821845 napi_value callback = argv[3 ];
822846 OpenWorker* worker = new OpenWorker (env, database, callback, location,
823847 createIfMissing, errorIfExists,
824848 compression, writeBufferSize, blockSize,
825849 maxOpenFiles, blockRestartInterval,
826- maxFileSize);
850+ maxFileSize, cacheSize, readOnly );
827851 worker->Queue ();
828852 delete [] location;
829853
0 commit comments