Skip to content

Commit

Permalink
After rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Mar 22, 2024
1 parent 02df582 commit 921d00f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 45 deletions.
73 changes: 30 additions & 43 deletions packages/realm_dart/lib/src/native/realm_core.dart
Expand Up @@ -14,7 +14,6 @@ import 'package:cancellation_token/cancellation_token.dart';
import 'package:crypto/crypto.dart';
// Hide StringUtf8Pointer.toNativeUtf8 and StringUtf16Pointer since these allows silently allocating memory. Use toUtf8Ptr instead
import 'package:ffi/ffi.dart' hide StringUtf8Pointer, StringUtf16Pointer;
import 'package:logging/logging.dart';
import 'package:path/path.dart' as path;
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:realm_common/realm_common.dart' as common show Decimal128;
Expand All @@ -26,6 +25,7 @@ import '../configuration.dart';
import '../credentials.dart';
import '../init.dart';
import '../list.dart';
import '../logging.dart';
import '../map.dart';
import '../migration.dart';
import '../realm_class.dart';
Expand All @@ -36,18 +36,18 @@ import '../session.dart';
import '../set.dart';
//import '../subscription.dart';
import '../user.dart';
import 'handle_base.dart';
import 'realm_bindings.dart';
import 'realm_library.dart';
import 'handle_base.dart';

// TODO: Use regular
part 'convert.dart';
part 'decimal128.dart';
part 'error_handling.dart';
part 'mutable_subscription_set_handle.dart';
part 'rooted_handle.dart';
part 'subscription_handle.dart';
part 'subscription_set_handle.dart';
part 'rooted_handle.dart';

