B4J Question KVS file size does not reduce after Remove and DeleteAll - SOLVED

Gandalf

Member
Licensed User
Longtime User
Hello,

I use B4J v9.10 with internal library KeyValueStore v2.30. When I remove particular entries or even delete all data in KVS, entries become unavailable for reading but KVS file size remains the same as before remove/delete. My app writes huge amount of statistics data and the file grows pretty quick. How can I clear old data from KVS file and reduce file size?
Here's a short non-ui test code:
KVS test code:
Sub Process_Globals
    Private KVS As KeyValueStore
End Sub

Sub AppStart (Args() As String)
    If File.Exists(File.DirApp, "test.kvs") Then File.Delete(File.DirApp, "test.kvs")
    KVS.Initialize(File.DirApp, "test.kvs")
    Log("Initial file size: " & File.Size(File.DirApp, "test.kvs"))
    For i = 0 To 100
        KVS.Put(i, "This is a test string")
    Next
    Log("File size after put: " & File.Size(File.DirApp, "test.kvs"))
    KVS.DeleteAll
    Log("File size after delete: " & File.Size(File.DirApp, "test.kvs"))
End Sub

and the output:
KVS test code output:
Initial file size: 3072
File size after put: 8192
File size after delete: 8192
 

agraham

Expert
Licensed User
Longtime User
If you look at the source code internally it is using the SQL library. As I understand it SQLite does not compact its files automatically so they just grow if needed but never shrink. As there is no capability to shrink it perhaps you could open a new file and copy the existing data to it.

I think SQLite may have an automatic compaction option but as I haven't used it since the Basic4ppc days I don't know how to invoke it.
 
Upvote 0

Gandalf

Member
Licensed User
Longtime User
Tests with updated library were successful. New app version is deployed to production server now. Old data should be cleared at midnight...
 
Upvote 0

Gandalf

Member
Licensed User
Longtime User
Looks like it works Ok on production server, so problem is SOLVED.
For newbies and lazy ones:
You can download updated library from here. Since it is internal library, overwrite old file in your B4J\Libraries folder, default path is C:\Program Files (x86)\Anywhere Software\B4J\Libraries
 
Upvote 0
Top