Skip to content

firestore::local::MemoryDocumentOverlayCache added. #9330

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 4 commits into from
Feb 14, 2022
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
44 changes: 44 additions & 0 deletions Firestore/Example/Firestore.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

110 changes: 110 additions & 0 deletions Firestore/core/src/local/document_overlay_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2022 Google LLC
*
* 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_LOCAL_DOCUMENT_OVERLAY_CACHE_H_
#define FIRESTORE_CORE_SRC_LOCAL_DOCUMENT_OVERLAY_CACHE_H_

#include <cstdlib>
#include <string>
#include <unordered_map>

#include "Firestore/core/src/model/document_key.h"
#include "Firestore/core/src/model/mutation.h"
#include "Firestore/core/src/model/mutation/overlay.h"
#include "Firestore/core/src/model/resource_path.h"
#include "absl/types/optional.h"

namespace firebase {
namespace firestore {
namespace local {

/**
* Provides methods to read and write document overlays.
*
* An overlay is a saved `Mutation`, that gives a local view of a document when
* applied to the remote version of the document.
*
* Each overlay stores the largest batch ID that is included in the overlay,
* which allows us to remove the overlay once all batches leading up to it have
* been acknowledged.
*/
class DocumentOverlayCache {
public:
using OverlayByDocumentKeyMap = std::unordered_map<model::DocumentKey,
model::mutation::Overlay,
model::DocumentKeyHash>;
using MutationByDocumentKeyMap = std::unordered_map<model::DocumentKey,
model::Mutation,
model::DocumentKeyHash>;

virtual ~DocumentOverlayCache() = default;

/**
* Gets the saved overlay mutation for the given document key.
*
* Returns an empty optional if there is no overlay for that key.
*/
virtual absl::optional<model::mutation::Overlay> GetOverlay(
const model::DocumentKey& key) const = 0;

/**
* Saves the given document key to mutation map to persistence as overlays.
*
* All overlays will have their largest batch id set to `largestBatchId`.
*/
virtual void SaveOverlays(int largest_batch_id,
const MutationByDocumentKeyMap& overlays) = 0;

/** Removes the overlay whose largest-batch-id equals to the given ID. */
virtual void RemoveOverlaysForBatchId(int batch_id) = 0;

/**
* Returns all saved overlays for the given collection.
*
* @param collection The collection path to get the overlays for.
* @param since_batch_id The minimum batch ID to filter by (exclusive).
* Only overlays that contain a change past `sinceBatchId` are returned.
* @return Mapping of each document key in the collection to its overlay.
*/
virtual OverlayByDocumentKeyMap GetOverlays(
const model::ResourcePath& collection, int since_batch_id) const = 0;

/**
* Returns `count` overlays with a batch ID higher than `sinceBatchId` for the
* provided collection group, processed by ascending batch ID.
*
* This method always returns all overlays for a batch even if the last batch
* contains more documents than the remaining limit.
*
* @param collection_group The collection group to get the overlays for.
* @param since_batch_id The minimum batch ID to filter by (exclusive).
* Only overlays that contain a change past `sinceBatchId` are returned.
* @param count The number of overlays to return. Can be exceeded if the last
* batch contains more entries.
* @return Mapping of each document key in the collection group to its
* overlay.
*/
virtual OverlayByDocumentKeyMap GetOverlays(
const std::string& collection_group,
int since_batch_id,
std::size_t count) const = 0;
};

} // namespace local
} // namespace firestore
} // namespace firebase

#endif // FIRESTORE_CORE_SRC_LOCAL_DOCUMENT_OVERLAY_CACHE_H_
69 changes: 69 additions & 0 deletions Firestore/core/src/local/leveldb_document_overlay_cache.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2022 Google LLC
*
* 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/local/leveldb_document_overlay_cache.h"

