Skip to content

Commit 72faa2c

Browse files
improvements to caching and prop fetching
should be fast as fuck boiiiiiiiiiii Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 58ebbc0 commit 72faa2c

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
3232
import timber.log.Timber
3333
import java.io.File
3434
import java.nio.charset.StandardCharsets
35+
import java.util.concurrent.Executors
3536

3637
@Suppress("NAME_SHADOWING", "unused")
3738
class RepoManager private constructor(mainApplication: MainApplication) : SyncManager() {
@@ -172,19 +173,23 @@ class RepoManager private constructor(mainApplication: MainApplication) : SyncMa
172173
val repoModules = repoUpdaters[i]!!.toUpdate()
173174
val repoData = repoDatas[i]
174175
if (BuildConfig.DEBUG) Timber.d("Registering %s", repoData.name)
176+
// max threads is equal to the number of cores
177+
val executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
175178
for (repoModule in repoModules!!) {
176179
try {
177180
if (repoModule.propUrl != null && repoModule.propUrl!!.isNotEmpty()) {
178-
repoData.storeMetadata(
179-
repoModule, doHttpGet(
180-
repoModule.propUrl!!, false
181+
executor.execute {
182+
repoData.storeMetadata(
183+
repoModule, doHttpGet(
184+
repoModule.propUrl!!, false
185+
)
181186
)
182-
)
183-
write(
184-
File(repoData.cacheRoot, repoModule.id + ".prop"), doHttpGet(
185-
repoModule.propUrl!!, false
187+
write(
188+
File(repoData.cacheRoot, repoModule.id + ".prop"), doHttpGet(
189+
repoModule.propUrl!!, false
190+
)
186191
)
187-
)
192+
}
188193
}
189194
if (repoData.tryLoadMetadata(repoModule) && (allowLowQualityModules || !isLowQualityModule(
190195
repoModule.moduleInfo

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,17 @@ class RepoUpdater(repoData2: RepoData) {
7272
repoData.preferenceId,
7373
results.size
7474
)
75+
val jsonObject = JSONObject()
7576
// apply the toApply list to the toUpdate list
7677
try {
77-
val jsonObject = JSONObject()
7878
jsonObject.put("modules", JSONArray(results))
7979
toUpdate = repoData.populate(jsonObject)
8080
} catch (e: Exception) {
8181
Timber.e(e)
8282
}
83+
// log first 100 chars of indexRaw
84+
indexRaw = jsonObject.toString().toByteArray()
85+
Timber.d("Index raw: %s", String(indexRaw!!, StandardCharsets.UTF_8).subSequence(0, 100))
8386
// Since we reuse instances this should work
8487
toApply = HashSet(repoData.moduleHashMap.values)
8588
(toApply as HashSet<RepoModule>).removeAll(toUpdate!!.toSet())
@@ -125,15 +128,19 @@ class RepoUpdater(repoData2: RepoData) {
125128

126129
fun finish(): Boolean {
127130
val success = AtomicBoolean(false)
131+
Timber.d("Finishing update for %s", repoData.preferenceId)
128132
// If repo is not enabled we don't need to do anything, just return true
129133
if (!repoData.isEnabled) {
134+
Timber.d("Repo %s is disabled, skipping", repoData.preferenceId)
130135
return true
131136
}
132137
if (indexRaw != null) {
138+
val tmpIndexRaw = indexRaw!!
139+
Timber.d("Updating database for %s", repoData.preferenceId)
133140
// new thread to update the database
134141
val thread = Thread {
135142
val startTime = System.currentTimeMillis()
136-
Timber.d("Updating database for %s", repoData.preferenceId)
143+
Timber.d("Updating database thread for %s", repoData.preferenceId)
137144
try {
138145
// 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
139146
// use room to insert to
@@ -145,31 +152,38 @@ class RepoUpdater(repoData2: RepoData) {
145152
).build()
146153
// all except first six can be null
147154
// this.indexRaw is the raw index file (json)
148-
val modules = JSONObject(String(indexRaw!!, StandardCharsets.UTF_8))
155+
val modules = JSONObject(String(tmpIndexRaw, StandardCharsets.UTF_8))
149156
// androidacy repo uses "data" key, others should use "modules" key. Both are JSONArrays
150-
val modulesArray: JSONArray = if (repoData.preferenceId == "androidacy_repo") {
151-
// get modules from "data" key. This is a JSONArray so we need to convert it to a JSONObject
157+
val modulesArray = try {
152158
modules.getJSONArray("data")
153-
} else {
154-
// get modules from "modules" key. This is a JSONArray so we need to convert it to a JSONObject
159+
} catch (e: Exception) {
155160
modules.getJSONArray("modules")
161+
} catch (e: Exception) {
162+
Timber.e(e)
163+
Timber.w("No modules were found in the index file for %s", repoData.preferenceId)
164+
Timber.d("Finished updating database for %s in %dms", repoData.preferenceId, System.currentTimeMillis() - startTime)
165+
success.set(false)
166+
return@Thread
156167
}
168+
Timber.d("Got modules for %s", repoData.preferenceId)
157169
val moduleListCacheDao = db.moduleListCacheDao()
158170
moduleListCacheDao.deleteByRepoId(repoData.preferenceId!!)
171+
Timber.d("Deleted old modules for %s", repoData.preferenceId)
159172
if (modulesArray.length() == 0) {
160173
Timber.w("No modules were found in the index file for %s", repoData.preferenceId)
161174
Timber.d("Finished updating database for %s in %dms", repoData.preferenceId, System.currentTimeMillis() - startTime)
162175
success.set(false)
163176
return@Thread
164177
}
178+
Timber.d("Iterating over modules for %s", repoData.preferenceId)
165179
// iterate over modules
166180
for (n in 0 until modulesArray.length()) {
167181
// get module
168182
val module = modulesArray.getJSONObject(n)
169183
try {
170184
// get module id
171185
// if codename is present, prefer that over id
172-
val id: String? =
186+
val id: String =
173187
if (module.has("codename") && module.getString("codename") != "") {
174188
module.getString("codename")
175189
} else {
@@ -294,7 +308,7 @@ class RepoUpdater(repoData2: RepoData) {
294308
safe = safe,
295309
lastUpdate = lastUpdate.toLong(),
296310
stats = downloads,
297-
codename = id ?: ""
311+
codename = id
298312
)
299313
moduleListCacheDao.insert(moduleListCache)
300314
} catch (ignored: Exception) {
@@ -320,6 +334,7 @@ class RepoUpdater(repoData2: RepoData) {
320334
db.close()
321335
success.set(true)
322336
} else {
337+
Timber.d("No index file found for %s", repoData.preferenceId)
323338
success.set(true) // assume we're reading from cache. this may be unsafe but it's better than nothing
324339
}
325340
return success.get()

0 commit comments

Comments
 (0)