Better synchronization in actfm sync; started building db indexes

pull/14/head
Sam Bosley 13 years ago
parent 0b34b8c8ae
commit 8e68160724

@ -627,6 +627,12 @@ public final class ActFmSyncService {
Order.asc(TagData.REMOTE_ID)));
return cursorToMap(cursor, taskDao, TagData.REMOTE_ID, TagData.ID);
}
@Override
protected Class<TagData> typeClass() {
return TagData.class;
}
}, done, "goals");
}
@ -747,6 +753,11 @@ public final class ActFmSyncService {
Order.asc(Task.REMOTE_ID)));
return cursorToMap(cursor, taskDao, Task.REMOTE_ID, Task.ID);
}
@Override
protected Class<Task> typeClass() {
return Task.class;
}
}, done, "active_tasks");
}
@ -796,6 +807,11 @@ public final class ActFmSyncService {
Order.asc(Task.REMOTE_ID)));
return cursorToMap(cursor, taskDao, Task.REMOTE_ID, Task.ID);
}
@Override
protected Class<Task> typeClass() {
return Task.class;
}
}, done, "tasks:" + tagData.getId(), "tag_id", tagData.getValue(TagData.REMOTE_ID));
}
@ -896,18 +912,15 @@ public final class ActFmSyncService {
final Update update = new Update(cursor);
new Thread(new Runnable() {
public void run() {
try {
Bitmap picture = null;
if(imageCache != null && imageCache.contains(update.getValue(update.PICTURE))) {
try {
picture = imageCache.get(update.getValue(update.PICTURE));
} catch (IOException e) {
e.printStackTrace();
}
Bitmap picture = null;
if(imageCache != null && imageCache.contains(update.getValue(Update.PICTURE))) {
try {
picture = imageCache.get(update.getValue(Update.PICTURE));
} catch (IOException e) {
e.printStackTrace();
}
pushUpdate(update.getId(), picture);
} finally {
}
pushUpdate(update.getId(), picture);
}
}).start();
}
@ -916,11 +929,6 @@ public final class ActFmSyncService {
}
}
private class UpdateListItemProcessor extends ListItemProcessor<Update> {
@Override
protected void mergeAndSave(JSONArray list, HashMap<Long,Long> locals) throws JSONException {
@ -946,6 +954,11 @@ public final class ActFmSyncService {
Order.asc(Update.REMOTE_ID)));
return cursorToMap(cursor, updateDao, Update.REMOTE_ID, Update.ID);
}
@Override
protected Class<Update> typeClass() {
return Update.class;
}
}
/**
@ -999,16 +1012,19 @@ public final class ActFmSyncService {
abstract protected HashMap<Long, Long> getLocalModels();
abstract protected Class<TYPE> typeClass();
abstract protected void mergeAndSave(JSONArray list,
HashMap<Long,Long> locals) throws JSONException;
public void process(JSONArray list) throws JSONException {
readRemoteIds(list);
HashMap<Long, Long> locals = getLocalModels();
mergeAndSave(list, locals);
synchronized (typeClass()) {
HashMap<Long, Long> locals = getLocalModels();
mergeAndSave(list, locals);
}
}
protected void readRemoteIds(JSONArray list) throws JSONException {
remoteIds = new Long[list.length()];
for(int i = 0; i < list.length(); i++)

@ -37,7 +37,7 @@ public class Database extends AbstractDatabase {
* Database version number. This variable must be updated when database
* tables are updated, as it determines whether a database needs updating.
*/
public static final int VERSION = 20;
public static final int VERSION = 21;
/**
* Database name (must be unique)
@ -143,6 +143,20 @@ public class Database extends AbstractDatabase {
append(')');
database.execSQL(sql.toString());
sql.setLength(0);
sql.append("CREATE UNIQUE INDEX IF NOT EXISTS t_rid ON ").
append(Task.TABLE).append('(').
append(Task.REMOTE_ID.name).
append(')');
database.execSQL(sql.toString());
sql.setLength(0);
sql.append("CREATE UNIQUE INDEX IF NOT EXISTS t_rid ON ").
append(TagData.TABLE).append('(').
append(TagData.REMOTE_ID.name).
append(')');
database.execSQL(sql.toString());
sql.setLength(0);
}
@Override
@ -263,6 +277,30 @@ public class Database extends AbstractDatabase {
database.execSQL("CREATE INDEX IF NOT EXISTS up_tid ON " +
Update.TABLE + "(" + Update.TAGS_LOCAL.name + ")");
} catch (SQLiteException e) {
Log.e("astrid", "db-upgrade-" + oldVersion + "-" + newVersion, e);
}
case 20: try {
String[] tables = new String[] { Task.TABLE.name, TagData.TABLE.name };
String [] ids = new String[] { Task.ID.name, TagData.ID.name };
String[] remoteIds = new String[] { Task.REMOTE_ID.name, TagData.REMOTE_ID.name };
for (int i = 0; i < tables.length; i++) {
String table = tables[i];
String id = ids[i];
String remoteId = remoteIds[i];
// Delete any items that have duplicate remote ids
String deleteDuplicates = String.format("DELETE FROM %s WHERE %s IN (SELECT %s.%s FROM %s, %s AS t2 WHERE %s.%s < t2.%s AND %s.%s = t2.%s AND %s.%s > 0 GROUP BY %s.%s",
table, id, table, id, table, table, table, id, id, table, remoteId, remoteId, table, remoteId, table, id);
// Change all items with remote id = 0 to be remote id = NULL
String changeZeroes = String.format("UPDATE %s SET %s = NULL WHERE %s = 0", table, remoteId, remoteId);
database.execSQL(deleteDuplicates);
database.execSQL(changeZeroes);
}
} catch (SQLiteException e) {
Log.e("astrid", "db-upgrade-" + oldVersion + "-" + newVersion, e);
}

Loading…
Cancel
Save