Skip to content

Control reyna SQLite db size

B2M Development edited this page Jun 26, 2015 · 4 revisions

Methods

Reyna supports limiting the size of its storage by exposing a set of static methods on the StoreService class:

  • setStorageSizeLimit(Context context, long limit) where limit is a number of bytes to which storage size should be limited. Minimum size of the limit is 2Mb. If user tries to set the limit to be less than the minimum size the limit is set to 2Mb.
  • getStorageSizeLimit(Context context)
  • resetStorageSizeLimit(Context context)

Scenarios

Storage size grows and soon going to reach the limit

On each insert operation, if limit - current storage size < 300Kb, Reyna starts trying to keep the storage from growing by finding the oldest message with the same url (url is used as a message type) and removing it from the storage. The idea behind this logic is that for some messages' types the message size stays roughly the same and by removing a message and inserting a new one with the same type storage size stays the same.

Storage limit changed to be lower than the current storage size

In this case on the next message insertion Reyna will perform the shrinking operation. Since there is no way to map the size of an entry to the final size it contributes to the whole storage, Reyna takes next approach:

  1. Calculate the size in percentages by which the current size exceeds the limit i.e. if the limit is 80 and the current size is 100, the exceeding part is 20% of the storage size
  2. Get the total number of messages in the storage and take the percentages calculated in the previous step (20% in our case) of that number i.e. if the number of messages in the storage is 1000 we need to take 20% of that number, which means 200 messages
  3. Delete this number of messages from the storage starting with the oldest ones
  4. Check the storage size
  5. If the storage size is still bigger than the limit, repeat the process starting from step 1
  6. Stop
  7. Perform vacuuming

NOTE Almost always the storage size after shrinking is less than 300Kb from the limit and therefore any further message insertion would trigger the removal of the oldest message with the same type as the message being inserted.