Skip to content

Commit

Permalink
Added removeOldestTilesAboveLimit & removeTilesOlderThan to backend
Browse files Browse the repository at this point in the history
Removed apparently useless `removeOldestTile` from backend (with debouncing mechanism)
Switched to `DateTime.timestamp` from `now` when backend tiles are concerned to avoid timezone bugs
Refactored part of `deleteTiles` backend method into independent method
Improved documentation
  • Loading branch information
JaffaKetchup committed Jan 1, 2024
1 parent c2e2a66 commit 5d5ce62
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 182 deletions.
56 changes: 37 additions & 19 deletions lib/src/backend/impls/objectbox/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ class _ObjectBoxBackendImpl implements ObjectBoxBackendInternal {
late Completer<void> _workerComplete;
late StreamSubscription<dynamic> _workerHandler;

// `deleteOldestTile` tracking & debouncing
late int _dotLength;
late Timer _dotDebouncer;
late String? _dotStore;
// TODO: Verify if necessary and remove if not
//`removeOldestTilesAboveLimit` tracking & debouncing
//late int _rotalLength;
//late Timer _rotalDebouncer;
//late String? _rotalStore;

Future<Map<String, dynamic>?> _sendCmd({
required _WorkerCmdType type,
Expand Down Expand Up @@ -93,8 +94,8 @@ class _ObjectBoxBackendImpl implements ObjectBoxBackendInternal {
// Reset non-comms-related non-resource-intensive state
_workerId = 0;
_workerRes.clear();
_dotStore = null;
_dotLength = 0;
//_rotalStore = null;
//_rotalLength = 0;

// Prepare to recieve `SendPort` from worker
_workerRes[0] = Completer();
Expand Down Expand Up @@ -157,7 +158,7 @@ class _ObjectBoxBackendImpl implements ObjectBoxBackendInternal {
// Resource-intensive state cleanup only (other cleanup done during init)
_sendPort = null; // Indicate ready for re-init
await _workerHandler.cancel();
_dotDebouncer.cancel();
//_rotalDebouncer.cancel();

print('passed _workerHandler cancel');

Expand Down Expand Up @@ -281,13 +282,19 @@ class _ObjectBoxBackendImpl implements ObjectBoxBackendInternal {
(await _sendCmd(
type: _WorkerCmdType.deleteStore,
args: {'storeName': storeName, 'url': url},
))!['wasOrphaned'];
))!['wasOrphan'];

@override
Future<void> removeOldestTile({
Future<int> removeOldestTilesAboveLimit({
required String storeName,
required int numToRemove,
}) async {
required int tilesLimit,
}) async =>
(await _sendCmd(
type: _WorkerCmdType.removeOldestTilesAboveLimit,
args: {'storeName': storeName, 'tilesLimit': tilesLimit},
))!['numOrphans'];

/* FOR ABOVE METHOD
// Attempts to avoid flooding worker with requests to delete oldest tile,
// and 'batches' them instead
Expand Down Expand Up @@ -315,13 +322,24 @@ class _ObjectBoxBackendImpl implements ObjectBoxBackendInternal {
_dotDebouncer =
Timer(const Duration(seconds: 1), () => _sendROTCmd(storeName));
_dotLength += numToRemove;
}
void _sendROTCmd(String storeName) {
_sendCmd(
type: _WorkerCmdType.removeOldestTile,
args: {'storeName': storeName, 'number': _dotLength},
);
_dotLength = 0;
}
// may need to be moved out
void _sendROTCmd(String storeName) {
_sendCmd(
type: _WorkerCmdType.removeOldestTile,
args: {'storeName': storeName, 'number': _dotLength},
);
_dotLength = 0;
}
*/

@override
Future<int> removeTilesOlderThan({
required String storeName,
required DateTime expiry,
}) async =>
(await _sendCmd(
type: _WorkerCmdType.removeTilesOlderThan,
args: {'storeName': storeName, 'expiry': expiry},
))!['numOrphans'];
}
Loading

0 comments on commit 5d5ce62

Please sign in to comment.