-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Port leveldb remote document cache to C++ #2186
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
6 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
69 changes: 69 additions & 0 deletions
69
Firestore/core/src/firebase/firestore/local/leveldb_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,69 @@ | ||
/* | ||
* 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_LEVELDB_REMOTE_DOCUMENT_CACHE_H_ | ||
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_LEVELDB_REMOTE_DOCUMENT_CACHE_H_ | ||
|
||
#if !defined(__OBJC__) | ||
#error "For now, this file must only be included by ObjC source files." | ||
#endif // !defined(__OBJC__) | ||
|
||
#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" | ||
#include "absl/strings/string_view.h" | ||
|
||
@class FSTLevelDB; | ||
@class FSTLocalSerializer; | ||
@class FSTMaybeDocument; | ||
@class FSTQuery; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace local { | ||
|
||
/** Cached Remote Documents backed by leveldb. */ | ||
class LevelDbRemoteDocumentCache { | ||
public: | ||
LevelDbRemoteDocumentCache(FSTLevelDB* db, FSTLocalSerializer* serializer); | ||
|
||
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); | ||
|
||
private: | ||
FSTMaybeDocument* DecodeMaybeDocument(absl::string_view encoded, | ||
const model::DocumentKey& key); | ||
|
||
FSTLevelDB* db_; | ||
FSTLocalSerializer* serializer_; | ||
}; | ||
|
||
} // namespace local | ||
} // namespace firestore | ||
} // namespace firebase | ||
|
||
NS_ASSUME_NONNULL_END | ||
|
||
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_LEVELDB_REMOTE_DOCUMENT_CACHE_H_ |
138 changes: 138 additions & 0 deletions
138
Firestore/core/src/firebase/firestore/local/leveldb_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,138 @@ | ||
/* | ||
* 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/leveldb_remote_document_cache.h" | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#include <string> | ||
|
||
#import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h" | ||
#import "Firestore/Source/Core/FSTQuery.h" | ||
#import "Firestore/Source/Local/FSTLevelDB.h" | ||
#import "Firestore/Source/Local/FSTLocalSerializer.h" | ||
#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h" | ||
#include "Firestore/core/src/firebase/firestore/util/status.h" | ||
#include "leveldb/db.h" | ||
|
||
using firebase::firestore::model::DocumentKey; | ||
using firebase::firestore::model::DocumentKeySet; | ||
using firebase::firestore::model::DocumentMap; | ||
using firebase::firestore::model::MaybeDocumentMap; | ||
using leveldb::Status; | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace local { | ||
|
||
LevelDbRemoteDocumentCache::LevelDbRemoteDocumentCache( | ||
FSTLevelDB* db, FSTLocalSerializer* serializer) | ||
: db_(db), serializer_(serializer) { | ||
} | ||
|
||
void LevelDbRemoteDocumentCache::AddEntry(FSTMaybeDocument* document) { | ||
std::string ldb_key = LevelDbRemoteDocumentKey::Key(document.key); | ||
db_.currentTransaction->Put(ldb_key, | ||
[serializer_ encodedMaybeDocument:document]); | ||
} | ||
|
||
void LevelDbRemoteDocumentCache::RemoveEntry(const DocumentKey& key) { | ||
std::string ldb_key = LevelDbRemoteDocumentKey::Key(key); | ||
db_.currentTransaction->Delete(ldb_key); | ||
} | ||
|
||
FSTMaybeDocument* _Nullable LevelDbRemoteDocumentCache::Get( | ||
const DocumentKey& key) { | ||
std::string ldb_key = LevelDbRemoteDocumentKey::Key(key); | ||
std::string value; | ||
Status status = db_.currentTransaction->Get(ldb_key, &value); | ||
if (status.IsNotFound()) { | ||
return nil; | ||
} else if (status.ok()) { | ||
return DecodeMaybeDocument(value, key); | ||
} else { | ||
HARD_FAIL("Fetch document for key (%s) failed with status: %s", | ||
key.ToString(), status.ToString()); | ||
} | ||
} | ||
|
||
MaybeDocumentMap LevelDbRemoteDocumentCache::GetAll( | ||
const DocumentKeySet& keys) { | ||
MaybeDocumentMap results; | ||
|
||
LevelDbRemoteDocumentKey currentKey; | ||
auto it = db_.currentTransaction->NewIterator(); | ||
|
||
for (const DocumentKey& key : keys) { | ||
it->Seek(LevelDbRemoteDocumentKey::Key(key)); | ||
if (!it->Valid() || !currentKey.Decode(it->key()) || | ||
currentKey.document_key() != key) { | ||
results = results.insert(key, nil); | ||
} else { | ||
results = results.insert(key, DecodeMaybeDocument(it->value(), key)); | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
DocumentMap LevelDbRemoteDocumentCache::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. | ||
std::string startKey = LevelDbRemoteDocumentKey::KeyPrefix(query.path); | ||
auto it = db_.currentTransaction->NewIterator(); | ||
it->Seek(startKey); | ||
|
||
LevelDbRemoteDocumentKey currentKey; | ||
for (; it->Valid() && currentKey.Decode(it->key()); it->Next()) { | ||
FSTMaybeDocument* maybeDoc = | ||
DecodeMaybeDocument(it->value(), currentKey.document_key()); | ||
if (!query.path.IsPrefixOf(maybeDoc.key.path())) { | ||
break; | ||
} else if ([maybeDoc isKindOfClass:[FSTDocument class]]) { | ||
results = | ||
results.insert(maybeDoc.key, static_cast<FSTDocument*>(maybeDoc)); | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
FSTMaybeDocument* LevelDbRemoteDocumentCache::DecodeMaybeDocument( | ||
absl::string_view encoded, const DocumentKey& key) { | ||
NSData* data = [[NSData alloc] initWithBytesNoCopy:(void*)encoded.data() | ||
length:encoded.size() | ||
freeWhenDone:NO]; | ||
|
||
NSError* error; | ||
FSTPBMaybeDocument* proto = | ||
[FSTPBMaybeDocument parseFromData:data error:&error]; | ||
if (!proto) { | ||
HARD_FAIL("FSTPBMaybeDocument failed to parse: %s", error); | ||
} | ||
|
||
FSTMaybeDocument* maybeDocument = [serializer_ decodedMaybeDocument:proto]; | ||
HARD_ASSERT(maybeDocument.key == key, | ||
"Read document has key (%s) instead of expected key (%s).", | ||
maybeDocument.key.ToString(), key.ToString()); | ||
return maybeDocument; | ||
} | ||
|
||
} // namespace local | ||
} // namespace firestore | ||
} // namespace firebase |
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
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.