BplusDotNet.LinkedFile.StoreNewChunk C# (CSharp) Method

StoreNewChunk() public method

public StoreNewChunk ( byte fromArray, int startingAt, int length ) : long
fromArray byte
startingAt int
length int
return long
        public long StoreNewChunk(byte[] fromArray, int startingAt, int length)
        {
            // get the first buffer as result value
            long currentBufferNumber = this.AllocateBuffer();
            //System.Diagnostics.Debug.WriteLine(" allocating chunk starting at "+currentBufferNumber);
            long result = currentBufferNumber;
            if (length<0 || startingAt<0)
            {
                throw new LinkedFileException("cannot store negative length chunk ("+startingAt+","+length+")");
            }
            int endingAt = startingAt+length;
            // special case: zero length chunk
            if (endingAt>fromArray.Length)
            {
                throw new LinkedFileException("array doesn't have this much data: "+endingAt);
            }
            int index = startingAt;
            // store header with length information
            byte[] buffer = new byte[this.buffersize];
            BufferFile.Store(length, buffer, 0);
            int fromIndex = startingAt;
            int firstLength = this.buffersize-BufferFile.INTSTORAGE;
            int stored = 0;
            if (firstLength>length)
            {
                firstLength=length;
            }
            Array.Copy(fromArray, fromIndex, buffer, BufferFile.INTSTORAGE, firstLength);
            stored += firstLength;
            fromIndex += firstLength;
            byte CurrentBufferType = HEAD;
            // store any remaining buffers (no length info)
            while (stored<length)
            {
                // store current buffer and get next block number
                long nextBufferNumber = this.AllocateBuffer();
                this.SetBuffer(currentBufferNumber, CurrentBufferType, buffer, 0, buffer.Length, nextBufferNumber);
                currentBufferNumber = nextBufferNumber;
                CurrentBufferType = BODY;
                int nextLength = this.buffersize;
                if (stored+nextLength>length)
                {
                    nextLength = length-stored;
                }
                Array.Copy(fromArray, fromIndex, buffer, 0, nextLength);
                stored += nextLength;
                fromIndex += nextLength;
            }
            // store final buffer
            this.SetBuffer(currentBufferNumber, CurrentBufferType, buffer, 0, buffer.Length, NULLBUFFERPOINTER);
            return result;
        }

Usage Example

Ejemplo n.º 1
0
 public byte[] this[string key]
 {
     set
     {
         long storage = m_archive.StoreNewChunk(value, 0, value.Length);
         //this.FreeChunksOnAbort.Add(storage);
         m_freeChunksOnAbort[storage] = storage;
         long valueFound;
         if (m_tree.ContainsKey(key, out valueFound))
         {
             //this.archive.ReleaseBuffers(valueFound);
             if (m_freeChunksOnAbort.ContainsKey(valueFound))
             {
                 // free it now
                 m_freeChunksOnAbort.Remove(valueFound);
                 m_archive.ReleaseBuffers(valueFound);
             }
             else
             {
                 // release at commit.
                 m_freeChunksOnCommit[valueFound] = valueFound;
             }
         }
         m_tree[key] = storage;
     }
     get
     {
         long map = m_tree[key];
         return(m_archive.GetChunk(map));
     }
 }