Skip to content

Commit

Permalink
Collections of Mixed (#3441)
Browse files Browse the repository at this point in the history
* Added stub

* Stubs with comments

* Notes

* Various fixes

* Updated core

* Finished stub

* Removed comment [skip-ci]

* Removed unused

* Added stub for lists in lists

* Fixed error with null and added basic test with all types

* Improved base testing for list, added support for add/insert/set and equality checking

* Improved test and removed useless content

* Added tests

* Small fixes

* Small fixes

* Added common method for creating a list and adding elements

* Fixed error [skip-ci]

* Added set and dictionary to primitive value

* Various improvements

* Added conversion per list

* Fixed Operator

* Added missing conversion methods

* Corrected wrapper

* Added test helper method for realm value types and removed unused reflextion extension method

* Added missing types for marshaling

* Small improvement in operator

* Small corrections

* Added error for sets adding collections

* Trting to uniform access

* Created common method for collections

* Various improvements [skip-ci]

* Fixed dictionary and added more tests

* Small corrections

* Improved tests for dictionaries

* Small fixes

* Simplified set collection on object

* Added test for dynamic and simplified collection generation

* Updated db

* Updated changelog [skip-ci]

* Updated another db

* Updated db

* Updated changelog

* Test

* Fix to generated files

* Test

* Small improvement

* Added field offset

* Working version

* Small renaming

* Reordering

* Working version

* Solution 1

* Final version of the RealmValue explicit layout

* Added changelog

* Removed set from RealmValue

* Continuing removal of set

* Simplified CreateInternalCollectionAndPopulate

* Ulterior removal of set and added method parameter name

* Removed other set-related things, added implicit method from array of RealmValue

* Changed name to method

* Added explicit sequence equals

* Unified set_collection and add_collection for dictionary

* Moved _intValue out of primitiveValue

* Removed unnecessary structs

* Changed return value

* Small corrections

* Fixed changelog

* Updated core and a couple of fixes

* Small corrections

* Updated dbs

* Updated core to 14.0.1 and changelog

* Added test for collection indexes

* Updated changelog

* Fixed realm studio version

* Updated guids to v24

* Corrected methods

* Corrected changelog and wrapper

* Added comments

* Corrected changelog

* Corrected changelog

* Updated core

* Updated changelog

* Used correct core version

* Fixed changelog

* Fixed changelog

* Added missing tests

* Fixed tests

* [RNET-1092] Added support for indexed RealmValue (#3544)

* Added support for indexed RealmValue

* Fixed changelog

* Corrected indexable property types in schema

* Added missing newline in the changelog

* Testing fix for CI
  • Loading branch information
papafe authored Mar 7, 2024
1 parent 311b797 commit 7c25a1d
Show file tree
Hide file tree
Showing 38 changed files with 2,152 additions and 388 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
* Opening realm with file format 23 or lower (Realm .NET versions earlier than 12.0.0) in read-only mode will crash. (Core 14.0.0)

### Enhancements
* Added support for list and dictionaries of `RealmValue` (`IList<RealmValue>` and `IDictionary<string, RealmValue>`) to be contained in a `RealmValue`. Lists and dictionaries can contain an arbitrary number of collections themselves. It is possible to convert an existing collection to a `RealmValue` using the new static methods `RealmValue.List` and `RealmValue.Dictionary` or using the implicit operators if converting from common types like `List`, `RealmValue[]` or `Dictionary`. Finally, it is possible to obtain the contained collections by using the new conversion method `AsList` and `AsDictionary`. For example:

```csharp
var list = new List<RealmValue> { 1, true, "stringVal" };

var rvo = realm.Write(() =>
{
return realm.Add(new RealmValueObject { RealmValueProperty = list});
});

var retrievedList = rvo.RealmValueProperty.AsList();
```
(PR [#3441](https://github.com/realm/realm-dotnet/pull/3441))
* Reduced memory usage of `RealmValue`. (PR [#3441](https://github.com/realm/realm-dotnet/pull/3441))
* Add support for passing a key paths collection (`KeyPathsCollection`) when using `IRealmCollection.SubscribeForNotifications`. Passing a `KeyPathsCollection` allows to specify which changes in properties should raise a notification.

A `KeyPathsCollection` can be obtained by:
Expand Down Expand Up @@ -63,6 +77,7 @@
var query5 = people.Filter("ListOfDogs[SIZE] = $0", 3)
```
(Core 14.0.0)
* Added support for indexed `RealmValue` properties. (PR [#3544](https://github.com/realm/realm-dotnet/pull/3544))

### Fixed
* Fixed RQL (`.Filter()`) queries like `indexed_property == NONE {x}` which mistakenly matched on only x instead of not x. This only applies when an indexed property with equality (==, or IN) matches with `NONE` on a list of one item. If the constant list contained more than one value then it was working correctly. (Core 13.27.0)
Expand Down
1 change: 1 addition & 0 deletions Realm/Realm.SourceGenerator/InfoClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ internal abstract record PropertyTypeInfo
ScalarType.ObjectId,
ScalarType.Guid,
ScalarType.Date,
ScalarType.RealmValue,
};

private static readonly HashSet<ScalarType> _primaryKeyTypes = new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal static class PropertyDefinitionExtensions
DateTimeOffsetTypeName,
ObjectIdTypeName,
GuidTypeName,
RealmValueTypeName,
};

internal static bool IsAutomatic(this PropertyDefinition property)
Expand Down
17 changes: 17 additions & 0 deletions Realm/Realm/DatabaseTypes/RealmDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public TValue this[string key]
ValidateKey(key);
var realmValue = Operator.Convert<TValue, RealmValue>(value);

if (realmValue.Type.IsCollection())
{
CollectionHelpers.PopulateCollection(Realm, _dictionaryHandle.SetCollection(key, realmValue.Type), realmValue);
return;
}

if (_isEmbedded && realmValue.Type != RealmValueType.Null)
{
if (IsDynamic)
Expand Down Expand Up @@ -124,6 +130,12 @@ public void Add(string key, TValue value)
ValidateKey(key);
var realmValue = Operator.Convert<TValue, RealmValue>(value);

if (realmValue.Type.IsCollection())
{
CollectionHelpers.PopulateCollection(Realm, _dictionaryHandle.AddCollection(key, realmValue.Type), realmValue);
return;
}

if (_isEmbedded && realmValue.Type != RealmValueType.Null)
{
if (IsDynamic)
Expand Down Expand Up @@ -163,6 +175,11 @@ public bool Remove(KeyValuePair<string, TValue> item)
return false;
}

if (realmValue.Type.IsCollection())
{
return false;
}

return _dictionaryHandle.Remove(item.Key, realmValue);
}

Expand Down
26 changes: 25 additions & 1 deletion Realm/Realm/DatabaseTypes/RealmList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using Realms.Dynamic;
using Realms.Helpers;
Expand Down Expand Up @@ -66,6 +67,12 @@ internal RealmList(Realm realm, ListHandle adoptedList, Metadata? metadata) : ba
ValidateIndex(index);
var realmValue = ValidateValueToInsert(value);

if (realmValue.Type.IsCollection())
{
CollectionHelpers.PopulateCollection(Realm, _listHandle.SetCollection(index, realmValue.Type), realmValue);
return;
}

if (_isEmbedded)
{
if (IsDynamic)
Expand All @@ -88,6 +95,12 @@ public void Add(T value)
{
var realmValue = ValidateValueToInsert(value);

if (realmValue.Type.IsCollection())
{
CollectionHelpers.PopulateCollection(Realm, _listHandle.AddCollection(realmValue.Type), realmValue);
return;
}

if (_isEmbedded)
{
if (IsDynamic)
Expand All @@ -112,6 +125,11 @@ public override int IndexOf([AllowNull] T value)
return -1;
}

if (realmValue.Type.IsCollection())
{
return -1;
}

return _listHandle.Find(realmValue);
}

Expand All @@ -120,6 +138,12 @@ public void Insert(int index, T value)
ValidateIndex(index);
var realmValue = ValidateValueToInsert(value);

if (realmValue.Type.IsCollection())
{
CollectionHelpers.PopulateCollection(Realm, _listHandle.InsertCollection(index, realmValue.Type), realmValue);
return;
}

if (_isEmbedded)
{
if (IsDynamic)
Expand Down Expand Up @@ -151,7 +175,7 @@ public override void RemoveAt(int index)
{
ValidateIndex(index);

_listHandle.Erase((IntPtr)index);
_listHandle.Erase(index);
}

#endregion
Expand Down
5 changes: 5 additions & 0 deletions Realm/Realm/DatabaseTypes/RealmSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public bool Add(T value)

var realmValue = Operator.Convert<T, RealmValue>(value);

if (realmValue.Type.IsCollection())
{
throw new InvalidOperationException("Set cannot contain other collections.");
}

AddToRealmIfNecessary(realmValue);
return _setHandle.Add(realmValue);
}
Expand Down
Loading

0 comments on commit 7c25a1d

Please sign in to comment.