feat: support HiveCollections as Database provider

- support Hive's new `CollectionBox` and `BoxCollection`

Signed-off-by: Lanna Michalke <l.michalke@famedly.com>
This commit is contained in:
Lanna Michalke 2022-05-17 11:41:02 +02:00
parent edec008bbe
commit 8fd3bbe28c
8 changed files with 1541 additions and 50 deletions

View File

@ -25,6 +25,7 @@ export 'src/client.dart';
export 'src/database/database_api.dart';
export 'src/database/hive_database.dart';
export 'src/database/fluffybox_database.dart';
export 'src/database/hive_collections_database.dart';
export 'src/event.dart';
export 'src/presence.dart';
export 'src/event_status.dart';

View File

@ -32,6 +32,8 @@ import 'package:matrix/src/utils/queued_to_device_event.dart';
import 'package:matrix/src/utils/run_benchmarked.dart';
/// This database does not support file caching!
@Deprecated(
'Use [HiveCollectionsDatabase] instead. Don\'t forget to properly migrate!')
class FluffyBoxDatabase extends DatabaseApi {
static const int version = 6;
final String name;
@ -1457,46 +1459,3 @@ class FluffyBoxDatabase extends DatabaseApi {
return raw;
}
}
class TupleKey {
final List<String> parts;
TupleKey(String key1, [String? key2, String? key3])
: parts = [
key1,
if (key2 != null) key2,
if (key3 != null) key3,
];
const TupleKey.byParts(this.parts);
TupleKey.fromString(String multiKeyString)
: parts = multiKeyString.split('|').toList();
@override
String toString() => parts.join('|');
@override
bool operator ==(other) => parts.toString() == other.toString();
}
dynamic _castValue(dynamic value) {
if (value is Map) {
return copyMap(value);
}
if (value is List) {
return value.map(_castValue).toList();
}
return value;
}
/// The store always gives back an `_InternalLinkedHasMap<dynamic, dynamic>`. This
/// creates a deep copy of the json and makes sure that the format is always
/// `Map<String, dynamic>`.
Map<String, dynamic> copyMap(Map map) {
final copy = Map<String, dynamic>.from(map);
for (final entry in copy.entries) {
copy[entry.key] = _castValue(entry.value);
}
return copy;
}

File diff suppressed because it is too large Load Diff

View File

@ -35,6 +35,8 @@ import 'package:matrix/src/utils/run_benchmarked.dart';
/// you use this.
///
/// This database does not support file caching!
@Deprecated(
'Use [HiveCollectionsDatabase] instead. Don\'t forget to properly migrate!')
class FamedlySdkHiveDatabase extends DatabaseApi {
static const int version = 5;
final String name;

View File

@ -19,7 +19,7 @@ dependencies:
base58check: ^2.0.0
olm: ^2.0.0
matrix_api_lite: ^1.0.0
hive: ^2.0.4
hive: ^2.2.1
image: ^3.1.1
ffi: ^1.0.0
js: ^0.6.3

View File

@ -26,16 +26,16 @@ import 'package:olm/olm.dart' as olm;
import 'fake_database.dart';
void main() {
group('HiveCollections Database Test', () {
testDatabase(
getHiveCollectionsDatabase(null),
);
});
group('FluffyBox Database Test', () {
testDatabase(
getFluffyBoxDatabase(null),
);
});
group('Hive Database Test', () {
testDatabase(
getHiveDatabase(null),
);
});
}
Future<bool> olmEnabled() async {

View File

@ -23,15 +23,33 @@ import 'package:matrix/matrix.dart';
import 'package:file/memory.dart';
import 'package:hive/hive.dart';
Future<DatabaseApi> getDatabase(Client? _) => getHiveDatabase(_);
Future<DatabaseApi> getDatabase(Client? _) => getHiveCollectionsDatabase(_);
bool hiveInitialized = false;
Future<HiveCollectionsDatabase> getHiveCollectionsDatabase(Client? c) async {
final fileSystem = MemoryFileSystem();
final testHivePath =
'${fileSystem.path}/build/.test_store/${Random().nextDouble()}';
if (!hiveInitialized) {
Directory(testHivePath).createSync(recursive: true);
Hive.init(testHivePath);
}
final db = HiveCollectionsDatabase(
'unit_test.${c?.hashCode}',
testHivePath,
);
await db.open();
return db;
}
// ignore: deprecated_member_use_from_same_package
Future<FluffyBoxDatabase> getFluffyBoxDatabase(Client? c) async {
final fileSystem = MemoryFileSystem();
final testHivePath =
'${fileSystem.path}/build/.test_store/${Random().nextDouble()}';
Directory(testHivePath).createSync(recursive: true);
// ignore: deprecated_member_use_from_same_package
final db = FluffyBoxDatabase(
'unit_test.${c?.hashCode}',
testHivePath,
@ -40,6 +58,7 @@ Future<FluffyBoxDatabase> getFluffyBoxDatabase(Client? c) async {
return db;
}
// ignore: deprecated_member_use_from_same_package
Future<FamedlySdkHiveDatabase> getHiveDatabase(Client? c) async {
if (!hiveInitialized) {
final fileSystem = MemoryFileSystem();
@ -49,6 +68,7 @@ Future<FamedlySdkHiveDatabase> getHiveDatabase(Client? c) async {
Hive.init(testHivePath);
hiveInitialized = true;
}
// ignore: deprecated_member_use_from_same_package
final db = FamedlySdkHiveDatabase('unit_test.${c?.hashCode}');
await db.open();
return db;

View File

@ -15,6 +15,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'package:hive/hive.dart';
import 'package:matrix/matrix.dart';
import '../test/fake_database.dart';
import 'test_config.dart';
@ -32,6 +33,8 @@ void test() async {
Client? testClientA, testClientB;
try {
Hive.init(null);
await olm.init();
olm.Account();
Logs().i('[LibOlm] Enabled');