public virtual void Test_OneBatch_NoOperations() { Assert.IsFalse(BatchCommitted, "The BatchCommitted flag should start off false."); using (Batch batch = BatchState.StartBatch()) { batch.Committed += new EventHandler(batch_Committed); } Assert.IsTrue(BatchCommitted, "The BatchCommitted flag wasn't changed to true."); }
public void ClearData(string dataDirectory) { if (DataAccess.IsInitialized) { using (Batch batch = BatchState.StartBatch()) { foreach (IEntity entity in DataAccess.Data.Indexer.GetEntities()) { DataAccess.Data.Deleter.Delete(entity); } } } }
public virtual void Test_TwoBatches_Nested() { Assert.IsFalse(BatchCommitted, "The BatchCommitted flag should start off false."); using (Batch batch = BatchState.StartBatch()) { batch.Committed += new EventHandler(batch_Committed); using (Batch batch2 = BatchState.StartBatch()) { batch2.Committed += new EventHandler(batch_Committed); } Assert.IsFalse(BatchCommitted, "The nested batch committed when it shouldn't have. It should leave it for the outer batch."); } Assert.IsTrue(BatchCommitted, "The BatchCommitted flag wasn't changed to true."); }
/// <summary> /// Deletes the provided entity. /// </summary> /// <param name="entity">The entity to delete.</param> public override void Delete(IEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } using (LogGroup logGroup = LogGroup.StartDebug("Deleting provided '" + entity.ShortTypeName + "' entity.")) { Db4oDataStore store = (Db4oDataStore)GetDataStore(entity); if (DataAccess.Data.IsStored(entity)) { using (Batch batch = BatchState.StartBatch()) { IDataReader reader = Provider.InitializeDataReader(); reader.AutoRelease = false; entity = reader.GetEntity(entity); if (entity == null) { throw new Exception("The entity wasn't found."); } // PreDelete must be called to ensure all references are correctly managed PreDelete(entity); // Delete the entity if (entity != null) { store.ObjectContainer.Delete(entity); store.Commit(); } } } } }
/// <summary> /// Updates the provided entity. /// </summary> /// <param name="entity">The entity to update.</param> public override void Update(IEntity entity) { using (LogGroup logGroup = LogGroup.Start("Updating the provided entity.", NLog.LogLevel.Debug)) { if (entity == null) { throw new ArgumentNullException("entity"); } Db4oDataStore store = (Db4oDataStore)GetDataStore(entity); if (!store.IsStored(entity)) { throw new ArgumentException("entity", "The provided entity of type '" + entity.GetType() + "' is not found in the store with name '" + store.Name + "'."); } if (entity.ID == Guid.Empty) { throw new ArgumentException("entity.ID must be set."); } using (Batch batch = BatchState.StartBatch()) { LogWriter.Debug("Entity type: " + entity.GetType().ToString()); LogWriter.Debug("Entity ID: " + entity.ID); // Preupdate must be called to ensure all references are correctly stored PreUpdate(entity); // Get a bound copy of the entity IDataReader reader = Provider.InitializeDataReader(); reader.AutoRelease = false; // Tell the reader not to unbind the entity from the store because it's still within the data access system IEntity existingEntity = reader.GetEntity(entity); if (existingEntity != null) { // Activate the found entity Provider.Activator.Activate(existingEntity); // Copy the provided data to the bound entity entity.CopyTo(existingEntity); // Remove all the referenced entities existingEntity.Deactivate(); store.ObjectContainer.Store(existingEntity); LogWriter.Debug("Entity updated."); PostUpdate(entity); store.Commit(); } else { throw new InvalidOperationException("Cannot update an entity that doesn't already exist in the data store. Save the entity first."); } } } }
/// <summary> /// Saves the provided entity into the provided data store. /// </summary> /// <param name="entity">The entity to save to the data store.</param> /// <param name="handleReferences">A value indicating whether to delete old references and save new references.</param> public override void Save(IEntity entity, bool handleReferences) { using (LogGroup logGroup = LogGroup.StartDebug("Saving entity of type '" + entity.ShortTypeName + "'.")) { if (entity == null) { throw new ArgumentNullException("entity"); } if (entity.ID == Guid.Empty) { throw new ArgumentException("entity.ID must be set."); } // Clone the entity so that it doesn't get bound to the store IEntity clonedEntity = entity.Clone(); Db4oDataStore store = (Db4oDataStore)GetDataStore(clonedEntity); if (store.ObjectContainer == null) { throw new InvalidOperationException("The ObjectContainer has not been initialized on the '" + store.Name + "' data store."); } using (Batch batch = BatchState.StartBatch()) { if (EntitiesUtilities.IsReference(clonedEntity.GetType()) && DataAccess.Data.IsStored(clonedEntity)) { LogWriter.Debug("Existing reference found. Skipping save."); // Just skip the saving altogether, if the reference already exists } else { if (clonedEntity == null) { throw new ArgumentNullException("entity"); } LogWriter.Debug("Entity type: " + clonedEntity.GetType().ToString()); LogWriter.Debug("Entity ID: " + clonedEntity.ID.ToString()); PreSave(clonedEntity, handleReferences); if (clonedEntity != null) { DataUtilities.StripReferences(clonedEntity); // Save the entity store.ObjectContainer.Store(clonedEntity); // Post save the original entity NOT the cloned entity PostSave(entity); store.Commit(); LogWriter.Debug("Entity stored in '" + store.Name + "' store."); } else { LogWriter.Debug("Cloned entity == null. Not stored."); } } } } }
public virtual void Test_OneBatch_TwoOperations() { // Check the flags to ensure they've been reset Assert.IsFalse(BatchCommitted, "The BatchCommitted flag should start off false."); Assert.IsFalse(StoreCommitted, "The StoreCommitted flag should start off false."); // Create mock entities TestUser user = new TestUser(); user.ID = Guid.NewGuid(); user.FirstName = "First"; user.LastName = "Last"; TestUser user2 = new TestUser(); user2.ID = Guid.NewGuid(); user2.FirstName = "First2"; user2.LastName = "Last2"; // Start the data operation batch using (Batch batch = BatchState.StartBatch()) { // Handle the Committed even of the batch batch.Committed += new EventHandler(batch_Committed); // Ensure that the flags are still set to defaults Assert.IsFalse(BatchCommitted, "The BatchCommitted flag should remain false until the batch disposes."); Assert.IsFalse(StoreCommitted, "The StoreCommitted flag should remain false until the batch disposes."); // Get the data store for the mock entities IDataStore store = DataAccess.Data.Stores[user.GetType()]; // Handle the committed even of the data store store.Committed += new EventHandler(store_Committed); // Save the first mock user DataAccess.Data.Saver.Save(user); Assert.AreEqual(1, BatchState.Batches.ToArray()[0].DataStores.Count, "Invalid number of data stores found."); Assert.AreEqual(1, BatchState.Batches.Count, "Invalid number of batches found."); Assert.IsFalse(BatchCommitted, "#1 The BatchCommitted flag should remain false until the batch disposes, even after saving an entity."); Assert.IsFalse(StoreCommitted, "#1 The StoreCommitted flag should remain false until the batch disposes, even after saving an entity."); // TODO: Check if needed. Should be but is causing problems with the test. DisposeMockData(false); // Pass false to stop the disposal triggering automatic commit InitializeMockData(); StoreCommitted = false; BatchCommitted = false; store = DataAccess.Data.Stores[user.GetType()]; store.Committed += new EventHandler(store_Committed); TestUser foundUser = DataAccess.Data.Reader.GetEntity <TestUser>("ID", user.ID); Assert.IsNull(foundUser, "The user was returned when it shouldn't have been committed yet."); DataAccess.Data.Saver.Save(user2); Assert.IsFalse(BatchCommitted, "#2 The BatchCommitted flag should remain false until the batch disposes, even after saving an entity."); Assert.IsFalse(StoreCommitted, "#2 The StoreCommitted flag should remain false until the batch disposes, even after saving an entity."); store = DataAccess.Data.Stores[user.GetType()]; store.Committed += new EventHandler(store_Committed); } // Commit should happen here, when batch disposes Assert.IsTrue(BatchCommitted, "The BatchCommitted flag wasn't changed to true."); Assert.IsTrue(StoreCommitted, "The StoreCommitted flag wasn't changed to true."); DisposeMockData(); InitializeMockData(); TestUser foundUser2 = DataAccess.Data.Reader.GetEntity <TestUser>("ID", user2.ID); Assert.IsNotNull(foundUser2, "The user wasn't returned when it should have been."); Assert.AreEqual(user2.ID.ToString(), foundUser2.ID.ToString(), "The found user ID doesn't match the expected ID."); }