Migrate some cursor utils to core-util.

This commit is contained in:
Greyson Parrelli
2022-03-23 12:56:11 -04:00
parent 954e45ed97
commit 135fde68c1
114 changed files with 269 additions and 204 deletions

View File

@@ -0,0 +1,78 @@
package org.signal.core.util;
import java.util.Locale;
/**
* A set of utilities to make working with Bitmasks easier.
*/
public final class Bitmask {
/**
* Reads a bitmasked boolean from a long at the requested position.
*/
public static boolean read(long value, int position) {
return read(value, position, 1) > 0;
}
/**
* Reads a bitmasked value from a long at the requested position.
*
* @param value The value your are reading state from
* @param position The position you'd like to read from
* @param flagBitSize How many bits are in each flag
* @return The value at the requested position
*/
public static long read(long value, int position, int flagBitSize) {
checkArgument(flagBitSize >= 0, "Must have a positive bit size! size: " + flagBitSize);
int bitsToShift = position * flagBitSize;
checkArgument(bitsToShift + flagBitSize <= 64 && position >= 0, String.format(Locale.US, "Your position is out of bounds! position: %d, flagBitSize: %d", position, flagBitSize));
long shifted = value >>> bitsToShift;
long mask = twoToThe(flagBitSize) - 1;
return shifted & mask;
}
/**
* Sets the value at the specified position in a single-bit bitmasked long.
*/
public static long update(long existing, int position, boolean value) {
return update(existing, position, 1, value ? 1 : 0);
}
/**
* Updates the value in a bitmasked long.
*
* @param existing The existing state of the bitmask
* @param position The position you'd like to update
* @param flagBitSize How many bits are in each flag
* @param value The value you'd like to set at the specified position
* @return The updated bitmask
*/
public static long update(long existing, int position, int flagBitSize, long value) {
checkArgument(flagBitSize >= 0, "Must have a positive bit size! size: " + flagBitSize);
checkArgument(value >= 0, "Value must be positive! value: " + value);
checkArgument(value < twoToThe(flagBitSize), String.format(Locale.US, "Value is larger than you can hold for the given bitsize! value: %d, flagBitSize: %d", value, flagBitSize));
int bitsToShift = position * flagBitSize;
checkArgument(bitsToShift + flagBitSize <= 64 && position >= 0, String.format(Locale.US, "Your position is out of bounds! position: %d, flagBitSize: %d", position, flagBitSize));
long clearMask = ~((twoToThe(flagBitSize) - 1) << bitsToShift);
long cleared = existing & clearMask;
long shiftedValue = value << bitsToShift;
return cleared | shiftedValue;
}
/** Simple method to do 2^n. Giving it a name just so it's clear what's happening. */
private static long twoToThe(long n) {
return 1 << n;
}
private static void checkArgument(boolean state, String message) {
if (!state) {
throw new IllegalArgumentException(message);
}
}
}

View File

@@ -177,4 +177,11 @@ public class Conversions {
((bytes[offset + 6] & 0xffL) << 8) |
((bytes[offset + 7] & 0xffL));
}
public static int toIntExact(long value) {
if ((int)value != value) {
throw new ArithmeticException("integer overflow");
}
return (int)value;
}
}

View File

@@ -0,0 +1,58 @@
package org.signal.core.util
import android.database.Cursor
import java.util.Optional
fun Cursor.requireString(column: String): String? {
return CursorUtil.requireString(this, column)
}
fun Cursor.requireNonNullString(column: String): String {
return CursorUtil.requireString(this, column)!!
}
fun Cursor.optionalString(column: String): Optional<String> {
return CursorUtil.getString(this, column)
}
fun Cursor.requireInt(column: String): Int {
return CursorUtil.requireInt(this, column)
}
fun Cursor.optionalInt(column: String): Optional<Int> {
return CursorUtil.getInt(this, column)
}
fun Cursor.requireFloat(column: String): Float {
return CursorUtil.requireFloat(this, column)
}
fun Cursor.requireLong(column: String): Long {
return CursorUtil.requireLong(this, column)
}
fun Cursor.requireBoolean(column: String): Boolean {
return CursorUtil.requireInt(this, column) != 0
}
fun Cursor.optionalBoolean(column: String): Optional<Boolean> {
return CursorUtil.getBoolean(this, column)
}
fun Cursor.requireBlob(column: String): ByteArray? {
return CursorUtil.requireBlob(this, column)
}
fun Cursor.requireNonNullBlob(column: String): ByteArray {
return CursorUtil.requireBlob(this, column)!!
}
fun Cursor.optionalBlob(column: String): Optional<ByteArray> {
return CursorUtil.getBlob(this, column)
}
fun Cursor.isNull(column: String): Boolean {
return CursorUtil.isNull(this, column)
}
fun Boolean.toInt(): Int = if (this) 1 else 0

