-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Port Memory remote document cache to C++ #2176
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_MEMORY_REMOTE_DOCUMENT_CACHE_H_ | ||
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_MEMORY_REMOTE_DOCUMENT_CACHE_H_ | ||
|
||
#if !defined(__OBJC__) | ||
#error "For now, this file must only be included by ObjC source files." | ||
#endif // !defined(__OBJC__) | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#include <vector> | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/document_key.h" | ||
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h" | ||
#include "Firestore/core/src/firebase/firestore/model/document_map.h" | ||
#include "Firestore/core/src/firebase/firestore/model/types.h" | ||
|
||
@class FSTLocalSerializer; | ||
@class FSTMaybeDocument; | ||
@class FSTMemoryLRUReferenceDelegate; | ||
@class FSTQuery; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace local { | ||
|
||
class MemoryRemoteDocumentCache { | ||
public: | ||
void AddEntry(FSTMaybeDocument *document); | ||
void RemoveEntry(const model::DocumentKey &key); | ||
|
||
FSTMaybeDocument *_Nullable Get(const model::DocumentKey &key); | ||
model::MaybeDocumentMap GetAll(const model::DocumentKeySet &keys); | ||
model::DocumentMap GetMatchingDocuments(FSTQuery *query); | ||
|
||
std::vector<model::DocumentKey> RemoveOrphanedDocuments( | ||
FSTMemoryLRUReferenceDelegate *reference_delegate, | ||
model::ListenSequenceNumber upper_bound); | ||
|
||
size_t CalculateByteSize(FSTLocalSerializer *serializer); | ||
gsoltis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private: | ||
/** Underlying cache of documents. */ | ||
model::MaybeDocumentMap docs_; | ||
}; | ||
|
||
} // namespace local | ||
} // namespace firestore | ||
} // namespace firebase | ||
|
||
NS_ASSUME_NONNULL_END | ||
|
||
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_MEMORY_REMOTE_DOCUMENT_CACHE_H_ |
125 changes: 125 additions & 0 deletions
125
Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h" | ||
|
||
#import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h" | ||
#import "Firestore/Source/Core/FSTQuery.h" | ||
#import "Firestore/Source/Local/FSTMemoryPersistence.h" | ||
|
||
using firebase::firestore::model::DocumentKey; | ||
using firebase::firestore::model::DocumentKeySet; | ||
using firebase::firestore::model::DocumentMap; | ||
using firebase::firestore::model::ListenSequenceNumber; | ||
using firebase::firestore::model::MaybeDocumentMap; | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace local { | ||
|
||
namespace { | ||
/** | ||
* Returns an estimate of the number of bytes used to store the given | ||
* document key in memory. This is only an estimate and includes the size | ||
* of the segments of the path, but not any object overhead or path separators. | ||
*/ | ||
size_t DocumentKeyByteSize(const DocumentKey &key) { | ||
size_t count = 0; | ||
for (const auto &segment : key.path()) { | ||
count += segment.size(); | ||
} | ||
return count; | ||
} | ||
} // namespace | ||
|
||
void MemoryRemoteDocumentCache::AddEntry(FSTMaybeDocument *document) { | ||
docs_ = docs_.insert(document.key, document); | ||
} | ||
|
||
void MemoryRemoteDocumentCache::RemoveEntry(const DocumentKey &key) { | ||
docs_ = docs_.erase(key); | ||
} | ||
|
||
FSTMaybeDocument *_Nullable MemoryRemoteDocumentCache::Get( | ||
const DocumentKey &key) { | ||
auto found = docs_.find(key); | ||
return found != docs_.end() ? found->second : nil; | ||
} | ||
|
||
MaybeDocumentMap MemoryRemoteDocumentCache::GetAll(const DocumentKeySet &keys) { | ||
MaybeDocumentMap results; | ||
for (const DocumentKey &key : keys) { | ||
// Make sure each key has a corresponding entry, which is null in case the | ||
// document is not found. | ||
// TODO(http://b/32275378): Don't conflate missing / deleted. | ||
results = results.insert(key, Get(key)); | ||
} | ||
return results; | ||
} | ||
|
||
DocumentMap MemoryRemoteDocumentCache::GetMatchingDocuments(FSTQuery *query) { | ||
DocumentMap results; | ||
|
||
// Documents are ordered by key, so we can use a prefix scan to narrow down | ||
// the documents we need to match the query against. | ||
DocumentKey prefix{query.path.Append("")}; | ||
for (auto it = docs_.lower_bound(prefix); it != docs_.end(); ++it) { | ||
const DocumentKey &key = it->first; | ||
if (!query.path.IsPrefixOf(key.path())) { | ||
break; | ||
} | ||
FSTMaybeDocument *maybeDoc = it->second; | ||
if (![maybeDoc isKindOfClass:[FSTDocument class]]) { | ||
continue; | ||
} | ||
FSTDocument *doc = static_cast<FSTDocument *>(maybeDoc); | ||
if ([query matchesDocument:doc]) { | ||
results = results.insert(key, doc); | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
std::vector<DocumentKey> MemoryRemoteDocumentCache::RemoveOrphanedDocuments( | ||
FSTMemoryLRUReferenceDelegate *reference_delegate, | ||
ListenSequenceNumber upper_bound) { | ||
std::vector<DocumentKey> removed; | ||
MaybeDocumentMap updated_docs = docs_; | ||
for (const auto &kv : docs_) { | ||
const DocumentKey &key = kv.first; | ||
if (![reference_delegate isPinnedAtSequenceNumber:upper_bound | ||
document:key]) { | ||
updated_docs = updated_docs.erase(key); | ||
removed.push_back(key); | ||
} | ||
} | ||
docs_ = updated_docs; | ||
return removed; | ||
} | ||
|
||
size_t MemoryRemoteDocumentCache::CalculateByteSize( | ||
FSTLocalSerializer *serializer) { | ||
size_t count = 0; | ||
for (const auto &kv : docs_) { | ||
count += DocumentKeyByteSize(kv.first); | ||
count += [[serializer encodedMaybeDocument:kv.second] serializedSize]; | ||
} | ||
return count; | ||
} | ||
|
||
} // namespace local | ||
} // namespace firestore | ||
} // namespace firebase |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.