include dict's full-file md5sum when querying for upgrades

Requires adding a new db field to cache the value.
This commit is contained in:
Eric House 2022-04-21 13:50:34 -07:00
parent ab29dfa6ce
commit 9778abd6a5
6 changed files with 87 additions and 7 deletions

View file

@ -56,7 +56,7 @@ public class DBHelper extends SQLiteOpenHelper {
private int addedVersion() { return mAddedVersion; } private int addedVersion() { return mAddedVersion; }
} }
private static final String DB_NAME = BuildConfig.DB_NAME; private static final String DB_NAME = BuildConfig.DB_NAME;
private static final int DB_VERSION = 30; private static final int DB_VERSION = 31;
public static final String GAME_NAME = "GAME_NAME"; public static final String GAME_NAME = "GAME_NAME";
public static final String VISID = "VISID"; public static final String VISID = "VISID";
@ -97,6 +97,7 @@ public class DBHelper extends SQLiteOpenHelper {
public static final String DICTNAME = "DICTNAME"; public static final String DICTNAME = "DICTNAME";
public static final String MD5SUM = "MD5SUM"; public static final String MD5SUM = "MD5SUM";
public static final String FULLSUM = "FULLSUM";
public static final String WORDCOUNT = "WORDCOUNT"; public static final String WORDCOUNT = "WORDCOUNT";
public static final String WORDCOUNTS = "WORDCOUNTS"; public static final String WORDCOUNTS = "WORDCOUNTS";
public static final String LANGCODE = "LANGCODE"; public static final String LANGCODE = "LANGCODE";
@ -179,11 +180,12 @@ public class DBHelper extends SQLiteOpenHelper {
}; };
private static final String[][] s_dictInfoColsAndTypes = { private static final String[][] s_dictInfoColsAndTypes = {
{ DICTNAME, "TEXT" } { DICTNAME, "TEXT" },
,{ LOC, "UNSIGNED INTEGER(1)" } { LOC, "UNSIGNED INTEGER(1)" },
,{ MD5SUM, "TEXT(32)" } { MD5SUM, "TEXT(32)" },
,{ WORDCOUNT,"INTEGER" } { FULLSUM, "TEXT(32)" },
,{ LANGCODE, "INTEGER" } { WORDCOUNT, "INTEGER" },
{ LANGCODE, "INTEGER" },
}; };
private static final String[][] s_dictBrowseColsAndTypes = { private static final String[][] s_dictBrowseColsAndTypes = {
@ -278,6 +280,7 @@ public class DBHelper extends SQLiteOpenHelper {
boolean madeSumTable = false; boolean madeSumTable = false;
boolean madeChatTable = false; boolean madeChatTable = false;
boolean madeDITable = false;
switch( oldVersion ) { switch( oldVersion ) {
case 5: case 5:
createTable( db, TABLE_NAMES.OBITS, s_obitsColsAndTypes ); createTable( db, TABLE_NAMES.OBITS, s_obitsColsAndTypes );
@ -298,6 +301,7 @@ public class DBHelper extends SQLiteOpenHelper {
case 12: case 12:
createTable( db, TABLE_NAMES.DICTINFO, s_dictInfoColsAndTypes ); createTable( db, TABLE_NAMES.DICTINFO, s_dictInfoColsAndTypes );
createTable( db, TABLE_NAMES.DICTBROWSE, s_dictBrowseColsAndTypes ); createTable( db, TABLE_NAMES.DICTBROWSE, s_dictBrowseColsAndTypes );
madeDITable = true;
case 13: case 13:
addSumColumn( db, LASTMOVE ); addSumColumn( db, LASTMOVE );
case 14: case 14:
@ -353,6 +357,10 @@ public class DBHelper extends SQLiteOpenHelper {
if ( !madeSumTable ) { if ( !madeSumTable ) {
addSumColumn( db, NEXTDUPTIMER ); addSumColumn( db, NEXTDUPTIMER );
} }
case 30:
if ( !madeDITable ) {
addColumn( db, TABLE_NAMES.DICTINFO, s_dictInfoColsAndTypes, FULLSUM );
}
break; break;
default: default:

View file

@ -2062,6 +2062,7 @@ public class DBUtils {
String[] columns = { DBHelper.LANGCODE, String[] columns = { DBHelper.LANGCODE,
DBHelper.WORDCOUNT, DBHelper.WORDCOUNT,
DBHelper.MD5SUM, DBHelper.MD5SUM,
DBHelper.FULLSUM,
/*DBHelper.LOC*/ }; /*DBHelper.LOC*/ };
String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name ); String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name );
initDB( context ); initDB( context );
@ -2076,12 +2077,19 @@ public class DBUtils {
cursor.getInt( cursor.getColumnIndex(DBHelper.WORDCOUNT)); cursor.getInt( cursor.getColumnIndex(DBHelper.WORDCOUNT));
result.md5Sum = result.md5Sum =
cursor.getString( cursor.getColumnIndex(DBHelper.MD5SUM)); cursor.getString( cursor.getColumnIndex(DBHelper.MD5SUM));
result.fullSum =
cursor.getString( cursor.getColumnIndex(DBHelper.FULLSUM));
// int loc = cursor.getInt(cursor.getColumnIndex(DBHelper.LOC)); // int loc = cursor.getInt(cursor.getColumnIndex(DBHelper.LOC));
// Log.d( TAG, "dictsGetInfo(): read sum %s/loc %d for %s", result.md5Sum, // Log.d( TAG, "dictsGetInfo(): read sum %s/loc %d for %s", result.md5Sum,
// loc, name ); // loc, name );
} }
cursor.close(); cursor.close();
} }
if ( null == result.fullSum ) { // force generation
result = null;
}
return result; return result;
} }
@ -2095,6 +2103,7 @@ public class DBUtils {
values.put( DBHelper.LANGCODE, info.langCode ); values.put( DBHelper.LANGCODE, info.langCode );
values.put( DBHelper.WORDCOUNT, info.wordCount ); values.put( DBHelper.WORDCOUNT, info.wordCount );
values.put( DBHelper.MD5SUM, info.md5Sum ); values.put( DBHelper.MD5SUM, info.md5Sum );
values.put( DBHelper.FULLSUM, info.fullSum );
values.put( DBHelper.LOC, dal.loc.ordinal() ); values.put( DBHelper.LOC, dal.loc.ordinal() );
initDB( context ); initDB( context );

View file

@ -310,6 +310,16 @@ public class DictLangCache {
return result; return result;
} }
public static String getDictFullSum( Context context, String dict )
{
String result = null;
DictInfo info = getInfo( context, dict );
if ( null != info ) {
result = info.fullSum;
}
return result;
}
public static long getFileLen( Context context, DictAndLoc dal ) public static long getFileLen( Context context, DictAndLoc dal )
{ {
File path = dal.getPath( context ); File path = dal.getPath( context );
@ -523,6 +533,9 @@ public class DictLangCache {
DictLoc.DOWNLOAD == dal.loc ); DictLoc.DOWNLOAD == dal.loc );
if ( null != info ) { if ( null != info ) {
info.name = dal.name; info.name = dal.name;
info.fullSum = Utils.getMD5SumFor( context, dal );
Assert.assertTrueNR( null != info.fullSum );
DBUtils.dictsSetInfo( context, dal, info ); DBUtils.dictsSetInfo( context, dal, info );
} else { } else {
Log.i( TAG, "getInfo(): unable to open dict %s", dal.name ); Log.i( TAG, "getInfo(): unable to open dict %s", dal.name );

View file

@ -66,6 +66,7 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
private static final String k_LANG = "lang"; private static final String k_LANG = "lang";
private static final String k_LANGCODE = "lc"; private static final String k_LANGCODE = "lc";
private static final String k_MD5SUM = "md5sum"; private static final String k_MD5SUM = "md5sum";
private static final String k_FULLSUM = "fullsum";
private static final String k_INDEX = "index"; private static final String k_INDEX = "index";
private static final String k_LEN = "len"; private static final String k_LEN = "len";
private static final String k_URL = "url"; private static final String k_URL = "url";
@ -231,12 +232,15 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
String langCode = DictLangCache.getLangCodeStr( context, lang ); String langCode = DictLangCache.getLangCodeStr( context, lang );
String langStr = DictLangCache.getLangName( context, lang ); String langStr = DictLangCache.getLangName( context, lang );
String sum = DictLangCache.getDictMD5Sum( context, dal.name ); String sum = DictLangCache.getDictMD5Sum( context, dal.name );
String fullSum = DictLangCache.getDictFullSum( context, dal.name );
Assert.assertTrueNR( null != fullSum );
long len = DictLangCache.getFileLen( context, dal ); long len = DictLangCache.getFileLen( context, dal );
try { try {
params.put( k_NAME, dal.name ); params.put( k_NAME, dal.name );
params.put( k_LANG, langStr ); params.put( k_LANG, langStr );
params.put( k_LANGCODE, langCode ); params.put( k_LANGCODE, langCode );
params.put( k_MD5SUM, sum ); params.put( k_MD5SUM, sum );
params.put( k_FULLSUM, fullSum );
params.put( k_INDEX, index ); params.put( k_INDEX, index );
params.put( k_LEN, len ); params.put( k_LEN, len );
} catch( org.json.JSONException jse ) { } catch( org.json.JSONException jse ) {

View file

@ -64,6 +64,7 @@ import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
@ -465,6 +466,34 @@ public class Utils {
return str; return str;
} }
public static String getMD5SumFor( Context context, DictUtils.DictAndLoc dal )
{
String result = null;
if ( DictUtils.DictLoc.BUILT_IN == dal.loc ) {
result = dal.loc.toString();
} else {
File file = dal.getPath( context );
try {
InputStream is = new FileInputStream( file );
MessageDigest md = MessageDigest.getInstance( "MD5" );
byte[] buf = new byte[1024*8];
for ( ; ; ) {
int nRead = is.read( buf );
if ( 0 >= nRead ) {
break;
}
md.update( buf, 0, nRead );
}
result = Utils.digestToString( md.digest() );
} catch ( Exception ex ) {
Log.ex( TAG, ex );
}
}
// Log.d( TAG, "getMD5SumFor(%s) => %s", dal.name, result );
return result;
}
public static String getMD5SumFor( byte[] bytes ) public static String getMD5SumFor( byte[] bytes )
{ {
String result = null; String result = null;

View file

@ -20,12 +20,29 @@
package org.eehouse.android.xw4.jni; package org.eehouse.android.xw4.jni;
import org.eehouse.android.xw4.BuildConfig;
public class DictInfo { public class DictInfo {
// set in java code // set in java code
public String name; public String name;
public String fullSum; // md5sum of the whole file
// set in jni code // set in jni code
public int langCode; public int langCode;
public int wordCount; public int wordCount;
public String md5Sum; public String md5Sum; // internal (skipping header?)
@Override
public String toString()
{
if ( BuildConfig.NON_RELEASE ) {
return new StringBuilder("{")
.append("name: ").append(name)
.append(", md5Sum: ").append(md5Sum)
.append(", fullSum: ").append(fullSum)
.append("}").toString();
} else {
return super.toString();
}
}
}; };