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,436
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Please, write the library version in the first post.

I have it in: AdditionalLibs\B4X.
The path in the IDE is correct.
1599023835200.png


Anyway I cannot see the GetDefault function.
1599023939572.png


No other keyvaluestore jar files in the B4J folder nor in the Libraries (nor classes with that name in the project, of course).
:oops:


(B4J 8.10)
 

LucaMs

Expert
Licensed User
Longtime User
Please, write the library version in the first post.

I have it in: AdditionalLibs\B4X.
The path in the IDE is correct.
View attachment 99420

Anyway I cannot see the GetDefault function.
View attachment 99421

No other keyvaluestore jar files in the B4J folder nor in the Libraries (nor classes with that name in the project, of course).
:oops:


(B4J 8.10)
I had to close and reopen my project, even though I had previously pressed CTRL-P (Clean Project) many times without success.
Now the list of members is correct (I can see-use GetDefault).
 

angel_

Well-Known Member
Licensed User
Longtime User
And Where is PuEncrypted And GetEncrypted Methods? can't compile...
Did you include KVS_ENCRYPTION to the build configuration
 

dennmtr

Member
Licensed User
Did you include KVS_ENCRYPTION to the build configuration

Wow! how do you know those hidden & magic tricks in the build configuration ...

The previous version didnt need this symbol, thank y
 

VB6_man

Member
When connected to B4J, an error pops up, which is not in Android IDE


Are you missing a link to the library?
An error occurred at line: 107 (KeyValueStore)
Dim cipher As B4XCipher

There is no KeyValueStore module in my program!

The error is caused by the kvs library itself.
When connecting the library that it requires, kvs stops seeing the kvs.PutBitmap (key, value) method
 
Last edited:

VB6_man

Member
I saw this spoller with config settings. And I already have an additional library in the folder. I made an addition, but it doesn't help.
But that's not the point!
I don't want any encryption. When I remove the encryption key in the configurator, the library says it has no methods:
PutBitmap / GetBitmap.
Kvs_Error.PNG
 
Last edited:

VB6_man

Member
Yes. It is a cross-platform scripting engine library - naturally without an interface. How do I then save image files across all platforms?
 

yiankos1

Well-Known Member
Licensed User
Longtime User
Status
Not open for further replies.
Top