/// <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);
             }
         }
     }
 }
Пример #2
0
        /// <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);
     }
 }
Пример #4
0
 /// <summary>
 ///     <para>Returns an enumerator that iterates through the collection.</para>
 /// </summary>
 /// <returns>
 ///     <para>A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection.</para>
 /// </returns>
 public IEnumerator <T> GetEnumerator()
 {
     _stream.Position = _headerLength;
     while (_stream.Position < _stream.Length)
     {
         var instance = Creator();
         Serializer.Deserialize(_stream, instance);
         yield return(instance);
     }
 }
 /// <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));
     }
 }
Пример #6
0
        /// <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>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>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));
 })
 { }
Пример #9
0
 /// <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));
 }
Пример #10
0
 /// <summary>
 ///     <para>Advances the enumerator to the next element of the collection.</para>
 /// </summary>
 /// <returns>
 ///     <para>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</para>
 /// </returns>
 /// <exception cref="System.InvalidOperationException">
 ///     <para>The collection was modified after the enumerator was created.</para>
 /// </exception>
 public bool MoveNext()
 {
     //if (!_list.Cursor.MoveNext()) return false;
     using (var itemValue = _list.Storage.StreamPool.GetItem())
     {
         DataBuffer valueBuffer;
         var        results = _list.ReadCursor(itemValue, out valueBuffer);
         if (results < 0)
         {
             return(false);
         }
         var stream = itemValue.Item;
         stream.Position = 0;
         _current        = _list.Creator();
         Serializer.Deserialize(stream, _current);
         return(true);
     }
 }
Пример #11
0
        /// <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;
        }
Пример #12
0
 /// <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);
 })
 { }
        private StorageEntry <T> GetCore <T>(DataBuffer buffer, MemoryStream stream, T instance)
        {
            var seg = buffer.ByteArraySegmentValue;

            if (stream == null)
            {
                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
            var expires = new DateTime(ticks);

            ticks = BitConverter.ToInt64(seg.Array, seg.Offset + sizeof(long));
            var updated = new DateTime(ticks);

            stream.Position = _headerLength;
            Serializer.Deserialize(stream, instance);
            return(new StorageEntry <T>(instance, updated, expires));
        }