Make story activation atomic with story insertion.

This commit is contained in:
dosiecki 2014-12-18 18:10:06 -08:00
parent 155683e07f
commit be960859a7
3 changed files with 34 additions and 9 deletions

View file

@ -205,7 +205,7 @@ public class BlurDatabaseHelper {
return hashes;
}
public void insertStories(StoriesResponse apiResponse) {
public void insertStories(StoriesResponse apiResponse, NBSyncService.ActivationMode actMode, long modeCutoff) {
// to insert classifiers, we need to determine the feed ID of the stories in this
// response, so sniff one out.
String impliedFeedId = null;
@ -244,8 +244,35 @@ public class BlurDatabaseHelper {
}
impliedFeedId = story.feedId;
}
bulkInsertValues(DatabaseConstants.STORY_TABLE, storyValues);
bulkInsertValues(DatabaseConstants.SOCIALFEED_STORY_MAP_TABLE, socialStoryValues);
// we don't use bulkInsertValues for stories, since we need to put markStoriesActive within the transaction
if (storyValues.size() > 0) {
synchronized (RW_MUTEX) {
dbRW.beginTransaction();
try {
for(ContentValues values: storyValues) {
dbRW.insertWithOnConflict(DatabaseConstants.STORY_TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
}
markStoriesActive(actMode, modeCutoff);
dbRW.setTransactionSuccessful();
} finally {
dbRW.endTransaction();
}
}
}
if (socialStoryValues.size() > 0) {
synchronized (RW_MUTEX) {
dbRW.beginTransaction();
try {
for(ContentValues values: socialStoryValues) {
dbRW.insertWithOnConflict(DatabaseConstants.SOCIALFEED_STORY_MAP_TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
}
markStoriesActive(actMode, modeCutoff);
dbRW.setTransactionSuccessful();
} finally {
dbRW.endTransaction();
}
}
}
// handle classifiers
if (apiResponse.classifiers != null) {

View file

@ -449,8 +449,7 @@ public class NBSyncService extends Service {
totalStoriesSeen += apiResponse.stories.length;
FeedStoriesSeen.put(fs, totalStoriesSeen);
dbHelper.insertStories(apiResponse);
markStoriesActive();
insertStories(apiResponse);
NbActivity.updateAllActivities(true);
if (apiResponse.stories.length == 0) {
@ -516,8 +515,8 @@ public class NBSyncService extends Service {
return true;
}
void markStoriesActive() {
dbHelper.markStoriesActive(ActMode, ModeCutoff);
void insertStories(StoriesResponse apiResponse) {
dbHelper.insertStories(apiResponse, ActMode, ModeCutoff);
}
void incrementRunningChild() {

View file

@ -81,8 +81,7 @@ public class UnreadsService extends SubService {
Log.e(this.getClass().getName(), "error fetching unreads batch, abandoning sync.");
break unreadsyncloop;
}
parent.dbHelper.insertStories(response);
parent.markStoriesActive();
parent.insertStories(response);
for (String hash : hashBatch) {
StoryHashQueue.remove(hash);
}