/// <summary> /// Attempts to add the natural key to the natural key records. /// </summary> /// <param name="aggregateRootType">Type of the aggregate root.</param> /// <param name="serializedNaturalKey">The serialized natural key.</param> /// <param name="checkpoint">The checkpoint.</param> /// <param name="naturalKeyRecord">The natural key record.</param> /// <returns>Returns <c>true</c> if the natural key record was successfully added; otherwise <c>false</c>.</returns> public bool TryAddNaturalKey(Type aggregateRootType, object serializedNaturalKey, long checkpoint, out NaturalKeyRecord naturalKeyRecord) { Guard.Against.Null(() => aggregateRootType); Guard.Against.Null(() => serializedNaturalKey); if (this.isDisposed) { throw new ObjectDisposedException(this.GetType().FullName); } using (new ExclusiveCodeBlock(this.mutex)) { this.Synchronize(); var naturalKeysType = default(List <MemoryMappedNaturalKey>); var maxNaturalKeysTypeCheckpoint = this.naturalKeysTypes.TryGetValue(aggregateRootType, out naturalKeysType) ? naturalKeysType.Max(naturalKey => naturalKey.Checkpoint) : 0L; if (maxNaturalKeysTypeCheckpoint != checkpoint) { naturalKeyRecord = null; return(false); } var memoryMappedNaturalKey = new MemoryMappedNaturalKey { Identity = Guid.NewGuid(), Type = aggregateRootType.GetSerializedName(), SerializedValue = (string)serializedNaturalKey, Checkpoint = checkpoint + 1, IsRemoved = false, }; var buffer = Encoding.UTF8.GetBytes(Serializer.Serialize(memoryMappedNaturalKey)); using (var accessor = this.file.CreateViewAccessor(this.writeOffset, 2 + buffer.Length)) { accessor.Write(0, (ushort)buffer.Length); accessor.WriteArray(2, buffer, 0, buffer.Length); } this.writeOffset += 2 + buffer.Length; naturalKeyRecord = new NaturalKeyRecord { Identity = memoryMappedNaturalKey.Identity, SerializedValue = memoryMappedNaturalKey.SerializedValue, Checkpoint = memoryMappedNaturalKey.Checkpoint, IsRemoved = memoryMappedNaturalKey.IsRemoved, }; } return(true); }
/// <summary> /// Removes the natural key with the specified identity from the natural key records. /// </summary> /// <param name="naturalKeyIdentity">The natural key identity.</param> public void Remove(Guid naturalKeyIdentity) { if (this.isDisposed) { throw new ObjectDisposedException(this.GetType().FullName); } using (new ExclusiveCodeBlock(this.mutex)) { this.Synchronize(); var naturalKeyRecords = this.store.Where(naturalKey => naturalKey.Identity == naturalKeyIdentity); if (naturalKeyRecords.Any(naturalKey => naturalKey.IsRemoved) || naturalKeyRecords.SingleOrDefault() == null) { return; } var memoryMappedNaturalKey = new MemoryMappedNaturalKey { Identity = naturalKeyIdentity, Type = naturalKeyRecords.Single().Type, SerializedValue = naturalKeyRecords.Single().SerializedValue, Checkpoint = this.currentCheckpoint + 1, IsRemoved = true, }; var buffer = Encoding.UTF8.GetBytes(Serializer.Serialize(memoryMappedNaturalKey)); using (var accessor = this.file.CreateViewAccessor(this.writeOffset, 2 + buffer.Length)) { accessor.Write(0, (ushort)buffer.Length); accessor.WriteArray(2, buffer, 0, buffer.Length); } this.writeOffset += 2 + buffer.Length; } }