final _pluginLib = () {
if (!isFlutterPlatform) {
Expand Down Expand Up @@ -93,20 +93,20 @@ class _RealmCore {
// This prevents reentrance if `realmCore` global variable is accessed during _RealmCore construction
realmCore = this;

_realmLib.realm_dart_init_debug_logger();
realmLib.realm_dart_init_debug_logger();
}

void loggerAttach() {
_realmLib.realm_dart_attach_logger(scheduler.nativePort);
realmLib.realm_dart_attach_logger(scheduler.nativePort);
}

void loggerDetach() {
_realmLib.realm_dart_detach_logger(scheduler.nativePort);
realmLib.realm_dart_detach_logger(scheduler.nativePort);
}

// for debugging only. Enable in realm_dart.cpp
// void invokeGC() {
// _realmLib.realm_dart_gc();
// realmLib.realm_dart_gc();
// }

SchemaHandle _createSchema(Iterable<SchemaObject> schema) {
Expand All @@ -119,8 +119,8 @@ class _RealmCore {
for (var i = 0; i < classCount; i++) {
final schemaObject = schema.elementAt(i);
final classInfo = (schemaClasses + i).ref;
final propertiesCount = schemaObject.properties.length;
final computedCount = schemaObject.properties.where((p) => p.isComputed).length;
final propertiesCount = schemaObject.length;
final computedCount = schemaObject.where((p) => p.isComputed).length;
final persistedCount = propertiesCount - computedCount;

classInfo.name = schemaObject.name.toCharPtr(arena);
Expand All @@ -133,7 +133,7 @@ class _RealmCore {
final properties = arena<realm_property_info_t>(propertiesCount);

for (var j = 0; j < propertiesCount; j++) {
final schemaProperty = schemaObject.properties[j];
final schemaProperty = schemaObject[j];
final propInfo = (properties + j).ref;
propInfo.name = schemaProperty.mapTo.toCharPtr(arena);
propInfo.public_name = (schemaProperty.mapTo != schemaProperty.name ? schemaProperty.name : '').toCharPtr(arena);
Expand Down Expand Up @@ -197,7 +197,7 @@ class _RealmCore {
realmLib.realm_config_set_max_number_of_active_versions(configHandle.pointer, config.maxNumberOfActiveVersions!);
}
if (config is LocalConfiguration) {
//_realmLib.realm_config_set_schema_mode(configHandle._pointer, realm_schema_mode.RLM_SCHEMA_MODE_ADDITIVE_DISCOVERED);
//realmLib.realm_config_set_schema_mode(configHandle.pointer, realm_schema_mode.RLM_SCHEMA_MODE_ADDITIVE_DISCOVERED);
if (config.initialDataCallback != null) {
realmLib.realm_config_set_data_initialization_function(
configHandle.pointer,
Expand Down Expand Up @@ -286,7 +286,7 @@ class _RealmCore {

// For sync and for dynamic Realms, we need to have a complete view of the schema in Core.
if (config.schemaObjects.isEmpty || config is FlexibleSyncConfiguration) {
_realmLib.realm_config_set_schema_subset_mode(configHandle._pointer, realm_schema_subset_mode.RLM_SCHEMA_SUBSET_MODE_COMPLETE);
realmLib.realm_config_set_schema_subset_mode(configHandle.pointer, realm_schema_subset_mode.RLM_SCHEMA_SUBSET_MODE_COMPLETE);
}

return configHandle;
Expand Down Expand Up @@ -754,12 +754,11 @@ class _RealmCore {

Map<String, RealmPropertyMetadata> _getPropertiesMetadata(Realm realm, int classKey, String? primaryKeyName, Arena arena) {
final propertyCountPtr = arena<Size>();
_realmLib.invokeGetBool(
() => _realmLib.realm_get_property_keys(realm.handle._pointer, classKey, nullptr, 0, propertyCountPtr), "Error getting property count");
invokeGetBool(() => realmLib.realm_get_property_keys(realm.handle.pointer, classKey, nullptr, 0, propertyCountPtr), "Error getting property count");

var propertyCount = propertyCountPtr.value;
final propertiesPtr = arena<realm_property_info_t>(propertyCount);
_realmLib.invokeGetBool(() => _realmLib.realm_get_class_properties(realm.handle._pointer, classKey, propertiesPtr, propertyCount, propertyCountPtr),
invokeGetBool(() => realmLib.realm_get_class_properties(realm.handle.pointer, classKey, propertiesPtr, propertyCount, propertyCountPtr),
"Error getting class properties.");

propertyCount = propertyCountPtr.value;
Expand All @@ -778,16 +777,6 @@ class _RealmCore {
return result;
}

RealmObjectHandle createRealmObject(Realm realm, int classKey) {
final realmPtr = _realmLib.invokeGetPointer(() => _realmLib.realm_object_create(realm.handle._pointer, classKey));
return RealmObjectHandle._(realmPtr, realm.handle);
}

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

RealmObjectHandle createRealmObject(Realm realm, int classKey) => RealmObjectHandle.create(realm.handle, classKey);

RealmObjectHandle createEmbeddedObject(RealmObjectBase obj, int propertyKey) => RealmObjectHandle.createEmbedded(obj.handle, propertyKey);
Expand Down Expand Up @@ -1612,8 +1601,8 @@ class _RealmCore {
}

RealmCallbackTokenHandle subscribeForSchemaNotifications(Realm realm) {
final pointer = _realmLib.invokeGetPointer(() => _realmLib.realm_add_schema_changed_callback(realm.handle._pointer,
Pointer.fromFunction(schema_change_callback), realm.toPersistentHandle(), _realmLib.addresses.realm_dart_delete_persistent_handle));
final pointer = invokeGetPointer(() => realmLib.realm_add_schema_changed_callback(realm.handle.pointer, Pointer.fromFunction(schema_change_callback),
realm.toPersistentHandle(), realmLib.addresses.realm_dart_delete_persistent_handle));

return RealmCallbackTokenHandle._(pointer, realm.handle);
}
Expand Down Expand Up @@ -1860,7 +1849,7 @@ class _RealmCore {

void logMessage(LogCategory category, LogLevel logLevel, String message) {
return using((arena) {
_realmLib.realm_dart_log(logLevel.index, category.toString().toCharPtr(arena), message.toCharPtr(arena));
realmLib.realm_dart_log(logLevel.index, category.toString().toCharPtr(arena), message.toCharPtr(arena));
});
}

Expand Down Expand Up @@ -2840,22 +2829,20 @@ class _RealmCore {
}
}

void setLogLevel(LogLevel level, {required LogCategory category}) {
using((arena) {
_realmLib.realm_set_log_level_category(category.toString().toCharPtr(arena), level.index);
});
}

List<String> getAllCategoryNames() {
return using((arena) {
final count = _realmLib.realm_get_category_names(0, nullptr);
final out_values = arena<Pointer<Char>>(count);
_realmLib.realm_get_category_names(count, out_values);
return [for (int i = 0; i < count; i++) out_values[i].cast<Utf8>().toDartString()];
});
}
}
void setLogLevel(LogLevel level, {required LogCategory category}) {
using((arena) {
realmLib.realm_set_log_level_category(category.toString().toCharPtr(arena), level.index);
});
}

List<String> getAllCategoryNames() {
return using((arena) {
final count = realmLib.realm_get_category_names(0, nullptr);
final out_values = arena<Pointer<Char>>(count);
realmLib.realm_get_category_names(count, out_values);
return [for (int i = 0; i < count; i++) out_values[i].cast<Utf8>().toDartString()];
});
}
}

class SchemaHandle extends HandleBase<realm_schema> {
Expand Down
2 changes: 2 additions & 0 deletions packages/realm_dart/lib/src/realm_class.dart
Expand Up @@ -11,6 +11,8 @@ import 'package:realm_common/realm_common.dart';

import 'configuration.dart';
import 'list.dart';
import 'logging.dart';
import 'map.dart';
import 'native/handle_base.dart';
import 'native/realm_core.dart';
import 'realm_object.dart';
Expand Down
8 changes: 6 additions & 2 deletions packages/realm_generator/lib/src/realm_model_info.dart
Expand Up @@ -44,8 +44,12 @@ class RealmModelInfo {
yield '{';
yield* notRequired.map((f) {
if (f.isRealmCollection) {
final collectionPrefix = f.type.isDartCoreList ? 'Iterable<' : f.type.isDartCoreSet ? 'Set<' : 'Map<String, ';
return '$collectionPrefix${f.type.basicMappedName}> ${f.name}${f.initializer},';
final collectionPrefix = f.type.isDartCoreList
? 'Iterable<'
: f.type.isDartCoreSet
? 'Set<'
: 'Map<String, ';
return '$collectionPrefix${f.type.basicMappedName}> ${f.name}${f.initializer},';
}
return '${f.mappedTypeName} ${f.name}${f.initializer},';
});
Expand Down

0 comments on commit 921d00f

Please sign in to comment.