Refactoring

pull/206/head
M66B 4 years ago
parent ae630eddfe
commit 09406ecac8

@ -379,7 +379,6 @@ public abstract class DB extends RoomDatabase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int threads = prefs.getInt("query_threads", DEFAULT_QUERY_THREADS);
boolean wal = prefs.getBoolean("wal", true);
int sqlite_cache = prefs.getInt("sqlite_cache", DEFAULT_CACHE_SIZE);
Log.i("DB query threads=" + threads + " wal=" + wal);
ExecutorService executorQuery = Helper.getBackgroundExecutor(threads, "query");
ExecutorService executorTransaction = Helper.getBackgroundExecutor(0, "transaction");
@ -397,46 +396,32 @@ public abstract class DB extends RoomDatabase {
" version=" + db.getVersion() +
" WAL=" + db.isWriteAheadLoggingEnabled());
// https://www.sqlite.org/pragma.html
for (String pragma : new String[]{
"synchronous", "journal_mode",
"wal_checkpoint", "wal_autocheckpoint",
"page_count", "page_size", "max_page_count", "freelist_count",
"cache_size", "cache_spill",
"soft_heap_limit", "hard_heap_limit", "mmap_size"})
try (Cursor cursor = db.query("PRAGMA " + pragma + ";")) {
Log.i("Get PRAGMA " + pragma + "=" + (cursor.moveToNext() ? cursor.getString(0) : "?"));
}
try {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int class_mb = am.getMemoryClass();
int cache_size = sqlite_cache * class_mb * 1024 / 100;
if (cache_size > 2000) {
// https://www.sqlite.org/pragma.html#pragma_cache_size
Integer cache_size = getCacheSizeKb(context);
if (cache_size != null) {
cache_size = -cache_size; // kibibytes
Log.i("Set PRAGMA cache_size=" + cache_size);
try (Cursor cursor = db.query("PRAGMA cache_size=" + cache_size + ";", null)) {
cursor.moveToNext(); // required
}
try (Cursor cursor = db.query("PRAGMA cache_size;")) {
Log.i("Get PRAGMA cache_size=" + (cursor.moveToNext() ? cursor.getInt(0) : "?"));
}
}
} catch (Throwable ex) {
Log.e(ex);
}
// Prevent long running operations from getting an exclusive lock
// https://www.sqlite.org/pragma.html#pragma_cache_spill
Log.i("Set PRAGMA cache_spill=0");
try (Cursor cursor = db.query("PRAGMA cache_spill=0;", null)) {
cursor.moveToNext(); // required
}
try (Cursor cursor = db.query("PRAGMA cache_spill;")) {
Log.i("Get PRAGMA cache_spill=" + (cursor.moveToNext() ? cursor.getInt(0) : "?"));
// https://www.sqlite.org/pragma.html
for (String pragma : new String[]{
"synchronous", "journal_mode",
"wal_checkpoint", "wal_autocheckpoint",
"page_count", "page_size", "max_page_count", "freelist_count",
"cache_size", "cache_spill",
"soft_heap_limit", "hard_heap_limit", "mmap_size"})
try (Cursor cursor = db.query("PRAGMA " + pragma + ";")) {
Log.i("Get PRAGMA " + pragma + "=" + (cursor.moveToNext() ? cursor.getString(0) : "?"));
}
if (BuildConfig.DEBUG && false) {
@ -448,6 +433,22 @@ public abstract class DB extends RoomDatabase {
});
}
static Integer getCacheSizeKb(Context context) {
try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int sqlite_cache = prefs.getInt("sqlite_cache", DEFAULT_CACHE_SIZE);
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int class_mb = am.getMemoryClass();
int cache_size = sqlite_cache * class_mb * 1024 / 100;
return (cache_size > 2000 ? cache_size : null);
} catch (Throwable ex) {
Log.e(ex);
return null;
}
}
private static void createTriggers(@NonNull SupportSQLiteDatabase db) {
List<String> image = new ArrayList<>();
for (String img : ImageHelper.IMAGE_TYPES)

@ -1261,7 +1261,9 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
swCheckpoints.setChecked(prefs.getBoolean("checkpoints", true));
int sqlite_cache = prefs.getInt("sqlite_cache", DB.DEFAULT_CACHE_SIZE);
int cache_size = sqlite_cache * class_mb * 1024 / 100;
Integer cache_size = DB.getCacheSizeKb(getContext());
if (cache_size == null)
cache_size = 2000;
tvSqliteCache.setText(getString(R.string.title_advanced_sqlite_cache,
NF.format(sqlite_cache),
Helper.humanReadableByteCount(cache_size * 1024L)));

Loading…
Cancel
Save