Automated g4 rollback of changelist 237051112.

*** Original change description ***

Add SimpleCache deletion functionality

This is needed now that index data may be stored outside
of the cache directory.

***

PiperOrigin-RevId: 239294713
This commit is contained in:
olly 2019-03-19 23:33:17 +00:00 committed by Oliver Woodman
parent 35d82e1808
commit d775b9b412
3 changed files with 34 additions and 146 deletions

View File

@ -61,34 +61,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@MonotonicNonNull private String tableName;
/**
* Deletes index data for the specified cache.
*
* @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.
*/
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.beginTransaction();
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);
}
}
/** @param databaseProvider Provides the database in which the index is stored. */
public CacheFileMetadataIndex(DatabaseProvider databaseProvider) {
this.databaseProvider = databaseProvider;
}
@ -96,13 +68,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/**
* Initializes the index for the given cache UID.
*
* @param uid The cache UID.
* @throws DatabaseIOException If an error occurs initializing the index.
*/
public void initialize(long uid) throws DatabaseIOException {
try {
String hexUid = Long.toHexString(uid);
tableName = getTableName(hexUid);
tableName = TABLE_PREFIX + hexUid;
SQLiteDatabase readableDatabase = databaseProvider.getReadableDatabase();
int version =
VersionTable.getVersion(
@ -113,7 +84,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
try {
VersionTable.setVersion(
writableDatabase, VersionTable.FEATURE_CACHE_FILE_METADATA, hexUid, TABLE_VERSION);
dropTable(writableDatabase, tableName);
writableDatabase.execSQL("DROP TABLE IF EXISTS " + tableName);
writableDatabase.execSQL("CREATE TABLE " + tableName + " " + TABLE_SCHEMA);
writableDatabase.setTransactionSuccessful();
} finally {
@ -225,12 +196,4 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/* having= */ null,
/* orderBy= */ null);
}
private static void dropTable(SQLiteDatabase writableDatabase, String tableName) {
writableDatabase.execSQL("DROP TABLE IF EXISTS " + tableName);
}
private static String getTableName(String hexUid) {
return TABLE_PREFIX + hexUid;
}
}

View File

@ -103,18 +103,6 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
return fileName.startsWith(FILE_NAME_ATOMIC);
}
/**
* Deletes index data for the specified cache.
*
* @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.
*/
public static void delete(DatabaseProvider databaseProvider, long uid)
throws DatabaseIOException {
DatabaseStorage.delete(databaseProvider, uid);
}
/**
* Creates an instance supporting database storage only.
*
@ -729,11 +717,6 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
private String hexUid;
private String tableName;
public static void delete(DatabaseProvider databaseProvider, long uid)
throws DatabaseIOException {
delete(databaseProvider, Long.toHexString(uid));
}
public DatabaseStorage(DatabaseProvider databaseProvider) {
this.databaseProvider = databaseProvider;
pendingUpdates = new SparseArray<>();
@ -742,7 +725,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
@Override
public void initialize(long uid) {
hexUid = Long.toHexString(uid);
tableName = getTableName(hexUid);
tableName = TABLE_PREFIX + hexUid;
}
@Override
@ -756,7 +739,20 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
@Override
public void delete() throws DatabaseIOException {
delete(databaseProvider, hexUid);
try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.beginTransaction();
try {
VersionTable.removeVersion(
writableDatabase, VersionTable.FEATURE_CACHE_CONTENT_METADATA, hexUid);
dropTable(writableDatabase);
writableDatabase.setTransactionSuccessful();
} finally {
writableDatabase.endTransaction();
}
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
@Override
@ -879,10 +875,14 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
private void initializeTable(SQLiteDatabase writableDatabase) throws DatabaseIOException {
VersionTable.setVersion(
writableDatabase, VersionTable.FEATURE_CACHE_CONTENT_METADATA, hexUid, TABLE_VERSION);
dropTable(writableDatabase, tableName);
dropTable(writableDatabase);
writableDatabase.execSQL("CREATE TABLE " + tableName + " " + TABLE_SCHEMA);
}
private void dropTable(SQLiteDatabase writableDatabase) {
writableDatabase.execSQL("DROP TABLE IF EXISTS " + tableName);
}
private void deleteRow(SQLiteDatabase writableDatabase, int key) {
writableDatabase.delete(tableName, WHERE_ID_EQUALS, new String[] {Integer.toString(key)});
}
@ -899,32 +899,5 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
values.put(COLUMN_METADATA, data);
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
}
private static void delete(DatabaseProvider databaseProvider, String hexUid)
throws DatabaseIOException {
try {
String tableName = getTableName(hexUid);
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.beginTransaction();
try {
VersionTable.removeVersion(
writableDatabase, VersionTable.FEATURE_CACHE_CONTENT_METADATA, hexUid);
dropTable(writableDatabase, tableName);
writableDatabase.setTransactionSuccessful();
} finally {
writableDatabase.endTransaction();
}
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
private static void dropTable(SQLiteDatabase writableDatabase, String tableName) {
writableDatabase.execSQL("DROP TABLE IF EXISTS " + tableName);
}
private static String getTableName(String hexUid) {
return TABLE_PREFIX + hexUid;
}
}
}

View File

@ -19,11 +19,9 @@ import android.os.ConditionVariable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.database.DatabaseIOException;
import com.google.android.exoplayer2.database.DatabaseProvider;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;
@ -38,13 +36,8 @@ import java.util.TreeSet;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/**
* A {@link Cache} implementation that maintains an in-memory representation.
*
* <p>Only one instance of SimpleCache is allowed for a given directory at a given time.
*
* <p>To delete a SimpleCache, use {@link #delete(File, DatabaseProvider)} rather than deleting the
* directory and its contents directly. This is necessary to ensure that associated index data is
* also removed.
* A {@link Cache} implementation that maintains an in-memory representation. Note, only one
* instance of SimpleCache is allowed for a given directory at a given time.
*/
public final class SimpleCache implements Cache {
@ -100,45 +93,6 @@ public final class SimpleCache implements Cache {
lockedCacheDirs.clear();
}
/**
* Deletes all content belonging to a cache instance.
*
* @param cacheDir The cache directory.
* @param databaseProvider The database in which index data is stored, or {@code null} if the
* cache used a legacy index.
*/
public static void delete(File cacheDir, @Nullable DatabaseProvider databaseProvider) {
if (!cacheDir.exists()) {
return;
}
File[] files = cacheDir.listFiles();
if (files == null) {
cacheDir.delete();
return;
}
if (databaseProvider != null) {
// Make a best effort to read the cache UID and delete associated index data before deleting
// cache directory itself.
long uid = loadUid(files);
if (uid != -1) {
try {
CacheFileMetadataIndex.delete(databaseProvider, uid);
} catch (DatabaseIOException e) {
Log.w(TAG, "Failed to delete file metadata: " + uid);
}
try {
CachedContentIndex.delete(databaseProvider, uid);
} catch (DatabaseIOException e) {
Log.w(TAG, "Failed to delete file metadata: " + uid);
}
}
}
Util.recursiveDelete(cacheDir);
}
/**
* Constructs the cache. The cache will delete any unrecognized files from the directory. Hence
* the directory cannot be used to store other files.
@ -570,14 +524,11 @@ public final class SimpleCache implements Cache {
return;
}
uid = loadUid(files);
if (uid == -1) {
try {
uid = createUid(cacheDir);
} catch (IOException e) {
initializationException = new CacheException("Failed to create cache UID: " + cacheDir);
return;
}
try {
uid = loadUid(cacheDir, files);
} catch (IOException e) {
initializationException = new CacheException("Failed to load cache UID: " + cacheDir);
return;
}
try {
@ -735,13 +686,14 @@ public final class SimpleCache implements Cache {
}
/**
* Loads the cache UID from the files belonging to the root directory.
* Loads the cache UID from the files belonging to the root directory, generating one if needed.
*
* @param directory The root directory.
* @param files The files belonging to the root directory.
* @return The loaded UID, or -1 if a UID has not yet been created.
* @return The cache loaded UID.
* @throws IOException If there is an error loading or generating the UID.
*/
private static long loadUid(File[] files) {
private static long loadUid(File directory, File[] files) throws IOException {
for (File file : files) {
String fileName = file.getName();
if (fileName.endsWith(UID_FILE_SUFFIX)) {
@ -754,7 +706,7 @@ public final class SimpleCache implements Cache {
}
}
}
return -1;
return createUid(directory);
}
@SuppressWarnings("TrulyRandom")