Convert RemappedRecordTables to kotlin.
This commit is contained in:
parent
2cdb1b8300
commit
2dc41f319c
2 changed files with 124 additions and 146 deletions
|
@ -1,146 +0,0 @@
|
|||
package org.thoughtcrime.securesms.database;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
import org.signal.core.util.CursorUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The backing datastore for {@link RemappedRecords}. See that class for more details.
|
||||
*/
|
||||
public class RemappedRecordTables extends DatabaseTable {
|
||||
|
||||
public static final String[] CREATE_TABLE = { Recipients.CREATE_TABLE,
|
||||
Threads.CREATE_TABLE };
|
||||
|
||||
private static class SharedColumns {
|
||||
protected static final String ID = "_id";
|
||||
protected static final String OLD_ID = "old_id";
|
||||
protected static final String NEW_ID = "new_id";
|
||||
}
|
||||
|
||||
private static final class Recipients extends SharedColumns {
|
||||
private static final String TABLE_NAME = "remapped_recipients";
|
||||
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
||||
OLD_ID + " INTEGER UNIQUE, " +
|
||||
NEW_ID + " INTEGER)";
|
||||
}
|
||||
|
||||
private static final class Threads extends SharedColumns {
|
||||
private static final String TABLE_NAME = "remapped_threads";
|
||||
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
||||
OLD_ID + " INTEGER UNIQUE, " +
|
||||
NEW_ID + " INTEGER)";
|
||||
}
|
||||
|
||||
RemappedRecordTables(Context context, SignalDatabase databaseHelper) {
|
||||
super(context, databaseHelper);
|
||||
}
|
||||
|
||||
@NonNull Map<RecipientId, RecipientId> getAllRecipientMappings() {
|
||||
SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();
|
||||
Map<RecipientId, RecipientId> recipientMap = new HashMap<>();
|
||||
|
||||
db.beginTransaction();
|
||||
try {
|
||||
List<Mapping> mappings = getAllMappings(Recipients.TABLE_NAME);
|
||||
|
||||
for (Mapping mapping : mappings) {
|
||||
RecipientId oldId = RecipientId.from(mapping.getOldId());
|
||||
RecipientId newId = RecipientId.from(mapping.getNewId());
|
||||
recipientMap.put(oldId, newId);
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
|
||||
return recipientMap;
|
||||
}
|
||||
|
||||
@NonNull Map<Long, Long> getAllThreadMappings() {
|
||||
SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();
|
||||
Map<Long, Long> threadMap = new HashMap<>();
|
||||
|
||||
db.beginTransaction();
|
||||
try {
|
||||
List<Mapping> mappings = getAllMappings(Threads.TABLE_NAME);
|
||||
|
||||
for (Mapping mapping : mappings) {
|
||||
threadMap.put(mapping.getOldId(), mapping.getNewId());
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
|
||||
return threadMap;
|
||||
}
|
||||
|
||||
void addRecipientMapping(@NonNull RecipientId oldId, @NonNull RecipientId newId) {
|
||||
addMapping(Recipients.TABLE_NAME, new Mapping(oldId.toLong(), newId.toLong()));
|
||||
}
|
||||
|
||||
void addThreadMapping(long oldId, long newId) {
|
||||
addMapping(Threads.TABLE_NAME, new Mapping(oldId, newId));
|
||||
}
|
||||
|
||||
public Cursor getAllRecipients() {
|
||||
return databaseHelper.getSignalReadableDatabase().query(Recipients.TABLE_NAME, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
public Cursor getAllThreads() {
|
||||
return databaseHelper.getSignalReadableDatabase().query(Threads.TABLE_NAME, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
private @NonNull List<Mapping> getAllMappings(@NonNull String table) {
|
||||
List<Mapping> mappings = new LinkedList<>();
|
||||
|
||||
try (Cursor cursor = databaseHelper.getSignalReadableDatabase().query(table, null, null, null, null, null, null)) {
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
long oldId = CursorUtil.requireLong(cursor, SharedColumns.OLD_ID);
|
||||
long newId = CursorUtil.requireLong(cursor, SharedColumns.NEW_ID);
|
||||
mappings.add(new Mapping(oldId, newId));
|
||||
}
|
||||
}
|
||||
|
||||
return mappings;
|
||||
}
|
||||
|
||||
private void addMapping(@NonNull String table, @NonNull Mapping mapping) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(SharedColumns.OLD_ID, mapping.getOldId());
|
||||
values.put(SharedColumns.NEW_ID, mapping.getNewId());
|
||||
|
||||
databaseHelper.getSignalWritableDatabase().insert(table, null, values);
|
||||
}
|
||||
|
||||
static final class Mapping {
|
||||
private final long oldId;
|
||||
private final long newId;
|
||||
|
||||
public Mapping(long oldId, long newId) {
|
||||
this.oldId = oldId;
|
||||
this.newId = newId;
|
||||
}
|
||||
|
||||
public long getOldId() {
|
||||
return oldId;
|
||||
}
|
||||
|
||||
public long getNewId() {
|
||||
return newId;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
package org.thoughtcrime.securesms.database
|
||||
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import androidx.core.content.contentValuesOf
|
||||
import org.signal.core.util.readToList
|
||||
import org.signal.core.util.requireLong
|
||||
import org.signal.core.util.select
|
||||
import org.signal.core.util.withinTransaction
|
||||
import org.thoughtcrime.securesms.database.RemappedRecordTables.SharedColumns.ID
|
||||
import org.thoughtcrime.securesms.database.RemappedRecordTables.SharedColumns.NEW_ID
|
||||
import org.thoughtcrime.securesms.database.RemappedRecordTables.SharedColumns.OLD_ID
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId
|
||||
import java.util.HashMap
|
||||
|
||||
/**
|
||||
* The backing datastore for [RemappedRecords]. See that class for more details.
|
||||
*/
|
||||
class RemappedRecordTables internal constructor(context: Context?, databaseHelper: SignalDatabase?) : DatabaseTable(context, databaseHelper) {
|
||||
|
||||
companion object {
|
||||
val CREATE_TABLE = arrayOf(Recipients.CREATE_TABLE, Threads.CREATE_TABLE)
|
||||
}
|
||||
|
||||
private object SharedColumns {
|
||||
const val ID = "_id"
|
||||
const val OLD_ID = "old_id"
|
||||
const val NEW_ID = "new_id"
|
||||
}
|
||||
|
||||
private object Recipients {
|
||||
const val TABLE_NAME = "remapped_recipients"
|
||||
const val CREATE_TABLE = """
|
||||
CREATE TABLE $TABLE_NAME (
|
||||
$ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
$OLD_ID INTEGER UNIQUE,
|
||||
$NEW_ID INTEGER
|
||||
)
|
||||
"""
|
||||
}
|
||||
|
||||
private object Threads {
|
||||
const val TABLE_NAME = "remapped_threads"
|
||||
const val CREATE_TABLE = """
|
||||
CREATE TABLE $TABLE_NAME (
|
||||
$ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
$OLD_ID INTEGER UNIQUE,
|
||||
$NEW_ID INTEGER
|
||||
)
|
||||
"""
|
||||
}
|
||||
|
||||
fun getAllRecipientMappings(): Map<RecipientId, RecipientId> {
|
||||
val recipientMap: MutableMap<RecipientId, RecipientId> = HashMap()
|
||||
|
||||
readableDatabase.withinTransaction { db ->
|
||||
val mappings = getAllMappings(db, Recipients.TABLE_NAME)
|
||||
for (mapping in mappings) {
|
||||
val oldId = RecipientId.from(mapping.oldId)
|
||||
val newId = RecipientId.from(mapping.newId)
|
||||
recipientMap[oldId] = newId
|
||||
}
|
||||
}
|
||||
|
||||
return recipientMap
|
||||
}
|
||||
|
||||
fun getAllThreadMappings(): Map<Long, Long> {
|
||||
val threadMap: MutableMap<Long, Long> = HashMap()
|
||||
|
||||
readableDatabase.withinTransaction { db ->
|
||||
val mappings = getAllMappings(db, Threads.TABLE_NAME)
|
||||
for (mapping in mappings) {
|
||||
threadMap[mapping.oldId] = mapping.newId
|
||||
}
|
||||
}
|
||||
|
||||
return threadMap
|
||||
}
|
||||
|
||||
fun addRecipientMapping(oldId: RecipientId, newId: RecipientId) {
|
||||
addMapping(Recipients.TABLE_NAME, Mapping(oldId.toLong(), newId.toLong()))
|
||||
}
|
||||
|
||||
fun addThreadMapping(oldId: Long, newId: Long) {
|
||||
addMapping(Threads.TABLE_NAME, Mapping(oldId, newId))
|
||||
}
|
||||
|
||||
fun getAllRecipients(): Cursor {
|
||||
return readableDatabase
|
||||
.select()
|
||||
.from(Recipients.TABLE_NAME)
|
||||
.run()
|
||||
}
|
||||
|
||||
fun getAllThreads(): Cursor {
|
||||
return readableDatabase
|
||||
.select()
|
||||
.from(Threads.TABLE_NAME)
|
||||
.run()
|
||||
}
|
||||
|
||||
private fun getAllMappings(db: SQLiteDatabase, table: String): List<Mapping> {
|
||||
return db.select()
|
||||
.from(table)
|
||||
.run()
|
||||
.readToList { cursor ->
|
||||
Mapping(
|
||||
oldId = cursor.requireLong(OLD_ID),
|
||||
newId = cursor.requireLong(NEW_ID)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun addMapping(table: String, mapping: Mapping) {
|
||||
val values = contentValuesOf(
|
||||
OLD_ID to mapping.oldId,
|
||||
NEW_ID to mapping.newId
|
||||
)
|
||||
databaseHelper.signalWritableDatabase.insert(table, null, values)
|
||||
}
|
||||
|
||||
private class Mapping(val oldId: Long, val newId: Long)
|
||||
}
|
Loading…
Add table
Reference in a new issue