|
| 1 | +// Copyright © Luka S (JaffaKetchup) under GPL-v3 |
| 2 | +// A full license can be found at .\LICENSE |
| 3 | + |
| 4 | +import 'dart:async'; |
| 5 | +import 'dart:collection'; |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:io'; |
| 8 | +import 'dart:isolate'; |
| 9 | +import 'dart:math'; |
| 10 | + |
| 11 | +import 'package:collection/collection.dart'; |
| 12 | +import 'package:flutter/material.dart'; |
| 13 | +import 'package:flutter/services.dart'; |
| 14 | +import 'package:meta/meta.dart'; |
| 15 | +import 'package:path/path.dart' as path; |
| 16 | +import 'package:path_provider/path_provider.dart'; |
| 17 | + |
| 18 | +import '../../../../../../flutter_map_tile_caching.dart'; |
| 19 | +import '../../../../../misc/int_extremes.dart'; |
| 20 | +import '../../../../export_internal.dart'; |
| 21 | +import '../models/generated/objectbox.g.dart'; |
| 22 | +import '../models/src/recovery.dart'; |
| 23 | +import '../models/src/recovery_region.dart'; |
| 24 | +import '../models/src/root.dart'; |
| 25 | +import '../database/models/store.dart'; |
| 26 | +import '../models/src/tile.dart'; |
| 27 | + |
| 28 | +export 'package:objectbox/objectbox.dart' show StorageException; |
| 29 | + |
| 30 | +part 'internal_workers/standard/cmd_type.dart'; |
| 31 | +part 'internal_workers/standard/worker.dart'; |
| 32 | +part 'internal_workers/shared.dart'; |
| 33 | +part 'internal_workers/thread_safe.dart'; |
| 34 | +part 'internal.dart'; |
| 35 | + |
| 36 | +/// Implementation of [FMTCBackend] that uses ObjectBox as the storage database |
| 37 | +/// |
| 38 | +/// On web, this redirects to a no-op implementation that throws |
| 39 | +/// [UnsupportedError]s when attempting to use [initialise] or [uninitialise], |
| 40 | +/// and [RootUnavailable] when trying to use any other method. |
| 41 | +final class FMTCObjectBoxBackend implements FMTCBackend { |
| 42 | + /// {@macro fmtc.backend.initialise} |
| 43 | + /// |
| 44 | + /// --- |
| 45 | + /// |
| 46 | + /// [maxDatabaseSize] is the maximum size the database file can grow |
| 47 | + /// to, in KB. Exceeding it throws [DbFullException] (from |
| 48 | + /// 'package:objectbox') on write operations. Defaults to 10 GB (10000000 KB). |
| 49 | + /// |
| 50 | + /// [macosApplicationGroup] should be set when creating a sandboxed macOS app, |
| 51 | + /// specify the application group (of less than 20 chars). See |
| 52 | + /// [the ObjectBox docs](https://docs.objectbox.io/getting-started) for |
| 53 | + /// details. |
| 54 | + /// |
| 55 | + /// [rootIsolateToken] should only be used in exceptional circumstances where |
| 56 | + /// this backend is being initialised in a seperate isolate (or background) |
| 57 | + /// thread. |
| 58 | + /// |
| 59 | + /// Avoid using [useInMemoryDatabase] outside of testing purposes. |
| 60 | + @override |
| 61 | + Future<void> initialise({ |
| 62 | + String? rootDirectory, |
| 63 | + int maxDatabaseSize = 10000000, |
| 64 | + String? macosApplicationGroup, |
| 65 | + RootIsolateToken? rootIsolateToken, |
| 66 | + @visibleForTesting bool useInMemoryDatabase = false, |
| 67 | + }) => |
| 68 | + FMTCObjectBoxBackendInternal._instance.initialise( |
| 69 | + rootDirectory: rootDirectory, |
| 70 | + maxDatabaseSize: maxDatabaseSize, |
| 71 | + macosApplicationGroup: macosApplicationGroup, |
| 72 | + useInMemoryDatabase: useInMemoryDatabase, |
| 73 | + rootIsolateToken: rootIsolateToken, |
| 74 | + ); |
| 75 | + |
| 76 | + /// {@macro fmtc.backend.uninitialise} |
| 77 | + /// |
| 78 | + /// If [immediate] is `true`, any operations currently underway will be lost, |
| 79 | + /// as the worker will be killed as quickly as possible (not necessarily |
| 80 | + /// instantly). |
| 81 | + /// If `false`, all operations currently underway will be allowed to complete, |
| 82 | + /// but any operations started after this method call will be lost. |
| 83 | + @override |
| 84 | + Future<void> uninitialise({ |
| 85 | + bool deleteRoot = false, |
| 86 | + bool immediate = false, |
| 87 | + }) => |
| 88 | + FMTCObjectBoxBackendInternal._instance |
| 89 | + .uninitialise(deleteRoot: deleteRoot, immediate: immediate); |
| 90 | +} |
0 commit comments