Esempio n. 1
0
        /// <summary>
        /// Adds new entry
        /// </summary>
        public StorageStreamMetadata Add(Guid streamId, int tag = 0)
        {
            // Get the position of first empty entry
            int  index          = map.FindFirstEmptyEntry();
            long streamPosition = index * StorageStreamMetadata.StructureSize;

            // Resize stream is needed
            if ((streamPosition + StorageStreamMetadata.StructureSize) > stream.Length)
            {
                int count = (int)stream.Length / StorageStreamMetadata.StructureSize;
                count += Math.Min(Math.Max((int)(count * 1.5), 512), 50000);

                long oldLength = stream.Length;
                stream.SetLength(count * StorageStreamMetadata.StructureSize);

                // Write zeros to newly allocated space
                long bytesToWrite = stream.Length - oldLength;
                stream.Position = oldLength;
                while (bytesToWrite > 0)
                {
                    int amount = (int)Math.Min(bytesToWrite, Tools.EmptyBuffer.Length);
                    stream.Write(Tools.EmptyBuffer, 0, amount);
                    bytesToWrite -= amount;
                }

                stream.Save();
            }

            StorageStreamMetadata streamMetadata = new StorageStreamMetadata(stream)
            {
                FirstSegmentPosition = null,
                StreamId             = streamId,
                Tag = tag,
                StreamTableIndex = index
            };

            entriesAddedInTransaction.Add(streamId, streamMetadata);
            //streamMetadata.Save();

            map.Set(index, true);
            items.Add(streamId, index);

            return(streamMetadata);
        }
Esempio n. 2
0
        /// <summary>
        /// Deletes a stream
        /// </summary>
        /// <param name="streamId">Stream Id</param>
        public void DeleteStream(Guid streamId)
        {
            CheckClosed();

            if (SystemStreamId.IsSystemStreamId(streamId))
            {
                throw new InvalidStreamIdException();
            }

            StartTransaction();
            try
            {
                // Before deleting, set stream size to zero to deallocate all of the space it occupies
                StorageStream tmpStream = OpenStream(streamId);
                tmpStream.SetLength(0);
                tmpStream.Close();

                openedStreams.Remove(streamId);
                streamTable.Remove(streamId);

                // Remove stream from list of changed streams
                tmpStream = streamsChangedDuringTransaction.SingleOrDefault(x => x.StreamId == streamId);
                if (tmpStream != null)
                {
                    streamsChangedDuringTransaction.Remove(tmpStream);
                }
                // Remove stream from list of created streams
                if (streamsCreatedDuringTransaction.Contains(streamId))
                {
                    streamsCreatedDuringTransaction.Remove(streamId);
                }

                CommitTransaction();
            }
            catch
            {
                RollbackTransaction();
                throw;
            }
        }