Search in sources :

Example 1 with DatabaseChangeDecorator

use of com.ichi2.utils.DatabaseChangeDecorator in project AnkiChinaAndroid by ankichinateam.

the class Collection method getChunk.

private int getChunk() {
    if (sChunk != 0) {
        return sChunk;
    }
    // the window size is saved in
    // io.requery.android.database.CursorWindow.sCursorWindowSize.
    // Values are copied here. Ideally, a getter would allow to access it.
    final int WINDOW_SIZE_KB = 2048;
    int sCursorWindowSize = WINDOW_SIZE_KB * 1024;
    // We have the ability to look into our sqlite implementation on Android and use it's value
    // as a ceiling. Try it, with a reasonable fallback in case of failure
    SupportSQLiteDatabase db = mDb.getDatabase();
    if (!(db instanceof DatabaseChangeDecorator)) {
        return sChunk;
    }
    String db_name = ((DatabaseChangeDecorator) db).getWrapped().getClass().getName();
    if ("io.requery.android.database.sqlite.SQLiteDatabase".equals(db_name)) {
        try {
            Field cursorWindowSize = io.requery.android.database.CursorWindow.class.getDeclaredField("sDefaultCursorWindowSize");
            cursorWindowSize.setAccessible(true);
            int possibleCursorWindowSize = cursorWindowSize.getInt(null);
            Timber.d("Reflectively discovered database default cursor window size %d", possibleCursorWindowSize);
            if (possibleCursorWindowSize > 0) {
                sCursorWindowSize = possibleCursorWindowSize;
            } else {
                Timber.w("Obtained unusable cursor window size: %d. Using default %d", possibleCursorWindowSize, sCursorWindowSize);
            }
        } catch (Exception e) {
            Timber.w(e, "Unable to get window size from requery cursor.");
        }
    }
    // reduce the actual size a little bit.
    sChunk = (int) (sCursorWindowSize * 15. / 16.);
    return sChunk;
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) Field(java.lang.reflect.Field) SuppressLint(android.annotation.SuppressLint) JSONException(com.ichi2.utils.JSONException) SQLiteDatabaseLockedException(android.database.sqlite.SQLiteDatabaseLockedException) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) IOException(java.io.IOException) NoSuchDeckException(com.ichi2.libanki.exception.NoSuchDeckException) DatabaseChangeDecorator(com.ichi2.utils.DatabaseChangeDecorator)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 SQLiteDatabaseLockedException (android.database.sqlite.SQLiteDatabaseLockedException)1 SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)1 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)1 NoSuchDeckException (com.ichi2.libanki.exception.NoSuchDeckException)1 DatabaseChangeDecorator (com.ichi2.utils.DatabaseChangeDecorator)1 JSONException (com.ichi2.utils.JSONException)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1