Esempio n. 1
0
        private void RemoveAtPosition()
        {
            // Tell the root set that any child subsets may be dirty
            if (root != this)
            {
                root.version       += 1;
                root.rootStateDirty = true;
            }

            version += 1;

            // The number of byte entries to remove
            long strRemoveSize = foundItemStart - foundItemEnd;

            file.Position = foundItemEnd;
            file.Shift(strRemoveSize);
            endPos = endPos + strRemoveSize;

            // If this removal leaves the set empty, we delete the file and update the
            // internal state as necessary.
            if (startPos == 8 && endPos == 8)
            {
                file.Delete();
                startPos = 0;
                endPos   = 0;
            }
        }
Esempio n. 2
0
            public void SetValue(long columnid, String value)
            {
                file.Position = 0;
                BinaryReader din = new BinaryReader(new DataFileStream(file));

                try {
                    int size = Math.Min((int)file.Length, Int32.MaxValue);
                    // If file is not empty,
                    if (size != 0)
                    {
                        // Check if the columnid already set,
                        int hsize = din.ReadInt32();

                        for (int i = 0; i < hsize; ++i)
                        {
                            long sid     = din.ReadInt64();
                            int  coffset = din.ReadInt32();
                            if (sid == columnid)
                            {
                                // Yes, so generate error,
                                throw new ApplicationException("Column value already set.");
                            }
                        }
                    }

                    BinaryWriter writer = new BinaryWriter(new DataFileStream(file), Encoding.Unicode);

                    // Ok to add column,
                    file.Position = 0;
                    if (size == 0)
                    {
                        writer.Write(1);
                        writer.Write(columnid);
                        writer.Write(0);
                    }
                    else
                    {
                        int count = din.ReadInt32();
                        ++count;
                        file.Position = 0;
                        writer.Write(count);
                        file.Shift(12);
                        writer.Write(columnid);
                        writer.Write((int)(file.Length - (count * 12) - 4));
                        file.Position = file.Length;
                    }

                    // Write the string
                    writer.Write((byte)1);
                    writer.Write(value);
                    writer.Flush();
                    writer.Close();
                } catch (IOException e) {
                    // Wrap IOException around a runtime exception
                    throw new ApplicationException(e.Message, e);
                }
            }
Esempio n. 3
0
            public void ReplicateTo(IDataFile destFile)
            {
                // This is a little complex. If 'destFile' is an instance of SubDataFile
                // we use the raw 'ReplicateTo' method on the data files and preserve
                // the header on the target by making a copy of it before the replicateTo
                // function.
                // Otherwise, we use a 'CopyTo' implementation.

                // If replicating to a SubDataFile
                if (destFile is SubDataFile)
                {
                    // Preserve the header of the target
                    SubDataFile targetFile = (SubDataFile)destFile;
                    long        headerSize = targetFile.start;
                    if (headerSize <= 8192)
                    {
                        IDataFile targetDf = targetFile.df;
                        // Make a copy of the header in the target,
                        int    iheadSize = (int)headerSize;
                        byte[] header    = new byte[iheadSize];
                        targetDf.Position = 0;
                        targetDf.Read(header, 0, iheadSize);

                        // Replicate the bases
                        df.ReplicateTo(targetDf);
                        // Now 'target_df' will be a replica of this, so we need to copy
                        // the previous header back on the target.
                        // Remove the replicated header on the target and copy the old one
                        // back.
                        targetDf.Position = start;
                        targetDf.Shift(iheadSize - start);
                        targetDf.Position = 0;
                        targetDf.Write(header, 0, iheadSize);
                        // Set position per spec
                        targetDf.Position = targetDf.Length;
                        // Done.
                        return;
                    }
                }

                // Fall back to a copy-to implementation
                destFile.Delete();
                destFile.Position = 0;
                df.Position       = start;
                df.CopyTo(destFile, df.Length - start);
            }
Esempio n. 4
0
        public int Remove(long blockId)
        {
            long p = Search(new Record(blockId, 0));

            if (p < 0)
            {
                p = -(p + 1);
            }

            IDataFile dfile    = DataFile;
            long      size     = dfile.Length;
            long      startPos = p * RecordSize;
            long      pos      = startPos;

            dfile.Position = pos;

            int count = 0;

            while (pos < size)
            {
                long readBlockId  = Input.ReadInt64();
                long readServerId = Input.ReadInt64();

                if (readBlockId != blockId)
                {
                    break;
                }

                pos += RecordSize;
                ++count;
            }

            if ((startPos - pos) != 0)
            {
                dfile.Position = pos;
                dfile.Shift(startPos - pos);
            }

            return(count);
        }
 private static void ByteBufferCopyTo(IDataFile source, IDataFile target, long size)
 {
     long pos = target.Position;
     // Make room to insert the data
     target.Shift(size);
     target.Position = pos;
     // Set a 1k buffer
     byte[] buf = new byte[1024];
     // While there is data to copy,
     while (size > 0) {
         // Read an amount of data from the source
         int toRead = (int) Math.Min(buf.Length, size);
         // Read it into the buffer
         source.Read(buf, 0, toRead);
         // Write from the buffer out to the target
         target.Write(buf, 0, toRead);
         // Update the ref
         size = size - toRead;
     }
 }
Esempio n. 6
0
 public void Shift(long offset)
 {
     df.Shift(offset);
 }
Esempio n. 7
0
 public void Shift(long offset)
 {
     transaction.CheckValid();
     LogChange();
     parent.Shift(offset);
 }