Search in sources :

Example 21 with SQLiteFullException

use of android.database.sqlite.SQLiteFullException in project mobile-center-sdk-android by Microsoft.

the class DatabaseManager method put.

/**
 * Stores the entry to the table. If the table is full, the oldest logs are discarded until the
 * new one can fit. If the log is larger than the max table size, database will be cleared and
 * the log is not inserted.
 *
 * @param values         The entry to be stored.
 * @param priorityColumn When storage full and deleting data, use this column to determine which entries to delete first.
 * @return If a log was inserted, the database identifier. Otherwise -1.
 */
public long put(@NonNull ContentValues values, @NonNull String priorityColumn) {
    Long id = null;
    Cursor cursor = null;
    try {
        while (id == null) {
            try {
                /* Insert data. */
                id = getDatabase().insertOrThrow(mDefaultTable, null, values);
            } catch (SQLiteFullException e) {
                /* Delete the oldest log. */
                AppCenterLog.debug(LOG_TAG, "Storage is full, trying to delete the oldest log that has the lowest priority which is lower or equal priority than the new log");
                if (cursor == null) {
                    String priority = values.getAsString(priorityColumn);
                    SQLiteQueryBuilder queryBuilder = SQLiteUtils.newSQLiteQueryBuilder();
                    queryBuilder.appendWhere(priorityColumn + " <= ?");
                    cursor = getCursor(queryBuilder, SELECT_PRIMARY_KEY, new String[] { priority }, priorityColumn + " , " + PRIMARY_KEY);
                }
                if (cursor.moveToNext()) {
                    long deletedId = cursor.getLong(0);
                    delete(deletedId);
                    AppCenterLog.debug(LOG_TAG, "Deleted log id=" + deletedId);
                } else {
                    throw e;
                }
            }
        }
    } catch (RuntimeException e) {
        id = -1L;
        AppCenterLog.error(LOG_TAG, String.format("Failed to insert values (%s) to database %s.", values.toString(), mDatabase), e);
    }
    if (cursor != null) {
        try {
            cursor.close();
        } catch (RuntimeException ignore) {
        }
    }
    return id;
}
Also used : SQLiteFullException(android.database.sqlite.SQLiteFullException) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 22 with SQLiteFullException

use of android.database.sqlite.SQLiteFullException in project mobile-center-sdk-android by Microsoft.

the class DatabaseManagerTest method failsToDeleteLogDuringPutWhenFull.

@Test
public void failsToDeleteLogDuringPutWhenFull() {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    SQLiteOpenHelper helperMock = mock(SQLiteOpenHelper.class);
    SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
    when(helperMock.getWritableDatabase()).thenReturn(sqLiteDatabase);
    /* Mock the select cursor we are using to find logs to evict to fail. */
    mockStatic(SQLiteUtils.class);
    Cursor cursor = mock(Cursor.class);
    SQLiteDiskIOException fatalException = new SQLiteDiskIOException();
    when(cursor.moveToNext()).thenThrow(fatalException);
    SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
    when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
    /* Simulate that database is full and that deletes fail because of the cursor. */
    when(sqLiteDatabase.insertOrThrow(anyString(), anyString(), any(ContentValues.class))).thenThrow(new SQLiteFullException());
    /* Instantiate real instance for DatabaseManager. */
    DatabaseManager databaseManager = new DatabaseManager(contextMock, "database", "table", 1, null, null, null);
    databaseManager.setSQLiteOpenHelper(helperMock);
    /* When we put a log, it will fail to purge. */
    assertEquals(-1, databaseManager.put(mock(ContentValues.class), "priority"));
}
Also used : Context(android.content.Context) SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) ContentValues(android.content.ContentValues) Returns(org.mockito.internal.stubbing.answers.Returns) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteFullException(android.database.sqlite.SQLiteFullException) SQLiteDiskIOException(android.database.sqlite.SQLiteDiskIOException) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with SQLiteFullException

use of android.database.sqlite.SQLiteFullException in project FileDownloader by lingochamp.

the class DownloadStatusCallback method handleError.

private void handleError(Exception exception) {
    Exception errProcessEx = exFiltrate(exception);
    if (errProcessEx instanceof SQLiteFullException) {
        // If the error is sqLite full exception already, no need to  update it to the database
        // again.
        handleSQLiteFullException((SQLiteFullException) errProcessEx);
    } else {
        // Normal case.
        try {
            model.setStatus(FileDownloadStatus.error);
            model.setErrMsg(exception.toString());
            database.updateError(model.getId(), errProcessEx, model.getSoFar());
        } catch (SQLiteFullException fullException) {
            errProcessEx = fullException;
            handleSQLiteFullException((SQLiteFullException) errProcessEx);
        }
    }
    processParams.setException(errProcessEx);
    onStatusChanged(FileDownloadStatus.error);
}
Also used : SQLiteFullException(android.database.sqlite.SQLiteFullException) SQLiteFullException(android.database.sqlite.SQLiteFullException) FileDownloadOutOfSpaceException(com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException) FileDownloadGiveUpRetryException(com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException) IOException(java.io.IOException)

Aggregations

SQLiteFullException (android.database.sqlite.SQLiteFullException)23 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)16 ContentValues (android.content.ContentValues)15 SQLiteDiskIOException (android.database.sqlite.SQLiteDiskIOException)14 IOException (java.io.IOException)13 SQLException (android.database.SQLException)12 SQLiteCantOpenDatabaseException (android.database.sqlite.SQLiteCantOpenDatabaseException)12 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)12 SQLiteDatabaseCorruptException (android.database.sqlite.SQLiteDatabaseCorruptException)12 Context (android.content.Context)3 Cursor (android.database.Cursor)3 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)3 Uri (android.net.Uri)3 ResolveInfo (android.content.pm.ResolveInfo)2 ServiceInfo (android.content.pm.ServiceInfo)2 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)2 SQLiteStatement (android.database.sqlite.SQLiteStatement)2 Bundle (android.os.Bundle)2 Date (java.util.Date)2 ImPlugin (org.awesomeapp.messenger.plugin.ImPlugin)2