|
|
@@ -393,44 +393,15 @@ class FileProcess {
|
|
|
.inFilter(scopeName, e)
|
|
|
.limit(100000);
|
|
|
|
|
|
- // oldIds.addAll(res.map((e) => filterMapByKeys(e, ["id"])).toList());
|
|
|
- // oldComparable.addAll(res
|
|
|
- // .map((e) => filterMapByKeys(e, _headers[filename] ?? []))
|
|
|
- // .toList());
|
|
|
oldIds.addAll(res.map((e) => {"id": e["id"]}));
|
|
|
oldComparable.addAll(res.map((e) => e..remove("id")));
|
|
|
}
|
|
|
|
|
|
- List<int> indexToRemove = [];
|
|
|
- List<int> indexToMaintain = [];
|
|
|
+ final comparisonResult = compareLists(oldComparable, mapsToInsert);
|
|
|
+ final indexToRemove = comparisonResult.removeIndices;
|
|
|
+ final indexToMaintain = comparisonResult.maintainIndices;
|
|
|
+ final indexToInsert = comparisonResult.insertIndices;
|
|
|
|
|
|
- final keys2check = idToRemove[filename] ?? [];
|
|
|
-
|
|
|
- for (int i = 0; i < oldComparable.length; i++) {
|
|
|
- final item = oldComparable[i];
|
|
|
- final index = findIndex(mapsToInsert, item);
|
|
|
- if (index > -1) {
|
|
|
- indexToMaintain.add(i);
|
|
|
- mapsToInsert.removeAt(index);
|
|
|
- } else {
|
|
|
- // final mawjoudin = oldComparable.fold(<Map<String, dynamic>>[], (t, e) {
|
|
|
- // return t..add(filterMapByKeys(e, keys2check));
|
|
|
- // });
|
|
|
-
|
|
|
- // final mawjood = mawjoudin
|
|
|
- // .firstWhere((e) => mapEquals(filterMapByKeys(item, keys2check), e),
|
|
|
- // orElse: () => {})
|
|
|
- // .keys
|
|
|
- // .isNotEmpty;
|
|
|
-
|
|
|
- // if (mawjood) {
|
|
|
- // indexToRemove.add(i);
|
|
|
- // } else {
|
|
|
- // print("saved item: $item");
|
|
|
- // }
|
|
|
- indexToRemove.add(i);
|
|
|
- }
|
|
|
- }
|
|
|
try {
|
|
|
// removing index to remove with id
|
|
|
for (var e in splitList(
|
|
|
@@ -445,64 +416,45 @@ class FileProcess {
|
|
|
// insering new data
|
|
|
await supabase
|
|
|
.from(tables[filename]!) // Replace with your actual table name
|
|
|
- .insert(mapsToInsert);
|
|
|
+ .insert(indexToInsert.map((e) => mapsToInsert.elementAt(e)).toList());
|
|
|
} catch (e, stackTrace) {
|
|
|
print('Error: $e\n$stackTrace');
|
|
|
}
|
|
|
print(
|
|
|
- " insert:${mapsToInsert.length} remove:${indexToRemove.length} maintain:${indexToMaintain.length}");
|
|
|
+ " insert:${indexToInsert.length} remove:${indexToRemove.length} maintain:${indexToMaintain.length}");
|
|
|
}
|
|
|
|
|
|
- bool mapEquals<T, U>(Map<T, U>? a, Map<T, U>? b) {
|
|
|
- if (a == null) {
|
|
|
- return b == null;
|
|
|
- }
|
|
|
- if (b == null || a.length != b.length) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (identical(a, b)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- for (final T key in a.keys) {
|
|
|
- if (!b.containsKey(key) || b[key] != a[key]) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
-/*
|
|
|
- bool mapEquals(Map<String, dynamic> map1, Map<String, dynamic> map2) {
|
|
|
- //if (map1.length != map2.length) return false;
|
|
|
- for (var key in map1.keys) {
|
|
|
- if (map1[key] != map2[key]) return false;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-*/
|
|
|
- int findIndex(List<dynamic> list, dynamic element) {
|
|
|
- for (int i = 0; i < list.length; i++) {
|
|
|
- if (mapEquals(list[i], element)) {
|
|
|
- return i;
|
|
|
+ ({
|
|
|
+ List<int> maintainIndices,
|
|
|
+ List<int> removeIndices,
|
|
|
+ List<int> insertIndices
|
|
|
+ }) compareLists(
|
|
|
+ List<Map<String, dynamic>> map1, List<Map<String, dynamic>> map2) {
|
|
|
+ List<int> maintainIndices = [];
|
|
|
+ List<int> removeIndices = [];
|
|
|
+ List<int> insertIndices = [];
|
|
|
+
|
|
|
+ // Find indices to maintain and remove in map1
|
|
|
+ for (int i = 0; i < map1.length; i++) {
|
|
|
+ if (map2.contains(map1[i])) {
|
|
|
+ maintainIndices.add(i); // Item exists in both lists
|
|
|
+ } else {
|
|
|
+ removeIndices.add(i); // Item does not exist in map2
|
|
|
}
|
|
|
}
|
|
|
- return -1; // Return -1 if the element is not found
|
|
|
- }
|
|
|
|
|
|
- Map<String, dynamic> filterMapByKeys(
|
|
|
- Map<String, dynamic> originalMap, List<String> keysToInclude) {
|
|
|
- // Create a new map to hold the filtered results
|
|
|
- Map<String, dynamic> filteredMap = {};
|
|
|
-
|
|
|
- // Iterate through the list of keys to include
|
|
|
- for (String key in keysToInclude) {
|
|
|
- // Check if the key exists in the original map
|
|
|
- if (originalMap.containsKey(key)) {
|
|
|
- filteredMap[key] = originalMap[key]; // Add to the new map
|
|
|
+ // Find indices to insert from map2
|
|
|
+ for (int i = 0; i < map2.length; i++) {
|
|
|
+ if (!map1.contains(map2[i])) {
|
|
|
+ insertIndices.add(i); // Item does not exist in map1
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return filteredMap;
|
|
|
+ return (
|
|
|
+ maintainIndices: maintainIndices,
|
|
|
+ removeIndices: removeIndices,
|
|
|
+ insertIndices: insertIndices
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
List<List<T>> splitList<T>(List<T> originalList, int maxSize) {
|