namespace firebase {
namespace firestore {
namespace local {

using model::DocumentKey;
using model::Mutation;
using model::ResourcePath;
using model::mutation::Overlay;

// TODO(dconeybe) Implement these methods.

LevelDbDocumentOverlayCache::LevelDbDocumentOverlayCache() {
}

absl::optional<Overlay> LevelDbDocumentOverlayCache::GetOverlay(
const DocumentKey& key) const {
(void)key;
return absl::nullopt;
}

void LevelDbDocumentOverlayCache::SaveOverlays(
int largest_batch_id, const MutationByDocumentKeyMap& overlays) {
(void)largest_batch_id;
(void)overlays;
}

void LevelDbDocumentOverlayCache::RemoveOverlaysForBatchId(int batch_id) {
(void)batch_id;
}

DocumentOverlayCache::OverlayByDocumentKeyMap
LevelDbDocumentOverlayCache::GetOverlays(const ResourcePath& collection,
int since_batch_id) const {
(void)collection;
(void)since_batch_id;
return {};
}

DocumentOverlayCache::OverlayByDocumentKeyMap
LevelDbDocumentOverlayCache::GetOverlays(const std::string& collection_group,
int since_batch_id,
std::size_t count) const {
(void)collection_group;
(void)since_batch_id;
(void)count;
return {};
}

} // namespace local
} // namespace firestore
} // namespace firebase
61 changes: 61 additions & 0 deletions Firestore/core/src/local/leveldb_document_overlay_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2022 Google LLC
*
* 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_LOCAL_LEVELDB_DOCUMENT_OVERLAY_CACHE_H_
#define FIRESTORE_CORE_SRC_LOCAL_LEVELDB_DOCUMENT_OVERLAY_CACHE_H_

#include <cstdlib>
#include <string>

#include "Firestore/core/src/local/document_overlay_cache.h"

namespace firebase {
namespace firestore {
namespace local {

class LevelDbDocumentOverlayCache final : public DocumentOverlayCache {
public:
LevelDbDocumentOverlayCache();

LevelDbDocumentOverlayCache(const LevelDbDocumentOverlayCache&) = delete;
LevelDbDocumentOverlayCache& operator=(const LevelDbDocumentOverlayCache&) =
delete;

LevelDbDocumentOverlayCache(LevelDbDocumentOverlayCache&&) = delete;
LevelDbDocumentOverlayCache& operator=(LevelDbDocumentOverlayCache&&) =
delete;

absl::optional<model::mutation::Overlay> GetOverlay(
const model::DocumentKey& key) const override;

void SaveOverlays(int largest_batch_id,
const MutationByDocumentKeyMap& overlays) override;

void RemoveOverlaysForBatchId(int batch_id) override;

OverlayByDocumentKeyMap GetOverlays(const model::ResourcePath& collection,
int since_batch_id) const override;

OverlayByDocumentKeyMap GetOverlays(const std::string& collection_group,
int since_batch_id,
std::size_t count) const override;
};

} // namespace local
} // namespace firestore
} // namespace firebase

#endif // FIRESTORE_CORE_SRC_LOCAL_LEVELDB_DOCUMENT_OVERLAY_CACHE_H_
7 changes: 7 additions & 0 deletions Firestore/core/src/local/leveldb_persistence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ LevelDbBundleCache* LevelDbPersistence::bundle_cache() {
return bundle_cache_.get();
}

LevelDbDocumentOverlayCache* LevelDbPersistence::document_overlay_cache(
const User& user) {
// TODO(dconeybe) Implement this once LevelDbDocumentOverlayCache is done.
(void)user;
return nullptr;
}

void LevelDbPersistence::RunInternal(absl::string_view label,
std::function<void()> block) {
HARD_ASSERT(transaction_ == nullptr,
Expand Down
4 changes: 4 additions & 0 deletions Firestore/core/src/local/leveldb_persistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "Firestore/core/src/credentials/user.h"
#include "Firestore/core/src/local/leveldb_bundle_cache.h"
#include "Firestore/core/src/local/leveldb_document_overlay_cache.h"
#include "Firestore/core/src/local/leveldb_index_manager.h"
#include "Firestore/core/src/local/leveldb_lru_reference_delegate.h"
#include "Firestore/core/src/local/leveldb_mutation_queue.h"
Expand Down Expand Up @@ -80,6 +81,9 @@ class LevelDbPersistence : public Persistence {

LevelDbBundleCache* bundle_cache() override;

LevelDbDocumentOverlayCache* document_overlay_cache(
const credentials::User& user) override;

LevelDbMutationQueue* GetMutationQueueForUser(
const credentials::User& user) override;

Expand Down
Loading