Skip to content

Commit

Permalink
Finished ObjectBoxBackend implementation of recovery system and int…
Browse files Browse the repository at this point in the history
…egration

Updated & removed dependencies
Removed internal `_WithBackendAccess`
  • Loading branch information
JaffaKetchup committed Feb 20, 2024
1 parent 37ef5e5 commit 797b34f
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 294 deletions.
5 changes: 0 additions & 5 deletions lib/flutter_map_tile_caching.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';
import 'package:latlong2/latlong.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'package:stream_transform/stream_transform.dart';
import 'package:watcher/watcher.dart';

import 'src/backend/export_internal.dart';
import 'src/bulk_download/instance.dart';
Expand All @@ -50,7 +47,6 @@ part 'src/bulk_download/thread.dart';
part 'src/bulk_download/tile_event.dart';
part 'src/fmtc.dart';
part 'src/misc/deprecations.dart';
part 'src/misc/with_backend_access.dart';
part 'src/providers/tile_provider.dart';
part 'src/regions/base_region.dart';
part 'src/regions/circle.dart';
Expand All @@ -61,7 +57,6 @@ part 'src/regions/recovered_region.dart';
part 'src/regions/rectangle.dart';
part 'src/root/directory.dart';
part 'src/root/import.dart';
part 'src/root/migrator.dart';
part 'src/root/recovery.dart';
part 'src/root/statistics.dart';
part 'src/providers/tile_provider_settings.dart';
Expand Down
28 changes: 22 additions & 6 deletions lib/src/backend/impls/objectbox/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'dart:isolate';

import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';

Expand Down Expand Up @@ -202,10 +204,6 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
FMTCBackendAccess.internal = null;
}

@override
Future<List<String>> listStores() async =>
(await _sendCmd(type: _WorkerCmdType.storeExists))!['stores'];

@override
Future<double> rootSize() async =>
(await _sendCmd(type: _WorkerCmdType.rootSize))!['size'];
Expand All @@ -214,6 +212,10 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
Future<int> rootLength() async =>
(await _sendCmd(type: _WorkerCmdType.rootLength))!['length'];

@override
Future<List<String>> listStores() async =>
(await _sendCmd(type: _WorkerCmdType.storeExists))!['stores'];