View File

@@ -0,0 +1,97 @@
package org.signal.core.util;
import android.database.Cursor;
import androidx.annotation.NonNull;
import java.util.Optional;
public final class CursorUtil {
private CursorUtil() {}
public static String requireString(@NonNull Cursor cursor, @NonNull String column) {
return cursor.getString(cursor.getColumnIndexOrThrow(column));
}
public static int requireInt(@NonNull Cursor cursor, @NonNull String column) {
return cursor.getInt(cursor.getColumnIndexOrThrow(column));
}
public static float requireFloat(@NonNull Cursor cursor, @NonNull String column) {
return cursor.getFloat(cursor.getColumnIndexOrThrow(column));
}
public static long requireLong(@NonNull Cursor cursor, @NonNull String column) {
return cursor.getLong(cursor.getColumnIndexOrThrow(column));
}
public static boolean requireBoolean(@NonNull Cursor cursor, @NonNull String column) {
return requireInt(cursor, column) != 0;
}
public static byte[] requireBlob(@NonNull Cursor cursor, @NonNull String column) {
return cursor.getBlob(cursor.getColumnIndexOrThrow(column));
}
public static boolean isNull(@NonNull Cursor cursor, @NonNull String column) {
return cursor.isNull(cursor.getColumnIndexOrThrow(column));
}
public static boolean requireMaskedBoolean(@NonNull Cursor cursor, @NonNull String column, int position) {
return Bitmask.read(requireLong(cursor, column), position);
}
public static int requireMaskedInt(@NonNull Cursor cursor, @NonNull String column, int position, int flagBitSize) {
return Conversions.toIntExact(Bitmask.read(requireLong(cursor, column), position, flagBitSize));
}
public static Optional<String> getString(@NonNull Cursor cursor, @NonNull String column) {
if (cursor.getColumnIndex(column) < 0) {
return Optional.empty();
} else {
return Optional.ofNullable(requireString(cursor, column));
}
}
public static Optional<Integer> getInt(@NonNull Cursor cursor, @NonNull String column) {
if (cursor.getColumnIndex(column) < 0) {
return Optional.empty();
} else {
return Optional.of(requireInt(cursor, column));
}
}
public static Optional<Boolean> getBoolean(@NonNull Cursor cursor, @NonNull String column) {
if (cursor.getColumnIndex(column) < 0) {
return Optional.empty();
} else {
return Optional.of(requireBoolean(cursor, column));
}
}
public static Optional<byte[]> getBlob(@NonNull Cursor cursor, @NonNull String column) {
if (cursor.getColumnIndex(column) < 0) {
return Optional.empty();
} else {
return Optional.ofNullable(requireBlob(cursor, column));
}
}
/**
* Reads each column as a string, and concatenates them together into a single string separated by |
*/
public static String readRowAsString(@NonNull Cursor cursor) {
StringBuilder row = new StringBuilder();
for (int i = 0, len = cursor.getColumnCount(); i < len; i++) {
row.append(cursor.getString(i));
if (i < len - 1) {
row.append(" | ");
}
}
return row.toString();
}
}

View File

@@ -0,0 +1,7 @@
package org.signal.core.util;
import androidx.annotation.NonNull;
public interface DatabaseId {
@NonNull String serialize();
}

View File

