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 5 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
10 changes: 10 additions & 0 deletions lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,16 @@ 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.
bool compact() {
return realmCore.compact(this);
}
}

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

import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
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 +1395,58 @@ Future<void> main([List<String>? args]) async {
expect(await realmIsCancelled, isTrue);
expect(progressReturned, isFalse);
});

test('Realm - local realm can be compacted', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
final compacted = realm.compact();
expect(compacted, true);
});

test('Realm - local realm can be compacted in worker isolate', () async {
blagoev marked this conversation as resolved.
Show resolved Hide resolved
final receivePort = ReceivePort();
await Isolate.spawn((SendPort sendPort) {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
final compacted = realm.compact();
Isolate.exit(sendPort, compacted);
}, receivePort.sendPort);

final compacted = await receivePort.first as bool;
expect(compacted, true);
});

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

baasTest('Realm - synced realm can be compacted', (appConfiguration) async {
final app = App(appConfiguration);
final credentials = Credentials.anonymous();
final user = await app.logIn(credentials);
final configuration = Configuration.flexibleSync(user, [Task.schema]);

final realm = getRealm(configuration);
final compacted = realm.compact();
expect(compacted, true);
});

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

final realm = getRealm(configuration);
final compacted = realm.compact();
expect(compacted, true);
});
}

List<int> generateEncryptionKey() {
Expand Down