Skip to content

Commit 7dfc74d

Browse files
remove all caching logic
cache is actually introducing regressions in performance and the android repo now responds faster than we can read from a database other repos still suck performance wise but caching those gets super tricky and runs into similar performance problems Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 01cdc46 commit 7dfc74d

4 files changed

Lines changed: 34 additions & 235 deletions

File tree

app/src/main/kotlin/com/fox2code/mmm/androidacy/AndroidacyRepoData.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,16 @@ class AndroidacyRepoData(cacheRoot: File?, testMode: Boolean) : RepoData(
305305
}
306306
val moduleId: String = try {
307307
jsonObject.getString("codename")
308+
// try to get id value
308309
} catch (e: JSONException) {
309-
Timber.e(
310-
"Module %s has no codename or json %s is invalid",
311-
jsonObject.optString("codename", "Unknown"),
312-
jsonObject.toString()
313-
)
314-
continue
310+
// try to get codename value
311+
try {
312+
jsonObject.getString("id")
313+
} catch (e2: JSONException) {
314+
// we should never get here, bail out
315+
Timber.e(e2, "Failed to parse module id")
316+
continue
317+
}
315318
}
316319
// Normally, we'd validate the module id here, but we don't need to because the server does it for us
317320
val lastUpdate: Long = try {

app/src/main/kotlin/com/fox2code/mmm/module/ModuleViewAdapter.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ class ModuleViewAdapter : RecyclerView.Adapter<ModuleViewAdapter.ViewHolder>() {
5656
}
5757
}
5858
} catch (e: Exception) {
59-
Timber.e(e, "Error while updating module holder. This may mean we're trying to update too early.")
59+
Timber.e(
60+
e,
61+
"Error while updating module holder. This may mean we're trying to update too early."
62+
)
6063
}
6164
}
6265

