Skip to content

Commit d058af9

Browse files
authored
Finish sizer interface (#3752)
* Use int64_t for CalculateSizeBytes This avoids a bunch of nasty casting/checking in other parts of the code * Use the Sizer for MutationBatches * Don't double-count document key size.
1 parent ffd7316 commit d058af9

14 files changed

+52
-53
lines changed

Firestore/Source/Local/FSTLRUGarbageCollector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ namespace model = firebase::firestore::model;
7070
(const std::unordered_map<model::TargetId, local::QueryData> &)
7171
liveQueries;
7272

73-
- (size_t)byteSize;
73+
- (int64_t)byteSize;
7474

7575
/** Returns the number of targets and orphaned documents cached. */
7676
- (size_t)sequenceNumberCount;

Firestore/Source/Local/FSTLevelDB.mm

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#import "Firestore/Source/Local/FSTLevelDB.h"
1818

19+
#include <limits>
1920
#include <memory>
2021
#include <unordered_map>
2122
#include <utility>
@@ -99,7 +100,7 @@
99100

100101
@interface FSTLevelDB ()
101102

102-
- (size_t)byteSize;
103+
- (int64_t)byteSize;
103104

104105
@property(nonatomic, assign, getter=isStarted) BOOL started;
105106

@@ -276,7 +277,7 @@ - (void)limboDocumentUpdated:(const DocumentKey &)key {
276277
[self writeSentinelForKey:key];
277278
}
278279

279-
- (size_t)byteSize {
280+
- (int64_t)byteSize {
280281
return [_db byteSize];
281282
}
282283

@@ -379,7 +380,7 @@ - (instancetype)initWithLevelDB:(std::unique_ptr<leveldb::DB>)db
379380
return self;
380381
}
381382

382-
- (size_t)byteSize {
383+
- (int64_t)byteSize {
383384
int64_t count = 0;
384385
auto iter = util::DirectoryIterator::Create(_directory);
385386
for (; iter->Valid(); iter->Next()) {
@@ -388,8 +389,9 @@ - (size_t)byteSize {
388389
}
389390
HARD_ASSERT(iter->status().ok(), "Failed to iterate leveldb directory: %s",
390391
iter->status().error_message().c_str());
391-
HARD_ASSERT(count >= 0 && count <= SIZE_MAX, "Overflowed counting bytes cached");
392-
return static_cast<size_t>(count);
392+
HARD_ASSERT(count >= 0 && count <= std::numeric_limits<int64_t>::max(),
393+
"Overflowed counting bytes cached");
394+
return count;
393395
}
394396

395397
- (const std::set<std::string> &)users {

Firestore/Source/Local/FSTMemoryPersistence.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,17 @@ - (BOOL)isPinnedAtSequenceNumber:(ListenSequenceNumber)upperBound
322322
return NO;
323323
}
324324

325-
- (size_t)byteSize {
325+
- (int64_t)byteSize {
326326
// Note that this method is only used for testing because this delegate is only
327327
// used for testing. The algorithm here (loop through everything, serialize it
328328
// and count bytes) is inefficient and inexact, but won't run in production.
329-
size_t count = 0;
329+
int64_t count = 0;
330330
ProtoSizer sizer(_serializer);
331331
count += _persistence.queryCache->CalculateByteSize(sizer);
332332
count += _persistence.remoteDocumentCache->CalculateByteSize(sizer);
333333
const MutationQueues &queues = [_persistence mutationQueues];
334334
for (const auto &entry : queues) {
335-
count += entry.second->CalculateByteSize(_serializer);
335+
count += entry.second->CalculateByteSize(sizer);
336336
}
337337
return count;
338338
}

Firestore/core/src/firebase/firestore/local/lru_garbage_collector.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ LruResults LruGarbageCollector::Collect(const LiveQueryMap& live_targets) {
113113
return LruResults::DidNotRun();
114114
}
115115

116-
size_t current_size = CalculateByteSize();
117-
if (current_size < static_cast<size_t>(params_.min_bytes_threshold)) {
116+
int64_t current_size = CalculateByteSize();
117+
if (current_size < params_.min_bytes_threshold) {
118118
// Not enough on disk to warrant collection. Wait another timeout cycle.
119119
LOG_DEBUG(
120120
"Garbage collection skipped; Cache size %s is lower than threshold %s",

Firestore/core/src/firebase/firestore/local/lru_garbage_collector.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class LruDelegate {
7373
/** Access to the underlying LRU Garbage collector instance. */
7474
virtual LruGarbageCollector* garbage_collector() = 0;
7575

76-
virtual size_t CalculateByteSize() const = 0;
76+
virtual int64_t CalculateByteSize() = 0;
7777

7878
/** Returns the number of targets and orphaned documents cached. */
7979
virtual size_t GetSequenceNumberCount() = 0;
@@ -121,7 +121,7 @@ class LruDelegateBridge : public LruDelegate {
121121
return target_.gc;
122122
}
123123

124-
size_t CalculateByteSize() const override {
124+
int64_t CalculateByteSize() override {
125125
return [target_ byteSize];
126126
}
127127

@@ -178,7 +178,7 @@ class LruGarbageCollector {
178178
public:
179179
LruGarbageCollector(LruDelegate* delegate, LruParams params);
180180

181-
size_t CalculateByteSize() const {
181+
int64_t CalculateByteSize() const {
182182
return delegate_->CalculateByteSize();
183183
}
184184

Firestore/core/src/firebase/firestore/local/memory_mutation_queue.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
3535
#include "Firestore/core/src/firebase/firestore/model/types.h"
3636

37-
@class FSTLocalSerializer;
3837
@class FSTMemoryPersistence;
3938

4039
NS_ASSUME_NONNULL_BEGIN
@@ -43,6 +42,8 @@ namespace firebase {
4342
namespace firestore {
4443
namespace local {
4544

45+
class Sizer;
46+
4647
class MemoryMutationQueue : public MutationQueue {
4748
public:
4849
explicit MemoryMutationQueue(FSTMemoryPersistence* persistence);
@@ -86,7 +87,7 @@ class MemoryMutationQueue : public MutationQueue {
8687

8788
bool ContainsKey(const model::DocumentKey& key);
8889

89-
size_t CalculateByteSize(FSTLocalSerializer* serializer);
90+
int64_t CalculateByteSize(const Sizer& sizer);
9091

9192
nanopb::ByteString GetLastStreamToken() override;
9293
void SetLastStreamToken(const nanopb::ByteString& token) override;

Firestore/core/src/firebase/firestore/local/memory_mutation_queue.mm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818

1919
#include <utility>
2020

21-
#import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
22-
#import "Firestore/Source/Local/FSTLocalSerializer.h"
2321
#import "Firestore/Source/Local/FSTMemoryPersistence.h"
2422

2523
#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h"
24+
#include "Firestore/core/src/firebase/firestore/local/sizer.h"
2625
#include "Firestore/core/src/firebase/firestore/model/mutation_batch.h"
2726
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
2827
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
@@ -257,10 +256,10 @@ MutationBatch batch(batch_id, local_write_time, std::move(base_mutations),
257256
return begin != range.end() && begin->key() == key;
258257
}
259258

260-
size_t MemoryMutationQueue::CalculateByteSize(FSTLocalSerializer* serializer) {
261-
size_t count = 0;
259+
int64_t MemoryMutationQueue::CalculateByteSize(const Sizer& sizer) {
260+
int64_t count = 0;
262261
for (const auto& batch : queue_) {
263-
count += [[serializer encodedMutationBatch:batch] serializedSize];
262+
count += sizer.CalculateByteSize(batch);
264263
};
265264
return count;
266265
}

Firestore/core/src/firebase/firestore/local/memory_query_cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class MemoryQueryCache : public QueryCache {
7676
bool Contains(const model::DocumentKey& key) override;
7777

7878
// Other methods and accessors
79-
size_t CalculateByteSize(const Sizer& sizer);
79+
int64_t CalculateByteSize(const Sizer& sizer);
8080

8181
size_t size() const override {
8282
return queries_.size();

Firestore/core/src/firebase/firestore/local/memory_query_cache.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@
122122
return references_.ContainsKey(key);
123123
}
124124

125-
size_t MemoryQueryCache::CalculateByteSize(const Sizer& sizer) {
126-
size_t count = 0;
125+
int64_t MemoryQueryCache::CalculateByteSize(const Sizer& sizer) {
126+
int64_t count = 0;
127127
for (const auto& kv : queries_) {
128128
count += sizer.CalculateByteSize(kv.second);
129129
}

Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class MemoryRemoteDocumentCache : public RemoteDocumentCache {
5858
FSTMemoryLRUReferenceDelegate* reference_delegate,
5959
model::ListenSequenceNumber upper_bound);
6060

61-
size_t CalculateByteSize(const Sizer& sizer);
61+
int64_t CalculateByteSize(const Sizer& sizer);
6262

6363
private:
6464
/** Underlying cache of documents. */

0 commit comments

Comments
 (0)