break dict info table in two, one browse state and one for what's been

stored in static data in DictLangCache up to now -- and remove that
static data in favor of the new table.
This commit is contained in:
Eric House 2012-09-12 19:50:03 -07:00
parent 67b0d44f26
commit 13507e6fc2
3 changed files with 86 additions and 31 deletions

View file

@ -28,7 +28,8 @@ public class DBHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME_SUM = "summaries";
public static final String TABLE_NAME_OBITS = "obits";
public static final String TABLE_NAME_DICTS = "dicts";
public static final String TABLE_NAME_DICTBROWSE = "dictbrowse";
public static final String TABLE_NAME_DICTINFO = "dictinfo";
private static final String DB_NAME = "xwdb";
private static final int DB_VERSION = 13;
@ -138,13 +139,18 @@ public class DBHelper extends SQLiteOpenHelper {
private void onCreateDictsDB( SQLiteDatabase db )
{
db.execSQL( "CREATE TABLE " + TABLE_NAME_DICTS + " ("
db.execSQL( "CREATE TABLE " + TABLE_NAME_DICTINFO + " ("
+ DICTNAME + " TEXT,"
+ MD5SUM + " TEXT(32),"
+ WORDCOUNT + " INTEGER,"
+ WORDCOUNTS + " TEXT,"
+ LANGCODE + " INTEGER,"
+ LOC + " INTEGER(2),"
+ LOC + " INTEGER(2)"
+ ");" );
db.execSQL( "CREATE TABLE " + TABLE_NAME_DICTBROWSE + " ("
+ DICTNAME + " TEXT,"
+ WORDCOUNT + " INTEGER,"
+ WORDCOUNTS + " TEXT,"
+ ITERMIN + " INTEGER(4),"
+ ITERMAX + " INTEGER(4),"
+ ITERPOS + " INTEGER,"

View file

@ -964,7 +964,7 @@ public class DBUtils {
DBHelper.WORDCOUNTS, DBHelper.WORDCOUNT,
DBHelper.ITERPREFIX };
String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name );
Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTS, columns,
Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTBROWSE, columns,
selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
result = new DictBrowseState();
@ -1021,16 +1021,83 @@ public class DBUtils {
}
values.put( DBHelper.WORDCOUNTS, TextUtils.join( ":", nums ) );
}
int result = db.update( DBHelper.TABLE_NAME_DICTS,
int result = db.update( DBHelper.TABLE_NAME_DICTBROWSE,
values, selection, null );
if ( 0 == result ) {
values.put( DBHelper.DICTNAME, name );
db.insert( DBHelper.TABLE_NAME_DICTS, null, values );
db.insert( DBHelper.TABLE_NAME_DICTBROWSE, null, values );
}
db.close();
}
}
public static DictInfo dictsGetInfo( Context context, String name )
{
DictInfo result = null;
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.LANGCODE,
DBHelper.WORDCOUNT,
DBHelper.MD5SUM,
DBHelper.LOC };
String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name );
Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTINFO, columns,
selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
result = new DictInfo();
result.name = name;
result.langCode =
cursor.getInt( cursor.getColumnIndex(DBHelper.LANGCODE));
result.wordCount =
cursor.getInt( cursor.getColumnIndex(DBHelper.WORDCOUNT));
result.md5Sum =
cursor.getString( cursor.getColumnIndex(DBHelper.MD5SUM));
}
cursor.close();
db.close();
}
return result;
}
public static void dictsSetInfo( Context context, DictUtils.DictAndLoc dal,
DictInfo info )
{
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection =
String.format( NAME_FMT, DBHelper.DICTNAME, dal.name );
ContentValues values = new ContentValues();
values.put( DBHelper.LANGCODE, info.langCode );
values.put( DBHelper.WORDCOUNT, info.wordCount );
values.put( DBHelper.MD5SUM, info.md5Sum );
values.put( DBHelper.LOC, dal.loc.ordinal() );
int result = db.update( DBHelper.TABLE_NAME_DICTINFO,
values, selection, null );
if ( 0 == result ) {
values.put( DBHelper.DICTNAME, dal.name );
db.insert( DBHelper.TABLE_NAME_DICTINFO, null, values );
}
db.close();
}
}
public static void dictsRemoveInfo( Context context,
DictUtils.DictAndLoc dal )
{
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection =
String.format( NAME_FMT, DBHelper.DICTNAME, dal.name );
db.delete( DBHelper.TABLE_NAME_DICTINFO, selection, null );
db.close();
}
}
private static void copyGameDB( Context context, boolean toSDCard )
{
String name = DBHelper.getDBName();

View file

@ -39,8 +39,6 @@ import org.eehouse.android.xw4.jni.DictInfo;
import org.eehouse.android.xw4.jni.CommonPrefs;
public class DictLangCache {
private static final HashMap<DictAndLoc,DictInfo> s_nameToLang =
new HashMap<DictAndLoc,DictInfo>();
private static String[] s_langNames;
private static int m_adaptedLang = -1;
@ -230,7 +228,8 @@ public class DictLangCache {
DictUtils.DictLoc loc, boolean added )
{
DictAndLoc dal = new DictAndLoc( name, loc );
s_nameToLang.remove( dal );
DBUtils.dictsRemoveInfo( context, dal );
if ( added ) {
getInfo( context, dal );
}
@ -352,15 +351,7 @@ public class DictLangCache {
private static DictInfo getInfo( Context context, String name )
{
DictInfo result = null;
Set<DictAndLoc> keys = s_nameToLang.keySet();
for ( DictAndLoc key : keys ) {
if ( key.name.equals(name) ) {
result = s_nameToLang.get( key );
break;
}
}
DictInfo result = DBUtils.dictsGetInfo( context, name );
if ( null == result ) {
DictUtils.DictLoc loc = DictUtils.getDictLoc( context, name );
result = getInfo( context, new DictAndLoc( name, loc ) );
@ -370,28 +361,19 @@ public class DictLangCache {
private static DictInfo getInfo( Context context, DictAndLoc dal )
{
DictInfo info;
if ( s_nameToLang.containsKey( dal ) ) {
info = s_nameToLang.get( dal );
} else {
DictInfo info = DBUtils.dictsGetInfo( context, dal.name );
if ( null == info ) {
String[] names = { dal.name };
DictUtils.DictPairs pairs = DictUtils.openDicts( context, names );
info = new DictInfo();
// It should not be possible for dict_getInfo to fail
// unless DictUtils.dictList() isn't doing its job and
// puts unchecked dicts on the list. Should probably
// assert that this returns true. Open question: do I
// always trust dicts in the BUILTIN and INTERNAL
// locations? Files can get damaged....
if ( XwJNI.dict_getInfo( pairs.m_bytes[0], pairs.m_paths[0],
JNIUtilsImpl.get(),
DictUtils.DictLoc.DOWNLOAD == dal.loc,
info ) ) {
info.name = dal.name;
s_nameToLang.put( dal, info );
DBUtils.dictsSetInfo( context, dal, info );
} else {
info = null;
DbgUtils.logf( "getInfo(): unable to open dict %s", dal.name );