PrivMX DOCS
Kotlin

KVDB Entries

Setting Entries in KVDB.

Sample code on this page is based on the initial assumptions.

Entries Inside KVDB

Entries inside KVDBs are set in binary format. Before creating an entry, you need to decide on the entry format.

For more information about the KVDB's architecture and best practices for creating entries, visit the KVDBs Documentation.

Creating KVDB Entry

When you create a new entry, the version parameter must be set to 0 - which is the default value.

val kvdbId = "KVDB_ID"
val kvdbEntryKey = "KVDB_ENTRY_KEY"
val kvdbEntryData = "Plain Data"
val publicMeta = ByteArray(0)
val privateMeta = ByteArray(0)

val entryId = kvdbApi.setEntry(
    kvdbId,
    kvdbEntryKey,
    publicMeta,
    privateMeta,
    kvdbEntryData.encodeToByteArray()
)

Updating KVDB Entry

If you want to update an existing KVDB entry - you have to provide current version of given entry. Even if certain fields haven’t changed, their current values must still be provided e.g. public and private meta.

val kvdbId = "KVDB_ID"
val kvdbEntryKey = "KVDB_ENTRY_KEY"
val kvdbEntry: KvdbEntry = kvdbApi.getEntry(kvdbId, kvdbEntryKey)
val newKvdbEntryData = "New data"

kvdbApi.setEntry(
    kvdbId,
    kvdbEntryKey,
    kvdbEntry.publicMeta,
    kvdbEntry.privateMeta,
    newKvdbEntryData.encodeToByteArray(),
    kvdbEntry.version!! + 1
)

Getting KVDB Entries

Define entry item class with decoded data and publicMeta:

data class KvdbEntryItem(
    val kvdbEntry: KvdbEntry,
    val decodedData: String,
    val decodedPublicMeta: KvdbEntryPublicMeta
)

Fetching the most recent entries in given KVDB:

val kvdbId = "KVDB_ID"
val startIndex = 0L
val pageSize = 100L

val kvdbEntriesPagingList = kvdbApi.listEntries(
    kvdbId,
    startIndex,
    pageSize,
    SortOrder.DESC
)

val kvdbEntries = kvdbEntriesPagingList.readItems.map {
    KvdbEntryItem(
        it,
        it.data.decodeToString(),
        Json.decodeFromString(it.publicMeta.decodeToString())
    )
}

Checking if an Entry exists

You can check if a KVDB entry with the given key exists within the specified KVDB instance.

val kvdbId = "KVDB_ID"
val kvdbEntryKey = "KVDB_ENTRY_KEY"

val hasEntry: Boolean = kvdbApi.hasEntry(kvdbId, kvdbEntryKey)

Deleting KVDB Entries

The snippet below deletes KVDB entry and all its data. This action is irreversible.

val kvdbId = "KVDB_ID"
val kvdbEntryKey = "KVDB_ENTRY_KEY"

kvdbApi.deleteEntry(kvdbId, kvdbEntryKey)

We use cookies on our website. We use them to ensure the proper functioning of the site and, if you agree, for purposes we set, such as analytics or marketing.

On this page