[client-app] Fix thread reindexing loop

Summary:
We would mark modified threads for reindexing, the thread search indexer
would reindex them, which would trigger a notification that the thread
had been modified, so we would mark the thread for reindexing, ...

Now we keep track in memory of which threads we've marked for reindexing
so we avoid re-marking them when the update notification arrives later.

Test Plan:
Run locally, verify same threads aren't getting continuously
reindexed and that the in-memory set doesn't grow unboundedly

Reviewers: spang, juan, evan

Reviewed By: juan, evan

Differential Revision: https://phab.nylas.com/D4289
This commit is contained in:
Mark Hahnenberg 2017-03-29 12:09:02 -07:00
parent fa50657af1
commit df1acb84b1

View file

@ -16,6 +16,7 @@ class ThreadSearchIndexStore {
constructor() { constructor() {
this.unsubscribers = [] this.unsubscribers = []
this.indexer = SearchIndexScheduler; this.indexer = SearchIndexScheduler;
this.threadsWaitingToBeIndexed = new Set();
} }
activate() { activate() {
@ -128,16 +129,23 @@ class ThreadSearchIndexStore {
let promises = [] let promises = []
if (type === 'persist') { if (type === 'persist') {
const alreadyIndexedThreads = threads.filter(t => t.isSearchIndexed) const threadsToIndex = _.uniq(threads.filter(t => !this.threadsWaitingToBeIndexed.has(t.id)), false /* isSorted */, t => t.id);
if (alreadyIndexedThreads.length > 0) { const threadsIndexed = threads.filter(t => t.isSearchIndexed && this.threadsWaitingToBeIndexed.has(t.id));
alreadyIndexedThreads.forEach(thread => {
for (const thread of threadsIndexed) {
this.threadsWaitingToBeIndexed.delete(thread.id);
}
if (threadsToIndex.length > 0) {
threadsToIndex.forEach(thread => {
// Mark already indexed threads as unindexed so that we re-index them // Mark already indexed threads as unindexed so that we re-index them
// with updates // with updates
thread.isSearchIndexed = false thread.isSearchIndexed = false;
this.threadsWaitingToBeIndexed.add(thread.id);
}) })
await DatabaseStore.inTransaction(t => t.persistModels(alreadyIndexedThreads)) await DatabaseStore.inTransaction(t => t.persistModels(threadsToIndex));
this.indexer.notifyHasIndexingToDo();
} }
this.indexer.notifyHasIndexingToDo();
} else if (type === 'unpersist') { } else if (type === 'unpersist') {
promises = threads.map(thread => this.unindexThread(thread, promises = threads.map(thread => this.unindexThread(thread,
{isBeingUnpersisted: true})) {isBeingUnpersisted: true}))