Skip to content

Commit

Permalink
Expose embeddedObject.getParent
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev authored and nielsenko committed Nov 3, 2022
1 parent 58c01e9 commit 0963093
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
21 changes: 19 additions & 2 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import '../scheduler.dart';
import '../subscription.dart';
import '../user.dart';
import '../session.dart';
import '../util.dart';
import 'realm_bindings.dart';
import '../migration.dart';

Expand Down Expand Up @@ -716,8 +717,20 @@ class _RealmCore {
}

RealmObjectHandle createEmbeddedObject(RealmObjectBase obj, int propertyKey) {
final realmPtr = _realmLib.invokeGetPointer(() => _realmLib.realm_set_embedded(obj.handle._pointer, propertyKey));
return RealmObjectHandle._(realmPtr, obj.realm.handle);
final objectPtr = _realmLib.invokeGetPointer(() => _realmLib.realm_set_embedded(obj.handle._pointer, propertyKey));
return RealmObjectHandle._(objectPtr, obj.realm.handle);
}

Tuple<RealmObjectHandle, int> getEmbeddedParent(EmbeddedObject obj) {
return using((Arena arena) {
final parentPtr = arena<Pointer<realm_object>>();
final classKeyPtr = arena<Uint32>();
_realmLib.invokeGetBool(() => _realmLib.realm_object_get_parent(obj.handle._pointer, parentPtr, classKeyPtr));

final handle = RealmObjectHandle._(parentPtr.value, obj.realm.handle);

return Tuple(handle, classKeyPtr.value);
});
}

RealmObjectHandle getOrCreateRealmObjectWithPrimaryKey(Realm realm, int classKey, Object? primaryKey) {
Expand Down Expand Up @@ -1054,6 +1067,10 @@ class _RealmCore {
return _realmLib.realm_equals(first._pointer.cast(), second._pointer.cast());
}

int getObjectKey(RealmObjectBase obj) {
return _realmLib.realm_object_get_key(obj.handle._pointer);
}

bool objectEquals(RealmObjectBase first, RealmObjectBase second) => _equals(first.handle, second.handle);
bool realmEquals(Realm first, Realm second) => _equals(first.handle, second.handle);
bool userEquals(User first, User second) => _equals(first.handle, second.handle);
Expand Down
15 changes: 15 additions & 0 deletions lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import 'results.dart';
import 'scheduler.dart';
import 'subscription.dart';
import 'session.dart';
import 'util.dart';

export 'package:realm_common/realm_common.dart'
show
Expand Down Expand Up @@ -757,6 +758,20 @@ class RealmMetadata {

return metadata;
}

Tuple<Type, RealmObjectMetadata> getByClassKey(int key) {
final type = _typeMap.entries.firstWhereOrNull((e) => e.value.classKey == key);
if (type != null) {
return Tuple(type.key, type.value);
}

final metadata = _stringMap.values.firstWhereOrNull((e) => e.classKey == key);
if (metadata != null) {
return Tuple(RealmObjectBase, metadata);
}

throw RealmError("Object with classKey $key not found in the current Realm's schema.");
}
}

