Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RealmMetadata and remove TableHandle #2230

Merged
merged 10 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Realm/Realm/DatabaseTypes/RealmObjectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using Realms.DataBinding;
using Realms.Exceptions;
using Realms.Helpers;
using Realms.Native;
using Realms.Schema;
using Realms.Weaving;

Expand Down Expand Up @@ -255,7 +256,7 @@ public IQueryable<dynamic> GetBacklinks(string objectType, string property)
Argument.Ensure(Realm.Metadata.TryGetValue(objectType, out var relatedMeta), $"Could not find schema for type {objectType}", nameof(objectType));
Argument.Ensure(relatedMeta.PropertyIndices.ContainsKey(property), $"Type {objectType} does not contain property {property}", nameof(property));

var resultsHandle = ObjectHandle.GetBacklinksForType(relatedMeta.Table, relatedMeta.PropertyIndices[property]);
var resultsHandle = ObjectHandle.GetBacklinksForType(relatedMeta.TableKey, relatedMeta.PropertyIndices[property]);
if (relatedMeta.Schema.IsEmbedded)
{
return new RealmResults<EmbeddedObject>(Realm, resultsHandle, relatedMeta);
Expand Down Expand Up @@ -435,17 +436,17 @@ public TypeInfo GetTypeInfo()

internal class Metadata
{
internal readonly TableHandle Table;
internal readonly TableKey TableKey;

internal readonly IRealmObjectHelper Helper;

internal readonly IReadOnlyDictionary<string, IntPtr> PropertyIndices;

internal readonly ObjectSchema Schema;

public Metadata(TableHandle table, IRealmObjectHelper helper, IDictionary<string, IntPtr> propertyIndices, ObjectSchema schema)
public Metadata(TableKey tableKey, IRealmObjectHelper helper, IDictionary<string, IntPtr> propertyIndices, ObjectSchema schema)
{
Table = table;
TableKey = tableKey;
Helper = helper;
PropertyIndices = new ReadOnlyDictionary<string, IntPtr>(propertyIndices);
Schema = schema;
Expand Down
6 changes: 3 additions & 3 deletions Realm/Realm/Handles/ObjectHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static class NativeMethods
public static extern IntPtr get_backlinks(ObjectHandle objectHandle, IntPtr property_index, out NativeException nativeException);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "object_get_backlinks_for_type", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr get_backlinks_for_type(ObjectHandle objectHandle, TableHandle source_table, IntPtr source_property_index, out NativeException nativeException);
public static extern IntPtr get_backlinks_for_type(ObjectHandle objectHandle, TableKey table_key, IntPtr source_property_index, out NativeException nativeException);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "object_get_thread_safe_reference", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr get_thread_safe_reference(ObjectHandle objectHandle, out NativeException ex);
Expand Down Expand Up @@ -260,9 +260,9 @@ public ResultsHandle GetBacklinks(IntPtr propertyIndex)
return new ResultsHandle(this, resultsPtr);
}

public ResultsHandle GetBacklinksForType(TableHandle table, IntPtr propertyIndex)
public ResultsHandle GetBacklinksForType(TableKey tableKey, IntPtr propertyIndex)
{
var resultsPtr = NativeMethods.get_backlinks_for_type(this, table, propertyIndex, out var nativeException);
var resultsPtr = NativeMethods.get_backlinks_for_type(this, tableKey, propertyIndex, out var nativeException);
nativeException.ThrowIfNecessary();

return new ResultsHandle(this, resultsPtr);
Expand Down
52 changes: 41 additions & 11 deletions Realm/Realm/Handles/SharedRealmHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public static extern IntPtr open_with_sync_async(Configuration configuration, Sy
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool refresh(SharedRealmHandle sharedRealm, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_get_table", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr get_table(SharedRealmHandle sharedRealm, [MarshalAs(UnmanagedType.LPWStr)] string tableName, IntPtr tableNameLength, out NativeException ex);
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_get_table_key", CallingConvention = CallingConvention.Cdecl)]
public static extern TableKey get_table_key(SharedRealmHandle sharedRealm, [MarshalAs(UnmanagedType.LPWStr)] string tableName, IntPtr tableNameLength, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_is_same_instance", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.U1)]
Expand All @@ -124,10 +124,10 @@ public static extern IntPtr open_with_sync_async(Configuration configuration, Sy
public static extern void write_copy(SharedRealmHandle sharedRealm, [MarshalAs(UnmanagedType.LPWStr)] string path, IntPtr path_len, byte[] encryptionKey, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_create_object", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr create_object(SharedRealmHandle sharedRealm, TableHandle table, out NativeException ex);
public static extern IntPtr create_object(SharedRealmHandle sharedRealm, TableKey table_key, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_create_object_unique", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr create_object_unique(SharedRealmHandle sharedRealm, TableHandle table, PrimitiveValue value,
public static extern IntPtr create_object_unique(SharedRealmHandle sharedRealm, TableKey table_key, PrimitiveValue value,
[MarshalAs(UnmanagedType.U1)] bool update,
[MarshalAs(UnmanagedType.U1)] out bool is_new, out NativeException ex);

Expand All @@ -148,6 +148,12 @@ public static extern IntPtr create_object_unique(SharedRealmHandle sharedRealm,
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_freeze", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr freeze(SharedRealmHandle sharedRealm, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_get_object_for_primary_key", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr get_object_for_primary_key(SharedRealmHandle realmHandle, TableKey table_key, PrimitiveValue value, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_create_results", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr create_results(SharedRealmHandle sharedRealm, TableKey table_key, out NativeException ex);

#pragma warning restore IDE1006 // Naming Styles
}

Expand Down Expand Up @@ -275,11 +281,11 @@ public bool Refresh()
return result;
}

public TableHandle GetTable(string tableName)
public TableKey GetTableKey(string tableName)
{
var result = NativeMethods.get_table(this, tableName, (IntPtr)tableName.Length, out var nativeException);
var tableKey = NativeMethods.get_table_key(this, tableName, (IntPtr)tableName.Length, out var nativeException);
nativeException.ThrowIfNecessary();
return new TableHandle(this, result);
return tableKey;
}

public bool IsSameInstance(SharedRealmHandle other)
Expand Down Expand Up @@ -331,14 +337,14 @@ public void GetSchema(Action<Native.Schema> callback)
nativeException.ThrowIfNecessary();
}

public ObjectHandle CreateObject(TableHandle table)
public ObjectHandle CreateObject(TableKey tableKey)
{
var result = NativeMethods.create_object(this, table, out NativeException ex);
var result = NativeMethods.create_object(this, tableKey, out NativeException ex);
ex.ThrowIfNecessary();
return new ObjectHandle(this, result);
}

public unsafe ObjectHandle CreateObjectWithPrimaryKey(Property pkProperty, object primaryKey, TableHandle table, string parentType, bool update, out bool isNew)
public unsafe ObjectHandle CreateObjectWithPrimaryKey(Property pkProperty, object primaryKey, TableKey tableKey, string parentType, bool update, out bool isNew)
{
if (primaryKey == null && !pkProperty.Type.IsNullable())
{
Expand All @@ -355,7 +361,7 @@ public unsafe ObjectHandle CreateObjectWithPrimaryKey(Property pkProperty, objec
};

var (primitiveValue, handles) = pkValue.ToNative();
var result = NativeMethods.create_object_unique(this, table, primitiveValue, update, out isNew, out var ex);
var result = NativeMethods.create_object_unique(this, tableKey, primitiveValue, update, out isNew, out var ex);
handles?.Dispose();
ex.ThrowIfNecessary();
return new ObjectHandle(this, result);
Expand All @@ -373,6 +379,30 @@ public SharedRealmHandle Freeze()
return new SharedRealmHandle(result);
}

public unsafe bool TryFindObject(TableKey tableKey, in RealmValue id, out ObjectHandle objectHandle)
{
var (primitiveValue, handles) = id.ToNative();
var result = NativeMethods.get_object_for_primary_key(this, tableKey, primitiveValue, out var ex);
handles?.Dispose();
ex.ThrowIfNecessary();

if (result == IntPtr.Zero)
{
objectHandle = null;
return false;
}

objectHandle = new ObjectHandle(this, result);
return true;
}

public ResultsHandle CreateResults(TableKey tableKey)
{
var result = NativeMethods.create_results(this, tableKey, out var nativeException);
nativeException.ThrowIfNecessary();
return new ResultsHandle(this, result);
}

[MonoPInvokeCallback(typeof(NativeMethods.GetNativeSchemaCallback))]
private static void GetNativeSchema(Native.Schema schema, IntPtr managedCallbackPtr)
{
Expand Down
7 changes: 4 additions & 3 deletions Realm/Realm/Handles/SortDescriptorHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Runtime.InteropServices;
using Realms.Native;

namespace Realms
{
Expand All @@ -29,7 +30,7 @@ private static class NativeMethods
public static extern void destroy(IntPtr queryHandle);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "sort_descriptor_add_clause", CallingConvention = CallingConvention.Cdecl)]
public static extern void add_clause(SortDescriptorHandle descriptor, TableHandle query, SharedRealmHandle realm,
public static extern void add_clause(SortDescriptorHandle descriptor, TableKey table_key, SharedRealmHandle realm,
[MarshalAs(UnmanagedType.LPArray), In] IntPtr[] property_index_chain, IntPtr column_keys_count,
[MarshalAs(UnmanagedType.U1)] bool ascending,
out NativeException ex);
Expand All @@ -39,9 +40,9 @@ public SortDescriptorHandle(RealmHandle root, IntPtr handle) : base(root, handle
{
}

public void AddClause(TableHandle table, SharedRealmHandle realm, IntPtr[] propertyIndexChain, bool ascending)
public void AddClause(SharedRealmHandle realm, TableKey tableKey, IntPtr[] propertyIndexChain, bool ascending)
{
NativeMethods.add_clause(this, table, realm, propertyIndexChain, (IntPtr)propertyIndexChain.Length, ascending, out var nativeException);
NativeMethods.add_clause(this, tableKey, realm, propertyIndexChain, (IntPtr)propertyIndexChain.Length, ascending, out var nativeException);
nativeException.ThrowIfNecessary();
}

Expand Down
94 changes: 0 additions & 94 deletions Realm/Realm/Handles/TableHandle.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Realm/Realm/Linq/RealmResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal RealmResults(Realm realm, ResultsHandle handle, RealmObjectBase.Metadat
}

internal RealmResults(Realm realm, RealmObjectBase.Metadata metadata)
: this(realm, metadata.Table.CreateResults(realm.SharedRealmHandle), metadata)
: this(realm, realm.SharedRealmHandle.CreateResults(metadata.TableKey), metadata)
{
}

Expand Down
2 changes: 1 addition & 1 deletion Realm/Realm/Linq/RealmResultsVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void AddSort(LambdaExpression lambda, bool ascending)
}

var propertyChain = TraverseSort(body);
_sortDescriptor.AddClause(_metadata.Table, _realm.SharedRealmHandle, propertyChain, ascending);
_sortDescriptor.AddClause(_realm.SharedRealmHandle, _metadata.TableKey, propertyChain, ascending);
}

private IntPtr[] TraverseSort(MemberExpression expression)
Expand Down
23 changes: 7 additions & 16 deletions Realm/Realm/Native/ObjectKey.cs → Realm/Realm/Native/TableKey.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2020 Realm Inc.
// Copyright 2021 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,27 +22,18 @@
namespace Realms.Native
{
[StructLayout(LayoutKind.Sequential)]
internal struct ObjectKey : IEquatable<ObjectKey>
internal struct TableKey : IEquatable<TableKey>
{
private Int64 value;
private UInt32 value;

public bool Equals(ObjectKey other) => value.Equals(other.value);
public bool Equals(TableKey other) => value.Equals(other.value);

public override bool Equals(object obj)
{
switch (obj)
{
case ObjectKey other:
return value.Equals(other.value);
default:
return false;
}
}
public override bool Equals(object obj) => obj is TableKey other && Equals(other);

public override int GetHashCode() => value.GetHashCode();

public static bool operator ==(ObjectKey left, ObjectKey right) => left.value == right.value;
public static bool operator ==(TableKey left, TableKey right) => left.value == right.value;

public static bool operator !=(ObjectKey left, ObjectKey right) => left.value != right.value;
public static bool operator !=(TableKey left, TableKey right) => left.value != right.value;
}
}
Loading