Skip to content

Commit

Permalink
Added support for ImportConflictStrategy.replace
Browse files Browse the repository at this point in the history
  • Loading branch information
JaffaKetchup committed Apr 2, 2024
1 parent ad693fa commit 49c7c76
Showing 1 changed file with 71 additions and 53 deletions.
124 changes: 71 additions & 53 deletions lib/src/backend/impls/objectbox/backend/internal_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1175,39 +1175,7 @@ Future<void> _worker(

return name;
},
).toList()
/*.then(
(importingStoreNames) {
final storesQuery = root
.box<ObjectBoxStore>()
.query(ObjectBoxStore_.name.oneOf(importingStoreNames))
.build();
final tilesQuery = (root.box<ObjectBoxTile>().query()
..linkMany(
ObjectBoxTile_.stores,
ObjectBoxStore_.name.oneOf(importingStoreNames),
))
.build();
root.runInTransaction(
TxMode.write,
() {
deleteTiles(
storesQuery: storesQuery,
tilesQuery: tilesQuery,
);
storesQuery.remove();
},
);
storesQuery.close();
tilesQuery.close();
return importingStoreNames;
},
)*/
,
).toList(),
})
.then(
(storesToImport) {
Expand All @@ -1231,16 +1199,14 @@ Future<void> _worker(
// It is important never to 'copy' from the import root to the
// in-use root

if (strategy == ImportConflictStrategy.skip ||
strategy == ImportConflictStrategy.rename) {
if (strategy != ImportConflictStrategy.merge) {
final importingTilesQuery =
(importingRoot.box<ObjectBoxTile>().query()
..linkMany(
ObjectBoxTile_.stores,
ObjectBoxStore_.name.oneOf(storesToImport),
))
.build();

final importingTiles = importingTilesQuery.stream();

final existingStoresQuery = root
Expand Down Expand Up @@ -1271,8 +1237,57 @@ Future<void> _worker(
root
.runInTransaction(
TxMode.write,
() => importingTiles.map(
(importingTile) {
() {
if (strategy == ImportConflictStrategy.replace) {
final storesQuery = root
.box<ObjectBoxStore>()
.query(
ObjectBoxStore_.name.oneOf(storesToImport),
)
.build();
final tilesQuery = (root.box<ObjectBoxTile>().query()
..linkMany(
ObjectBoxTile_.stores,
ObjectBoxStore_.name.oneOf(storesToImport),
))
.build();

deleteTiles(
storesQuery: storesQuery,
tilesQuery: tilesQuery,
);

final importingStoresQuery = importingRoot
.box<ObjectBoxStore>()
.query(ObjectBoxStore_.name.oneOf(storesToImport))
.build();

final importingStores = importingStoresQuery.find();

storesQuery.remove();

root.box<ObjectBoxStore>().putMany(
List.generate(
importingStores.length,
(i) => ObjectBoxStore(
name: importingStores[i].name,
length: importingStores[i].length,
size: importingStores[i].size,
hits: importingStores[i].hits,
misses: importingStores[i].misses,
metadataJson: importingStores[i].metadataJson,
),
growable: false,
),
mode: PutMode.insert,
);

storesQuery.close();
tilesQuery.close();
importingStoresQuery.close();
}

return importingTiles.map((importingTile) {
final existingTile = (existingTilesQuery
..param(ObjectBoxTile_.url).value =
importingTile.url)
Expand Down Expand Up @@ -1335,27 +1350,30 @@ Future<void> _worker(
importingTile.bytes.lengthInBytes;

return 1;
},
),
});
},
)
.where((e) => e != null)
.length
.then((numImportedTiles) {
updateRootStatistics(
deltaLength: rootDeltaLength,
deltaSize: rootDeltaSize,
);
updateRootStatistics(
deltaLength: rootDeltaLength,
deltaSize: rootDeltaSize,
);

importingTilesQuery.close();
existingStoresQuery.close();
existingTilesQuery.close();
cleanup();
importingTilesQuery.close();
existingStoresQuery.close();
existingTilesQuery.close();
cleanup();

sendRes(
id: cmd.id,
data: {'expectStream': true, 'complete': numImportedTiles},
);
});
sendRes(
id: cmd.id,
data: {
'expectStream': true,
'complete': numImportedTiles,
},
);
});
} else {
throw UnimplementedError();
}
Expand Down

0 comments on commit 49c7c76

Please sign in to comment.