/// <summary> /// <para>Adds new objects to the list.</para> /// </summary> /// <param name="instances"> /// <para>The objects to add.</para> /// </param> public void AddRange(IEnumerable <T> instances) { if (instances == null) { throw new ArgumentNullException("instances"); } using (var enm = instances.GetEnumerator()) { if (!enm.MoveNext()) { return; } SetCursorAndCheckPopulated(); using (var item = Storage.StreamPool.GetItem()) { var stream = item.Item; Serializer.Serialize(stream, enm.Current); Cursor.Put(SerializingObjectStorage.GetWriteBuffer(stream), true); while (enm.MoveNext()) { stream.Position = 0; Serializer.Serialize(stream, enm.Current); Cursor.Put(SerializingObjectStorage.GetWriteBuffer(stream), true); } } } }
/// <summary> /// Reads the current value of <see cref="Cursor"/>. /// </summary> /// <param name="itemValue">A <see cref="ResourcePoolItem{MemoryStream}"/> that provides /// streams used for deserializing the value.</param> /// <param name="valueBuffer">Outputs the <see cref="DataBuffer"/> value read.</param> /// <returns>The <see cref="Int32"/> length of the cursor read.</returns> public int ReadCursor(ResourcePoolItem <MemoryStream> itemValue, out DataBuffer valueBuffer) { var valueStream = itemValue.Item; valueBuffer = SerializingObjectStorage.GetReadBuffer(valueStream); var results = Cursor.Get(valueBuffer, true); if (results < 0) { return(results); } var bfExcessValue = SerializingObjectStorage.GetExcessBuffer(valueStream, valueBuffer, results); if (!bfExcessValue.IsEmpty) { var len = valueBuffer.Length; valueBuffer = bfExcessValue; if (!Storage.Storage.SupportsIncompleteReads) { bfExcessValue = bfExcessValue.RestrictOffset(len); results = Cursor.Get(len, bfExcessValue, false); if (results >= 0) { results += len; } } else { results = Cursor.Get(0, bfExcessValue, false); } } return(results); }
private ObjectListForSingles(SerializingObjectStorage storage, DataBuffer keySpace, StorageKey key, Func <T> creator) { Storage = storage; Key = key; _keySpace = keySpace; Creator = creator; _pooled = storage.StreamPool.GetItem(); }
/// <summary> /// <para>Initializes a new instance of the <see cref="ObjectListForSingles{T, THeader}"/> class /// for an existing list.</para> /// </summary> /// <param name="storage"> /// <para>The <see cref="SerializingObjectStorage"/> containing the list.</para> /// </param> /// <param name="keySpace"> /// <para>The <see cref="DataBuffer"/> keyspace the list is stored in.</para> /// </param> /// <param name="key"> /// <para>The single <see cref="StorageKey"/> all the binary entries are /// stored under.</para> /// </param> /// <param name="buffer"> /// <para><see cref="DataBuffer"/> that holds the entire list. If /// <see cref="DataBuffer.IsEmpty"/> is true, then the list is read /// from <paramref name="storage"/>.</para> /// </param> /// <param name="creator"> /// <para>The <see cref="Func{T}"/> that produces new <typeparamref name="T"/> /// instances for deserialization.</para> /// </param> /// <param name="headerCreator"> /// <para>The <see cref="Func{THeader}"/> that produces new <typeparamref name="THeader"/> /// instances for deserialization.</para> /// </param> public ObjectListForSingles(SerializingObjectStorage storage, DataBuffer keySpace, StorageKey key, DataBuffer buffer, Func <T> creator, Func <THeader> headerCreator) : this(storage, keySpace, key, creator) { ArraySegment <byte> seg; if (buffer.IsEmpty) { _stream = _pooled.Item; buffer = SerializingObjectStorage.GetReadBuffer(_stream); var entryLen = storage.Storage.Get(keySpace, key, buffer); if (entryLen < 0) { throw new ApplicationException(string.Format( "No list found for key space '{0}', key '{1}'", keySpace, key)); } var bfExcess = SerializingObjectStorage.GetExcessBuffer(_stream, buffer, entryLen); if (bfExcess.IsEmpty) { buffer = buffer.Restrict(entryLen); } else { if (storage.Storage.SupportsIncompleteReads) { storage.Storage.Get(keySpace, key, buffer.Length, bfExcess.RestrictOffset(buffer.Length)); } else { storage.Storage.Get(keySpace, key, 0, bfExcess); } buffer = bfExcess; } seg = buffer.ByteArraySegmentValue; } else { _pooled.Dispose(); _pooled = null; seg = buffer.ByteArraySegmentValue; _stream = new MemoryStream(seg.Array, seg.Offset, seg.Count); } var ticks = BitConverter.ToInt64(seg.Array, seg.Offset); // expires listed first, since SetExpires can change it by itself // and this way it doesn't have to worry about future changes // to header Expires = new DateTime(ticks); _stream.Position = sizeof(long); _header = headerCreator(); Serializer.Deserialize(_stream, _header); _headerLength = _stream.Position; }
/// <summary> /// <para>Adds a new object to the list.</para> /// </summary> /// <param name="instance"> /// <para>The object to add.</para> /// </param> public void Add(T instance) { SetCursorAndCheckPopulated(); using (var item = Storage.StreamPool.GetItem()) { var stream = item.Item; Serializer.Serialize(stream, instance); Cursor.Put(SerializingObjectStorage.GetWriteBuffer(stream), true); } }
/// <summary> /// <para>Removes all objects from the list.</para> /// </summary> public void Clear() { using (var item = Storage.StreamPool.GetItem()) { var stream = item.Item; stream.Write(BitConverter.GetBytes(Expires.Ticks), 0, sizeof(long)); Serializer.Serialize(stream, _header); Storage.Storage.Put(_keySpace, Key, SerializingObjectStorage.GetWriteBuffer(stream)); } }
/// <summary> /// <para>Adds a new object to the list.</para> /// </summary> /// <param name="instance"> /// <para>The object to add.</para> /// </param> public void Add(T instance) { var pos = (int)_stream.Position; var lenStm = (int)_stream.Length; _stream.Position = lenStm; Serializer.Serialize(_stream, instance); var buffer = SerializingObjectStorage.GetWriteBuffer(_stream); Storage.Storage.Put(_keySpace, Key, lenStm, 0, buffer.RestrictOffset(lenStm)); _stream.Position = pos; }
/// <summary> /// <para>Initializes a new instance of the <see cref="ObjectListForMultiples{T, THeader}"/> class /// for a new list.</para> /// </summary> /// <param name="storage"> /// <para>The <see cref="SerializingObjectStorage"/> containing the list.</para> /// </param> /// <param name="keySpace"> /// <para>The <see cref="DataBuffer"/> keyspace the list is stored in.</para> /// </param> /// <param name="key"> /// <para>The single <see cref="StorageKey"/> all the binary entries are /// stored under.</para> /// </param> /// <param name="creator"> /// <para>The <see cref="Func{T}"/> that produces new <typeparamref name="T"/> /// instances for deserialization.</para> /// </param> /// <param name="header"> /// <para>The header <typeparamref name="THeader"/> instance to be stored.</para> /// </param> /// <param name="expires">The expiration <see cref="DateTime"/> of the new list.</param> public ObjectListForMultiples(SerializingObjectStorage storage, DataBuffer keySpace, StorageKey key, Func <T> creator, THeader header, DateTime expires) : this(storage, keySpace, key, creator, (inst, itemValue) => { inst.Expires = expires; inst._header = header; var stream = itemValue.Item; stream.Write(BitConverter.GetBytes(expires.Ticks), 0, sizeof(long)); Serializer.Serialize(stream, header); inst.Storage.Storage.Put(keySpace, key, SerializingObjectStorage.GetWriteBuffer(stream)); }) { }
/// <summary> /// <para>Initializes a new instance of the <see cref="ObjectListForSingles{T, THeader}"/> class /// for a new list.</para> /// </summary> /// <param name="storage"> /// <para>The <see cref="SerializingObjectStorage"/> containing the list.</para> /// </param> /// <param name="keySpace"> /// <para>The <see cref="DataBuffer"/> keyspace the list is stored in.</para> /// </param> /// <param name="key"> /// <para>The single <see cref="StorageKey"/> all the binary entries are /// stored under.</para> /// </param> /// <param name="creator"> /// <para>The <see cref="Func{T}"/> that produces new <typeparamref name="T"/> /// instances for deserialization.</para> /// </param> /// <param name="header"> /// <para>The header <typeparamref name="THeader"/> instance to be stored.</para> /// </param> /// <param name="expires">The expiration <see cref="DateTime"/> of the new list.</param> public ObjectListForSingles(SerializingObjectStorage storage, DataBuffer keySpace, StorageKey key, Func <T> creator, THeader header, DateTime expires) : this(storage, keySpace, key, creator) { _header = header; Expires = expires; _stream = _pooled.Item; _stream.Write(BitConverter.GetBytes(expires.Ticks), 0, sizeof(long)); Serializer.Serialize(_stream, header); _headerLength = _stream.Position; storage.Storage.Put(keySpace, key, SerializingObjectStorage.GetWriteBuffer(_stream)); }
private ObjectListForMultiples(SerializingObjectStorage storage, DataBuffer keySpace, StorageKey key, Func <T> creator, Action <ObjectListForMultiples <T, THeader>, ResourcePoolItem <MemoryStream> > cont) { Storage = storage; _keySpace = Storage.SupportsKeySpaces ? keySpace : DataBuffer.Empty; _supportsKeySpacePartitions = Storage.GetKeySpacePartitionSupport( _keySpace); Key = _supportsKeySpacePartitions ? key : new StorageKey(key.Key, 0); Creator = creator; using (var itemValue = storage.StreamPool.GetItem()) { cont(this, itemValue); } }
/// <summary> /// Overriden. Obtains an instance from this factory. /// </summary> /// <returns>An <see cref="IObjectStorage"/> instance.</returns> public override IObjectStorage ObtainInstance() { var memoryPool = new MemoryStreamPool(); if (_memoryPoolInitialBufferSize.HasValue) { memoryPool.InitialBufferSize = _memoryPoolInitialBufferSize.Value; } var storage = _storageSection.ObtainInstance(); var ret = new SerializingObjectStorage(); ret.Initialize(new SerializingObjectStorageConfig { StreamPool = memoryPool, Storage = storage }); return(ret); }
/// <summary> /// <para>Adds new objects to the list.</para> /// </summary> /// <param name="instances"> /// <para>The objects to add.</para> /// </param> public void AddRange(IEnumerable <T> instances) { if (instances == null) { throw new ArgumentNullException("instances"); } var pos = (int)_stream.Position; var lenStm = (int)_stream.Length; _stream.Position = lenStm; foreach (var instance in instances) { Serializer.Serialize(_stream, instance); } var buffer = SerializingObjectStorage.GetWriteBuffer(_stream); Storage.Storage.Put(_keySpace, Key, lenStm, 0, buffer.RestrictOffset(lenStm)); _stream.Position = pos; }
/// <summary> /// <para>Initializes a new instance of the <see cref="ObjectListForMultiples{T, THeader}"/> class /// for an existing list.</para> /// </summary> /// <param name="storage"> /// <para>The <see cref="SerializingObjectStorage"/> containing the list.</para> /// </param> /// <param name="keySpace"> /// <para>The <see cref="DataBuffer"/> keyspace the list is stored in.</para> /// </param> /// <param name="key"> /// <para>The single <see cref="StorageKey"/> all the binary entries are /// stored under.</para> /// </param> /// <param name="creator"> /// <para>The <see cref="Func{T}"/> that produces new <typeparamref name="T"/> /// instances for deserialization.</para> /// </param> /// <param name="headerCreator"> /// <para>The <see cref="Func{THeader}"/> that produces new <typeparamref name="THeader"/> /// instances for deserialization.</para> /// </param> public ObjectListForMultiples(SerializingObjectStorage storage, DataBuffer keySpace, StorageKey key, Func <T> creator, Func <THeader> headerCreator) : this(storage, keySpace, key, creator, (inst, itemValue) => { inst.SetCursor(); DataBuffer valueBuffer; if (inst.ReadCursor(itemValue, out valueBuffer) < 0) { throw new ApplicationException(string.Format( "No list for multiples header found for key space '{0}', key '{1}'", keySpace, key)); } var seg = valueBuffer.ByteArraySegmentValue; var ticks = BitConverter.ToInt64(seg.Array, seg.Offset); inst.Expires = new DateTime(ticks); var stream = itemValue.Item; stream.Position = sizeof(long); inst._header = headerCreator(); Serializer.Deserialize(stream, inst._header); }) { }