Skip to content

LevelDB module

Francesco Saverio Castellano edited this page Mar 20, 2020 · 15 revisions

This module wraps LevelDB database library. You can use it to implement local data storage in your app.

LevelDB.open(string dbPath, [object options])
Opens a LevelDB database on the specified directory. If the directory does not exist it is created.
Returns an integer whose value is the handle that must be used to do get/put/delete operations on the database.
Every LevelDB database can only be opened by one single process at a time. LevelDB library is thread-safe. Therefore you may generally want to open the database(s) in the main thread and then share the database handle(s) across every thread.
Optionally it is possible to specify LevelDB options by passing options as a second argument. Its value must be a javascript object that can contain the following properties:

  • create_if_missing: boolean
  • error_if_exists: boolean
  • paranoid_checks: boolean
  • write_buffer_size: integer
  • max_open_files: integer
  • block_size: integer
  • block_cache_size: integer
  • block_restart_interval: integer
  • bloom_filter_policy_bits: integer
  • compression: boolean
  • cache_size: integer

For the meaning of each property and to know the default values refer to this page:
https://rdrr.io/github/richfitz/rleveldb/man/leveldb_open.html
The default value of create_if_missing is set to true.
The following examples shows how to open/create a LevelDB database with snappy compression set to true and block size set to 512MB:

var options = {
  compression: true,
  block_cache_size: (512*1024*1024)
}

var db = LevelDB.open("./mydb", options);

LevelDB.put(db, "mykey", "myval");

print("The value of 'mykey' is: " + LevelDB.get(db, "mykey"));

 
LevelDB.put(integer dbHandle, string key, string value)
Write value in key.  
 
LevelDB.read(integer dbHandle, string key)
Reads and returns the value in key.  
 
LevelDB.delete(integer dbHandle, string key)
Deletes the key and its content.    

Clone this wiki locally