2017-08-07 13:02:30 +00:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2020-02-21 13:13:01 +00:00
|
|
|
import android.database.Cursor;
|
|
|
|
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2020-02-21 13:13:01 +00:00
|
|
|
import androidx.room.Room;
|
2017-08-07 13:02:30 +00:00
|
|
|
|
|
|
|
import org.schabi.newpipe.database.AppDatabase;
|
|
|
|
|
|
|
|
import static org.schabi.newpipe.database.AppDatabase.DATABASE_NAME;
|
2019-04-28 20:43:52 +00:00
|
|
|
import static org.schabi.newpipe.database.Migrations.MIGRATION_1_2;
|
|
|
|
import static org.schabi.newpipe.database.Migrations.MIGRATION_2_3;
|
2017-08-07 13:02:30 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
public final class NewPipeDatabase {
|
2018-03-08 13:39:24 +00:00
|
|
|
private static volatile AppDatabase databaseInstance;
|
2017-08-07 13:02:30 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
private NewPipeDatabase() {
|
|
|
|
//no instance
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
private static AppDatabase getDatabase(final Context context) {
|
2018-03-08 13:39:24 +00:00
|
|
|
return Room
|
|
|
|
.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
|
2019-04-28 20:43:52 +00:00
|
|
|
.addMigrations(MIGRATION_1_2, MIGRATION_2_3)
|
2018-01-17 21:24:59 +00:00
|
|
|
.build();
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
2017-08-07 13:02:30 +00:00
|
|
|
|
|
|
|
@NonNull
|
2020-03-31 17:20:15 +00:00
|
|
|
public static AppDatabase getInstance(@NonNull final Context context) {
|
2018-03-08 13:39:24 +00:00
|
|
|
AppDatabase result = databaseInstance;
|
|
|
|
if (result == null) {
|
|
|
|
synchronized (NewPipeDatabase.class) {
|
|
|
|
result = databaseInstance;
|
|
|
|
if (result == null) {
|
2020-03-31 17:20:15 +00:00
|
|
|
databaseInstance = getDatabase(context);
|
|
|
|
result = databaseInstance;
|
2018-03-08 13:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-01-15 20:30:52 +00:00
|
|
|
}
|
2020-02-21 13:13:01 +00:00
|
|
|
|
|
|
|
public static void checkpoint() {
|
2020-03-07 18:10:32 +00:00
|
|
|
if (databaseInstance == null) {
|
2020-02-21 13:13:01 +00:00
|
|
|
throw new IllegalStateException("database is not initialized");
|
|
|
|
}
|
|
|
|
Cursor c = databaseInstance.query("pragma wal_checkpoint(full)", null);
|
2020-03-07 18:10:32 +00:00
|
|
|
if (c.moveToFirst() && c.getInt(0) == 1) {
|
2020-02-21 13:13:01 +00:00
|
|
|
throw new RuntimeException("Checkpoint was blocked from completing");
|
|
|
|
}
|
|
|
|
}
|
2017-08-07 13:02:30 +00:00
|
|
|
}
|