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;
}
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"));
}
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);
}
Aggregations