Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

realm compact #1005

Merged
merged 23 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Support results of primitives, ie. `RealmResult<int>`. (Issue [#162](https://github.com/realm/realm-dart/issues/162))
* Support notifications on all managed realm lists, including list of primitives, ie. `RealmList<int>.changes` is supported. ([#893](https://github.com/realm/realm-dart/pull/893))
* Support named backlinks on realm models. You can now add and annotate a realm object iterator field with `@Backlink(#fieldName)`. ([#996](https://github.com/realm/realm-dart/pull/996))
* Added Realm file compaction support. ([#1005](https://github.com/realm/realm-dart/pull/1005))
* Allow `@Indexed` attribute on all indexable type, and ensure appropriate indexes are created in the realm. ([#797](https://github.com/realm/realm-dart/issues/797))

### Fixed
Expand Down
8 changes: 8 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2168,6 +2168,14 @@ class _RealmCore {
return completer.future;
});
}

bool compact(Realm realm) {
return using((arena) {
final out_did_compact = arena<Bool>();
_realmLib.invokeGetBool(() => _realmLib.realm_compact(realm.handle._pointer, out_did_compact));
return out_did_compact.value;
});
}
}

class LastError {
Expand Down
34 changes: 34 additions & 0 deletions lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,40 @@ class Realm implements Finalizable {
throw RealmError('Starting a write transaction on a frozen Realm is not allowed.');
}
}

/// Compacts a Realm file. A Realm file usually contains free/unused space.
///
/// This method removes this free space and the file size is thereby reduced.
/// Objects within the Realm file are untouched.
/// Note: The file system should have free space for at least a copy of the Realm file. This method must not be called inside a transaction.
/// The Realm file is left untouched if any file operation fails.
static bool compact(Configuration config) {
late Configuration compactConfig;
if (config is LocalConfiguration) {
blagoev marked this conversation as resolved.
Show resolved Hide resolved
compactConfig = Configuration.local(config.schemaObjects.toList(),
schemaVersion: config.schemaVersion,
fifoFilesFallbackPath: config.fifoFilesFallbackPath,
path: config.path,
encryptionKey: config.encryptionKey,
disableFormatUpgrade: true,
isReadOnly: config.isReadOnly);
} else if (config is FlexibleSyncConfiguration || config is DisconnectedSyncConfiguration) {
compactConfig = Configuration.disconnectedSync(config.schemaObjects.toList(),
fifoFilesFallbackPath: config.fifoFilesFallbackPath, path: config.path, encryptionKey: config.encryptionKey);
} else if (config is InMemoryConfiguration) {
blagoev marked this conversation as resolved.
Show resolved Hide resolved
compactConfig = config;
}
else {
throw RealmError("Unsupported realm configuration type ${config.runtimeType}");
}

final realm = Realm(compactConfig);
try {
return realmCore.compact(realm);
} finally {
realm.close();
}
}
}

/// Provides a scope to safely write data to a [Realm]. Can be created using [Realm.beginWrite] or
Expand Down
169 changes: 169 additions & 0 deletions test/realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'dart:math';
import 'package:test/test.dart' hide test, throws;
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
Expand Down Expand Up @@ -1394,6 +1396,173 @@ Future<void> main([List<String>? args]) async {
expect(await realmIsCancelled, isTrue);
expect(progressReturned, isFalse);
});

void addDataForCompact(Realm realm) {
realm.write(() {
for (var i = 0; i < 2500; i++) {
realm.add(Task(ObjectId()));
}
});

realm.write(() => realm.deleteAll<Task>());

realm.write(() {
for (var i = 10; i < 20; i++) {
blagoev marked this conversation as resolved.
Show resolved Hide resolved
realm.add(Task(ObjectId()));
}
});
}

Future<int> createRealmForCompact(Configuration config) async {
var realm = getRealm(config);

if (config is FlexibleSyncConfiguration) {
realm.subscriptions.update((mutableSubscriptions) {
mutableSubscriptions.add(realm.all<Task>());
});
await realm.subscriptions.waitForSynchronization();
blagoev marked this conversation as resolved.
Show resolved Hide resolved
}

addDataForCompact(realm);

final beforeSize = await File(config.path).stat().then((value) => value.size);
blagoev marked this conversation as resolved.
Show resolved Hide resolved

if (config is FlexibleSyncConfiguration) {
await realm.syncSession.waitForDownload();
await realm.syncSession.waitForUpload();
}

realm.close();
return beforeSize;
}

void validateCompact(bool compacted, String realmPath, int before) async {
expect(compacted, true);
final afterSize = await File(realmPath).stat().then((value) => value.size);
expect(before, greaterThan(afterSize));
}
blagoev marked this conversation as resolved.
Show resolved Hide resolved

test('Realm - local realm can be compacted', () async {
var config = Configuration.local([Task.schema], path: p.join(Configuration.defaultStoragePath, "${generateRandomString(8)}.realm"));
final beforeCompact = await createRealmForCompact(config);
blagoev marked this conversation as resolved.
Show resolved Hide resolved

final compacted = Realm.compact(config);

validateCompact(compacted, config.path, beforeCompact);

//test the realm can be opened.
final realm = getRealm(config);
});

test('Realm - local realm can be compacted in worker isolate', () async {
blagoev marked this conversation as resolved.
Show resolved Hide resolved
var config = Configuration.local([Task.schema], path: p.join(Configuration.defaultStoragePath, "${generateRandomString(8)}.realm"));
final beforeCompact = await createRealmForCompact(config);

final receivePort = ReceivePort();
await Isolate.spawn((List<Object> args) async {
SendPort sendPort = args[0] as SendPort;
final path = args[1] as String;
var config = Configuration.local([Task.schema], path: path);
final compacted = Realm.compact(config);
Isolate.exit(sendPort, compacted);
}, [receivePort.sendPort, config.path]);

final compacted = await receivePort.first as bool;

validateCompact(compacted, config.path, beforeCompact);

//test the realm can be opened.
blagoev marked this conversation as resolved.
Show resolved Hide resolved
final realm = getRealm(config);
});

test('Realm - local encrypted realm can be compacted', () async {
final config = Configuration.local([Task.schema],
encryptionKey: generateEncryptionKey(), path: p.join(Configuration.defaultStoragePath, "${generateRandomString(8)}.realm"));

final beforeCompact = await createRealmForCompact(config);

final compacted = Realm.compact(config);

validateCompact(compacted, config.path, beforeCompact);

//test the realm can be opened.
final realm = getRealm(config);
});

test('Realm - in-memory realm can be compacted', () async {
blagoev marked this conversation as resolved.
Show resolved Hide resolved
var config = Configuration.inMemory([Task.schema], path: p.join(Configuration.defaultStoragePath, "${generateRandomString(8)}.realm"));
final beforeCompact = await createRealmForCompact(config);

final compacted = Realm.compact(config);

validateCompact(compacted, config.path, beforeCompact);

//test the realm can be opened.
final realm = getRealm(config);
});

test('Realm - readonly realm can not be compacted', () async {
var path = p.join(Configuration.defaultStoragePath, "${generateRandomString(8)}.realm");
var config = Configuration.local([Task.schema], path: path);
final beforeCompact = await createRealmForCompact(config);

config = Configuration.local([Task.schema], isReadOnly: true, path: path);
expect(() => Realm.compact(config), throws<RealmException>("Can't compact a read-only Realm"));

//test the realm can be opened.
final realm = getRealm(config);
});

test('Realm - disconnected sync realm can be compacted', () async {
var config = Configuration.disconnectedSync([Task.schema], path: p.join(Configuration.defaultStoragePath, "${generateRandomString(8)}.realm"));

final beforeCompact = await createRealmForCompact(config);

final compacted = Realm.compact(config);

validateCompact(compacted, config.path, beforeCompact);

//test the realm can be opened.
final realm = getRealm(config);
});

baasTest('Realm - synced realm can be compacted', (appConfiguration) async {
final app = App(appConfiguration);
final credentials = Credentials.anonymous();
final user = await app.logIn(credentials);
final path = p.join(Configuration.defaultStoragePath, "${generateRandomString(8)}.realm");
blagoev marked this conversation as resolved.
Show resolved Hide resolved
final config = Configuration.flexibleSync(user, [Task.schema], path: path);
final beforeCompact = await createRealmForCompact(config);
user.logOut();
Future<void>.delayed(Duration(seconds: 5));

final compacted = Realm.compact(config);

validateCompact(compacted, config.path, beforeCompact);

//test the realm can be opened.
final realm = getRealm(Configuration.disconnectedSync([Task.schema], path: path));
});

baasTest('Realm - synced encrypted realm can be compacted', (appConfiguration) async {
final app = App(appConfiguration);
final credentials = Credentials.anonymous();
final path = p.join(Configuration.defaultStoragePath, "${generateRandomString(8)}.realm");
final user = await app.logIn(credentials);
List<int> key = List<int>.generate(encryptionKeySize, (i) => random.nextInt(256));
final config =
Configuration.flexibleSync(user, [Task.schema], encryptionKey: key, path: path);
final beforeCompact = await createRealmForCompact(config);
user.logOut();
Future<void>.delayed(Duration(seconds: 5));

final compacted = Realm.compact(config);

validateCompact(compacted, config.path, beforeCompact);

//test the realm can be opened.
final realm = getRealm(Configuration.disconnectedSync([Task.schema], path: path, encryptionKey: key));
});
}

List<int> generateEncryptionKey() {
Expand Down
11 changes: 9 additions & 2 deletions test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,20 @@ Future<void> tryDeleteRealm(String path) async {
return;
}

final dummy = File("");
const duration = Duration(milliseconds: 100);
for (var i = 0; i < 5; i++) {
try {
Realm.deleteRealm(path);
await File('$path.lock').delete();

//delete lock file
await File('$path.lock').delete().onError((error, stackTrace) => dummy);

//delete compaction space file
await File('$path.tmp_compaction_space').delete().onError((error, stackTrace) => dummy);
blagoev marked this conversation as resolved.
Show resolved Hide resolved

blagoev marked this conversation as resolved.
Show resolved Hide resolved
return;
} catch (e) {
const duration = Duration(milliseconds: 100);
print('Failed to delete realm at path $path. Trying again in ${duration.inMilliseconds}ms');
await Future<void>.delayed(duration);
}
Expand Down