Skip to content

Commit

Permalink
Read embeddedness from disk
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev committed Sep 16, 2020
1 parent e81fbe3 commit 7b9e687
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 7 deletions.
83 changes: 83 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,92 @@ v10 (TBD)
* We no longer support Realm Cloud (legacy), but instead the new "MongoDB Realm" Cloud. MongoDB Realm is a serverless platform that enables developers to quickly build applications without having to set up server infrastructure. MongoDB Realm is built on top of MongoDB Atlas, automatically integrating the connection to your database. ([#2011](https://github.com/realm/realm-dotnet/pull/2011))
* Remove support for Query-based sync, including the configuration parameters and the RLMSyncSubscription and SyncSubscription types. ([#2011](https://github.com/realm/realm-dotnet/pull/2011))
* Remove everything related to sync permissions, including both the path-based permission system and the object-level privileges for query-based sync. ([#2011](https://github.com/realm/realm-dotnet/pull/2011))
* Moved all API for dynamic access on the `Realm` class to `Realm.DynamicApi`:
* `Realm.CreateObject(string className, object primaryKey)` is now `Realm.DynamicApi.CreateObject(string className, object primaryKey)`.
* `Realm.All(string className)` is now `Realm.DynamicApi.All(string className)`.
* `Realm.RemoveAll(string className)` is now `Realm.DynamicApi.RemoveAll(string className)`.
* `Realm.Find(string className, long? primaryKey)` is now `Realm.DynamicApi.Find(string className, long? primaryKey)`.
* `Realm.Find(string className, string primaryKey)` is now `Realm.DynamicApi.Find(string className, string primaryKey)`.

### Enhancements
* Add support for the Decimal128 data type. This is a 128-bit IEEE 754 decimal floating point number. Properties of this type can be declared either as `MongoDB.Bson.Decimal128` type or the built-in `decimal` type. Note that .NET's built-in decimal is 96-bit, so it cannot represent the full range of numbers, representable by `Decimal128`. (PR [#2014](https://github.com/realm/realm-dotnet/pull/2014))
* Add support for embedded objects. Embedded objects are objects which are owned by a single parent object, and are deleted when that parent object is deleted or their parent no longer references them. Embedded objects are declared by subclassing `EmbeddedObject` instead of `RealmObject`. Reassigning an embedded object is not allowed and neither is linking to it from multiple parents. Querying for embedded objects directly is also disallowed as they should be viewed as complex structures belonging to their parents as opposed to standalone objects. A trivial example is:

```csharp
public class Address : EmbeddedObject
{
public string Street { get; set; }

public string City { get; set; }
}

public class Person : RealmObject
{
public string Name { get; set; }

// Address is an embedded object - you reference it as usual
public Address Address { get; set; }
}

public class Company : RealmObject
{
public string PhoneNumber { get; set; }

// Embedded objects can be contained in lists too
public IList<Address> OfficeAddresses { get; }
}
```

* Added new dynamic methods for instantiating embedded objects:
* `Realm.DynamicApi.CreateEmbeddedObjectForProperty` should be used to create an embedded object and assign it to a parent's property. For example:

```csharp
// static API
var person = new Person();
person.Address = new Address
{
City = "New York"
};

// dynamic API
var dynamicPerson = realm.DynamicApi.CreateObject("Person");
var address = realm.DynamicApi.CreateEmbeddedObjectForProperty(dynamicPerson, "Address")
address.City = "New York";
```

* `Realm.DynamicApi.AddEmbeddedObjectToList` should be used to create an embedded object and add it to a parent's list property.
* `Realm.DynamicApi.InsertEmbeddedObjectInList` should be used to create an embedded object and insert it in a parent's list property at a specified index.
* `Realm.DynamicApi.SetEmbeddedObjectInList` should be used to create an embedded object and set it at an index in a parent's list property.

```csharp
// static API
var company = new Company();
company.OfficeAddresses.Add(new Address
{
City = "New York"
});

company.OfficeAddresses.Insert(0, new Address
{
City = "Palo Alto"
});

company.OfficeAddresses[1] = new Address
{
City = "New Jersey"
};

// dynamic API
var dynamicCompany = realm.DynamicApi.CreateObject("Company");
var officeToAdd = realm.DynamicApi.AddEmbeddedObjectToList(dynamicCompany.OfficeAddresses) ;
officeToAdd.City = "New York";

var officeToInsert = realm.DynamicApi.InsertEmbeddedObjectInList(dynamicCompany. OfficeAddresses, 0);
officeToInsert.City = "Palo Alto";

var officeToSet = realm.DynamicApi.SetEmbeddedObjectInList(dynamicCompany. OfficeAddresses, 1);
officeToSet.City = "New Jersey";
```

### Fixed

Expand Down
3 changes: 1 addition & 2 deletions Realm/Realm/Schema/RealmSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ internal static RealmSchema CreateFromObjectStoreSchema(Native.Schema nativeSche
{
var objectSchema = Marshal.PtrToStructure<Native.SchemaObject>(IntPtr.Add(nativeSchema.objects, i * Native.SchemaObject.Size));

// V10TODO: read isEmbedded from OS schema
var builder = new ObjectSchema.Builder(objectSchema.name, isEmbedded: false);
var builder = new ObjectSchema.Builder(objectSchema.name, objectSchema.is_embedded);

for (var n = objectSchema.properties_start; n < objectSchema.properties_end; n++)
{
Expand Down
30 changes: 25 additions & 5 deletions Tests/Realm.Tests/Database/InstanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ public void GetInstance_WhenDynamic_ReadsSchemaFromDisk()
{
var config = new RealmConfiguration(Path.GetTempFileName())
{
ObjectClasses = new[] { typeof(AllTypesObject) }
ObjectClasses = new[] { typeof(AllTypesObject), typeof(ObjectWithEmbeddedProperties), typeof(EmbeddedAllTypesObject), typeof(RecursiveEmbeddedObject) }
};

try
Expand All @@ -702,23 +702,43 @@ public void GetInstance_WhenDynamic_ReadsSchemaFromDisk()
Int32Property = 42,
RequiredStringProperty = "This is required!"
}));

realm.Write(() => realm.Add(new ObjectWithEmbeddedProperties
{
AllTypesObject = new EmbeddedAllTypesObject
{
Int32Property = 24,
StringProperty = "This is not required!"
}
}));
}

config.IsDynamic = true;

using (var dynamicRealm = Realm.GetInstance(config))
{
Assert.That(dynamicRealm.Schema.Count == 1);
Assert.That(dynamicRealm.Schema.Count == 4);

var objectSchema = dynamicRealm.Schema.Find(nameof(AllTypesObject));
Assert.That(objectSchema, Is.Not.Null);
var allTypesSchema = dynamicRealm.Schema.Find(nameof(AllTypesObject));
Assert.That(allTypesSchema, Is.Not.Null);
Assert.That(allTypesSchema.IsEmbedded, Is.False);

var hasExpectedProp = objectSchema.TryFindProperty(nameof(AllTypesObject.RequiredStringProperty), out var requiredStringProp);
var hasExpectedProp = allTypesSchema.TryFindProperty(nameof(AllTypesObject.RequiredStringProperty), out var requiredStringProp);
Assert.That(hasExpectedProp);
Assert.That(requiredStringProp.Type, Is.EqualTo(PropertyType.String));

var ato = dynamicRealm.DynamicApi.All(nameof(AllTypesObject)).Single();
Assert.That(ato.RequiredStringProperty, Is.EqualTo("This is required!"));

var embeddedAllTypesSchema = dynamicRealm.Schema.Find(nameof(EmbeddedAllTypesObject));
Assert.That(embeddedAllTypesSchema, Is.Not.Null);
Assert.That(embeddedAllTypesSchema.IsEmbedded, Is.True);

Assert.That(embeddedAllTypesSchema.TryFindProperty(nameof(EmbeddedAllTypesObject.StringProperty), out var stringProp), Is.True);
Assert.That(stringProp.Type, Is.EqualTo(PropertyType.String | PropertyType.Nullable));

var embeddedParent = dynamicRealm.DynamicApi.All(nameof(ObjectWithEmbeddedProperties)).Single();
Assert.That(embeddedParent.AllTypesObject.StringProperty, Is.EqualTo("This is not required!"));
}
}
finally
Expand Down

0 comments on commit 7b9e687

Please sign in to comment.