1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2 #ifndef DBWRAPPER_H
3 #define DBWRAPPER_H
4 
5 #include <map>
6 #include <node.h>
7 
8 #include "rocksdb/db.h"
9 #include "rocksdb/slice.h"
10 #include "rocksdb/options.h"
11 
12 using namespace v8;
13 
14 // Used to encapsulate a particular instance of an opened database.
15 //
16 // This object should not be used directly in C++; it exists solely to provide
17 // a mapping from a JavaScript object to a C++ code that can use the RocksDB
18 // API.
19 class DBWrapper : public node::ObjectWrap {
20   public:
21     static void Init(Handle<Object> exports);
22 
23   private:
24     explicit DBWrapper();
25     ~DBWrapper();
26 
27     // Helper methods
28     static bool HasFamilyNamed(std::string& name, DBWrapper* db);
29     static bool AddToBatch(ROCKSDB_NAMESPACE::WriteBatch& batch, bool del,
30                            Handle<Array> array);
31     static bool AddToBatch(ROCKSDB_NAMESPACE::WriteBatch& batch, bool del,
32                            Handle<Array> array, DBWrapper* db_wrapper,
33                            std::string cf);
34     static Handle<Value> CompactRangeDefault(const v8::Arguments& args);
35     static Handle<Value> CompactColumnFamily(const Arguments& args);
36     static Handle<Value> CompactOptions(const Arguments& args);
37     static Handle<Value> CompactAll(const Arguments& args);
38 
39     // C++ mappings of API methods
40     static Persistent<v8::Function> constructor;
41     static Handle<Value> Open(const Arguments& args);
42     static Handle<Value> New(const Arguments& args);
43     static Handle<Value> Get(const Arguments& args);
44     static Handle<Value> Put(const Arguments& args);
45     static Handle<Value> Delete(const Arguments& args);
46     static Handle<Value> Dump(const Arguments& args);
47     static Handle<Value> WriteBatch(const Arguments& args);
48     static Handle<Value> CreateColumnFamily(const Arguments& args);
49     static Handle<Value> CompactRange(const Arguments& args);
50     static Handle<Value> Close(const Arguments& args);
51 
52     // Internal fields
53     ROCKSDB_NAMESPACE::Options options_;
54     ROCKSDB_NAMESPACE::Status status_;
55     ROCKSDB_NAMESPACE::DB* db_;
56     std::unordered_map<std::string, ROCKSDB_NAMESPACE::ColumnFamilyHandle*>
57         columnFamilies_;
58 };
59 
60 #endif
61