Skip to content

Commit

Permalink
Merge branch 'master' into realm_app
Browse files Browse the repository at this point in the history
  • Loading branch information
desistefanova committed Apr 12, 2022
2 parents a359038 + 911c2ea commit 1489eee
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ x.x.x Release notes (yyyy-MM-dd)
* Added a property `Configuration.initialDataCallback`. This is a callback executed when a Realm file is first created and allows you to populate some initial data necessary for your application. ([#298](https://github.com/realm/realm-dart/issues/298))
* Support application configuration ([#306](https://github.com/realm/realm-dart/pull/306/))
* Support application ([#446](https://github.com/realm/realm-dart/pull/446/))
* Support should realm compact on open callback `Configuration.shouldCompactCallback` as option when configuring a Realm to determine if it should be compacted before being returned. ([#466](https://github.com/realm/realm-dart/pull/466/))
* Support ObjectId ([#468](https://github.com/realm/realm-dart/pull/468))

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion flutter/realm_flutter/ios/Classes/RealmPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ void dummy(void) {
realm_list_size(NULL, 0);
realm_results_snapshot(NULL);
realm_http_transport_new(NULL, NULL, NULL);
realm_config_set_should_compact_on_launch_function(NULL, NULL, NULL);
realm_app_credentials_new_anonymous();
realm_app_config_new(NULL, NULL);
realm_app_config_new(NULL, NULL);
realm_sync_client_config_new();
}

Expand Down
21 changes: 20 additions & 1 deletion lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import 'dart:io';

import 'native/realm_core.dart';

import 'realm_object.dart';
import 'realm_property.dart';
import 'package:path/path.dart' as _path;
import 'realm_class.dart';

Expand All @@ -33,7 +35,14 @@ class Configuration {

/// Creates a [Configuration] with schema objects for opening a [Realm].
Configuration(List<SchemaObject> schemaObjects,
{String? path, this.fifoFilesFallbackPath, this.isReadOnly = false, this.isInMemory = false, this.schemaVersion = 0, this.disableFormatUpgrade = false, this.initialDataCallback})
{String? path,
this.fifoFilesFallbackPath,
this.isReadOnly = false,
this.isInMemory = false,
this.schemaVersion = 0,
this.disableFormatUpgrade = false,
this.initialDataCallback,
this.shouldCompactCallback})
: schema = RealmSchema(schemaObjects),
path = path ?? defaultPath;

Expand Down Expand Up @@ -110,6 +119,16 @@ class Configuration {
/// add some initial data that your app needs. The function will not execute for existing
/// Realms, even if all objects in the Realm are deleted.
final Function(Realm realm)? initialDataCallback;

/// The function called when opening a Realm for the first time
/// during the life of a process to determine if it should be compacted
/// before being returned to the user.
///
/// [totalSize] - The total file size (data + free space)
/// [usedSize] - The total bytes used by data in the file.
/// It returns true to indicate that an attempt to compact the file should be made.
/// The compaction will be skipped if another process is currently accessing the realm file.
final Function(int totalSize, int usedSize)? shouldCompactCallback;
}

/// A collection of properties describing the underlying schema of a [RealmObject].
Expand Down
15 changes: 15 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ class _RealmCore {
_realmLib.realm_config_set_data_initialization_function(configHandle._pointer, Pointer.fromFunction(initial_data_callback, FALSE), config.toGCHandle());
}

if (config.shouldCompactCallback != null) {
_realmLib.realm_config_set_should_compact_on_launch_function(
configHandle._pointer, Pointer.fromFunction(should_compact_callback, 0), config.toGCHandle());
}

return configHandle;
});
}
Expand All @@ -187,6 +192,16 @@ class _RealmCore {
return FALSE;
}

static int should_compact_callback(Pointer<Void> userdata, int totalSize, int usedSize) {
final Configuration? config = userdata.toObject();
if (config == null) {
return 0;
}
config.shouldCompactCallback!(totalSize, usedSize);

return 1;
}

SchedulerHandle createScheduler(int isolateId, int sendPort) {
final schedulerPtr = _realmLib.realm_dart_create_scheduler(isolateId, sendPort);
return SchedulerHandle._(schedulerPtr);
Expand Down
1 change: 1 addition & 0 deletions src/realm_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void dummy(void) {
realm_list_size(nullptr, 0);
realm_results_add_notification_callback(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
realm_results_snapshot(nullptr);
realm_config_set_should_compact_on_launch_function(nullptr, nullptr, nullptr);
realm_app_config_new(nullptr, nullptr);
realm_sync_client_config_new();
realm_app_credentials_new_anonymous();
Expand Down
14 changes: 14 additions & 0 deletions test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,18 @@ Future<void> main([List<String>? args]) async {
expect(callbackEx, isNotNull);
expect(callbackEx.toString(), contains('The Realm is already in a write transaction'));
});

test('Configuration - do not compact on open realm', () {
var config = Configuration([Dog.schema, Person.schema], shouldCompactCallback: (totalSize, usedSize) {
return false;
});
final realm = getRealm(config);
});

test('Configuration - compact on open realm', () {
var config = Configuration([Dog.schema, Person.schema], shouldCompactCallback: (totalSize, usedSize) {
return totalSize > 0;
});
final realm = getRealm(config);
});
}

0 comments on commit 1489eee

Please sign in to comment.