Пример #1
0
        internal override Realm CreateRealm(RealmSchema schema)
        {
            var srHandle = new SharedRealmHandle();

            var configuration = new Native.Configuration
            {
                Path      = DatabasePath,
                read_only = IsReadOnly,
                delete_if_migration_needed = ShouldDeleteIfMigrationNeeded,
                schema_version             = SchemaVersion
            };

            Migration migration = null;

            if (MigrationCallback != null)
            {
                migration = new Migration(this, schema);
                migration.PopulateConfiguration(ref configuration);
            }

            var srPtr = IntPtr.Zero;

            try
            {
                srPtr = srHandle.Open(configuration, schema, EncryptionKey);
            }
            catch (ManagedExceptionDuringMigrationException)
            {
                throw new AggregateException("Exception occurred in a Realm migration callback. See inner exception for more details.", migration?.MigrationException);
            }

            srHandle.SetHandle(srPtr);
            return(new Realm(srHandle, this, schema));
        }
Пример #2
0
        internal static Realm GetInstance(RealmConfiguration config, RealmSchema schema)
        {
            config = config ?? RealmConfiguration.DefaultConfiguration;

            var srHandle = new SharedRealmHandle();

            if (schema == null)
            {
                if (config.ObjectClasses != null)
                {
                    schema = RealmSchema.CreateSchemaForClasses(config.ObjectClasses);
                }
                else
                {
                    schema = RealmSchema.Default;
                }
            }

            var configuration = new Native.Configuration
            {
                Path      = config.DatabasePath,
                read_only = config.ReadOnly,
                delete_if_migration_needed = config.ShouldDeleteIfMigrationNeeded,
                schema_version             = config.SchemaVersion
            };

            Migration migration = null;

            if (config.MigrationCallback != null)
            {
                migration = new Migration(config, schema);
                migration.PopulateConfiguration(ref configuration);
            }

            var srPtr = IntPtr.Zero;

            try
            {
                srPtr = srHandle.Open(configuration, schema, config.EncryptionKey);
            }
            catch (ManagedExceptionDuringMigrationException)
            {
                throw new AggregateException("Exception occurred in a Realm migration callback. See inner exception for more details.", migration?.MigrationException);
            }

            RuntimeHelpers.PrepareConstrainedRegions();
            try { /* Retain handle in a constrained execution region */ }
            finally
            {
                srHandle.SetHandle(srPtr);
            }

            return(new Realm(srHandle, config, schema));
        }
        internal override Realm CreateRealm(RealmSchema schema)
        {
            var configuration = new Configuration
            {
                Path      = DatabasePath,
                read_only = IsReadOnly,
                delete_if_migration_needed = ShouldDeleteIfMigrationNeeded,
                schema_version             = SchemaVersion
            };

            Migration migration = null;

            if (MigrationCallback != null)
            {
                migration = new Migration(this, schema);
                migration.PopulateConfiguration(ref configuration);
            }

            if (ShouldCompactOnLaunch != null)
            {
                var handle = GCHandle.Alloc(ShouldCompactOnLaunch);
                configuration.should_compact_callback         = ShouldCompactOnLaunchCallback;
                configuration.managed_should_compact_delegate = GCHandle.ToIntPtr(handle);
            }

            var srPtr = IntPtr.Zero;

            try
            {
                srPtr = SharedRealmHandle.Open(configuration, schema, EncryptionKey);
            }
            catch (ManagedExceptionDuringMigrationException)
            {
                throw new AggregateException("Exception occurred in a Realm migration callback. See inner exception for more details.", migration?.MigrationException);
            }

            var srHandle = new SharedRealmHandle();

            srHandle.SetHandle(srPtr);
            if (Dynamic && !schema.Any())
            {
                srHandle.GetSchema(nativeSchema => schema = RealmSchema.CreateFromObjectStoreSchema(nativeSchema));
            }

            return(new Realm(srHandle, this, schema));
        }
Пример #4
0
        public static Realm GetInstance(RealmConfiguration config = null)
        {
            config = config ?? RealmConfiguration.DefaultConfiguration;

            // TODO cache these initializers but note complications with ObjectClasses
            var schemaInitializer = new SchemaInitializerHandle();

            if (config.ObjectClasses == null)
            {
                foreach (var realmObjectClass in RealmObjectClasses)
                {
                    var objectSchemaHandle = GenerateObjectSchema(realmObjectClass);
                    NativeSchema.initializer_add_object_schema(schemaInitializer, objectSchemaHandle);
                }
            }
            else
            {
                foreach (var selectedRealmObjectClass in config.ObjectClasses)
                {
                    if (selectedRealmObjectClass.BaseType != typeof(RealmObject))
                    {
                        throw new ArgumentException($"The class {selectedRealmObjectClass.FullName} must descend directly from RealmObject");
                    }

                    Debug.Assert(RealmObjectClasses.Contains(selectedRealmObjectClass));  // user-specified class must have been picked up by our static ctor
                    var objectSchemaHandle = GenerateObjectSchema(selectedRealmObjectClass);
                    NativeSchema.initializer_add_object_schema(schemaInitializer, objectSchemaHandle);
                }
            }

            var schemaHandle = new SchemaHandle(schemaInitializer);

            var srHandle = new SharedRealmHandle();

            var    readOnly     = MarshalHelpers.BoolToIntPtr(config.ReadOnly);
            var    durability   = MarshalHelpers.BoolToIntPtr(false);
            var    databasePath = config.DatabasePath;
            IntPtr srPtr        = IntPtr.Zero;

            try {
                srPtr = NativeSharedRealm.open(schemaHandle,
                                               databasePath, (IntPtr)databasePath.Length,
                                               readOnly, durability,
                                               config.EncryptionKey,
                                               config.SchemaVersion);
            } catch (RealmMigrationNeededException) {
                if (config.ShouldDeleteIfMigrationNeeded)
                {
                    DeleteRealm(config);
                }
                else
                {
                    throw; // rethrow te exception
                    //TODO when have Migration but also consider programmer control over auto migration
                    //MigrateRealm(configuration);
                }
                // create after deleting old reopen after migrating
                srPtr = NativeSharedRealm.open(schemaHandle,
                                               databasePath, (IntPtr)databasePath.Length,
                                               readOnly, durability,
                                               config.EncryptionKey,
                                               config.SchemaVersion);
            }

            RuntimeHelpers.PrepareConstrainedRegions();
            try { /* Retain handle in a constrained execution region */ }
            finally
            {
                srHandle.SetHandle(srPtr);
            }

            return(new Realm(srHandle, config));
        }
Пример #5
0
        internal override Realm CreateRealm(RealmSchema schema)
        {
            var srHandle = new SharedRealmHandle();

            var configuration = new Native.Configuration
            {
                Path = DatabasePath,
                read_only = IsReadOnly,
                delete_if_migration_needed = ShouldDeleteIfMigrationNeeded,
                schema_version = SchemaVersion
            };

            Migration migration = null;
            if (MigrationCallback != null)
            {
                migration = new Migration(this, schema);
                migration.PopulateConfiguration(ref configuration);
            }

            var srPtr = IntPtr.Zero;
            try
            {
                srPtr = srHandle.Open(configuration, schema, EncryptionKey);
            }
            catch (ManagedExceptionDuringMigrationException)
            {
                throw new AggregateException("Exception occurred in a Realm migration callback. See inner exception for more details.", migration?.MigrationException);
            }

            srHandle.SetHandle(srPtr);
            return new Realm(srHandle, this, schema);
        }