Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
JaffaKetchup committed Mar 31, 2024
1 parent 522205d commit c48ba68
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 164 deletions.
12 changes: 7 additions & 5 deletions example/lib/screens/export_import/components/import.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ class Import extends StatefulWidget {
super.key,
required this.path,
required this.changeForceOverrideExisting,
required this.conflictStrategy,
required this.changeConflictStrategy,
});

final String path;
final void Function({required bool forceOverrideExisting})
changeForceOverrideExisting;

final ImportConflictStrategy conflictStrategy;
final void Function(ImportConflictStrategy) changeConflictStrategy;

@override
State<Import> createState() => _ImportState();
}
Expand All @@ -24,8 +29,6 @@ class _ImportState extends State<Import> {
late Future<List<String>> importableStores =
FMTCRoot.external(pathToArchive: widget.path).listStores;

ImportConflictStrategy selectedConflictStrategy = ImportConflictStrategy.skip;

@override
void didUpdateWidget(covariant Import oldWidget) {
super.didUpdateWidget(oldWidget);
Expand Down Expand Up @@ -163,7 +166,7 @@ class _ImportState extends State<Import> {
width: double.infinity,
child: DropdownButton(
isExpanded: true,
value: selectedConflictStrategy,
value: widget.conflictStrategy,
items: _conflictStrategies
.map(
(e) => DropdownMenuItem(
Expand Down Expand Up @@ -197,8 +200,7 @@ class _ImportState extends State<Import> {
),
)
.toList(growable: false),
onChanged: (choice) =>
setState(() => selectedConflictStrategy = choice!),
onChanged: (choice) => widget.changeConflictStrategy(choice!),
),
),
],
Expand Down
9 changes: 8 additions & 1 deletion example/lib/screens/export_import/export_import.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class _ExportImportPopupState extends State<ExportImportPopup> {
final selectedStores = <String>{};
Future<FileSystemEntityType>? typeOfPath;
bool forceOverrideExisting = false;
ImportConflictStrategy selectedConflictStrategy = ImportConflictStrategy.skip;
bool isProcessing = false;

void onPathChanged({required bool forceOverrideExisting}) => setState(() {
Expand Down Expand Up @@ -111,6 +112,10 @@ class _ExportImportPopupState extends State<ExportImportPopup> {
child: Import(
path: pathController.text,
changeForceOverrideExisting: onPathChanged,
conflictStrategy: selectedConflictStrategy,
changeConflictStrategy: (c) => setState(
() => selectedConflictStrategy = c,
),
),
);
},
Expand Down Expand Up @@ -197,7 +202,9 @@ class _ExportImportPopupState extends State<ExportImportPopup> {
final stopwatch = Stopwatch()..start();
await FMTCRoot.external(
pathToArchive: pathController.text,
).import();
).import(
strategy: selectedConflictStrategy,
);
stopwatch.stop();
if (context.mounted) {
final elapsedTime =
Expand Down
5 changes: 2 additions & 3 deletions lib/src/backend/impls/objectbox/backend/internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,10 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
}) async {
await _checkImportPathType(path);

final res = (await _sendCmdOneShot(
_sendCmdStreamed(
type: _WorkerCmdType.importStores,
args: {'path': path, 'strategy': strategy, 'stores': storeNames},
))!;
print(res);
).listen(print);

return (
stores: Future.sync(
Expand Down
161 changes: 6 additions & 155 deletions lib/src/backend/impls/objectbox/backend/internal_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ Future<void> _worker(

if (hasConflict) return false;

root.box<ObjectBoxStore>().putQueued(
root.box<ObjectBoxStore>().put(
ObjectBoxStore(
name: name,
length: importingStore.length,
Expand All @@ -1073,7 +1073,7 @@ Future<void> _worker(
.count() ==
0) {
nameToState[name] = name;
root.box<ObjectBoxStore>().putQueued(
root.box<ObjectBoxStore>().put(
ObjectBoxStore(
name: name,
length: importingStore.length,
Expand All @@ -1087,12 +1087,12 @@ Future<void> _worker(
return importingStore;
} else {
final newName =
nameToState[name] = name + DateTime.now().toString();
nameToState[name] = '$name [Imported ${DateTime.now()}]';
final newStore = importingStore..name = newName;
importingRoot
.box<ObjectBoxStore>()
.put(newStore, mode: PutMode.update);
root.box<ObjectBoxStore>().putQueued(
root.box<ObjectBoxStore>().put(
ObjectBoxStore(
name: newName,
length: importingStore.length,
Expand All @@ -1116,7 +1116,7 @@ Future<void> _worker(
..param(ObjectBoxStore_.name).value = name)
.count() ==
0) {
root.box<ObjectBoxStore>().putQueued(
root.box<ObjectBoxStore>().put(
ObjectBoxStore(
name: name,
length: importingStore.length,
Expand Down Expand Up @@ -1166,10 +1166,6 @@ Future<void> _worker(
})
.then(
(storesToImport) {
if (!root.awaitQueueSubmitted()) {
throw StateError('Store import queue failed unexpectedly');
}

sendRes(
id: cmd.id,
data: {
Expand All @@ -1194,152 +1190,7 @@ Future<void> _worker(
},
);

/*final storesQuery = importingRoot.box<ObjectBoxStore>().query().build();
final tilesQuery = importingRoot.box<ObjectBoxTile>().query().build();
final specificStoresQuery = root
.box<ObjectBoxStore>()
.query(ObjectBoxStore_.name.equals(''))
.build();
final specificTilesQuery = root
.box<ObjectBoxTile>()
.query(ObjectBoxTile_.url.equals(''))
.build();
final storesToUpdate = <String, ObjectBoxStore>{};
final storesObjectsForRelations = <String, ObjectBoxStore>{};
final storesToSkipTiles = <String>[];
int rootDeltaLength = 0;
int rootDeltaSize = 0;
final importingStores = importingRoot.runInTransaction(
TxMode.read,
storesQuery.stream,
);
root
.runInTransaction(
TxMode.write,
() => importingStores.listen(
(importingStore) {
if (!storesToImport.contains(importingStore.name)) {
storesToSkipTiles.add(importingStore.name);
return;
}
final existingStore = (specificStoresQuery
..param(ObjectBoxStore_.name).value =
importingStore.name)
.findUnique();
if (existingStore == null) {
sendRes(
id: cmd.id,
data: {
'expectStream': true,
'storeName': importingStore.name,
'newStoreName': null,
'conflict': false,
},
);
root.box<ObjectBoxStore>().put(
storesObjectsForRelations[importingStore.name] =
ObjectBoxStore(
name: importingStore.name,
length: importingStore.length,
size: importingStore.size,
hits: importingStore.hits,
misses: importingStore.misses,
metadataJson: importingStore.metadataJson,
),
mode: PutMode.insert,
);
return;
}
if (strategy == ImportConflictStrategy.skip) {
storesToSkipTiles.add(importingStore.name);
sendRes(
id: cmd.id,
data: {
'expectStream': true,
'storeName': importingStore.name,
'newStoreName': null,
'conflict': true,
},
);
return;
}
String? newName;
if (strategy == ImportConflictStrategy.rename) {
newName =
'${existingStore.name} (Imported ${DateTime.now()})';
importingRoot.box<ObjectBoxStore>().put(
importingStore..name = newName,
mode: PutMode.update,
);
}
sendRes(
id: cmd.id,
data: {
'expectStream': true,
'storeName': importingStore.name,
'newStoreName': newName,
'conflict': true,
},
);
if (strategy == ImportConflictStrategy.replace) {
final tilesQuery = (root.box<ObjectBoxTile>().query()
..linkMany(
ObjectBoxTile_.stores,
ObjectBoxStore_.name.equals(existingStore.name),
))
.build();
deleteTiles(
storeName: existingStore.name,
tilesQuery: tilesQuery,
);
root.box<ObjectBoxStore>().remove(existingStore.id);
tilesQuery.close();
}
if (strategy != ImportConflictStrategy.merge) {
root.box<ObjectBoxStore>().put(
storesObjectsForRelations[importingStore.name] =
ObjectBoxStore(
name: newName ?? importingStore.name,
length: importingStore.length,
size: importingStore.size,
hits: importingStore.hits,
misses: importingStore.misses,
metadataJson: importingStore.metadataJson,
),
mode: PutMode.insert,
);
}
},
),
)
.asFuture<void>()
.then(
(_) {
sendRes(
id: cmd.id,
data: {'expectStream': true, 'tiles': null},
);
final importingTiles = importingRoot.runInTransaction(
TxMode.read,
tilesQuery.stream,
);
/*
root
.runInTransaction(
TxMode.write,
Expand Down

0 comments on commit c48ba68

Please sign in to comment.