Search in sources :

Example 16 with DatabaseIOException

use of com.google.android.exoplayer2.database.DatabaseIOException in project ExoPlayer by google.

the class CacheFileMetadataIndex method delete.

/**
 * Deletes index data for the specified cache.
 *
 * <p>This method may be slow and shouldn't normally be called on the main thread.
 *
 * @param databaseProvider Provides the database in which the index is stored.
 * @param uid The cache UID.
 * @throws DatabaseIOException If an error occurs deleting the index data.
 */
@WorkerThread
public static void delete(DatabaseProvider databaseProvider, long uid) throws DatabaseIOException {
    String hexUid = Long.toHexString(uid);
    try {
        String tableName = getTableName(hexUid);
        SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
        writableDatabase.beginTransactionNonExclusive();
        try {
            VersionTable.removeVersion(writableDatabase, VersionTable.FEATURE_CACHE_FILE_METADATA, hexUid);
            dropTable(writableDatabase, tableName);
            writableDatabase.setTransactionSuccessful();
        } finally {
            writableDatabase.endTransaction();
        }
    } catch (SQLException e) {
        throw new DatabaseIOException(e);
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) DatabaseIOException(com.google.android.exoplayer2.database.DatabaseIOException) WorkerThread(androidx.annotation.WorkerThread)

Example 17 with DatabaseIOException

use of com.google.android.exoplayer2.database.DatabaseIOException in project ExoPlayer by google.

the class CacheFileMetadataIndex method set.

/**
 * Sets metadata for a given file.
 *
 * <p>This method may be slow and shouldn't normally be called on the main thread.
 *
 * @param name The name of the file.
 * @param length The file length.
 * @param lastTouchTimestamp The file last touch timestamp.
 * @throws DatabaseIOException If an error occurs setting the metadata.
 */
@WorkerThread
public void set(String name, long length, long lastTouchTimestamp) throws DatabaseIOException {
    Assertions.checkNotNull(tableName);
    try {
        SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, name);
        values.put(COLUMN_LENGTH, length);
        values.put(COLUMN_LAST_TOUCH_TIMESTAMP, lastTouchTimestamp);
        writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */
        null, values);
    } catch (SQLException e) {
        throw new DatabaseIOException(e);
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) DatabaseIOException(com.google.android.exoplayer2.database.DatabaseIOException) WorkerThread(androidx.annotation.WorkerThread)

Example 18 with DatabaseIOException

use of com.google.android.exoplayer2.database.DatabaseIOException in project ExoPlayer by google.

the class CacheFileMetadataIndex method initialize.

/**
 * Initializes the index for the given cache UID.
 *
 * <p>This method may be slow and shouldn't normally be called on the main thread.
 *
 * @param uid The cache UID.
 * @throws DatabaseIOException If an error occurs initializing the index.
 */
@WorkerThread
public void initialize(long uid) throws DatabaseIOException {
    try {
        String hexUid = Long.toHexString(uid);
        tableName = getTableName(hexUid);
        SQLiteDatabase readableDatabase = databaseProvider.getReadableDatabase();
        int version = VersionTable.getVersion(readableDatabase, VersionTable.FEATURE_CACHE_FILE_METADATA, hexUid);
        if (version != TABLE_VERSION) {
            SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
            writableDatabase.beginTransactionNonExclusive();
            try {
                VersionTable.setVersion(writableDatabase, VersionTable.FEATURE_CACHE_FILE_METADATA, hexUid, TABLE_VERSION);
                dropTable(writableDatabase, tableName);
                writableDatabase.execSQL("CREATE TABLE " + tableName + " " + TABLE_SCHEMA);
                writableDatabase.setTransactionSuccessful();
            } finally {
                writableDatabase.endTransaction();
            }
        }
    } catch (SQLException e) {
        throw new DatabaseIOException(e);
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) DatabaseIOException(com.google.android.exoplayer2.database.DatabaseIOException) WorkerThread(androidx.annotation.WorkerThread)

Example 19 with DatabaseIOException

use of com.google.android.exoplayer2.database.DatabaseIOException in project ExoPlayer by google.

the class CacheFileMetadataIndex method getAll.

/**
 * Returns all file metadata keyed by file name. The returned map is mutable and may be modified
 * by the caller.
 *
 * <p>This method may be slow and shouldn't normally be called on the main thread.
 *
 * @return The file metadata keyed by file name.
 * @throws DatabaseIOException If an error occurs loading the metadata.
 */
@WorkerThread
public Map<String, CacheFileMetadata> getAll() throws DatabaseIOException {
    try (Cursor cursor = getCursor()) {
        Map<String, CacheFileMetadata> fileMetadata = new HashMap<>(cursor.getCount());
        while (cursor.moveToNext()) {
            String name = checkNotNull(cursor.getString(COLUMN_INDEX_NAME));
            long length = cursor.getLong(COLUMN_INDEX_LENGTH);
            long lastTouchTimestamp = cursor.getLong(COLUMN_INDEX_LAST_TOUCH_TIMESTAMP);
            fileMetadata.put(name, new CacheFileMetadata(length, lastTouchTimestamp));
        }
        return fileMetadata;
    } catch (SQLException e) {
        throw new DatabaseIOException(e);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(android.database.SQLException) Cursor(android.database.Cursor) DatabaseIOException(com.google.android.exoplayer2.database.DatabaseIOException) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

SQLiteDatabase (android.database.sqlite.SQLiteDatabase)13 DatabaseIOException (com.google.android.exoplayer2.database.DatabaseIOException)12 SQLException (android.database.SQLException)11 DownloadBuilder (com.google.android.exoplayer2.testutil.DownloadBuilder)7 Test (org.junit.Test)7 WorkerThread (androidx.annotation.WorkerThread)6 ContentValues (android.content.ContentValues)5 Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 HashMap (java.util.HashMap)1