Keep data as a linked list on disk. A alternative way to reduce redundant operation for DiskLruCache

Overview

DiskLinkedList

Keep data as a linked list on disk. An alternative way to reduce redundant operation for DiskLruCache

Use-case

Android have build-in DiskLruCache.java, but it has too many redundant operations on runtime, DiskLinkedList using memory-mapped file mechanism to keep data on disk as a linked list.

Advantage

  • Compatible with all systems running Java (Android, JVM, Kotlin/JVM).
  • Get existed / remove (move / free): O(1).
  • New put / get (allocation): O(n) on worst fragment or O(1) on defragmented.
  • High performance.
  • Inconsiderable I/O execution.
  • Easy to use.

File data structure

1 byte (B) to make use or not | 4B prev | 4B next | 4B length | extra byte array

Each entry, DiskLinkedList storage as a node and keep as structure above.

Usage

Create new disk linked list data

val diskLinkedList = DiskLinkedList.open(
    file = File("data.dm"),       // file on disk.
    initialByteSize = 20 * 1024L, // initial size of file.
    scaleFactor = 0.75f           // file size scale factor on data larger than current size.
)

// Create new record.
diskLinkedList.addFirst("Hello, World!".toData())
diskLinkedList.log()              // 0=Hello, World!

Data on disk

img_2.png


More and more

// Add "Second Node" to first.
diskLinkedList.addFirst("Second Node".toData())
diskLinkedList.log()
// Output: 26=Second Node > 0=Hello, World!

// Keep "Second Node" to move later.
val second = diskLinkedList.first!!

// Add 3 nodes to first.
repeat(3) { index ->
    diskLinkedList.addFirst("Hello $index".toData())
}
diskLinkedList.log()
// Output: 90=Hello 2 > 70=Hello 1 > 50=Hello 0 > 26=Second Node > 0=Hello, World!

// Move "Second Node" to first.
diskLinkedList.moveToFirst(second)
diskLinkedList.log()
// Output: 26=Second Node > 90=Hello 2 > 70=Hello 1 > 50=Hello 0 > 0=Hello, World!

Sync and de-fragment

  • DiskLinkedList NOT auto-sync to file, it only sync to file after synchronize executed.
  • Beside of that, to avoid fragment on file, DiskLinkedList support defragment function to do.
// Remove last.
diskLinkedList.removeLast()
diskLinkedList.log()
// Output: 26=Second Node > 90=Hello 2 > 70=Hello 1 > 50=Hello 0

// Then continue to add 2 nodes at first.
repeat(2) { index ->
    diskLinkedList.addFirst("Bye $index".toData())
}
diskLinkedList.log()
// Output: 110=Bye 1 > 0=Bye 0 > 26=Second Node > 90=Hello 2 > 70=Hello 1 > 50=Hello 0
// New node "Bye 1" will add at pointer 110 instead of 0 -> FRAGMENTED!!!

// Defragment data.
diskLinkedList.defragment()
diskLinkedList.log()
// Output: 0=Bye 1 > 18=Bye 0 > 36=Second Node > 60=Hello 2 > 80=Hello 1 > 100=Hello 0

// Sync every change to disk.
diskLinkedList.synchronize()

Dependency

Maven

<dependency>
    <groupId>org.cuongnv.disklinkedlist</groupId>
    <artifactId>disklinkedlist</artifactId>
    <version>0.5.0</version>
</dependency>

Gradle Kotlin DSL

implementation("org.cuongnv.disklinkedlist:disklinkedlist:0.5.0")

Gradle Groovy

implementation 'org.cuongnv.disklinkedlist:disklinkedlist:0.5.0'

License

Copyright 2021 Cuong V. Nguyen (github.com/cuongnv126).

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
learn RxJava in new way!
learn RxJava in new way!

RxLab amazing tool to learn ReactiveX Programming with animation, schedulers and time project on GitHub. this is an open source Android application wr

TSBattery a new way to save your battery avoid cancer apps hacker it.

TSBattery TSBattery a new way to save your battery avoid cancer apps hacker it. TSBattery 是一个旨在使 QQ、TIM 变得更省电的开源 Xposed 模块 Get startted 此模块支持原生 Xposed

