Skip to content

Commit

Permalink
Fixed bug in bulk downloader (htWriteTile(s))
Browse files Browse the repository at this point in the history
Discovered bug in `rootSize` calculation
  • Loading branch information
JaffaKetchup committed Feb 28, 2024
1 parent 4153e1c commit 2745f0e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
33 changes: 27 additions & 6 deletions lib/src/backend/impls/objectbox/backend/internal_thread_safe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,30 @@ class _ObjectBoxBackendThreadSafeImpl implements FMTCBackendInternalThreadSafe {
(throw StoreNotExists(storeName: storeName));

if (existingTile == null) {
// If tile doesn't exist, just add to this store
storesToUpdate[storeName] = store
..length += 1
..size += bytes.lengthInBytes;
} else {
storesToUpdate[storeName] = store
..size += -existingTile.bytes.lengthInBytes + bytes.lengthInBytes;
// If tile exists in this store, just update size, otherwise
// length and size
// Also update size of all related stores
bool didContainAlready = false;

for (final relatedStore in existingTile.stores) {
if (relatedStore.name == storeName) didContainAlready = true;

storesToUpdate[relatedStore.name] =
(storesToUpdate[relatedStore.name] ?? relatedStore)
..size +=
-existingTile.bytes.lengthInBytes + bytes.lengthInBytes;
}

if (!didContainAlready) {
storesToUpdate[storeName] = store
..length += 1
..size += bytes.lengthInBytes;
}
}

tiles.put(
Expand Down Expand Up @@ -139,20 +150,30 @@ class _ObjectBoxBackendThreadSafeImpl implements FMTCBackendInternalThreadSafe {
.findUnique();

if (existingTile == null) {
storesToUpdate[storeName] = store
// If tile doesn't exist, just add to this store
storesToUpdate[storeName] = (storesToUpdate[storeName] ?? store)
..length += 1
..size += bytess[i].lengthInBytes;
} else {
storesToUpdate[storeName] = store
..size +=
-existingTile.bytes.lengthInBytes + bytess[i].lengthInBytes;
// If tile exists in this store, just update size, otherwise
// length and size
// Also update size of all related stores
bool didContainAlready = false;

for (final relatedStore in existingTile.stores) {
if (relatedStore.name == storeName) didContainAlready = true;

storesToUpdate[relatedStore.name] =
(storesToUpdate[relatedStore.name] ?? relatedStore)
..size += -existingTile.bytes.lengthInBytes +
bytess[i].lengthInBytes;
}

if (!didContainAlready) {
storesToUpdate[storeName] = store
..length += 1
..size += bytess[i].lengthInBytes;
}
}

return ObjectBoxTile(
Expand Down
7 changes: 3 additions & 4 deletions lib/src/backend/impls/objectbox/backend/internal_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ Future<void> _worker(
},
);
case _WorkerCmdType.rootSize:
// TODO: Invalid, considers ALL tiles (related ones multiple times), not UNIQUE tiles
final query = root
.box<ObjectBoxStore>()
.query()
Expand Down Expand Up @@ -583,10 +584,8 @@ Future<void> _worker(
sendRes(
id: cmd.id,
data: {
'numOrphans': deleteTiles(
storeName: storeName,
tilesQuery: tilesQuery,
),
'numOrphans':
deleteTiles(storeName: storeName, tilesQuery: tilesQuery),
},
);

Expand Down

0 comments on commit 2745f0e

Please sign in to comment.