@@ -0,0 +1,146 @@
/**
* Copyright (C) 2011 Whisper Systems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.signal.core.util;
import java.io.IOException;
/**
* Utility for generating hex dumps.
*/
public class Hex {
private final static int HEX_DIGITS_START = 10;
private final static int ASCII_TEXT_START = HEX_DIGITS_START + (16*2 + (16/2));
final static String EOL = System.getProperty("line.separator");
private final static char[] HEX_DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
public static String toString(byte[] bytes) {
return toString(bytes, 0, bytes.length);
}
public static String toString(byte[] bytes, int offset, int length) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < length; i++) {
appendHexChar(buf, bytes[offset + i]);
buf.append(' ');
}
return buf.toString();
}
public static String toStringCondensed(byte[] bytes) {
StringBuffer buf = new StringBuffer();
for (int i=0;i<bytes.length;i++) {
appendHexChar(buf, bytes[i]);
}
return buf.toString();
}
public static byte[] fromStringCondensed(String encoded) throws IOException {
final char[] data = encoded.toCharArray();
final int len = data.length;
if ((len & 0x01) != 0) {
throw new IOException("Odd number of characters.");
}
final byte[] out = new byte[len >> 1];
// two characters form the hex value.
for (int i = 0, j = 0; j < len; i++) {
int f = Character.digit(data[j], 16) << 4;
j++;
f = f | Character.digit(data[j], 16);
j++;
out[i] = (byte) (f & 0xFF);
}
return out;
}
public static byte[] fromStringOrThrow(String encoded) {
try {
return fromStringCondensed(encoded);
} catch (IOException e) {
throw new AssertionError(e);
}
}
public static String dump(byte[] bytes) {
return dump(bytes, 0, bytes.length);
}
public static String dump(byte[] bytes, int offset, int length) {
StringBuffer buf = new StringBuffer();
int lines = ((length - 1) / 16) + 1;
int lineOffset;
int lineLength;
for (int i = 0; i < lines; i++) {
lineOffset = (i * 16) + offset;
lineLength = Math.min(16, (length - (i * 16)));
appendDumpLine(buf, i, bytes, lineOffset, lineLength);
buf.append(EOL);
}
return buf.toString();
}
private static void appendDumpLine(StringBuffer buf, int line, byte[] bytes, int lineOffset, int lineLength) {
buf.append(HEX_DIGITS[(line >> 28) & 0xf]);
buf.append(HEX_DIGITS[(line >> 24) & 0xf]);
buf.append(HEX_DIGITS[(line >> 20) & 0xf]);
buf.append(HEX_DIGITS[(line >> 16) & 0xf]);
buf.append(HEX_DIGITS[(line >> 12) & 0xf]);
buf.append(HEX_DIGITS[(line >> 8) & 0xf]);
buf.append(HEX_DIGITS[(line >> 4) & 0xf]);
buf.append(HEX_DIGITS[(line ) & 0xf]);
buf.append(": ");
for (int i = 0; i < 16; i++) {
int idx = i + lineOffset;
if (i < lineLength) {
int b = bytes[idx];
appendHexChar(buf, b);
} else {
buf.append(" ");
}
if ((i % 2) == 1) {
buf.append(' ');
}
}
for (int i = 0; i < 16 && i < lineLength; i++) {
int idx = i + lineOffset;
int b = bytes[idx];
if (b >= 0x20 && b <= 0x7e) {
buf.append((char)b);
} else {
buf.append('.');
}
}
}
private static void appendHexChar(StringBuffer buf, int b) {
buf.append(HEX_DIGITS[(b >> 4) & 0xf]);
buf.append(HEX_DIGITS[b & 0xf]);
}
}

View File

@@ -0,0 +1,21 @@
package org.signal.core.util;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
public final class ListUtil {
private ListUtil() {}
public static <E> List<List<E>> chunk(@NonNull List<E> list, int chunkSize) {
List<List<E>> chunks = new ArrayList<>(list.size() / chunkSize);
for (int i = 0; i < list.size(); i += chunkSize) {
List<E> chunk = list.subList(i, Math.min(list.size(), i + chunkSize));
chunks.add(chunk);
}
return chunks;
}
}

View File

