Monthly Archive: 5月 2011

Android 2.1でのSQLite FTS3テーブル

SQLiteでFTS3テーブルを構築する際のポイントがいくつかあるようなのでまとめ。
構築済みのテーブルから全文検索用にFTS3テーブルへ移行/FTS3テーブルを追加する場合の注意点リストにも。

▼SQLite FTS3テーブルの特徴
サイズが膨張するためテキスト検索が不要な情報はテーブルを分ける
・FTS3テーブルの値は文字列扱いになり条件に不等号が使えない
_id もTEXTになりPRIMARY KEYですらなくなる → 代わりに docid が自動的に作られる

▼設計
1エントリにテキストと数値が混在し、検索しないテキストが多い場合や数値の条件検索をSQLiteで行う場合はテーブルを切り分ける。

// 通常のテーブル
create table normaltable ( _id integer primary key autoincrement, i_dat0 integer, ... );

// FTS3テーブル (自動的にdocid integer primary key autoincrementが追加される)
create virtual table ftstable USING fts3( stext text, ... );

▼データ管理
設計にもよるが1つのデータを通常のテーブル+FTS3テーブルに分けて登録する場合は、下記の検索方法を使用するため、insert、deleteなどの操作をする場合は両方のテーブルで行う。

public long registerEntry(ContentValues cv) {
	// ContentValues を通常テーブル用+FTS3テーブル用に分割
	ContentValues cv_n=new ContentValues(cv);
	ContentValues cv_fts=new ContentValues();
	cv_fts.put("stext", cv.getAsString("stext")); cv_n.remove("stext");
	cv_fts.put("s_dat0", cv.getAsString("s_dat0")); cv_n.remove("s_dat0");
	cv_fts.put("s_dat1", cv.getAsString("s_dat1")); cv_n.remove("s_dat1");
	return registerEntry(cv_n, cv_fts);
}

public long registerEntry(ContentValues cv_n, ContentValues cv_fts) {
	mDb.insert("ftstable", null, cv_fts);
	return mDb.insert("normaltable", null, cv_n);
}

▼検索
通常のテーブルとFTS3テーブルを _id と docid で参照して両方のデータを含むCursorを取得/Adapter処理できる。

// mDb.rawQuery用検索クエリ例. MATCH部分は*ワイルドカード可
select dt._id, dt.i_dat0, vt.stext
	from normaltable dt, ftstable, vt
	where dt._id=vt.docid AND vt.stext MATCH "Keyword";

 

 

1行メモ

▼ServiceからFLAG_ACTIVITY_NEW_TASKを使わずにActivityを起動する時もPendingIntentを利用

http://groups.google.com/group/android-developers/browse_thread/thread/e95740e776982f89

▼ホーム常駐AppWidget基本サンプル(1.6~): クリックはRemoteViews.setOnClickPendingIntent

http://www.atmarkit.co.jp/fsmart/articles/android10/android10_1.html

▼GIMPでアルファチャンネル付きのPNG作成: カラーモードRGB・レイヤーマスクを作成/編集/適用

http://smileboom.com/tkool/alpha.html

▼開発マシンを移行した後eclipseからデバッグ実行時にエラーが現れる問題

“Re-installation failed due to different application signatures”であれば一旦アプリをアンインストールorシグネチャを旧マシンからコピー

▼タブレットアプリの開発時にhdpiではなくmdpiリソースになってしまう問題

(dpiという意味では正しいので仕様) コード内でリソースにDPIを指定すれば取得できる。

getResources().getDisplayMetrics().densityDpi=DisplayMetrics.DENSITY_HIGH;

▼Serviceが止まりすぎると再起動して貰えなくなる

http://www.swingingblue.net/mt/archives/002768.html

▼自作のAccessibilityServiceが有効かどうかをチェックする

http://stackoverflow.com/questions/5081145/android-how-do-you-check-if-a-particular-accessibilityservice-is-enabled

▼テキストの大きさに合わせてフォントサイズを自動調整するTextView

http://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds/5535672#5535672

▼リリース用signed APKをインストールしていると開発版をEclipseから実行してもADTがインストールしないためデバッグできない

いったんアンインストールする必要があるが、データを残すには adb uninstall -k (パッケージ名)

▼PreferenceActivityで現在の設定値をsummaryに表示するメソッド例 setSummaryAll

#onCreate などで setSummaryAll(getPreferenceScreen());

▼PreferenceScreenのsetSummaryによる要約の指定が反映されない

対策はonContentChanged() または getListView().invalidate()

▼logcat read: Invalid argument が現れたら以下のコマンドでadbのログをクリアしてEclipseを再起動

platform-tools/adb logcat -c

▼Toastによるポップアップメッセージ

Toast.makeText(this, "Message String", Toast.LENGTH_SHORT).show();

SQLiteのバージョン差 Android OS 2.1 (SQLite 3.5.9) と 2.2以降 (SQLite 3.6.22)に注意

▼android.database.sqlite.SQLiteMisuseException: library routine called out of sequence 主に複数スレッドからの同時アクセスによるエラー

http://www.sqlite.org/cvstrac/wiki?p=LibraryRoutineCalledOutOfSequence

