2017-08-07 13:02:30 +00:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
2019-05-08 17:17:54 +00:00
|
|
|
import static org.schabi.newpipe.database.AppDatabase.DATABASE_NAME;
|
|
|
|
import static org.schabi.newpipe.database.Migrations.MIGRATION_1_2;
|
|
|
|
import static org.schabi.newpipe.database.Migrations.MIGRATION_2_3;
|
|
|
|
import static org.schabi.newpipe.database.Migrations.MIGRATION_3_4;
|
|
|
|
import static org.schabi.newpipe.database.Migrations.MIGRATION_4_5;
|
|
|
|
|
2017-08-07 13:02:30 +00:00
|
|
|
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;
|
|
|
|
|
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-05-08 17:17:54 +00:00
|
|
|
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5)
|
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");
|
|
|
|
}
|
2020-08-16 08:24:58 +00:00
|
|
|
final 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");
|
|
|
|
}
|
|
|
|
}
|
2021-07-10 12:46:51 +00:00
|
|
|
|
|
|
|
public static void close() {
|
|
|
|
if (databaseInstance != null) {
|
|
|
|
synchronized (NewPipeDatabase.class) {
|
|
|
|
if (databaseInstance != null) {
|
|
|
|
databaseInstance.close();
|
|
|
|
databaseInstance = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-07 13:02:30 +00:00
|
|
|
}
|