Key-Value Store

From Lianjapedia
Revision as of 00:14, 14 January 2022 by Barrymavin (Talk | contribs)

Jump to: navigation, search

Overview

Lianja 6.3 introduces a new Key-Value Store (KVS) to support data streaming and IoT applications.

A key-value database is a type of nonrelational database that uses a simple key-value method to store data. A key-value database stores data as a collection of key-value pairs in which a key serves as a unique identifier. Both keys and values can be anything, ranging from simple objects to complex compound objects.

KVS is implemented as a collection of functions built into Lianja.

It is a high performance object store for storing keys with values that may be objects that are automatically JSON encoded and decoded.

The KVS itself is a B+ tree index which contains keys and values inside the KVS file.

It is a 64-bit file store so it can contain a huge number of indexed Key/Value pairs. Deleted space is reused.

It handles automatic locking supporting concurrent high speed CRUD operations.

KVS can be used for data streaming applications as it is very high performance and, with the kvs_removeLast() function, can be used to implement a stack of data, together with kvs_removeFirst() which can be used to implement a queue of data.

KVS is used in Lianja to handle forthcoming offline database support. This will be documented later.

It is planned to enhance KVS later with sharding/partitioning to facilitate high performance clustered indexing.

KVS Built-in Functions

kvs_add()

Add key/value for the specified key. If the value is an object or array it is automatically JSON encoded.

ok = kvs_add(kvsid, cKey | nKey, objectName | arrayName | cExpr)

kvs_clear()

Remove all key/value pairs from the store.

kvs_clear(kvsid)

kvs_close()

Close the KVS.

kvs_close(kvsid)

kvs_contains()

Returns true if key contained in the store.

logical = kvs_contains(kvsid,  cKey | nKey)

kvs_count()

Returns count of number of key/value pairs in the store.

numeric = kvs_count(kvsid)

kvs_create()

Create a new KVS with keys of nKeylen length.

kvsid = kvs_create(cFilename[.kvs], nKeylen [, bEnableLogging])

kvs_generate()

Generate nCount new key/value pairs. From v6.4.

numeric = kvs_generate(kvsid, nCount)

kvs_get()

Fetch key/value for the specified key. If the value is an object then cMembername specifies a member (property) to return.

value = kvs_get(kvsid, cKey | nKey [,cDefault [,cMembername] ])

kvs_getAll()

Fetch all keys/values for the specified key. CKeyPattern may end in * to match wildcard keys. Returns a dynarray of arrays.

object = kvs_getAll(kvsid [, cKey | cKeyPattern ])

kvs_getRange()

Fetch a range of key/value pairs (offset starts at 1). Returns an array of two elements, key and value.

array = kvs_getRange(kvsid, nOffset, nCount)

kvs_getInfo()

Displays information about the specified KVS file.

kvs_info(kvsid)

kvs_keys()

Fetch all keys for the specified key. CKeyPattern may end in * to match wildcard keys.

array = kvs_keys(kvsid, [, cKey | cKeyPattern ])

kvs_list()

List the key/values in the store to the console (useful during development for debugging).

kvs_list(kvsid)

kvs_listLog()

List binary transaction log associated with the store to the console (useful during development for debugging).

kvs_listLog(kvsid)

kvs_loadFromFile()

Load key/value pairs from the specified file. From v6.4.

numeric = kvs_loadFromFile(kvsid, cFilename.txt)

kvs_max()

Returns max key.

kvs_max(kvsid)

kvs_min()

Returns min key.

kvs_min(kvsid)

kvs_next()

Position on next key/value pair returns an array of two elements: key and value.

array = kvs_next(kvsid)

kvs_open()

Open an existing KVS. lShared is supported from v6.4, allowing the KVS to be opened shared or exclusive.

kvsid = kvs_open(cFilename.kvs [,lShared])

kvs_rebuild()

Rebuild the KVS file from the binary transaction log.

kvs_rebuild(cFilename)

kvs_remove()

Remove the specified key.

kvs_remove(kvsid, cKey | nKey)

kvs_removeFirst()

Use for queue. Returns an array of two elements: key and value.

array = kvs_removeFirst(kvsid)

kvs_removeLast()

Use for stack. Returns an array of two elements: key and value.

array = kvs_removeLast(kvsid)

kvs_removeRange()

Fetch a range of key/value pairs (offset starts at 1). From v6.4.

numeric = kvs_removeRange(kvsid, nOffset, nCount)

kvs_rewind()

Position on first key/value pair. Returns an array of two elements: key and value.

array = kvs_rewind(kvsid)

kvs_saveToFile()

Save key/value pairs to the specified file. From v6.4.

numeric = kvs_saveToFile(kvsid, cFilename.txt)

kvs_set()

Add/update key/value for the specified key. If the value is an object or array, it is automatically JSON encoded.

ok = kvs_set(kvsid, cKey | nKey, objectName | arrayName | cExpr)

kvs_update()

Update key/value for the specified key. If the value is an object or array, it is automatically JSON encoded.

ok = kvs_update(kvsid, cKey | nKey, objectName | arrayName | cExpr)

kvs_values()

Fetch all values for the specified key. CKeyPattern may end in * to match wildcard keys.

array = kvs_values(kvsid, [, cKey | cKeyPattern ])