/// <summary> /// <para>Deletes an object from the store by version.</para> /// </summary> /// <param name="keySpace"> /// <para>The key space of the entry.</para> /// </param> /// <param name="key"> /// <para>The key of the entry.</para> /// </param> /// <param name="updated"> /// <para>The last updated <see cref="DateTime"/> of the version to be deleted. If the entry's last updated date is not newer than this value, then the entry will be deleted, otherwise not.</para> /// </param> /// <returns> /// <para> /// <see langword="true"/> if the entry was deleted; otherwise <see langword="false"/>. The entry is deleted if it existed prior and was as old or older than <paramref name="updated"/>.</para> /// </returns> public bool DeleteVersion(DataBuffer keySpace, StorageKey key, DateTime updated) { using (var item = StreamPool.GetItem()) { var stream = item.Item; if (stream.Capacity < sizeof(long)) { stream.Capacity = sizeof(long); } var buffer = GetReadBuffer(stream); buffer = buffer.Restrict(sizeof(long)); var len = Storage.Get(keySpace, key, sizeof(long), buffer); if (len < 0) { return(false); } var seg = buffer.ByteArraySegmentValue; var ticks = BitConverter.ToInt64(seg.Array, seg.Offset); var entryUpdated = new DateTime(ticks); if (entryUpdated <= updated) { return(Storage.Delete(keySpace, key)); } return(false); } }
/// <summary> /// <para>Reads an object from the store.</para> /// </summary> /// <typeparam name="T"> /// <para>The type of the object.</para> /// </typeparam> /// <param name="keySpace"> /// <para>The key space of the entry.</para> /// </param> /// <param name="key"> /// <para>The key of the entry.</para> /// </param> /// <param name="creator"> /// <para>Delegate that provides an empty instance for /// deserialization.</para> /// </param> /// <returns> /// <para>A <see cref="StorageEntry{T}"/> containing the entry.</para> /// </returns> public StorageEntry <T> Get <T>(DataBuffer keySpace, StorageKey key, Func <T> creator) { if (Storage.StreamsData) { var data = Storage.GetBuffer(keySpace, key); if (data == null) { return(StorageEntry <T> .NotFound); } DataBuffer buffer = data; using (var stream = new MemoryStream(data, false)) { return(GetCore(buffer, stream, creator())); } } else { using (var item = StreamPool.GetItem()) { DataBuffer buffer; var stream = GetStream(item, keySpace, key, out buffer); if (stream == null) { return(StorageEntry <T> .NotFound); } return(GetCore(buffer, stream, creator())); } } }
/// <summary> /// <para>Writes an object to the store.</para> /// </summary> /// <typeparam name="T"> /// <para>The type of the object.</para> /// </typeparam> /// <param name="keySpace"> /// <para>The key space of the entry.</para> /// </param> /// <param name="key"> /// <para>The key of the entry.</para> /// </param> /// <param name="entry"> /// <para>The entry to write.</para> /// </param> public void Put <T>(DataBuffer keySpace, StorageKey key, StorageEntry <T> entry) { AddKeySpaceInfo <T>(keySpace); using (var item = StreamPool.GetItem()) { var stream = item.Item; stream.Write(BitConverter.GetBytes(entry.Expires.Ticks), 0, sizeof(long)); stream.Write(BitConverter.GetBytes(entry.Updated.Ticks), 0, sizeof(long)); Serializer.Serialize(stream, entry.Instance); Storage.Put(keySpace, key, GetWriteBuffer(stream)); } }
/// <summary> /// <para>Gets the expiration time of an object.</para> /// </summary> /// <param name="keySpace"> /// <para>The key space of the entry.</para> /// </param> /// <param name="key"> /// <para>The key of the entry.</para> /// </param> /// <returns> /// <para>A <see cref="Nullable{DateTime}"/> containing the expiration time if found; otherwise <see langword="null"/>.</para> /// </returns> public DateTime?GetExpires(DataBuffer keySpace, StorageKey key) { using (var item = StreamPool.GetItem()) { var stream = item.Item; if (stream.Capacity < sizeof(long)) { stream.Capacity = sizeof(long); } var buffer = GetReadBuffer(stream); buffer = buffer.Restrict(sizeof(long)); var len = Storage.Get(keySpace, key, 0, buffer); if (len < 0) { return(null); } var seg = buffer.ByteArraySegmentValue; var ticks = BitConverter.ToInt64(seg.Array, seg.Offset); return(new DateTime(ticks)); } }