1
0
mirror of https://github.com/etesync/android synced 2025-07-07 15:18:16 +00:00

LocalCache: use the new Etebase FileSystemCache.

This commit is contained in:
Tom Hacohen 2020-10-06 18:38:38 +03:00
parent 9ce152f5be
commit 46dbb22ff3
2 changed files with 26 additions and 74 deletions

View File

@ -138,7 +138,7 @@ dependencies {
implementation "org.jetbrains.anko:anko-commons:0.10.4" implementation "org.jetbrains.anko:anko-commons:0.10.4"
implementation "com.etesync:journalmanager:1.1.1" implementation "com.etesync:journalmanager:1.1.1"
def etebaseVersion = '0.2.1' def etebaseVersion = '0.2.2'
implementation "com.etebase:client:$etebaseVersion" implementation "com.etebase:client:$etebaseVersion"
def acraVersion = '5.3.0' def acraVersion = '5.3.0'

View File

@ -3,34 +3,15 @@ package com.etesync.syncadapter
import android.content.Context import android.content.Context
import com.etebase.client.* import com.etebase.client.*
import com.etebase.client.Collection import com.etebase.client.Collection
import com.etebase.client.exceptions.EtebaseException
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import java.io.File import java.io.File
import java.util.* import java.util.*
/*
File structure:
cache_dir/
user1/ <--- the name of the user
stoken <-- the stokens of the collection fetch
cols/
UID1/ - The uid of the first col
...
UID2/ - The uid of the second col
col <-- the col itself
stoken <-- the stoken of the items fetch
items/
item_uid1 <-- the item with uid 1
item_uid2
...
*/
class EtebaseLocalCache private constructor(context: Context, username: String) { class EtebaseLocalCache private constructor(context: Context, username: String) {
private val fsCache: FileSystemCache = FileSystemCache.create(context.filesDir.absolutePath, username)
private val filesDir: File = File(context.filesDir, username) private val filesDir: File = File(context.filesDir, username)
private val colsDir: File private val colsDir: File = File(filesDir, "cols")
init {
colsDir = File(filesDir, "cols")
colsDir.mkdirs()
}
private fun getCollectionItemsDir(colUid: String): File { private fun getCollectionItemsDir(colUid: String): File {
val colsDir = File(filesDir, "cols") val colsDir = File(filesDir, "cols")
@ -39,102 +20,73 @@ class EtebaseLocalCache private constructor(context: Context, username: String)
} }
private fun clearUserCache() { private fun clearUserCache() {
filesDir.deleteRecursively() fsCache.clearUserCache()
} }
fun saveStoken(stoken: String) { fun saveStoken(stoken: String) {
val stokenFile = File(filesDir, "stoken") fsCache.saveStoken(stoken)
stokenFile.writeText(stoken)
} }
fun loadStoken(): String? { fun loadStoken(): String? {
val stokenFile = File(filesDir, "stoken") return fsCache.loadStoken()
return if (stokenFile.exists()) stokenFile.readText() else null
} }
fun collectionSaveStoken(colUid: String, stoken: String) { fun collectionSaveStoken(colUid: String, stoken: String) {
val colDir = File(colsDir, colUid) fsCache.collectionSaveStoken(colUid, stoken)
val stokenFile = File(colDir, "stoken")
stokenFile.writeText(stoken)
} }
fun collectionLoadStoken(colUid: String): String? { fun collectionLoadStoken(colUid: String): String? {
val colDir = File(colsDir, colUid) return fsCache.collectionLoadStoken(colUid)
val stokenFile = File(colDir, "stoken")
return if (stokenFile.exists()) stokenFile.readText() else null
} }
fun collectionList(colMgr: CollectionManager, withDeleted: Boolean = false): List<CachedCollection> { fun collectionList(colMgr: CollectionManager, withDeleted: Boolean = false): List<CachedCollection> {
return colsDir.list().map { return fsCache._unstable_collectionList(colMgr).filter {
val colDir = File(colsDir, it) withDeleted || !it.isDeleted
val colFile = File(colDir, "col") }.map{
val content = colFile.readBytes()
colMgr.cacheLoad(content)
}.filter { withDeleted || !it.isDeleted }.map{
CachedCollection(it, it.meta) CachedCollection(it, it.meta)
} }
} }
fun collectionGet(colMgr: CollectionManager, colUid: String): CachedCollection? { fun collectionGet(colMgr: CollectionManager, colUid: String): CachedCollection {
val colDir = File(colsDir, colUid) return fsCache.collectionGet(colMgr, colUid).let {
val colFile = File(colDir, "col")
if (!colFile.exists()) {
return null
}
val content = colFile.readBytes()
return colMgr.cacheLoad(content).let {
CachedCollection(it, it.meta) CachedCollection(it, it.meta)
} }
} }
fun collectionSet(colMgr: CollectionManager, collection: Collection) { fun collectionSet(colMgr: CollectionManager, collection: Collection) {
val colDir = File(colsDir, collection.uid) fsCache.collectionSet(colMgr, collection)
colDir.mkdirs()
val colFile = File(colDir, "col")
colFile.writeBytes(colMgr.cacheSaveWithContent(collection))
val itemsDir = getCollectionItemsDir(collection.uid)
itemsDir.mkdirs()
} }
fun collectionUnset(colMgr: CollectionManager, colUid: String) { fun collectionUnset(colMgr: CollectionManager, colUid: String) {
val colDir = File(colsDir, colUid) fsCache.collectionUnset(colMgr, colUid)
colDir.deleteRecursively()
} }
fun itemList(itemMgr: ItemManager, colUid: String, withDeleted: Boolean = false): List<CachedItem> { fun itemList(itemMgr: ItemManager, colUid: String, withDeleted: Boolean = false): List<CachedItem> {
val itemsDir = getCollectionItemsDir(colUid) return fsCache._unstable_itemList(itemMgr, colUid).filter {
return itemsDir.list().map { withDeleted || !it.isDeleted
val itemFile = File(itemsDir, it) }.map {
val content = itemFile.readBytes()
itemMgr.cacheLoad(content)
}.filter { withDeleted || !it.isDeleted }.map {
CachedItem(it, it.meta, it.contentString) CachedItem(it, it.meta, it.contentString)
} }
} }
fun itemGet(itemMgr: ItemManager, colUid: String, itemUid: String): CachedItem? { fun itemGet(itemMgr: ItemManager, colUid: String, itemUid: String): CachedItem? {
val itemsDir = getCollectionItemsDir(colUid) // Need the try because the inner call doesn't return null on missing, but an error
val itemFile = File(itemsDir, itemUid) val ret = try {
if (!itemFile.exists()) { fsCache.itemGet(itemMgr, colUid, itemUid)
} catch (e: EtebaseException) {
return null return null
} }
val content = itemFile.readBytes() return ret.let {
return itemMgr.cacheLoad(content).let {
CachedItem(it, it.meta, it.contentString) CachedItem(it, it.meta, it.contentString)
} }
} }
fun itemSet(itemMgr: ItemManager, colUid: String, item: Item) { fun itemSet(itemMgr: ItemManager, colUid: String, item: Item) {
val itemsDir = getCollectionItemsDir(colUid) fsCache.itemSet(itemMgr, colUid, item)
val itemFile = File(itemsDir, item.uid)
itemFile.writeBytes(itemMgr.cacheSaveWithContent(item))
} }
fun itemUnset(itemMgr: ItemManager, colUid: String, itemUid: String) { fun itemUnset(itemMgr: ItemManager, colUid: String, itemUid: String) {
val itemsDir = getCollectionItemsDir(colUid) fsCache.itemUnset(itemMgr, colUid, itemUid)
val itemFile = File(itemsDir, itemUid)
itemFile.delete()
} }
companion object { companion object {