B4A Library [B4X] KVS - KeyValueStore library

Status
Not open for further replies.
A key / value persistent store. The data is serialized using B4XSerializator and is stored in an internal database. The database can be shared between B4A, B4i and B4J.

Using KVS is similar to using a Map. You initialize it once and then you can put or get items with Put, Get or GetDefault methods.

The supported types of objects are:

Lists, Maps, Strings, primitives (numbers), user defined types and arrays (only arrays of bytes and arrays of objects are supported).
Including combinations of these types (a list that holds maps for example).
Custom types should be declared in the main module or in B4XMainPage.
Bitmaps are supported with PutBitmap / GetBitmap methods.

Adding encryption:
Encryption is also supported but is not enabled by default as it requires some configuration:

1. Add KVS_ENCRYPTION to the build configuration (Ctrl + B):

1594889639522.png


2.
B4A: Add reference to B4XEncryption.
B4i: Add reference to iEncryption
B4J:
Add reference to jB4XEncryption
Download bouncycastle: https://www.bouncycastle.org/download/bcprov-jdk15on-154.jar and put it in the additional libraries folder.
Add to main module:
B4X:
#AdditionalJar: bcprov-jdk15on-154
3. Use PutEncrypted / GetEncrypted

Usage example:
B4X:
xui.SetDataFolder("kvs")
    kvs.Initialize(xui.DefaultFolder, "kvs.dat")
    kvs.Put("time", DateTime.Now)

    'fetch this value
    Log(DateTime.Time(kvs.Get("time")))
    Log(kvs.Get("doesn't exist"))
    Log(kvs.GetDefault("doesn't exist", 10))
    'put a Bitmap
    kvs.PutBitmap("bitmap1", xui.LoadBitmap(File.DirAssets, "smiley.gif"))
    'fetch a bitmap
    ImageView1.SetBitmap(kvs.GetBitmap("bitmap1"))
    'remove the bitmap from the store
    kvs.Remove("bitmap1")
    'put an array with two custom types
    kvs.Put("2 custom types", Array(CreateCustomType(1, "one"), CreateCustomType(2, "two")))
    'get them
    Dim mytypes() As Object = kvs.Get("2 custom types") 'the array type must be object or bytes

This library is named KeyValueStore. There is an older library named KeyValueStore2. Don't confuse between the two. It is recommended to use this one.
It is an internal library now.

Updates

v2.31 - New Vacuum method. This method frees unused space from the database file. It can be used after doing large deletes to free space.
 

Attachments

  • KeyValueStore.b4xlib
    2.2 KB · Views: 1,432
Last edited:

VittorioBarile

Member
Licensed User
How could i get and put a bidimensional array with custom type in kvs? It gives to me this error:

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (setup) Create, isFirst = true **
Error occurred on line: 86 (KeyValueStore)
java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.Object[][]
    at cloud.pagamico.app.setup._leggiorariallarmi(setup.java:1725)
    at cloud.pagamico.app.setup._activity_create(setup.java:853)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at cloud.pagamico.app.setup.afterFirstLayout(setup.java:105)
    at cloud.pagamico.app.setup.access$000(setup.java:17)
    at cloud.pagamico.app.setup$WaitForLayout.run(setup.java:83)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
** Activity (setup) Resume **


my Code

B4X:
Dim tmpListaAllarmi(10,8) as Allarme
Dim allarmi(10,8) As Object = Starter.kvs.GetDefault("orariAllarmi", tmpListaAllarmi)
 
Status
Not open for further replies.
Top