/// Exposes a set of dynamic methods on the Realm object. These don't use strongly typed
Expand Down
12 changes: 11 additions & 1 deletion lib/src/realm_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,17 @@ mixin RealmObjectBase on RealmEntity implements Finalizable {
mixin RealmObject on RealmObjectBase {}

/// @nodoc
mixin EmbeddedObject on RealmObjectBase {}
mixin EmbeddedObject on RealmObjectBase {
RealmObjectBase? getParent() {
if (!isManaged) {
return null;
}

final parent = realmCore.getEmbeddedParent(this);
final metadata = realm.metadata.getByClassKey(parent.item2);
return realm.createObject(metadata.item1, parent.item1, metadata.item2);
}
}

/// @nodoc
//RealmObject package internal members
Expand Down
6 changes: 6 additions & 0 deletions lib/src/util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Tuple<T1, T2> {
T1 item1;
T2 item2;

Tuple(this.item1, this.item2);
}
2 changes: 1 addition & 1 deletion src/realm-core
Submodule realm-core updated 97 files
+0 −51 CHANGELOG.md
+37 −0 Jenkinsfile
+1 −1 Package.swift
+2 −2 dependencies.list
+12 −58 evergreen/config.yml
+1 −1 evergreen/hang_analyzer/src/hang_analyzer.py
+2 −2 evergreen/install_baas.sh
+17 −41 src/realm.h
+4 −0 src/realm/CMakeLists.txt
+9 −15 src/realm/alloc_slab.cpp
+2 −2 src/realm/cluster.cpp
+6 −5 src/realm/db.cpp
+2 −1 src/realm/db.hpp
+3 −0 src/realm/group.cpp
+8 −9 src/realm/group.hpp
+1 −1 src/realm/group_writer.cpp
+7 −4 src/realm/keys.hpp
+0 −53 src/realm/mixed.hpp
+6 −5 src/realm/obj.cpp
+0 −5 src/realm/obj.hpp
+12 −14 src/realm/object-store/c_api/app.cpp
+1 −1 src/realm/object-store/c_api/config.cpp
+0 −16 src/realm/object-store/c_api/query.cpp
+2 −0 src/realm/object-store/keypath_helpers.hpp
+0 −13 src/realm/object-store/object_schema.cpp
+12 −1 src/realm/object-store/object_schema.hpp
+43 −8 src/realm/object-store/object_store.cpp
+1 −1 src/realm/object-store/object_store.hpp
+4 −16 src/realm/object-store/sectioned_results.cpp
+2 −2 src/realm/object-store/sectioned_results.hpp
+5 −5 src/realm/object-store/shared_realm.cpp
+3 −5 src/realm/object-store/shared_realm.hpp
+1 −60 src/realm/object-store/sync/app.cpp
+0 −6 src/realm/object-store/sync/app.hpp
+0 −17 src/realm/object-store/sync/generic_network_transport.cpp
+0 −2 src/realm/object-store/sync/generic_network_transport.hpp
+27 −19 src/realm/object-store/sync/sync_session.cpp
+4 −3 src/realm/object-store/sync/sync_session.hpp
+35 −26 src/realm/object_converter.cpp
+11 −7 src/realm/object_converter.hpp
+58 −63 src/realm/parser/driver.cpp
+2 −0 src/realm/parser/driver.hpp
+10 −8 src/realm/parser/keypath_mapping.cpp
+6 −0 src/realm/parser/keypath_mapping.hpp
+1 −1 src/realm/parser/query_parser.hpp
+2 −2 src/realm/query.cpp
+1 −1 src/realm/query.hpp
+2 −5 src/realm/query_expression.hpp
+0 −5 src/realm/set.hpp
+43 −33 src/realm/string_data.hpp
+16 −1 src/realm/sync/changeset_parser.hpp
+23 −18 src/realm/sync/client.cpp
+12 −1 src/realm/sync/client.hpp
+13 −23 src/realm/sync/config.hpp
+3 −24 src/realm/sync/noinst/client_impl_base.cpp
+2 −3 src/realm/sync/noinst/client_impl_base.hpp
+4 −3 src/realm/sync/noinst/client_reset.cpp
+6 −14 src/realm/sync/noinst/client_reset_recovery.cpp
+1 −1 src/realm/sync/protocol.hpp
+50 −113 src/realm/table.cpp
+16 −8 src/realm/table.hpp
+36 −28 src/realm/timestamp.hpp
+6 −6 src/realm/util/aes_cryptor.hpp
+30 −0 src/realm/util/copy_dir_recursive.cpp
+29 −0 src/realm/util/copy_dir_recursive.hpp
+22 −31 src/realm/util/encrypted_file_mapping.cpp
+12 −6 src/realm/util/encrypted_file_mapping.hpp
+0 −5 src/realm/util/file.hpp
+60 −0 src/realm/util/file_is_regular.cpp
+18 −0 src/realm/util/file_is_regular.hpp
+19 −7 src/realm/util/file_mapper.cpp
+11 −21 src/realm/util/file_mapper.hpp
+4 −62 src/realm/util/future.hpp
+9 −1 src/realm/util/serializer.cpp
+6 −2 src/realm/util/serializer.hpp
+42 −57 test/benchmark-sync/bench_transform.cpp
+2 −8 test/object-store/audit.cpp
+19 −6 test/object-store/c_api/c_api.cpp
+1,598 −743 test/object-store/migrations.cpp
+1 −87 test/object-store/sectioned_results.cpp
+19 −38 test/object-store/sync/app.cpp
+1 −16 test/object-store/sync/client_reset.cpp
+226 −328 test/object-store/sync/flx_sync.cpp
+0 −42 test/object-store/sync/sync_test_utils.hpp
+22 −12 test/object-store/util/baas_admin_api.cpp
+2 −2 test/object-store/util/baas_admin_api.hpp
+21 −29 test/object-store/util/test_file.cpp
+0 −4 test/test_alloc.cpp
+22 −504 test/test_group.cpp
+9 −3 test/test_lang_bind_helper.cpp
+88 −64 test/test_parser.cpp
+36 −11 test/test_shared.cpp
+2 −0 test/test_string_data.cpp
+1 −0 test/test_table.cpp
+2 −2 test/test_transactions.cpp
+4 −21 test/util/unit_test.cpp
+10 −26 test/util/unit_test.hpp
30 changes: 30 additions & 0 deletions test/embedded_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,36 @@ Future<void> main([List<String>? args]) async {

expect(parent.recursiveList, isEmpty);
});

test('EmbeddedObject.getParent returns parent', () async {
final realm = getLocalRealm();

final parent =
ObjectWithEmbedded('123', recursiveObject: RecursiveEmbedded1('1.1', child: RecursiveEmbedded2('2.1')), recursiveList: [RecursiveEmbedded1('1.2')]);

realm.write(() {
realm.add(parent);
});

final child1 = parent.recursiveObject!;

expect(child1.getParent(), parent);
expect(child1.child!.getParent(), child1);

expect(parent.recursiveList[0].getParent(), parent);
});

test('EmbeddedObject.getParent when unmanaged returns null', () async {
final parent =
ObjectWithEmbedded('123', recursiveObject: RecursiveEmbedded1('1.1', child: RecursiveEmbedded2('2.1')), recursiveList: [RecursiveEmbedded1('1.2')]);

final child1 = parent.recursiveObject!;

expect(child1.getParent(), null);
expect(child1.child!.getParent(), null);

expect(parent.recursiveList[0].getParent(), null);
});
}

extension on RealmObjectBase {
Expand Down

0 comments on commit 0963093

Please sign in to comment.