internal Realm(SharedRealmHandle sharedRealmHandle, RealmConfigurationBase config, RealmSchema schema) { Config = config; RealmState state = null; var statePtr = sharedRealmHandle.GetManagedStateHandle(); if (statePtr != IntPtr.Zero) { state = GCHandle.FromIntPtr(statePtr).Target as RealmState; } if (state == null) { state = new RealmState(); sharedRealmHandle.SetManagedStateHandle(GCHandle.ToIntPtr(state.GCHandle)); _states.Value[config.DatabasePath] = new WeakReference <RealmState>(state); } state.AddRealm(this); _state = state; SharedRealmHandle = sharedRealmHandle; Metadata = schema.ToDictionary(t => t.Name, CreateRealmObjectMetadata); Schema = schema; }
/// <summary> /// Compacts a Realm file. A Realm file usually contains free/unused space. This method removes this free space and the file size is thereby reduced. Objects within the Realm file are untouched. /// </summary> /// <remarks> /// The realm file must not be open on other threads. /// The file system should have free space for at least a copy of the Realm file. /// This method must not be called inside a transaction. /// The Realm file is left untouched if any file operation fails. /// </remarks> /// <param name="config">Optional configuration.</param> /// <returns><c>true</c> if successful, <c>false</c> if any file operation failed.</returns> public static bool Compact(RealmConfigurationBase config = null) { using (var realm = GetInstance(config)) { return(realm.SharedRealmHandle.Compact()); } }
/// <summary> /// Factory for asynchronously obtaining a <see cref="Realm"/> instance. /// </summary> /// <remarks> /// If the configuration points to a remote realm belonging to a Realm Object Server /// the realm will be downloaded and fully synchronized with the server prior to the completion /// of the returned Task object. /// Otherwise this method behaves identically to <see cref="GetInstance(RealmConfigurationBase)"/> /// and immediately returns a completed Task. /// </remarks> /// <returns>A <see cref="Task{Realm}"/> that is completed once the remote realm is fully synchronized or immediately if it's a local realm.</returns> /// <param name="config">A configuration object that describes the realm.</param> public static Task<Realm> GetInstanceAsync(RealmConfigurationBase config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } var schema = config.ObjectClasses != null ? RealmSchema.CreateSchemaForClasses(config.ObjectClasses) : RealmSchema.Default; return config.CreateRealmAsync(schema); }
/// <summary> /// Deletes all the files associated with a realm. /// </summary> /// <param name="configuration">A <see cref="RealmConfigurationBase"/> which supplies the realm path.</param> public static void DeleteRealm(RealmConfigurationBase configuration) { var fullpath = configuration.DatabasePath; if (IsRealmOpen(fullpath)) { throw new RealmPermissionDeniedException("Unable to delete Realm because it is still open."); } File.Delete(fullpath); File.Delete(fullpath + ".log_a"); // eg: name at end of path is EnterTheMagic.realm.log_a File.Delete(fullpath + ".log_b"); File.Delete(fullpath + ".log"); File.Delete(fullpath + ".lock"); File.Delete(fullpath + ".note"); }
/// <summary> /// Deletes all the files associated with a realm. /// </summary> /// <param name="configuration">A <see cref="RealmConfigurationBase"/> which supplies the realm path.</param> public static void DeleteRealm(RealmConfigurationBase configuration) { // TODO add cache checking when implemented, https://github.com/realm/realm-dotnet/issues/308 // when cache checking, uncomment in IntegrationTests.cs RealmInstanceTests.DeleteRealmFailsIfOpenSameThread and add a variant to test open on different thread var lockOnWhileDeleting = new object(); lock (lockOnWhileDeleting) { var fullpath = configuration.DatabasePath; File.Delete(fullpath); File.Delete(fullpath + ".log_a"); // eg: name at end of path is EnterTheMagic.realm.log_a File.Delete(fullpath + ".log_b"); File.Delete(fullpath + ".log"); File.Delete(fullpath + ".lock"); File.Delete(fullpath + ".note"); } }
internal static Realm GetInstance(RealmConfigurationBase config, RealmSchema schema) { Argument.NotNull(config, nameof(config)); if (schema == null) { if (config.ObjectClasses != null) { schema = RealmSchema.CreateSchemaForClasses(config.ObjectClasses); } else { schema = config.IsDynamic ? RealmSchema.Empty : RealmSchema.Default; } } return(config.CreateRealm(schema)); }
internal static Realm GetInstance(RealmConfigurationBase config, RealmSchema schema) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (schema == null) { if (config.ObjectClasses != null) { schema = RealmSchema.CreateSchemaForClasses(config.ObjectClasses); } else { schema = RealmSchema.Default; } } return(config.CreateRealm(schema)); }
/// <summary> /// Factory for asynchronously obtaining a <see cref="Realm"/> instance. /// </summary> /// <remarks> /// If the configuration points to a remote realm belonging to a Realm Object Server /// the realm will be downloaded and fully synchronized with the server prior to the completion /// of the returned Task object. /// Otherwise this method behaves identically to <see cref="GetInstance(RealmConfigurationBase)"/> /// and immediately returns a completed Task. /// </remarks> /// <returns>A <see cref="Task{Realm}"/> that is completed once the remote realm is fully synchronized or immediately if it's a local realm.</returns> /// <param name="config">A configuration object that describes the realm.</param> public static Task <Realm> GetInstanceAsync(RealmConfigurationBase config = null) { if (config == null) { config = RealmConfiguration.DefaultConfiguration; } RealmSchema schema; if (config.ObjectClasses != null) { schema = RealmSchema.CreateSchemaForClasses(config.ObjectClasses); } else if (config.IsDynamic) { schema = RealmSchema.Empty; } else { schema = RealmSchema.Default; } return(config.CreateRealmAsync(schema)); }
/// <summary> /// Factory for asynchronously obtaining a <see cref="Realm"/> instance. /// </summary> /// <remarks> /// If the configuration points to a remote realm belonging to a Realm Object Server /// the realm will be downloaded and fully synchronized with the server prior to the completion /// of the returned Task object. /// Otherwise this method behaves identically to <see cref="GetInstance(RealmConfigurationBase)"/> /// and immediately returns a completed Task. /// </remarks> /// <returns>A <see cref="Task{Realm}"/> that is completed once the remote realm is fully synchronized or immediately if it's a local realm.</returns> /// <param name="config">A configuration object that describes the realm.</param> public static Task <Realm> GetInstanceAsync(RealmConfigurationBase config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } RealmSchema schema; if (config.ObjectClasses != null) { schema = RealmSchema.CreateSchemaForClasses(config.ObjectClasses); } else if (config.IsDynamic) { schema = RealmSchema.Empty; } else { schema = RealmSchema.Default; } return(config.CreateRealmAsync(schema)); }
/// <summary> /// Factory for obtaining a <see cref="Realm"/> instance for this thread. /// </summary> /// <param name="config">Optional configuration.</param> /// <returns>A <see cref="Realm"/> instance.</returns> /// <exception cref="RealmFileAccessErrorException"> /// Thrown if the file system returns an error preventing file creation. /// </exception> public static Realm GetInstance(RealmConfigurationBase config = null) { return(GetInstance(config ?? RealmConfiguration.DefaultConfiguration, null)); }
/// <summary> /// Writes a compacted copy of the Realm to the path in the specified config. If the configuration object has /// non-null <see cref="RealmConfigurationBase.EncryptionKey"/>, the copy will be encrypted with that key. /// </summary> /// <remarks> /// The destination file cannot already exist. /// <para/> /// If this is called from within a transaction it writes the current data, and not the data as it was when /// the last transaction was committed. /// </remarks> /// <param name="config">Configuration, specifying the path and optionally the encryption key for the copy.</param> public void WriteCopy(RealmConfigurationBase config) { SharedRealmHandle.WriteCopy(config.DatabasePath, config.EncryptionKey); }
/// <summary> /// Compacts a Realm file. A Realm file usually contains free/unused space. This method removes this free space and the file size is thereby reduced. Objects within the Realm files are untouched. /// </summary> /// <remarks> /// The realm file must not be open on other threads. /// The file system should have free space for at least a copy of the Realm file. /// This method must not be called inside a transaction. /// The Realm file is left untouched if any file operation fails. /// </remarks> /// <param name="config">Optional configuration.</param> /// <returns><c>true</c> if successful, <c>false</c> if any file operation failed.</returns> public static bool Compact(RealmConfigurationBase config = null) { RealmPCLHelpers.ThrowProxyShouldNeverBeUsed(); return(false); }
/// <summary> /// Factory for obtaining a <see cref="Realm"/> instance for this thread. /// </summary> /// <param name="config">Optional configuration.</param> /// <returns>A <see cref="Realm"/> instance.</returns> /// <exception cref="RealmFileAccessErrorException"> /// Thrown if the file system returns an error preventing file creation. /// </exception> public static Realm GetInstance(RealmConfigurationBase config = null) { RealmPCLHelpers.ThrowProxyShouldNeverBeUsed(); return(null); }
/// <summary> /// Deletes all the files associated with a realm. /// </summary> /// <param name="configuration">A <see cref="RealmConfigurationBase"/> which supplies the realm path.</param> public static void DeleteRealm(RealmConfigurationBase configuration) { RealmPCLHelpers.ThrowProxyShouldNeverBeUsed(); }
/// <summary> /// Factory for asynchronously obtaining a <see cref="Realm"/> instance. /// </summary> /// <remarks> /// If the configuration points to a remote realm belonging to a Realm Object Server /// the realm will be downloaded and fully synchronized with the server prior to the completion /// of the returned Task object. /// Otherwise this method behaves identically to <see cref="GetInstance(RealmConfigurationBase)"/> /// and immediately returns a completed Task. /// </remarks> /// <returns>A <see cref="Task{Realm}"/> that is completed once the remote realm is fully synchronized or immediately if it's a local realm.</returns> /// <param name="config">A configuration object that describes the realm.</param> public static Task <Realm> GetInstanceAsync(RealmConfigurationBase config) { RealmPCLHelpers.ThrowProxyShouldNeverBeUsed(); return(null); }
/// <summary> /// Writes a compacted copy of the Realm to the path in the specified config. If the configuration object has /// non-null <see cref="RealmConfigurationBase.EncryptionKey"/>, the copy will be encrypted with that key. /// </summary> /// <remarks> /// The destination file cannot already exist. /// <para/> /// If this is called from within a transaction it writes the current data, and not the data as it was when /// the last transaction was committed. /// </remarks> /// <param name="config">Configuration, specifying the path and optionally the encryption key for the copy.</param> public void WriteCopy(RealmConfigurationBase config) { RealmPCLHelpers.ThrowProxyShouldNeverBeUsed(); }