importDatabaseLines function
Future<Database>
importDatabaseLines(
- List srcData,
- DatabaseFactory dstFactory,
- String dstPath, {
- SembastCodec? codec,
- List<
String> ? storeNames,
Import the exported data (using exportDatabaseLines) into a new database
An optional storeNames
can specify the list of stores to import. If null
All stores are exported.
Implementation
Future<Database> importDatabaseLines(
List srcData, DatabaseFactory dstFactory, String dstPath,
{SembastCodec? codec, List<String>? storeNames}) async {
if (srcData.isEmpty) {
throw const FormatException('invalid export format (empty)');
}
Object? metaMap = srcData.first;
if (metaMap is Map) {
_checkMeta(metaMap);
} else {
throw const FormatException('invalid export format header');
}
var mapSrcData = newModel();
metaMap.forEach((key, value) {
mapSrcData[key as String] = value;
});
String? currentStore;
var keys = <Object?>[];
var values = <Object?>[];
var stores = <Object?>[];
void closeCurrentStore() {
if (currentStore != null) {
if (keys.isNotEmpty) {
final storeExport = <String, Object?>{
_name: currentStore,
_keys: List<Object>.from(keys),
_values: List<Object>.from(values)
};
stores.add(storeExport);
keys.clear();
values.clear();
currentStore = null;
}
}
}
for (var line in srcData.skip(1)) {
if (line is Map) {
closeCurrentStore();
var storeName = line[_store]?.toString();
if (storeName != null) {
currentStore = storeName;
}
} else if (currentStore == null) {
// skipping
} else if (line is List && currentStore != null) {
if (line.length >= 2) {
var key = line[0];
var value = line[1];
if (key != null && value != null) {
keys.add(key);
values.add(value);
}
}
} else {
// skipping
}
}
closeCurrentStore();
mapSrcData[_stores] = stores;
return await importDatabase(mapSrcData, dstFactory, dstPath,
codec: codec, storeNames: storeNames);
}