Skip to content

Commit

Permalink
Revert to using property indices
Browse files Browse the repository at this point in the history
(cherry picked from commit f00e79a7ebed995bfeb7b64914a593d34eb7ef91)
  • Loading branch information
nirinchev committed Oct 2, 2020
1 parent fb91f5d commit 2009c81
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 441 deletions.
35 changes: 16 additions & 19 deletions Realm/Realm/Dynamic/MetaRealmObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
MethodInfo getter = null;
if (property.Type.UnderlyingType() == PropertyType.LinkingObjects)
{
arguments.Add(Expression.Constant(_metadata.ComputedProperties[property.Name]));
arguments.Add(Expression.Constant(_metadata.PropertyIndices[property.Name]));
getter = GetGetMethod(DummyHandle.GetBacklinks);
}
else if (property.Type.IsArray())
{
arguments.Add(Expression.Field(GetLimitedSelf(), RealmObjectRealmField));
arguments.Add(Expression.Constant(_metadata.ColumnKeys[property.Name]));
arguments.Add(Expression.Constant(_metadata.PropertyIndices[property.Name]));
arguments.Add(Expression.Constant(property.ObjectType, typeof(string)));
switch (property.Type.UnderlyingType())
{
Expand Down Expand Up @@ -144,7 +144,7 @@ public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
}
else
{
arguments.Add(Expression.Constant(_metadata.ColumnKeys[property.Name]));
arguments.Add(Expression.Constant(_metadata.PropertyIndices[property.Name]));
switch (property.Type.UnderlyingType())
{
case PropertyType.Int:
Expand Down Expand Up @@ -186,7 +186,6 @@ public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
else
{
expression = Expression.Call(self, RealmObjectGetBacklinksForHandle_RealmObject, Expression.Constant(binder.Name), expression);

}
}

Expand Down Expand Up @@ -214,7 +213,7 @@ public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicM

var arguments = new List<Expression>
{
Expression.Constant(_metadata.ColumnKeys[property.Name])
Expression.Constant(_metadata.PropertyIndices[property.Name])
};

MethodInfo setter = null;
Expand Down Expand Up @@ -306,24 +305,22 @@ private bool IsTargetEmbedded(Property property)
return metadata.Schema.IsEmbedded;
}

// GetString(colKey)
// GetByteArray(colKey)
private static MethodInfo GetGetMethod<TResult>(Func<ColumnKey, TResult> @delegate) => @delegate.GetMethodInfo();

// GetPrimitive(colKey, propertyType)
private static MethodInfo GetGetMethod<TResult>(Func<ColumnKey, PropertyType, TResult> @delegate) => @delegate.GetMethodInfo();

// GetString(propertyIndex)
// GetByteArray(propertyIndex)
// GetBacklinks(propertyIndex)
private static MethodInfo GetGetMethod<TResult>(Func<IntPtr, TResult> @delegate) => @delegate.GetMethodInfo();

// GetList(realm, colKey, objectType)
// GetObject(realm, colKey, objectType)
private static MethodInfo GetGetMethod<TResult>(Func<Realm, ColumnKey, string, TResult> @delegate) => @delegate.GetMethodInfo();
// GetPrimitive(propertyIndex, propertyType)
private static MethodInfo GetGetMethod<TResult>(Func<IntPtr, PropertyType, TResult> @delegate) => @delegate.GetMethodInfo();

// GetList(realm, propertyIndex, objectType)
// GetObject(realm, propertyIndex, objectType)
private static MethodInfo GetGetMethod<TResult>(Func<Realm, IntPtr, string, TResult> @delegate) => @delegate.GetMethodInfo();

// SetXXX(colKey)
private static MethodInfo GetSetMethod<TValue>(Action<ColumnKey, TValue> @delegate) => @delegate.GetMethodInfo();
// SetXXX(propertyIndex)
private static MethodInfo GetSetMethod<TValue>(Action<IntPtr, TValue> @delegate) => @delegate.GetMethodInfo();

// SetObject(this, colKey)
private static MethodInfo GetSetMethod<TValue>(Action<Realm, ColumnKey, TValue> @delegate) => @delegate.GetMethodInfo();
// SetObject(this, propertyIndex)
private static MethodInfo GetSetMethod<TValue>(Action<Realm, IntPtr, TValue> @delegate) => @delegate.GetMethodInfo();
}
}
108 changes: 54 additions & 54 deletions Realm/Realm/Handles/ObjectHandle.cs

Large diffs are not rendered by default.

126 changes: 53 additions & 73 deletions Realm/Realm/Handles/QueryHandle.cs

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions Realm/Realm/Handles/SharedRealmHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ private static class NativeMethods
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool refresh(SharedRealmHandle sharedRealm, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_realm_get_table_info", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr get_table_info(SharedRealmHandle sharedRealm, [MarshalAs(UnmanagedType.LPWStr)] string tableName, IntPtr tableNameLength, [Out] ColumnKey[] column_keys, 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_is_same_instance", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.U1)]
Expand Down Expand Up @@ -242,12 +242,11 @@ public bool Refresh()
return result;
}

public (TableHandle TableHandle, ColumnKey[] ColumnKeys) GetTableInfo(string tableName, int propertiesCount)
public TableHandle GetTable(string tableName)
{
var columnKeys = new ColumnKey[propertiesCount];
var result = NativeMethods.get_table_info(this, tableName, (IntPtr)tableName.Length, columnKeys, out var nativeException);
var result = NativeMethods.get_table(this, tableName, (IntPtr)tableName.Length, out var nativeException);
nativeException.ThrowIfNecessary();
return (new TableHandle(this, result), columnKeys);
return new TableHandle(this, result);
}

public bool IsSameInstance(SharedRealmHandle other)
Expand Down
10 changes: 3 additions & 7 deletions Realm/Realm/Handles/SortDescriptorHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@

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

namespace Realms
{
internal class SortDescriptorHandle : RealmHandle
{
// This is a delegate type meant to represent one of the "query operator" methods such as float_less and bool_equal
internal delegate void Operation<T>(QueryHandle queryPtr, ColumnKey columnKey, T value);

private static class NativeMethods
{
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "sort_descriptor_destroy", CallingConvention = CallingConvention.Cdecl)]
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,
[MarshalAs(UnmanagedType.LPArray), In] ColumnKey[] column_key_chain, IntPtr column_keys_count,
[MarshalAs(UnmanagedType.LPArray), In] IntPtr[] property_index_chain, IntPtr column_keys_count,
[MarshalAs(UnmanagedType.U1)] bool ascending,
out NativeException ex);
}
Expand All @@ -43,9 +39,9 @@ public SortDescriptorHandle(RealmHandle root, IntPtr handle) : base(root, handle
{
}

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

Expand Down
Loading

0 comments on commit 2009c81

Please sign in to comment.