A compose friendly way to deal with in app updates on android

In-App update compose A way to make in app updates in compose How to include in your project The library is available via MavenCentral: allprojects {

This is a easy way to publish MQTT message and receive MQTT message

SMQ-CLIENT This is a easy way to publish MQTT message and receive MQTT message This is provider a spring stater for quick use Recive message form the

Access and process various types of personal data in Android with a set of easy, uniform, and privacy-friendly APIs.
Access and process various types of personal data in Android with a set of easy, uniform, and privacy-friendly APIs.

PrivacyStreams PrivacyStreams is an Android library for easy and privacy-friendly personal data access and processing. It offers a functional programm

A simple Android utils library to write any type of data into cache files and read them later.

CacheUtilsLibrary This is a simple Android utils library to write any type of data into cache files and then read them later, using Gson to serialize

An easy-to-use, cross-platform measurement tool that pulls data out of CD pipelines and analysis the four key metrics for you.
An easy-to-use, cross-platform measurement tool that pulls data out of CD pipelines and analysis the four key metrics for you.

Maintained by SEA team, ThoughtWorks Inc. Read this in other languages: English, 简体中文 Table of Contents About the Project Usage How to Compute Contrib

Preferences data store example

DataStore Example this example shows how you can use data store to store data in key value pairs and get rid of shared preferences Medium Article: htt

A library for calculating on string data.
A library for calculating on string data.

FookCalc What is FookCalc? A library for calculating on string data. Gradle Add the following to your project's root build.gradle file repositories {

Owner
Cuong V. Nguyen
Cuong V. Nguyen
A Telegram bot utilities that help to reduce the code amount

Flume Party A Telegram bot utilities that help to reduce code amount. Real project examples Pull Party Bot: 19% of code has been reduced. Resistance B

pool party 1 Jun 8, 2022
Java implementation of a Disk-based LRU cache which specifically targets Android compatibility.

Disk LRU Cache A cache that uses a bounded amount of space on a filesystem. Each cache entry has a string key and a fixed number of values. Each key m

Jake Wharton 5.7k Dec 31, 2022
Android library to easily serialize and cache your objects to disk using key/value pairs.

Deprecated This project is no longer maintained. No new issues or pull requests will be accepted. You can still use the source or fork the project to

Anup Cowkur 667 Dec 22, 2022
DiskCache - Simple and readable disk cache for kotlin and android applications

DiskCache Simple and readable disk cache for kotlin and android applications (with journaled lru strategy) This is a simple lru disk cache, based on t

Giovanni Corte 14 Dec 2, 2022
Provides custom lint rules developed by Bottle Rocket Studios to help keep our code cleaner

Provides custom lint rules developed by Bottle Rocket Studios to help keep our code cleaner, detect and mitigate possible security issues, and allow us to write rules around best practices and usage as necessary in the future

Bottle Rocket Studios 1 Feb 11, 2022
Reactor is key value database and is a great alternative to Shared Preferences.

Reactor Reactor is a fast and secure key-value library for Android, and has an embedded database based on the JSON structure and is a great alternativ

mr amir abbas 37 Oct 30, 2022
KmmCaching - An application that illustrates fetching data from remote data source and caching it in local storage

An application that illustrates fetching data from remote data source and caching it in local storage for both IOS and Android platforms using Kotlin Multiplatform Mobile and SqlDelight.

Felix Kariuki 5 Oct 6, 2022
A wholesome todo list application for staying organized and feeling good.

Tasks Of Affirmation This is a wholesome todo list application. The purpose of this application is to help users stay organized in their day-to-day li

Adam McNeilly 105 Dec 22, 2022
:iphone: [Android Library] Get device information in a super easy way.

EasyDeviceInfo Android library to get device information in a super easy way. The library is built for simplicity and approachability. It not only eli

Nishant Srivastava 1.7k Dec 22, 2022
[Deprecated] Sexy way to execute async/background tasks on Android

Groundy library for Android @Deprecated Unfortunatenly this library is no longer maintained, we encourage you to use other widely supported solutions

Telly 228 Nov 14, 2022