feat: Implement new Hive Database

The hive database now implements the whole API except for storing files which
should be better done by the flutter_cache_manager package inside of the
flutter app. All tests already run with Hive now but the Moor database is still
tested too. We needed to change some wait jobs in the tests because the Hive
database is not 100% in memory for the tests like Moor.
For now both database implementations are equal and the developer can pick
which one to use but we plan to get rid of Moor in the future.
This commit is contained in:
Christian Pauly 2021-06-15 09:54:27 +02:00
parent 28dc8b4ff0
commit 14ee16fe16
9 changed files with 1100 additions and 6 deletions

View File

@ -39,3 +39,4 @@ export 'src/timeline.dart';
export 'src/user.dart';
export 'src/database/database.dart' show Database;
export 'src/database/database_api.dart';
export 'src/database/hive_database.dart';

File diff suppressed because it is too large Load Diff

View File

@ -20,6 +20,7 @@ dependencies:
olm: ^2.0.0
isolate: ^2.0.3
matrix_api_lite: ^0.3.3
hive: ^2.0.4
dev_dependencies:
test: ^1.15.7
@ -27,5 +28,5 @@ dev_dependencies:
moor_generator: ^4.0.0
build_runner: ^1.11.1
pedantic: ^1.11.0
dapackages: ^1.4.0
file: ^6.1.1
#flutter_test: {sdk: flutter}

View File

@ -540,7 +540,7 @@ void main() {
newOlmAccount: pickledOlmAccount,
);
await Future.delayed(Duration(milliseconds: 50));
await Future.delayed(Duration(milliseconds: 500));
expect(client1.isLogged(), true);
expect(client1.rooms.length, 2);
@ -552,7 +552,7 @@ void main() {
);
await client2.init();
await Future.delayed(Duration(milliseconds: 100));
await Future.delayed(Duration(milliseconds: 500));
expect(client2.isLogged(), true);
expect(client2.accessToken, client1.accessToken);

View File

@ -27,7 +27,10 @@ import 'fake_database_native.dart';
void main() {
/// All Tests related to the ChatTime
group('Moor Database Test', () {
testDatabase(getDatabase(null), 0);
testDatabase(getMoorDatabase(null), 0);
});
group('Hive Database Test', () {
testDatabase(getHiveDatabase(null), 0);
});
}

View File

@ -386,6 +386,9 @@ void main() {
null,
false);
// There is a non awaiting setInboundGroupSession call on the database
await Future.delayed(Duration(seconds: 1));
await matrix.dispose(closeDatabase: true);
});
});

View File

@ -216,7 +216,7 @@ void main() {
await client1.userDeviceKeys[client2.userID].startVerification();
expect(req1.state, KeyVerificationState.askSSSS);
await req1.openSSSS(recoveryKey: ssssKey);
await Future.delayed(Duration(milliseconds: 10));
await Future.delayed(Duration(seconds: 1));
expect(req1.state, KeyVerificationState.waitingAccept);
await req1.cancel();

View File

@ -96,6 +96,7 @@ void main() {
client.encryption.keyManager.setInboundGroupSession(
roomId, sessionId, senderKey, sessionPayload,
forwarded: true);
await Future.delayed(Duration(milliseconds: 500));
var dbSessions = await client.database.getInboundGroupSessionsToUpload();
expect(dbSessions.isNotEmpty, true);
await client.encryption.keyManager.backgroundTasks();

View File

@ -16,11 +16,35 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'dart:io';
import 'dart:math';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:famedlysdk/src/database/hive_database.dart';
import 'package:file/memory.dart';
import 'package:hive/hive.dart';
import 'package:moor/moor.dart';
import 'package:moor/ffi.dart' as moor;
Future<Database> getDatabase(Client _) async {
Future<DatabaseApi> getDatabase(Client _) => getHiveDatabase(_);
Future<Database> getMoorDatabase(Client _) async {
moorRuntimeOptions.dontWarnAboutMultipleDatabases = true;
return Database(moor.VmDatabase.memory());
}
bool hiveInitialized = false;
Future<FamedlySdkHiveDatabase> getHiveDatabase(Client c) async {
if (!hiveInitialized) {
final fileSystem = MemoryFileSystem();
final testHivePath =
'${fileSystem.path}/build/.test_store/${Random().nextDouble()}';
Directory(testHivePath).createSync(recursive: true);
Hive.init(testHivePath);
hiveInitialized = true;
}
final db = FamedlySdkHiveDatabase('unit_test.${c.hashCode}');
await db.open();
return db;
}