Skip to content

Commit

Permalink
config inMemory (#280)
Browse files Browse the repository at this point in the history
* InMemory implemented in Configuration - Fixes #91 

* InMemory tests added to configuration_test

* CHANGELOG updated
  • Loading branch information
desistefanova authored Mar 7, 2022
1 parent 96e91df commit e5d6e54
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ x.x.x Release notes (yyyy-MM-dd)
* Added support checking if Realm lists and Realm objects are valid. ([#183](https://github.com/realm/realm-dart/pull/183))
* Support query on lists of realm objects. ([#239](https://github.com/realm/realm-dart/pull/239))
* Added support for opening Realm in read-only mode. ([#260](https://github.com/realm/realm-dart/pull/260))
* Added support for opening in-memory Realms. ([#280](https://github.com/realm/realm-dart/pull/280))
* Primary key fields no longer required to be `final` in data model classes ([#240](https://github.com/realm/realm-dart/pull/240))
* List fields no longer required to be `final` in data model classes. ([#253](https://github.com/realm/realm-dart/pull/253))

Expand Down
20 changes: 16 additions & 4 deletions lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ class Configuration {
RealmSchema get schema => _schema;

/// Creates a [Configuration] with schema objects for opening a [Realm].
/// [readOnly] controls whether a [Realm] is opened as readonly.
/// This allows opening it from locked locations such as resources,
/// bundled with an application. The realm file must already exists.
Configuration(List<SchemaObject> schemaObjects, {bool readOnly = false})
///
/// [readOnly] controls whether a [Realm] is opened as read-only.
/// [inMemory] specifies if a [Realm] should be opened in-memory.
Configuration(List<SchemaObject> schemaObjects, {bool readOnly = false, bool inMemory = false})
: _schema = RealmSchema(schemaObjects),
_handle = realmCore.createConfig() {
schemaVersion = 0;
path = defaultPath;
if (readOnly) {
isReadOnly = true;
}
if (inMemory) {
isInMemory = true;
}
realmCore.setSchema(this);
}

Expand Down Expand Up @@ -97,6 +100,15 @@ class Configuration {
/// The realm file must already exists at [path]
bool get isReadOnly => realmCore.getConfigReadOnly(this);
set isReadOnly(bool value) => realmCore.setConfigReadOnly(this, value);

/// Specifies if a [Realm] should be opened in-memory.
///
/// This still requires a [path] (can be the default path) to identify the [Realm] so other processes can open the same [Realm].
/// The file will also be used as swap space if the [Realm] becomes bigger than what fits in memory,
/// but it is not persistent and will be removed when the last instance is closed.
/// When all in-memory instance of [Realm] is closed all data in that [Realm] is deleted.
bool get isInMemory => realmCore.getConfigInMemory(this);
set isInMemory(bool value) => realmCore.setConfigInMemory(this, value);
}

/// A collection of properties describing the underlying schema of a [RealmObject].
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 @@ -163,6 +163,14 @@ class _RealmCore {
_realmLib.realm_config_set_schema_mode(config.handle._pointer, mode);
}

bool getConfigInMemory(Configuration config) {
return _realmLib.realm_config_get_in_memory(config.handle._pointer);
}

void setConfigInMemory(Configuration config, bool value) {
_realmLib.realm_config_set_in_memory(config.handle._pointer, value);
}

ConfigHandle createConfig() {
final configPtr = _realmLib.realm_config_new();
return ConfigHandle._(configPtr);
Expand Down
17 changes: 17 additions & 0 deletions test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,21 @@ Future<void> main([List<String>? args]) async {
expect(() => realm.write(() {}), throws<RealmException>("Can't perform transactions on read-only Realms."));
realm.close();
});

test('Configuration inMemory - no files after closing realm', () {
Configuration config = Configuration([Car.schema], inMemory: true);
var realm = Realm(config);
realm.write(() => realm.add(Car('Tesla')));
realm.close();
expect(Realm.existsSync(config.path), false);
});

test('Configuration inMemory can not be readOnly', () {
Configuration config = Configuration([Car.schema], inMemory: true);
var realm = Realm(config);

config.isReadOnly = true;
expect(() => Realm(config), throws<RealmException>("Realm at path '${config.path}' already opened with different read permissions"));
realm.close();
});
}

0 comments on commit e5d6e54

Please sign in to comment.