@override
Future<bool> storeExists({
required String storeName,
Expand Down Expand Up @@ -429,6 +431,20 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
args: {'storeName': storeName},
);

@override
Future<List<RecoveredRegion>> listRecoverableRegions() async =>
(await _sendCmd(
type: _WorkerCmdType.listRecoverableRegions,
))!['recoverableRegions'];

@override
Future<RecoveredRegion> getRecoverableRegion({
required int id,
}) async =>
(await _sendCmd(
type: _WorkerCmdType.getRecoverableRegion,
))!['recoverableRegion'];

@override
Future<void> startRecovery({
required int id,
Expand All @@ -441,8 +457,8 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
);

@override
Future<void> endRecovery({
Future<void> cancelRecovery({
required int id,
}) =>
_sendCmd(type: _WorkerCmdType.endRecovery, args: {'id': id});
_sendCmd(type: _WorkerCmdType.cancelRecovery, args: {'id': id});
}
81 changes: 66 additions & 15 deletions lib/src/backend/impls/objectbox/worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ part of 'backend.dart';
enum _WorkerCmdType {
initialise_, // Only valid as a response
destroy_, // Only valid as a request
listStores,
rootSize,
rootLength,
listStores,
storeExists,
createStore,
resetStore,
Expand All @@ -27,8 +27,10 @@ enum _WorkerCmdType {
setBulkMetadata,
removeMetadata,
resetMetadata,
listRecoverableRegions,
getRecoverableRegion,
startRecovery,
endRecovery,
cancelRecovery,
}

Future<void> _worker(
Expand Down Expand Up @@ -137,18 +139,6 @@ Future<void> _worker(

// TODO: Consider final message
Isolate.exit();
case _WorkerCmdType.listStores:
final query = root
.box<ObjectBoxStore>()
.query()
.build()
.property(ObjectBoxStore_.name);

sendRes(id: cmd.id, data: {'stores': query.find()});

query.close();

break;
case _WorkerCmdType.rootSize:
final query = root
.box<ObjectBoxStore>()
Expand All @@ -171,6 +161,15 @@ Future<void> _worker(

query.close();

break;
case _WorkerCmdType.listStores:
sendRes(
id: cmd.id,
data: {
'stores': root.box<ObjectBoxStore>().getAll().map((e) => e.name),
},
);

break;
case _WorkerCmdType.storeExists:
final query = root
Expand Down Expand Up @@ -685,6 +684,58 @@ Future<void> _worker(

sendRes(id: cmd.id);

break;
case _WorkerCmdType.listRecoverableRegions:
sendRes(
id: cmd.id,
data: {
'recoverableRegions': root.box<ObjectBoxRecovery>().getAll().map(
(r) => RecoveredRegion(
id: r.id,
storeName: r.storeName,
time: r.creationTime,
bounds: r.typeId == 0
? LatLngBounds(
LatLng(r.rectNwLat!, r.rectNwLng!),
LatLng(r.rectSeLat!, r.rectSeLng!),
)
: null,
center: r.typeId == 1
? LatLng(r.circleCenterLat!, r.circleCenterLng!)
: null,
line: r.typeId == 2 || r.typeId == 3
? List.generate(
r.lineLats!.length,
(i) => LatLng(r.lineLats![i], r.lineLngs![i]),
)
: null,
radius: r.typeId == 1
? r.circleRadius!
: r.typeId == 2
? r.lineRadius!
: null,
minZoom: r.minZoom,
maxZoom: r.maxZoom,
start: r.startTile,
end: r.endTile,
),
),
},
);

break;
case _WorkerCmdType.getRecoverableRegion:
final id = cmd.args['id']! as int;

final query = root
.box<ObjectBoxRecovery>()
.query(ObjectBoxRecovery_.refId.equals(id))
.build();

sendRes(id: cmd.id, data: {'recoverableRegion': query.findUnique()});

query.close();

break;
case _WorkerCmdType.startRecovery:
final id = cmd.args['id']! as int;
Expand All @@ -702,7 +753,7 @@ Future<void> _worker(
sendRes(id: cmd.id);

break;
case _WorkerCmdType.endRecovery:
case _WorkerCmdType.cancelRecovery:
root
.box<ObjectBoxRecovery>()
.query(ObjectBoxRecovery_.refId.equals(cmd.args['id']! as int))
Expand Down
31 changes: 25 additions & 6 deletions lib/src/backend/interfaces/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ abstract interface class FMTCBackendInternal with FMTCBackendAccess {
/// initialised.
Directory? get rootDirectory;

/// {@template fmtc.backend.listStores}
/// List all the available stores
/// {@endtemplate}
Future<List<String>> listStores();

/// {@template fmtc.backend.rootSize}
/// Retrieve the total number of KiBs of all tiles' bytes (not 'real total'
/// size) from all stores
Expand All @@ -90,6 +85,11 @@ abstract interface class FMTCBackendInternal with FMTCBackendAccess {
/// {@endtemplate}
Future<int> rootLength();

/// {@template fmtc.backend.listStores}
/// List all the available stores
/// {@endtemplate}
Future<List<String>> listStores();

/// {@template fmtc.backend.storeExists}
/// Check whether the specified store currently exists
/// {@endtemplate}
Expand Down Expand Up @@ -274,13 +274,32 @@ abstract interface class FMTCBackendInternal with FMTCBackendAccess {
required String storeName,
});

/// List all registered recovery regions
///
/// Not all regions are failed, requires the [RootRecovery] object to
/// determine this.
Future<List<RecoveredRegion>> listRecoverableRegions();

/// Retrieve the specified registered recovery region
///
/// Not all regions are failed, requires the [RootRecovery] object to
/// determine this.
Future<RecoveredRegion> getRecoverableRegion({
required int id,
});

/// Create a recovery store with a recoverable region from the specified
/// components
Future<void> startRecovery({
required int id,
required String storeName,
required DownloadableRegion region,
});

Future<void> endRecovery({
/// {@template fmtc.backend.cancelRecovery}
/// Safely cancel the specified recoverable region
/// {@endtemplate}
Future<void> cancelRecovery({
required int id,
});
}
12 changes: 0 additions & 12 deletions lib/src/misc/with_backend_access.dart

This file was deleted.

Loading

0 comments on commit 797b34f

Please sign in to comment.