Skip to content

Commit

Permalink
[SVN] r1542 Inventory in /Libraries/twister-lib-android/
Browse files Browse the repository at this point in the history
[ADD] Finalize BLOB storage for images
[FIX] Import to read to BLOBs
[FIX] Don't query InventoryProvider twice (bumptech/glide#420)
  • Loading branch information
TWiStErRob committed Apr 18, 2015
1 parent 07dc4d9 commit 279119a
Show file tree
Hide file tree
Showing 24 changed files with 231 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public void onDestroy(SQLiteDatabase db) {
execFile(db, String.format(DB_TEST_FILE, dbName));
}
if (devMode) {
backupDB(db, "onOpen_backup");
if (dumpOnOpen) {
backupDB(db, "onOpen_backup");
}
execFile(db, String.format(DB_DEVELOPMENT_FILE, dbName));
}
LOG.info("Opened database: {}", dbToString(db));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.twisterrob.android.utils.concurrent;

public interface Executable<Param> {
@SuppressWarnings("unchecked")
void executeParallel(Param... params);
@SuppressWarnings("unchecked")
void executeSerial(Param... params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Locale;

import javax.annotation.Nullable;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
Expand All @@ -17,6 +19,17 @@ public static String dbToString(final SQLiteDatabase database) {
String path = database != null? database.getPath() : null;
return String.format(Locale.ROOT, "v%d@%s", version, path);
}
public static boolean getBoolean(Cursor cursor, String columnName) {
int col = cursor.getColumnIndex(columnName);
return cursor.getInt(col) != 0;
}
public static boolean getOptionalBoolean(Cursor cursor, String columnName, boolean defaultValue) {
int col = cursor.getColumnIndex(columnName);
if (col != DatabaseOpenHelper.CURSOR_NO_COLUMN) {
return cursor.getInt(col) != 0;
}
return defaultValue;
}

public static int getOptionalInt(Cursor cursor, String columnName, int defaultValue) {
int col = cursor.getColumnIndex(columnName);
Expand Down Expand Up @@ -57,41 +70,64 @@ public static String getOptionalString(Cursor cursor, String columnName, String
return defaultValue;
}

public static Long singleResult(@NonNull Cursor cursor) {
public static Long singleLong(@NonNull Cursor cursor, @Nullable String columnName) {
try {
if (cursor.getCount() == 0 || cursor.getColumnCount() == 0) {
throw new IllegalArgumentException("Empty cursor");
}
if (1 < cursor.getCount()) {
throw new IllegalArgumentException("Multiple rows returned");
}
if (1 < cursor.getColumnCount()) {
throw new IllegalArgumentException("Multiple columns returned");
checkSingleRow(cursor);
int columnIndex;
if (columnName == null) {
checkSingleColumn(cursor);
columnIndex = 0;
} else {
columnIndex = cursor.getColumnIndexOrThrow(columnName);
}
if (!cursor.moveToFirst()) {
throw new IllegalArgumentException("Cannot move to first item");
}
return cursor.isNull(0)? null : cursor.getLong(0);
return cursor.isNull(columnIndex)? null : cursor.getLong(columnIndex);
} finally {
cursor.close();
}
}

public static String singleResultFromColumn(@NonNull Cursor cursor, @NonNull String columnName) {
public static String singleString(@NonNull Cursor cursor, @Nullable String columnName) {
try {
if (cursor.getCount() == 0 || cursor.getColumnCount() == 0) {
throw new IllegalArgumentException("Empty cursor");
}
if (1 < cursor.getCount()) {
throw new IllegalArgumentException("Multiple rows returned");
checkSingleRow(cursor);
if (columnName == null) {
checkSingleColumn(cursor);
return cursor.getString(0);
} else {
return cursor.getString(cursor.getColumnIndexOrThrow(columnName));
}
if (!cursor.moveToFirst()) {
throw new IllegalArgumentException("Cannot move to first item");
} finally {
cursor.close();
}
}

public static byte[] singleBlob(@NonNull Cursor cursor, @Nullable String columnName) {
try {
checkSingleRow(cursor);
if (columnName == null) {
checkSingleColumn(cursor);
return cursor.getBlob(0);
} else {
return cursor.getBlob(cursor.getColumnIndexOrThrow(columnName));
}
int columnIndex = cursor.getColumnIndexOrThrow(columnName);
return cursor.isNull(columnIndex)? null : cursor.getString(columnIndex);
} finally {
cursor.close();
}
}

private static void checkSingleRow(@NonNull Cursor cursor) {
if (cursor.getCount() == 0 || cursor.getColumnCount() == 0) {
throw new IllegalArgumentException("Empty cursor");
}
if (1 < cursor.getCount()) {
throw new IllegalArgumentException("Multiple rows returned");
}
if (!cursor.moveToFirst()) {
throw new IllegalArgumentException("Cannot move to first item");
}
}
private static void checkSingleColumn(@NonNull Cursor cursor) {
if (1 < cursor.getColumnCount()) {
throw new IllegalArgumentException("Multiple columns returned");
}
}
}
6 changes: 3 additions & 3 deletions src/main/assets/MagicHomeInventory.schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ CREATE TABLE Item (
_id INTEGER NOT NULL,
name NVARCHAR NOT NULL, -- user entered
description TEXT NULL, -- user entered
image VARCHAR NULL, -- relative path
image BLOB NULL, -- JPEG image
category INTEGER DEFAULT 0 -- uncategorized
CONSTRAINT fk_Item_category
REFERENCES Category(_id)
Expand Down Expand Up @@ -193,7 +193,7 @@ CREATE TABLE Property (
_id INTEGER NOT NULL,
name NVARCHAR NOT NULL, -- user entered
description TEXT NULL, -- user entered
image VARCHAR NULL, -- relative path
image BLOB NULL, -- JPEG image
type INTEGER DEFAULT 0 -- other
CONSTRAINT fk_Property_type
REFERENCES PropertyType(_id)
Expand Down Expand Up @@ -228,7 +228,7 @@ CREATE TABLE Room (
_id INTEGER NOT NULL,
name NVARCHAR NOT NULL, -- user entered
description TEXT NULL, -- user entered
image VARCHAR NULL, -- relative path
image BLOB NULL, -- JPEG image
type INTEGER DEFAULT 0 -- other
CONSTRAINT fk_Room_type
REFERENCES RoomType(_id)
Expand Down
36 changes: 24 additions & 12 deletions src/main/java/net/twisterrob/inventory/android/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import android.support.annotation.*;

import com.bumptech.glide.*;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import com.bumptech.glide.load.resource.bitmap.ImageVideoBitmapDecoder;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gif.GifResourceDecoder;
Expand Down Expand Up @@ -73,20 +76,29 @@ class Pic {
private static final LoggingListener<String, GlideDrawable> IMAGE_LOGGING_LISTENER =
new LoggingListener<>("image");

public static final DrawableRequestBuilder<Integer> SVG_REQUEST = Glide
.with(App.getAppContext())
.fromResource()
private static <T> DrawableRequestBuilder<T> baseRequest(Class<T> clazz) {
ModelLoader<T, InputStream> loader = Glide.buildModelLoader(clazz, InputStream.class, App.getAppContext());
DrawableRequestBuilder<T> builder = Glide
.with(App.getAppContext())
.using((StreamModelLoader<T>)loader)
.from(clazz)
.animate(android.R.anim.fade_in)
.error(R.drawable.image_error);
if (DISABLE && BuildConfig.DEBUG) {
builder = builder
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
;
}
return builder;
}

public static final DrawableRequestBuilder<Integer> SVG_REQUEST = baseRequest(Integer.class)
.listener(SVG_LOGGING_LISTENER)
.decoder(getSvgDecoder())
.animate(android.R.anim.fade_in)
.error(R.drawable.image_error);
.decoder(getSvgDecoder());

public static final DrawableRequestBuilder<String> IMAGE_REQUEST = Glide
.with(App.getAppContext())
.fromString()
.listener(IMAGE_LOGGING_LISTENER)
.animate(android.R.anim.fade_in)
.error(R.drawable.image_error);
public static final DrawableRequestBuilder<String> IMAGE_REQUEST = baseRequest(String.class)
.listener(IMAGE_LOGGING_LISTENER);

private static GifBitmapWrapperResourceDecoder getSvgDecoder() {
Context context = App.getAppContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void recalculate() {
// sum 0 rows is NULL, count 0 rows is 0...
String sql = "select coalesce(sum(length(name) + length(location)), 0) + count() * 4 * 3 from Search";
Cursor cursor = App.db().getReadableDatabase().rawQuery(sql, null);
return (long)DatabaseTools.singleResult(cursor);
return (long)DatabaseTools.singleLong(cursor, null);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void onConfigure(SQLiteDatabase db) {
}
};
// TODO App.getPrefEditor().remove(Prefs.CURRENT_LANGUAGE).apply();
m_helper.setDumpOnOpen(BuildConfig.DEBUG);
m_helper.setDevMode(BuildConfig.DEBUG);
}

Expand Down Expand Up @@ -73,6 +72,7 @@ private Cursor rawQuery(SQLiteDatabase db, int queryResource, Object... params)
try {
long start = System.nanoTime();
Cursor cursor = db.rawQuery(m_resources.getString(queryResource), StringTools.toStringArray(params));
cursor.moveToFirst(); // make sure the query runs now
long end = System.nanoTime();
LOG.debug("rawQuery({}, {}): {}ms", name, paramString, (end - start) / 10000000);
return cursor;
Expand Down Expand Up @@ -179,28 +179,40 @@ public Cursor getCategory(long itemID) {
return rawQuery(R.string.query_category, itemID);
}

public long createProperty(long type, String name, String description, String image) {
return rawInsert(R.string.query_property_create, type, name, description, image);
public long createProperty(long type, String name, String description) {
return rawInsert(R.string.query_property_create, type, name, description);
}
public Long findProperty(String name) {
return getID(R.string.query_property_find, name);
}
public void updateProperty(long id, long type, String name, String description, String image) {
execSQL(R.string.query_property_update, type, name, description, image, id);
public void updateProperty(long id, long type, String name, String description) {
execSQL(R.string.query_property_update, type, name, description, id);
}
public void addPropertyImage(long id, byte[] imageContents) {
execSQL(R.string.query_property_image_set, imageContents, id);
}
public Cursor getPropertyImage(long id) {
return rawQuery(R.string.query_property_image_get, id);
}
public void deleteProperty(long id) {
execSQL(R.string.query_property_delete, id);
}

public long createRoom(long propertyID, long type, String name, String description, String image) {
rawInsert(R.string.query_room_create, propertyID, type, name, description, image);
public long createRoom(long propertyID, long type, String name, String description) {
rawInsert(R.string.query_room_create, propertyID, type, name, description);
return findRoom(propertyID, name); // last_insert_rowid() doesn't work with INSTEAD OF INSERT triggers on VIEWs
}
public Long findRoom(long propertyID, String name) {
return getID(R.string.query_room_find, propertyID, name);
}
public void updateRoom(long id, long type, String name, String description, String image) {
execSQL(R.string.query_room_update, type, name, description, image, id);
public void updateRoom(long id, long type, String name, String description) {
execSQL(R.string.query_room_update, type, name, description, id);
}
public void addRoomImage(long id, byte[] imageContents) {
execSQL(R.string.query_room_image_set, imageContents, id);
}
public Cursor getRoomImage(long id) {
return rawQuery(R.string.query_room_image_get, id);
}
public void deleteRoom(long id) {
execSQL(R.string.query_room_delete, id);
Expand All @@ -221,17 +233,23 @@ public void moveRooms(long propertyID, long[] roomIDs) {
}
}

private long createItem(Long parentID, long category, String name, String description, String image) {
return rawInsert(R.string.query_item_create, parentID, category, name, description, image);
private long createItem(Long parentID, long category, String name, String description) {
return rawInsert(R.string.query_item_create, parentID, category, name, description);
}
public long createItem(long parentID, long category, String name, String description, String image) {
return createItem((Long)parentID, category, name, description, image);
public long createItem(long parentID, long category, String name, String description) {
return createItem((Long)parentID, category, name, description);
}
public Long findItem(long parentID, String name) {
return getID(R.string.query_item_find, parentID, name);
}
public void updateItem(long id, long category, String name, String description, String image) {
execSQL(R.string.query_item_update, category, name, description, image, id);
public void updateItem(long id, long category, String name, String description) {
execSQL(R.string.query_item_update, category, name, description, id);
}
public void addItemImage(long id, byte[] imageContents) {
execSQL(R.string.query_item_image_set, imageContents, id);
}
public Cursor getItemImage(long id) {
return rawQuery(R.string.query_item_image_get, id);
}
public void deleteItem(long id) {
execSQL(R.string.query_item_delete, id);
Expand Down Expand Up @@ -338,7 +356,7 @@ public void clearImages() {
} finally {
db.endTransaction();
}
db.execSQL("VACUUM"); // must be outside a transaction
//db.execSQL("VACUUM"); // must be outside a transaction
}

public void rebuildSearch() {
Expand All @@ -351,6 +369,6 @@ public void rebuildSearch() {
} finally {
db.endTransaction();
}
db.execSQL("VACUUM"); // must be outside a transaction
//db.execSQL("VACUUM"); // must be outside a transaction
}
}
Loading

0 comments on commit 279119a

Please sign in to comment.