Skip to content

Commit

Permalink
Refactor _RealmQueryHandler (now QueryHandle)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Apr 13, 2024
1 parent 649dd8d commit 88d827a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 116 deletions.
2 changes: 1 addition & 1 deletion packages/realm_dart/lib/src/native/config_handle.dart
Expand Up @@ -12,7 +12,7 @@ class ConfigHandle extends HandleBase<realm_config> {
final configHandle = ConfigHandle._(configPtr);

if (config.schemaObjects.isNotEmpty) {
final schemaHandle = _RealmCore._createSchema(config.schemaObjects);
final schemaHandle = SchemaHandle(config.schemaObjects);
realmLib.realm_config_set_schema(configHandle.pointer, schemaHandle.pointer);
}

Expand Down
17 changes: 17 additions & 0 deletions packages/realm_dart/lib/src/native/query_handle.dart
@@ -0,0 +1,17 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

part of 'realm_core.dart';

class QueryHandle extends RootedHandleBase<realm_query> {
QueryHandle._(Pointer<realm_query> pointer, RealmHandle root) : super(root, pointer, 256);

RealmResultsHandle findAll() {
try {
final resultsPointer = invokeGetPointer(() => realmLib.realm_query_find_all(pointer));
return RealmResultsHandle._(resultsPointer, _root);
} finally {
release();
}
}
}
123 changes: 10 additions & 113 deletions packages/realm_dart/lib/src/native/realm_core.dart
Expand Up @@ -46,8 +46,10 @@ part 'convert.dart';
part 'decimal128.dart';
part 'error_handling.dart';
part 'mutable_subscription_set_handle.dart';
part 'query_handle.dart';
part 'realm_handle.dart';
part 'rooted_handle.dart';
part 'schema_handle.dart';
part 'subscription_handle.dart';
part 'subscription_set_handle.dart';

Expand Down Expand Up @@ -203,70 +205,6 @@ class _RealmCore {
// realmLib.realm_dart_gc();
// }

static SchemaHandle _createSchema(Iterable<SchemaObject> schema) {
return using((Arena arena) {
final classCount = schema.length;

final schemaClasses = arena<realm_class_info_t>(classCount);
final schemaProperties = arena<Pointer<realm_property_info_t>>(classCount);

for (var i = 0; i < classCount; i++) {
final schemaObject = schema.elementAt(i);
final classInfo = schemaClasses.elementAt(i).ref;
final propertiesCount = schemaObject.length;
final computedCount = schemaObject.where((p) => p.isComputed).length;
final persistedCount = propertiesCount - computedCount;

classInfo.name = schemaObject.name.toCharPtr(arena);
classInfo.primary_key = "".toCharPtr(arena);
classInfo.num_properties = persistedCount;
classInfo.num_computed_properties = computedCount;
classInfo.key = RLM_INVALID_CLASS_KEY;
classInfo.flags = schemaObject.baseType.flags;

final properties = arena<realm_property_info_t>(propertiesCount);

for (var j = 0; j < propertiesCount; j++) {
final schemaProperty = schemaObject[j];
final propInfo = properties.elementAt(j).ref;
propInfo.name = schemaProperty.mapTo.toCharPtr(arena);
propInfo.public_name = (schemaProperty.mapTo != schemaProperty.name ? schemaProperty.name : '').toCharPtr(arena);
propInfo.link_target = (schemaProperty.linkTarget ?? "").toCharPtr(arena);
propInfo.link_origin_property_name = (schemaProperty.linkOriginProperty ?? "").toCharPtr(arena);
propInfo.type = schemaProperty.propertyType.index;
propInfo.collection_type = schemaProperty.collectionType.index;
propInfo.flags = realm_property_flags.RLM_PROPERTY_NORMAL;

if (schemaProperty.optional) {
propInfo.flags |= realm_property_flags.RLM_PROPERTY_NULLABLE;
}

switch (schemaProperty.indexType) {
case RealmIndexType.regular:
propInfo.flags |= realm_property_flags.RLM_PROPERTY_INDEXED;
break;
case RealmIndexType.fullText:
propInfo.flags |= realm_property_flags.RLM_PROPERTY_FULLTEXT_INDEXED;
break;
default:
break;
}

if (schemaProperty.primaryKey) {
classInfo.primary_key = schemaProperty.mapTo.toCharPtr(arena);
propInfo.flags |= realm_property_flags.RLM_PROPERTY_PRIMARY_KEY;
}
}

schemaProperties[i] = properties;
schemaProperties.elementAt(i).value = properties;
}

final schemaPtr = invokeGetPointer(() => realmLib.realm_schema_new(schemaClasses, classCount, schemaProperties));
return SchemaHandle._(schemaPtr);
});
}

String getPathForUser(User user) {
final syncConfigPtr = invokeGetPointer(() => realmLib.realm_flx_sync_config_new(user.handle.pointer));
try {
Expand Down Expand Up @@ -732,36 +670,14 @@ class _RealmCore {
return RealmResultsHandle._(pointer, realm.handle);
}

RealmResultsHandle queryClass(Realm realm, int classKey, String query, List<Object?> args) {
return using((arena) {
final length = args.length;
final argsPointer = arena<realm_query_arg_t>(length);
for (var i = 0; i < length; ++i) {
_intoRealmQueryArg(args[i], argsPointer.elementAt(i), arena);
}
final queryHandle = _RealmQueryHandle._(
invokeGetPointer(
() => realmLib.realm_query_parse(
realm.handle.pointer,
classKey,
query.toCharPtr(arena),
length,
argsPointer,
),
),
realm.handle);
return _queryFindAll(queryHandle);
});
}

RealmResultsHandle queryResults(RealmResults target, String query, List<Object> args) {
return using((arena) {
final length = args.length;
final argsPointer = arena<realm_query_arg_t>(length);
for (var i = 0; i < length; ++i) {
_intoRealmQueryArg(args[i], argsPointer.elementAt(i), arena);
}
final queryHandle = _RealmQueryHandle._(
final queryHandle = QueryHandle._(
invokeGetPointer(
() => realmLib.realm_query_parse_for_results(
target.handle.pointer,
Expand All @@ -771,27 +687,18 @@ class _RealmCore {
),
),
target.realm.handle);
return _queryFindAll(queryHandle);
return queryHandle.findAll();
});
}

RealmResultsHandle _queryFindAll(_RealmQueryHandle queryHandle) {
try {
final resultsPointer = invokeGetPointer(() => realmLib.realm_query_find_all(queryHandle.pointer));
return RealmResultsHandle._(resultsPointer, queryHandle._root);
} finally {
queryHandle.release();
}
}

RealmResultsHandle queryList(RealmList target, String query, List<Object?> args) {
return using((arena) {
final length = args.length;
final argsPointer = arena<realm_query_arg_t>(length);
for (var i = 0; i < length; ++i) {
_intoRealmQueryArg(args[i], argsPointer.elementAt(i), arena);
}
final queryHandle = _RealmQueryHandle._(
final queryHandle = QueryHandle._(
invokeGetPointer(
() => realmLib.realm_query_parse_for_list(
target.handle.pointer,
Expand All @@ -801,7 +708,7 @@ class _RealmCore {
),
),
target.realm.handle);
return _queryFindAll(queryHandle);
return queryHandle.findAll();
});
}

Expand All @@ -812,7 +719,7 @@ class _RealmCore {
for (var i = 0; i < length; ++i) {
_intoRealmQueryArg(args[i], argsPointer.elementAt(i), arena);
}
final queryHandle = _RealmQueryHandle._(
final queryHandle = QueryHandle._(
invokeGetPointer(
() => realmLib.realm_query_parse_for_set(
target.handle.pointer,
Expand All @@ -822,7 +729,7 @@ class _RealmCore {
),
),
target.realm.handle);
return _queryFindAll(queryHandle);
return queryHandle.findAll();
});
}

Expand All @@ -835,7 +742,7 @@ class _RealmCore {
}

final results = mapGetValues(target);
final queryHandle = _RealmQueryHandle._(
final queryHandle = QueryHandle._(
invokeGetPointer(
() => realmLib.realm_query_parse_for_results(
results.pointer,
Expand All @@ -845,7 +752,7 @@ class _RealmCore {
),
),
target.realm.handle);
return _queryFindAll(queryHandle);
return queryHandle.findAll();
});
}

Expand Down Expand Up @@ -2698,12 +2605,6 @@ class _RealmCore {
}
}

class SchemaHandle extends HandleBase<realm_schema> {
SchemaHandle._(Pointer<realm_schema> pointer) : super(pointer, 24);

SchemaHandle.unowned(super.pointer) : super.unowned();
}

class SchedulerHandle extends HandleBase<realm_scheduler> {
SchedulerHandle._(Pointer<realm_scheduler> pointer) : super(pointer, 24);
}
Expand Down Expand Up @@ -2750,10 +2651,6 @@ class RealmMapHandle extends CollectionHandleBase<realm_dictionary> {
RealmMapHandle._(Pointer<realm_dictionary> pointer, RealmHandle root) : super(root, pointer, 96); // TODO: check size
}

class _RealmQueryHandle extends RootedHandleBase<realm_query> {
_RealmQueryHandle._(Pointer<realm_query> pointer, RealmHandle root) : super(root, pointer, 256);
}

class RealmCallbackTokenHandle extends RootedHandleBase<realm_callback_token> {
RealmCallbackTokenHandle._(Pointer<realm_callback_token> pointer, RealmHandle root) : super(root, pointer, 32);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/realm_dart/lib/src/native/realm_handle.dart
Expand Up @@ -75,4 +75,26 @@ class RealmHandle extends HandleBase<shared_realm> {
final configHandle = ConfigHandle(config);
invokeGetBool(() => realmLib.realm_convert_with_config(pointer, configHandle.pointer, false));
}

RealmResultsHandle queryClass(int classKey, String query, List<Object?> args) {
return using((arena) {
final length = args.length;
final argsPointer = arena<realm_query_arg_t>(length);
for (var i = 0; i < length; ++i) {
_intoRealmQueryArg(args[i], argsPointer.elementAt(i), arena);
}
final queryHandle = QueryHandle._(
invokeGetPointer(
() => realmLib.realm_query_parse(
pointer,
classKey,
query.toCharPtr(arena),
length,
argsPointer,
),
),
this);
return queryHandle.findAll();
});
}
}
74 changes: 74 additions & 0 deletions packages/realm_dart/lib/src/native/schema_handle.dart
@@ -0,0 +1,74 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

part of 'realm_core.dart';

class SchemaHandle extends HandleBase<realm_schema> {
SchemaHandle._(Pointer<realm_schema> pointer) : super(pointer, 24);

SchemaHandle.unowned(super.pointer) : super.unowned();

factory SchemaHandle(Iterable<SchemaObject> schema) {
return using((Arena arena) {
final classCount = schema.length;

final schemaClasses = arena<realm_class_info_t>(classCount);
final schemaProperties = arena<Pointer<realm_property_info_t>>(classCount);

for (var i = 0; i < classCount; i++) {
final schemaObject = schema.elementAt(i);
final classInfo = schemaClasses.elementAt(i).ref;
final propertiesCount = schemaObject.length;
final computedCount = schemaObject.where((p) => p.isComputed).length;
final persistedCount = propertiesCount - computedCount;

classInfo.name = schemaObject.name.toCharPtr(arena);
classInfo.primary_key = "".toCharPtr(arena);
classInfo.num_properties = persistedCount;
classInfo.num_computed_properties = computedCount;
classInfo.key = _RealmCore.RLM_INVALID_CLASS_KEY;
classInfo.flags = schemaObject.baseType.flags;

final properties = arena<realm_property_info_t>(propertiesCount);

for (var j = 0; j < propertiesCount; j++) {
final schemaProperty = schemaObject[j];
final propInfo = properties.elementAt(j).ref;
propInfo.name = schemaProperty.mapTo.toCharPtr(arena);
propInfo.public_name = (schemaProperty.mapTo != schemaProperty.name ? schemaProperty.name : '').toCharPtr(arena);
propInfo.link_target = (schemaProperty.linkTarget ?? "").toCharPtr(arena);
propInfo.link_origin_property_name = (schemaProperty.linkOriginProperty ?? "").toCharPtr(arena);
propInfo.type = schemaProperty.propertyType.index;
propInfo.collection_type = schemaProperty.collectionType.index;
propInfo.flags = realm_property_flags.RLM_PROPERTY_NORMAL;

if (schemaProperty.optional) {
propInfo.flags |= realm_property_flags.RLM_PROPERTY_NULLABLE;
}

switch (schemaProperty.indexType) {
case RealmIndexType.regular:
propInfo.flags |= realm_property_flags.RLM_PROPERTY_INDEXED;
break;
case RealmIndexType.fullText:
propInfo.flags |= realm_property_flags.RLM_PROPERTY_FULLTEXT_INDEXED;
break;
default:
break;
}

if (schemaProperty.primaryKey) {
classInfo.primary_key = schemaProperty.mapTo.toCharPtr(arena);
propInfo.flags |= realm_property_flags.RLM_PROPERTY_PRIMARY_KEY;
}
}

schemaProperties[i] = properties;
schemaProperties.elementAt(i).value = properties;
}

final schemaPtr = invokeGetPointer(() => realmLib.realm_schema_new(schemaClasses, classCount, schemaProperties));
return SchemaHandle._(schemaPtr);
});
}
}
7 changes: 5 additions & 2 deletions packages/realm_dart/lib/src/realm_class.dart
Expand Up @@ -440,8 +440,11 @@ class Realm implements Finalizable {
/// and [Predicate Programming Guide.](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Predicates/AdditionalChapters/Introduction.html#//apple_ref/doc/uid/TP40001789)
RealmResults<T> query<T extends RealmObject>(String query, [List<Object?> args = const []]) {
final metadata = _metadata.getByType(T);
final handle = realmCore.queryClass(this, metadata.classKey, query, args);
return RealmResultsInternal.create<T>(handle, this, metadata);
return RealmResultsInternal.create<T>(
_handle.queryClass(metadata.classKey, query, args),
this,
metadata,
);
}

/// Deletes all [RealmObject]s of type `T` in the `Realm`
Expand Down

0 comments on commit 88d827a

Please sign in to comment.