@@ -0,0 +1,293 @@
package org.signal.core.util;
import android.content.ContentValues;
import android.database.Cursor;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.sqlite.db.SupportSQLiteDatabase;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public final class SqlUtil {
/** The maximum number of arguments (i.e. question marks) allowed in a SQL statement. */
private static final int MAX_QUERY_ARGS = 999;
private SqlUtil() {}
public static boolean tableExists(@NonNull SupportSQLiteDatabase db, @NonNull String table) {
try (Cursor cursor = db.query("SELECT name FROM sqlite_master WHERE type=? AND name=?", new String[] { "table", table })) {
return cursor != null && cursor.moveToNext();
}
}
public static @NonNull List<String> getAllTables(@NonNull SupportSQLiteDatabase db) {
List<String> tables = new LinkedList<>();
try (Cursor cursor = db.query("SELECT name FROM sqlite_master WHERE type=?", new String[] { "table" })) {
while (cursor.moveToNext()) {
tables.add(cursor.getString(0));
}
}
return tables;
}
/**
* Splits a multi-statement SQL block into independent statements. It is assumed that there is
* only one statement per line, and that each statement is terminated by a semi-colon.
*/
public static @NonNull List<String> splitStatements(@NonNull String sql) {
return Arrays.stream(sql.split(";\n"))
.map(String::trim)
.collect(Collectors.toList());
}
public static boolean isEmpty(@NonNull SupportSQLiteDatabase db, @NonNull String table) {
try (Cursor cursor = db.query("SELECT COUNT(*) FROM " + table, null)) {
if (cursor.moveToFirst()) {
return cursor.getInt(0) == 0;
} else {
return true;
}
}
}
public static boolean columnExists(@NonNull SupportSQLiteDatabase db, @NonNull String table, @NonNull String column) {
try (Cursor cursor = db.query("PRAGMA table_info(" + table + ")", null)) {
int nameColumnIndex = cursor.getColumnIndexOrThrow("name");
while (cursor.moveToNext()) {
String name = cursor.getString(nameColumnIndex);
if (name.equals(column)) {
return true;
}
}
}
return false;
}
public static String[] buildArgs(Object... objects) {
String[] args = new String[objects.length];
for (int i = 0; i < objects.length; i++) {
if (objects[i] == null) {
throw new NullPointerException("Cannot have null arg!");
} else if (objects[i] instanceof DatabaseId) {
args[i] = ((DatabaseId) objects[i]).serialize();
} else {
args[i] = objects[i].toString();
}
}
return args;
}
public static String[] buildArgs(long argument) {
return new String[] { Long.toString(argument) };
}
/**
* Returns an updated query and args pairing that will only update rows that would *actually*
* change. In other words, if {@link SupportSQLiteDatabase#update(String, int, ContentValues, String, Object[])}
* returns > 0, then you know something *actually* changed.
*/
public static @NonNull Query buildTrueUpdateQuery(@NonNull String selection,
@NonNull String[] args,
@NonNull ContentValues contentValues)
{
StringBuilder qualifier = new StringBuilder();
Set<Map.Entry<String, Object>> valueSet = contentValues.valueSet();
List<String> fullArgs = new ArrayList<>(args.length + valueSet.size());
fullArgs.addAll(Arrays.asList(args));
int i = 0;
for (Map.Entry<String, Object> entry : valueSet) {
if (entry.getValue() != null) {
if (entry.getValue() instanceof byte[]) {
byte[] data = (byte[]) entry.getValue();
qualifier.append("hex(").append(entry.getKey()).append(") != ? OR ").append(entry.getKey()).append(" IS NULL");
fullArgs.add(Hex.toStringCondensed(data).toUpperCase(Locale.US));
} else {
qualifier.append(entry.getKey()).append(" != ? OR ").append(entry.getKey()).append(" IS NULL");
fullArgs.add(String.valueOf(entry.getValue()));
}
} else {
qualifier.append(entry.getKey()).append(" NOT NULL");
}
if (i != valueSet.size() - 1) {
qualifier.append(" OR ");
}
i++;
}
return new Query("(" + selection + ") AND (" + qualifier + ")", fullArgs.toArray(new String[0]));
}
public static @NonNull Query buildCollectionQuery(@NonNull String column, @NonNull Collection<? extends Object> values) {
if (values.isEmpty()) {
throw new IllegalArgumentException("Must have values!");
}
StringBuilder query = new StringBuilder();
Object[] args = new Object[values.size()];
int i = 0;
for (Object value : values) {
query.append("?");
args[i] = value;
if (i != values.size() - 1) {
query.append(", ");
}
i++;
}
return new Query(column + " IN (" + query.toString() + ")", buildArgs(args));
}
public static @NonNull List<Query> buildCustomCollectionQuery(@NonNull String query, @NonNull List<String[]> argList) {
return buildCustomCollectionQuery(query, argList, MAX_QUERY_ARGS);
}
@VisibleForTesting
static @NonNull List<Query> buildCustomCollectionQuery(@NonNull String query, @NonNull List<String[]> argList, int maxQueryArgs) {
int batchSize = maxQueryArgs / argList.get(0).length;
return ListUtil.chunk(argList, batchSize)
.stream()
.map(argBatch -> buildSingleCustomCollectionQuery(query, argBatch))
.collect(Collectors.toList());
}
private static @NonNull Query buildSingleCustomCollectionQuery(@NonNull String query, @NonNull List<String[]> argList) {
StringBuilder outputQuery = new StringBuilder();
String[] outputArgs = new String[argList.get(0).length * argList.size()];
int argPosition = 0;
for (int i = 0, len = argList.size(); i < len; i++) {
outputQuery.append("(").append(query).append(")");
if (i < len - 1) {
outputQuery.append(" OR ");
}
String[] args = argList.get(i);
for (String arg : args) {
outputArgs[argPosition] = arg;
argPosition++;
}
}
return new Query(outputQuery.toString(), outputArgs);
}
public static @NonNull Query buildQuery(@NonNull String where, @NonNull Object... args) {
return new SqlUtil.Query(where, SqlUtil.buildArgs(args));
}
public static String[] appendArg(@NonNull String[] args, String addition) {
String[] output = new String[args.length + 1];
System.arraycopy(args, 0, output, 0, args.length);
output[output.length - 1] = addition;
return output;
}
public static List<Query> buildBulkInsert(@NonNull String tableName, @NonNull String[] columns, List<ContentValues> contentValues) {
return buildBulkInsert(tableName, columns, contentValues, MAX_QUERY_ARGS);
}
@VisibleForTesting
static List<Query> buildBulkInsert(@NonNull String tableName, @NonNull String[] columns, List<ContentValues> contentValues, int maxQueryArgs) {
int batchSize = maxQueryArgs / columns.length;
return ListUtil.chunk(contentValues, batchSize)
.stream()
.map(batch -> buildSingleBulkInsert(tableName, columns, batch))
.collect(Collectors.toList());
}
private static Query buildSingleBulkInsert(@NonNull String tableName, @NonNull String[] columns, List<ContentValues> contentValues) {
StringBuilder builder = new StringBuilder();
builder.append("INSERT INTO ").append(tableName).append(" (");
for (int i = 0; i < columns.length; i++) {
builder.append(columns[i]);
if (i < columns.length - 1) {
builder.append(", ");
}
}
builder.append(") VALUES ");
StringBuilder placeholder = new StringBuilder();
placeholder.append("(");
for (int i = 0; i < columns.length; i++) {
placeholder.append("?");
if (i < columns.length - 1) {
placeholder.append(", ");
}
}
placeholder.append(")");
for (int i = 0, len = contentValues.size(); i < len; i++) {
builder.append(placeholder);
if (i < len - 1) {
builder.append(", ");
}
}
String query = builder.toString();
String[] args = new String[columns.length * contentValues.size()];
int i = 0;
for (ContentValues values : contentValues) {
for (String column : columns) {
Object value = values.get(column);
args[i] = value != null ? values.get(column).toString() : "null";
i++;
}
}
return new Query(query, args);
}
public static class Query {
private final String where;
private final String[] whereArgs;
private Query(@NonNull String where, @NonNull String[] whereArgs) {
this.where = where;
this.whereArgs = whereArgs;
}
public String getWhere() {
return where;
}
public String[] getWhereArgs() {
return whereArgs;
}
}
}