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:
parent
edec008bbe
commit
8fd3bbe28c
|
|
@ -25,6 +25,7 @@ export 'src/client.dart';
|
||||||
export 'src/database/database_api.dart';
|
export 'src/database/database_api.dart';
|
||||||
export 'src/database/hive_database.dart';
|
export 'src/database/hive_database.dart';
|
||||||
export 'src/database/fluffybox_database.dart';
|
export 'src/database/fluffybox_database.dart';
|
||||||
|
export 'src/database/hive_collections_database.dart';
|
||||||
export 'src/event.dart';
|
export 'src/event.dart';
|
||||||
export 'src/presence.dart';
|
export 'src/presence.dart';
|
||||||
export 'src/event_status.dart';
|
export 'src/event_status.dart';
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,8 @@ import 'package:matrix/src/utils/queued_to_device_event.dart';
|
||||||
import 'package:matrix/src/utils/run_benchmarked.dart';
|
import 'package:matrix/src/utils/run_benchmarked.dart';
|
||||||
|
|
||||||
/// This database does not support file caching!
|
/// This database does not support file caching!
|
||||||
|
@Deprecated(
|
||||||
|
'Use [HiveCollectionsDatabase] instead. Don\'t forget to properly migrate!')
|
||||||
class FluffyBoxDatabase extends DatabaseApi {
|
class FluffyBoxDatabase extends DatabaseApi {
|
||||||
static const int version = 6;
|
static const int version = 6;
|
||||||
final String name;
|
final String name;
|
||||||
|
|
@ -1457,46 +1459,3 @@ class FluffyBoxDatabase extends DatabaseApi {
|
||||||
return raw;
|
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
|
|
@ -35,6 +35,8 @@ import 'package:matrix/src/utils/run_benchmarked.dart';
|
||||||
/// you use this.
|
/// you use this.
|
||||||
///
|
///
|
||||||
/// This database does not support file caching!
|
/// This database does not support file caching!
|
||||||
|
@Deprecated(
|
||||||
|
'Use [HiveCollectionsDatabase] instead. Don\'t forget to properly migrate!')
|
||||||
class FamedlySdkHiveDatabase extends DatabaseApi {
|
class FamedlySdkHiveDatabase extends DatabaseApi {
|
||||||
static const int version = 5;
|
static const int version = 5;
|
||||||
final String name;
|
final String name;
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ dependencies:
|
||||||
base58check: ^2.0.0
|
base58check: ^2.0.0
|
||||||
olm: ^2.0.0
|
olm: ^2.0.0
|
||||||
matrix_api_lite: ^1.0.0
|
matrix_api_lite: ^1.0.0
|
||||||
hive: ^2.0.4
|
hive: ^2.2.1
|
||||||
image: ^3.1.1
|
image: ^3.1.1
|
||||||
ffi: ^1.0.0
|
ffi: ^1.0.0
|
||||||
js: ^0.6.3
|
js: ^0.6.3
|
||||||
|
|
|
||||||
|
|
@ -26,16 +26,16 @@ import 'package:olm/olm.dart' as olm;
|
||||||
import 'fake_database.dart';
|
import 'fake_database.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
group('HiveCollections Database Test', () {
|
||||||
|
testDatabase(
|
||||||
|
getHiveCollectionsDatabase(null),
|
||||||
|
);
|
||||||
|
});
|
||||||
group('FluffyBox Database Test', () {
|
group('FluffyBox Database Test', () {
|
||||||
testDatabase(
|
testDatabase(
|
||||||
getFluffyBoxDatabase(null),
|
getFluffyBoxDatabase(null),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
group('Hive Database Test', () {
|
|
||||||
testDatabase(
|
|
||||||
getHiveDatabase(null),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> olmEnabled() async {
|
Future<bool> olmEnabled() async {
|
||||||
|
|
|
||||||
|
|
@ -23,15 +23,33 @@ import 'package:matrix/matrix.dart';
|
||||||
import 'package:file/memory.dart';
|
import 'package:file/memory.dart';
|
||||||
import 'package:hive/hive.dart';
|
import 'package:hive/hive.dart';
|
||||||
|
|
||||||
Future<DatabaseApi> getDatabase(Client? _) => getHiveDatabase(_);
|
Future<DatabaseApi> getDatabase(Client? _) => getHiveCollectionsDatabase(_);
|
||||||
|
|
||||||
bool hiveInitialized = false;
|
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 {
|
Future<FluffyBoxDatabase> getFluffyBoxDatabase(Client? c) async {
|
||||||
final fileSystem = MemoryFileSystem();
|
final fileSystem = MemoryFileSystem();
|
||||||
final testHivePath =
|
final testHivePath =
|
||||||
'${fileSystem.path}/build/.test_store/${Random().nextDouble()}';
|
'${fileSystem.path}/build/.test_store/${Random().nextDouble()}';
|
||||||
Directory(testHivePath).createSync(recursive: true);
|
Directory(testHivePath).createSync(recursive: true);
|
||||||
|
// ignore: deprecated_member_use_from_same_package
|
||||||
final db = FluffyBoxDatabase(
|
final db = FluffyBoxDatabase(
|
||||||
'unit_test.${c?.hashCode}',
|
'unit_test.${c?.hashCode}',
|
||||||
testHivePath,
|
testHivePath,
|
||||||
|
|
@ -40,6 +58,7 @@ Future<FluffyBoxDatabase> getFluffyBoxDatabase(Client? c) async {
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ignore: deprecated_member_use_from_same_package
|
||||||
Future<FamedlySdkHiveDatabase> getHiveDatabase(Client? c) async {
|
Future<FamedlySdkHiveDatabase> getHiveDatabase(Client? c) async {
|
||||||
if (!hiveInitialized) {
|
if (!hiveInitialized) {
|
||||||
final fileSystem = MemoryFileSystem();
|
final fileSystem = MemoryFileSystem();
|
||||||
|
|
@ -49,6 +68,7 @@ Future<FamedlySdkHiveDatabase> getHiveDatabase(Client? c) async {
|
||||||
Hive.init(testHivePath);
|
Hive.init(testHivePath);
|
||||||
hiveInitialized = true;
|
hiveInitialized = true;
|
||||||
}
|
}
|
||||||
|
// ignore: deprecated_member_use_from_same_package
|
||||||
final db = FamedlySdkHiveDatabase('unit_test.${c?.hashCode}');
|
final db = FamedlySdkHiveDatabase('unit_test.${c?.hashCode}');
|
||||||
await db.open();
|
await db.open();
|
||||||
return db;
|
return db;
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
* 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/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
import 'package:hive/hive.dart';
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
import '../test/fake_database.dart';
|
import '../test/fake_database.dart';
|
||||||
import 'test_config.dart';
|
import 'test_config.dart';
|
||||||
|
|
@ -32,6 +33,8 @@ void test() async {
|
||||||
Client? testClientA, testClientB;
|
Client? testClientA, testClientB;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
Hive.init(null);
|
||||||
|
|
||||||
await olm.init();
|
await olm.init();
|
||||||
olm.Account();
|
olm.Account();
|
||||||
Logs().i('[LibOlm] Enabled');
|
Logs().i('[LibOlm] Enabled');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue