Skip to content

Backport LocalStoreTest changes #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.firebase.firestore.util.Assert.hardAssert;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.firebase.database.collection.ImmutableSortedMap;
import com.google.firebase.firestore.core.Query;
import com.google.firebase.firestore.model.Document;
Expand Down Expand Up @@ -55,6 +56,21 @@ class LocalDocumentsView {
this.indexManager = indexManager;
}

@VisibleForTesting
RemoteDocumentCache getRemoteDocumentCache() {
return remoteDocumentCache;
}

@VisibleForTesting
MutationQueue getMutationQueue() {
return mutationQueue;
}

@VisibleForTesting
IndexManager getIndexManager() {
return indexManager;
}

/**
* Returns the the local view of the document identified by {@code key}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ final class MemoryMutationQueue implements MutationQueue {
private ByteString lastStreamToken;

private final MemoryPersistence persistence;
private final StatsCollector statsCollector;

MemoryMutationQueue(MemoryPersistence persistence, StatsCollector statsCollector) {
MemoryMutationQueue(MemoryPersistence persistence) {
this.persistence = persistence;
this.statsCollector = statsCollector;
queue = new ArrayList<>();

batchesByDocumentKey = new ImmutableSortedSet<>(emptyList(), DocumentReference.BY_KEY);
Expand Down Expand Up @@ -156,16 +154,12 @@ public MutationBatch addMutationBatch(
.addToCollectionParentIndex(mutation.getKey().getPath().popLast());
}

statsCollector.recordRowsWritten(STATS_TAG, 1);

return batch;
}

@Nullable
@Override
public MutationBatch lookupMutationBatch(int batchId) {
statsCollector.recordRowsRead(STATS_TAG, 1);

int index = indexOfBatchId(batchId);
if (index < 0 || index >= queue.size()) {
return null;
Expand Down Expand Up @@ -214,8 +208,6 @@ public List<MutationBatch> getAllMutationBatchesAffectingDocumentKey(DocumentKey
result.add(batch);
}

statsCollector.recordRowsRead(STATS_TAG, result.size());

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,30 @@ public final class MemoryPersistence extends Persistence {
private final MemoryIndexManager indexManager;
private final MemoryQueryCache queryCache;
private final MemoryRemoteDocumentCache remoteDocumentCache;
private final StatsCollector statsCollector;
private ReferenceDelegate referenceDelegate;

private boolean started;

public static MemoryPersistence createEagerGcMemoryPersistence() {
return createEagerGcMemoryPersistence(StatsCollector.NO_OP_STATS_COLLECTOR);
}

public static MemoryPersistence createEagerGcMemoryPersistence(StatsCollector statsCollector) {
MemoryPersistence persistence = new MemoryPersistence(statsCollector);
MemoryPersistence persistence = new MemoryPersistence();
persistence.setReferenceDelegate(new MemoryEagerReferenceDelegate(persistence));
return persistence;
}

public static MemoryPersistence createLruGcMemoryPersistence(
LruGarbageCollector.Params params,
StatsCollector statsCollector,
LocalSerializer serializer) {
MemoryPersistence persistence = new MemoryPersistence(statsCollector);
LruGarbageCollector.Params params, LocalSerializer serializer) {
MemoryPersistence persistence = new MemoryPersistence();
persistence.setReferenceDelegate(
new MemoryLruReferenceDelegate(persistence, params, serializer));
return persistence;
}

/** Use static helpers to instantiate */
private MemoryPersistence(StatsCollector statsCollector) {
this.statsCollector = statsCollector;
private MemoryPersistence() {
mutationQueues = new HashMap<>();
indexManager = new MemoryIndexManager();
queryCache = new MemoryQueryCache(this);
remoteDocumentCache = new MemoryRemoteDocumentCache(this, statsCollector);
remoteDocumentCache = new MemoryRemoteDocumentCache(this);
}

@Override
Expand Down Expand Up @@ -101,7 +93,7 @@ private void setReferenceDelegate(ReferenceDelegate delegate) {
MutationQueue getMutationQueue(User user) {
MemoryMutationQueue queue = mutationQueues.get(user);
if (queue == null) {
queue = new MemoryMutationQueue(this, statsCollector);
queue = new MemoryMutationQueue(this);
mutationQueues.put(user, queue);
}
return queue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ final class MemoryRemoteDocumentCache implements RemoteDocumentCache {
private ImmutableSortedMap<DocumentKey, Pair<MaybeDocument, SnapshotVersion>> docs;

private final MemoryPersistence persistence;
private StatsCollector statsCollector;

MemoryRemoteDocumentCache(MemoryPersistence persistence, StatsCollector statsCollector) {
MemoryRemoteDocumentCache(MemoryPersistence persistence) {
docs = ImmutableSortedMap.Builder.emptyMap(DocumentKey.comparator());
this.statsCollector = statsCollector;
this.persistence = persistence;
}

Expand All @@ -60,14 +58,12 @@ public void add(MaybeDocument document, SnapshotVersion readTime) {

@Override
public void remove(DocumentKey key) {
statsCollector.recordRowsDeleted(STATS_TAG, 1);
docs = docs.remove(key);
}

@Nullable
@Override
public MaybeDocument get(DocumentKey key) {
statsCollector.recordRowsRead(STATS_TAG, 1);
Pair<MaybeDocument, SnapshotVersion> entry = docs.get(key);
return entry != null ? entry.first : null;
}
Expand All @@ -82,7 +78,6 @@ public Map<DocumentKey, MaybeDocument> getAll(Iterable<DocumentKey> keys) {
result.put(key, get(key));
}

statsCollector.recordRowsRead(STATS_TAG, result.size());
return result;
}

Expand All @@ -101,13 +96,9 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(
Iterator<Map.Entry<DocumentKey, Pair<MaybeDocument, SnapshotVersion>>> iterator =
docs.iteratorFrom(prefix);

int rowsRead = 0;

while (iterator.hasNext()) {
Map.Entry<DocumentKey, Pair<MaybeDocument, SnapshotVersion>> entry = iterator.next();

++rowsRead;

DocumentKey key = entry.getKey();
if (!queryPath.isPrefixOf(key.getPath())) {
break;
Expand All @@ -129,8 +120,6 @@ public ImmutableSortedMap<DocumentKey, Document> getAllDocumentsMatchingQuery(
}
}

statsCollector.recordRowsRead(STATS_TAG, rowsRead);

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

/** A queue of mutations to apply to the remote store. */
interface MutationQueue {
/** The tag used by the StatsCollector. */
String STATS_TAG = "mutations";

/**
* Starts the mutation queue, performing any initial reads that might be required to establish
* invariants, etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
* instances (indicating that the document is known to not exist).
*/
interface RemoteDocumentCache {
/** The tag used by the StatsCollector. */
String STATS_TAG = "remote_documents";

/**
* Adds or replaces an entry in the cache.
*
Expand Down
Loading