▼Cursor制限?のためSQLiteの1レコードは1MB以内 [検証]

http://n2works.net/column/pickup/id/82

▼コンテンツデータベースが大きいときにはアプリとは別にダウンロードして利用する [サンプルコード/英語]

http://penguinman-techtalk.blogspot.com/2010/08/loading-large-reference-database-in.html

▼GIMPでアルファチャンネル付きのPNG作成: カラーモードRGB・レイヤーマスクを作成/編集/適用

http://smileboom.com/tkool/alpha.html

Android 2.1でのBuffer速度

Java VMでのBuffer速度ベンチマークを測定している方がいたので、Xperia (SO-01B/Android 2.1)でどれくらいの差があるかを大ざっぱにチェックした記録。ソース(javaのみ)

結果の値は全てmsec. (/L:ByteOrder.LITTLE_ENDIAN, /B:ByteOrder.BIG_ENDIAN)
※Xperia (Android 2.1)のByteOrder.nativeOrder() は LITTLE_ENDIAN

▼領域確保 配列長(32768, 131072)のint[]、IntBuffer、ByteBuffer (*4bytes)の確保時間

領域確保(整数型32,768分) 1回目 2回目 3回目 4回目 5回目
int[] new 1 94 1 1 1
IntBuffer allocateDirect/L 15 4 1 1 2
IntBuffer allocateDirect/B 287 1 2 1 1
IntBuffer allocate/L 2 1 91 1 1
IntBuffer allocate/B 1 1 1 92 1
IntBuffer wrap 8 1 1 0 91
ByteBuffer allocateDirect/L 1 1 2 2 2
ByteBuffer allocateDirect/B 1 1 1 1 1
ByteBuffer allocate/L 69 91 1 1 0
ByteBuffer allocate/B 1 0 92 1 1
ByteBuffer wrap 0 0 0 91 0
領域確保(整数型131,072分) 1回目 2回目 3回目 4回目 5回目
int[] new 98 120 93 91 90
IntBuffer allocateDirect/L 94 96 95 94 144
IntBuffer allocateDirect/B 3 3 3 3 3
IntBuffer allocate/L 1 1 0 1 1
IntBuffer allocate/B 91 91 31 91 91
IntBuffer wrap 92 92 90 90 90
ByteBuffer allocateDirect/L 96 96 93 94 95
ByteBuffer allocateDirect/B 3 3 3 3 3
ByteBuffer allocate/L 91 92 90 90 90
ByteBuffer allocate/B 91 90 90 90 90
ByteBuffer wrap 92 89 90 91 89

メモリ確保なのでGCが動いてあまり数値が一定しないが基本的には高速。またwrap自体は0~1msで行われるが表の値は new int[] もしくは new byte[] の時間も含む。
allocateDirect(/L)とallocate/Bが比較的遅め。確保法/バイトオーダーで速さが入れ替わっている。

▼処理速度 (個別に値をセット/一括コピー)

int[] array, IntBuffer ib, ByteBuffer bb の以下の処理時間を測定。

for (int i=0; i<SIZE; i++) array[i]=i;
for (int i=0; i<SIZE; i++) ib.put(i);
for (int i=0; i<SIZE; i++) bb.putInt(i);
セット速度(整数型32,768分) 1回目 2回目 3回目 4回目 5回目 最長を除いた4回の平均*
int[] new 16 15 17 18 17 16
IntBuffer allocateDirect/L 317 317 345 315 314 316
IntBuffer allocateDirect/B 444 464 442 371 422 420
IntBuffer allocate/L 282 284 311 280 281 282
IntBuffer allocate/B 342 270 759 147 268 257
IntBuffer wrap 62 25 85 63 64 54
ByteBuffer allocateDirect/L 354 268 264 264 264 265
ByteBuffer allocateDirect/B 405 386 246 372 371 344
ByteBuffer allocate/L 56 232 229 230 254 187
ByteBuffer allocate/B 232 220 216 215 137 197
ByteBuffer wrap 217 229 216 216 217 217

値のセットはどのBufferも遅い。

System.arraycopy(array, 0, array2, 0, SIZE);
ib.put(array);
bb.put(bb_src); // bb_srcはByteBuffer.wrap[bytearray2]
コピー速度(整数型131,072分) 1回目 2回目 3回目 4回目 5回目 最長を除いた4回の平均*
int[] new 2 3 2 2 2 2
IntBuffer allocateDirect/L 2 2 2 2 3 2
IntBuffer allocateDirect/B 9 9 9 9 9 9
IntBuffer allocate/L 988 824 862 581 835 776
IntBuffer allocate/B 519 878 270 820 500 527
IntBuffer wrap 12 12 13 12 12 12
ByteBuffer allocateDirect/L 5 4 5 5 5 5
ByteBuffer allocateDirect/B 97 99 94 96 94 95
ByteBuffer allocate/L 94 94 95 39 94 80
ByteBuffer allocate/B 97 95 94 94 94 94
ByteBuffer wrap 94 109 94 94 95 94

コピーであれば allocateDirect(/L) が高速。

*裏でGCが動くなど、極端に遅い外れ値を除くため5回中もっとも遅かった値を除いて4回を平均