app/src/main/kotlin/com/fox2code/mmm/repo/RepoData.kt

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import com.fox2code.mmm.XRepo
1414
import com.fox2code.mmm.manager.ModuleInfo
1515
import com.fox2code.mmm.utils.io.Files.Companion.write
1616
import com.fox2code.mmm.utils.io.PropUtils.Companion.readProperties
17-
import com.fox2code.mmm.utils.room.ModuleListCacheDatabase
1817
import com.fox2code.mmm.utils.room.ReposListDatabase
1918
import org.json.JSONException
2019
import org.json.JSONObject
@@ -377,40 +376,7 @@ open class RepoData(url: String, cacheRoot: File) : XRepo() {
377376

378377
// should update (lastUpdate > 15 minutes)
379378
fun shouldUpdate(): Boolean {
380-
if (BuildConfig.DEBUG) Timber.d("Repo $preferenceId should update check called")
381-
val db = Room.databaseBuilder(
382-
INSTANCE!!.applicationContext,
383-
ReposListDatabase::class.java,
384-
"ReposList.db",
385-
).allowMainThreadQueries().build()
386-
val repo = db.reposListDao().getById(preferenceId!!)
387-
// get modulelistcache
388-
val db2 = Room.databaseBuilder(
389-
INSTANCE!!.applicationContext,
390-
ModuleListCacheDatabase::class.java,
391-
"ModuleListCache.db",
392-
).allowMainThreadQueries().build()
393-
val moduleListCache = db2.moduleListCacheDao().getByRepoId(preferenceId!!)
394-
if (repo != null) {
395-
return if (repo.lastUpdate != 0 && moduleListCache.isNotEmpty() && moduleListCache.size > 1) {
396-
val lastUpdate = repo.lastUpdate.toLong()
397-
val currentTime = System.currentTimeMillis()
398-
val diff = currentTime - lastUpdate
399-
val diffMinutes = diff / (60 * 1000) % 60
400-
if (BuildConfig.DEBUG) Timber.d("Repo $preferenceId updated: $diffMinutes minutes ago")
401-
db.close()
402-
db2.close()
403-
diffMinutes > if (BuildConfig.DEBUG) 15 else 30
404-
} else {
405-
if (BuildConfig.DEBUG) Timber.d("Repo $preferenceId shouldUpdate true: lastUpdate is " + repo.lastUpdate + " and moduleListCache is " + moduleListCache.size)
406-
db.close()
407-
db2.close()
408-
true
409-
}
410-
} else {
411-
db.close()
412-
db2.close()
413-
}
379+
Timber.w("shouldUpdate() called but cache not implemented for %s", preferenceId)
414380
return true
415381
}
416382

app/src/main/kotlin/com/fox2code/mmm/repo/RepoUpdater.kt

Lines changed: 20 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import androidx.room.Room
88
import com.fox2code.mmm.BuildConfig
99
import com.fox2code.mmm.MainApplication
1010
import com.fox2code.mmm.utils.io.net.Http.Companion.doHttpGet
11-
import com.fox2code.mmm.utils.room.ModuleListCache
1211
import com.fox2code.mmm.utils.room.ModuleListCacheDatabase
1312
import com.fox2code.mmm.utils.room.ReposListDatabase
1413
import org.json.JSONArray
@@ -62,6 +61,7 @@ class RepoUpdater(repoData2: RepoData) {
6261
// now we have the cache, we need to check if it's up to date
6362
var results = moduleListCacheDao.getByRepoId(repoData.preferenceId!!)
6463
if (results.isNotEmpty()) {
64+
Timber.d("Got %d modules from cache for %s", results.size, repoData.preferenceId)
6565
toUpdate = emptyList()
6666
toApply = HashSet()
6767
// if results is not empty, check each module to see if it's null. this should never happen, but if it does, remove it from the cache
@@ -92,9 +92,26 @@ class RepoUpdater(repoData2: RepoData) {
9292
results.size
9393
)
9494
val jsonObject = JSONObject()
95+
val jsonArray = JSONArray()
96+
for (module in results) {
97+
val moduleJson = JSONObject()
98+
moduleJson.put("id", module!!.codename)
99+
moduleJson.put("name", module.name)
100+
moduleJson.put("description", module.description)
101+
moduleJson.put("author", module.author)
102+
moduleJson.put("donate", module.donate)
103+
moduleJson.put("config", module.config)
104+
moduleJson.put("support", module.support)
105+
moduleJson.put("version", module.version)
106+
moduleJson.put("versionCode", module.versionCode)
107+
moduleJson.put("minApi", module.minApi)
108+
moduleJson.put("maxApi", module.maxApi)
109+
moduleJson.put("minMagisk", module.minMagisk)
110+
jsonArray.put(moduleJson)
111+
}
95112
// apply the toApply list to the toUpdate list
96113
try {
97-
jsonObject.put("modules", JSONArray(results))
114+
jsonObject.put("modules", jsonArray)
98115
toUpdate = repoData.populate(jsonObject)
99116
} catch (e: Exception) {
100117
Timber.e(e)
@@ -105,9 +122,7 @@ class RepoUpdater(repoData2: RepoData) {
105122
"Index raw: %s",
106123
String(indexRaw!!, StandardCharsets.UTF_8).subSequence(0, 100)
107124
)
108-
// Since we reuse instances this should work
109-
toApply = HashSet(repoData.moduleHashMap.values)
110-
(toApply as HashSet<RepoModule>).removeAll(toUpdate!!.toSet())
125+
if (BuildConfig.DEBUG) Timber.d("Returning %d modules for %s", toUpdate!!.size, repoData.preferenceId)
111126
// Return repo to update
112127
return toUpdate!!.size
113128
}
@@ -158,194 +173,6 @@ class RepoUpdater(repoData2: RepoData) {
158173
val success = AtomicBoolean(false)
159174
if (BuildConfig.DEBUG) Timber.d("Finishing update for %s", repoData.preferenceId)
160175
if (indexRaw != null) {
161-
val tmpIndexRaw = indexRaw!!
162-
if (BuildConfig.DEBUG) Timber.d("Updating database for %s", repoData.preferenceId)
163-
// new thread to update the database
164-
val thread = Thread {
165-
val startTime = System.currentTimeMillis()
166-
if (BuildConfig.DEBUG) Timber.d("Updating database thread for %s", repoData.preferenceId)
167-
try {
168-
// iterate over modules, using this.supportedProperties as a template to attempt to get each property from the module. everything that is not null is added to the module
169-
// use room to insert to
170-
// props avail:
171-
val db = Room.databaseBuilder(
172-
MainApplication.INSTANCE!!,
173-
ModuleListCacheDatabase::class.java,
174-
"ModuleListCache.db"
175-
).build()
176-
// all except first six can be null
177-
// this.indexRaw is the raw index file (json)
178-
val modules = JSONObject(String(tmpIndexRaw, StandardCharsets.UTF_8))
179-
// androidacy repo uses "data" key, others should use "modules" key. Both are JSONArrays
180-
val modulesArray = try {
181-
modules.getJSONArray("data")
182-
} catch (e: Exception) {
183-
modules.getJSONArray("modules")
184-
} catch (e: Exception) {
185-
Timber.e(e)
186-
Timber.w("No modules were found in the index file for %s", repoData.preferenceId)
187-
if (BuildConfig.DEBUG) Timber.d("Finished updating database for %s in %dms", repoData.preferenceId, System.currentTimeMillis() - startTime)
188-
success.set(false)
189-
return@Thread
190-
}
191-
if (BuildConfig.DEBUG) Timber.d("Got modules for %s", repoData.preferenceId)
192-
val moduleListCacheDao = db.moduleListCacheDao()
193-
moduleListCacheDao.deleteByRepoId(repoData.preferenceId!!)
194-
if (BuildConfig.DEBUG) Timber.d("Deleted old modules for %s", repoData.preferenceId)
195-
if (modulesArray.length() == 0) {
196-
Timber.w("No modules were found in the index file for %s", repoData.preferenceId)
197-
if (BuildConfig.DEBUG) Timber.d("Finished updating database for %s in %dms", repoData.preferenceId, System.currentTimeMillis() - startTime)
198-
success.set(false)
199-
return@Thread
200-
}
201-
if (BuildConfig.DEBUG) Timber.d("Iterating over modules for %s", repoData.preferenceId)
202-
// iterate over modules
203-
for (n in 0 until modulesArray.length()) {
204-
// get module
205-
val module = modulesArray.getJSONObject(n)
206-
try {
207-
// get module id
208-
// if codename is present, prefer that over id
209-
val id: String =
210-
if (module.has("codename") && module.getString("codename") != "") {
211-
module.getString("codename")
212-
} else {
213-
module.getString("id")
214-
}
215-
// get module name
216-
val name = module.getString("name")
217-
// get module version
218-
val version = module.getString("version")
219-
// get module version code
220-
val versionCode = module.getInt("versionCode")
221-
// get module author
222-
val author = module.getString("author")
223-
// get module description
224-
val description = module.getString("description")
225-
// get module min api
226-
val minApi: String =
227-
if (module.has("minApi") && module.getString("minApi") != "") {
228-
module.getString("minApi")
229-
} else {
230-
"0"
231-
}
232-
// coerce min api to int
233-
val minApiInt = minApi.toInt()
234-
// get module max api and set to 0 if it's "" or null
235-
val maxApi: String =
236-
if (module.has("maxApi") && module.getString("maxApi") != "") {
237-
module.getString("maxApi")
238-
} else {
239-
"0"
240-
}
241-
// coerce max api to int
242-
val maxApiInt = maxApi.toInt()
243-
// get module min magisk
244-
val minMagisk: String =
245-
if (module.has("minMagisk") && module.getString("minMagisk") != "") {
246-
module.getString("minMagisk")
247-
} else {
248-
"0"
249-
}
250-
// coerce min magisk to int
251-
val minMagiskInt = minMagisk.toInt()
252-
// get module need ramdisk
253-
val needRamdisk: Boolean = if (module.has("needRamdisk")) {
254-
module.getBoolean("needRamdisk")
255-
} else {
256-
false
257-
}
258-
// get module support
259-
val support: String? = if (module.has("support")) {
260-
module.getString("support")
261-
} else {
262-
""
263-
}
264-
// get module donate
265-
val donate: String? = if (module.has("donate")) {
266-
module.getString("donate")
267-
} else {
268-
""
269-
}
270-
// get module config
271-
val config: String? = if (module.has("config")) {
272-
module.getString("config")
273-
} else {
274-
""
275-
}
276-
// get module change boot
277-
val changeBoot: Boolean = if (module.has("changeBoot")) {
278-
module.getBoolean("changeBoot")
279-
} else {
280-
false
281-
}
282-
// get module mmt reborn
283-
val mmtReborn: Boolean = if (module.has("mmtReborn")) {
284-
module.getBoolean("mmtReborn")
285-
} else {
286-
false
287-
}
288-
// try to get updated_at or lastUpdate value for lastUpdate
289-
val lastUpdate: Int = if (module.has("updated_at")) {
290-
module.getInt("updated_at")
291-
} else if (module.has("lastUpdate")) {
292-
module.getInt("lastUpdate")
293-
} else {
294-
0
295-
}
296-
// now downloads or stars
297-
val downloads: Int = if (module.has("downloads")) {
298-
module.getInt("downloads")
299-
} else if (module.has("stars")) {
300-
module.getInt("stars")
301-
} else {
302-
0
303-
}
304-
// get module repo id
305-
val repoId = repoData.preferenceId
306-
// get safe property. for now, only supported by androidacy repo and they use "vt_status" key
307-
var safe = false
308-
if (repoData.name == "Androidacy Modules Repo") {
309-
if (module.has("vt_status")) {
310-
if (module.getString("vt_status") == "Clean") {
311-
safe = true
312-
}
313-
}
314-
}
315-
val moduleListCache = ModuleListCache(
316-
name = name,
317-
version = version,
318-
versionCode = versionCode,
319-
author = author,
320-
description = description,
321-
minApi = minApiInt,
322-
maxApi = maxApiInt,
323-
minMagisk = minMagiskInt,
324-
needRamdisk = needRamdisk,
325-
support = support ?: "",
326-
donate = donate ?: "",
327-
config = config ?: "",
328-
changeBoot = changeBoot,
329-
mmtReborn = mmtReborn,
330-
repoId = repoId!!,
331-
safe = safe,
332-
lastUpdate = lastUpdate.toLong(),
333-
stats = downloads,
334-
codename = id
335-
)
336-
moduleListCacheDao.insert(moduleListCache)
337-
} catch (ignored: Exception) {
338-
}
339-
}
340-
db.close()
341-
val endTime = System.currentTimeMillis()
342-
val timeTaken = endTime - startTime
343-
if (BuildConfig.DEBUG) Timber.d("Time taken to parse modules: $timeTaken ms")
344-
} catch (ignored: Exception) {
345-
}
346-
}
347-
thread.start()
348-
indexRaw = null
349176
// set lastUpdate
350177
val db = Room.databaseBuilder(
351178
MainApplication.INSTANCE!!.applicationContext,

0 commit comments

Comments
 (0)