Example #1
0
        public void InsertRecord(params object[] fields)
        {
            long insertLocation;

            //Find available spot to place record....
            if (_fileHeader.BlocksInFile == 0)
            {
                var block = RecordBlock.CreateNew(_stream, _schema, 0);

                _fileHeader.BlocksInFile++;
                WriteFileHeader();

                insertLocation = block.Insert(fields);
            }
            else
            {
                var lastBlock = RecordBlock.LoadFromStream(_stream, _schema, _fileHeader.BlocksInFile - 1);
                if (lastBlock.BlockHeader.FreeBytes > _schema.GetRecordSize())
                {
                    insertLocation = lastBlock.Insert(fields);
                }
                else
                {
                    var newBlock = RecordBlock.CreateNew(_stream, _schema, _fileHeader.BlocksInFile);
                    insertLocation = newBlock.Insert(fields);

                    _fileHeader.BlocksInFile++;
                    WriteFileHeader();
                }
            }

            _indexManager.AddRecord(fields, insertLocation, _schema);
        }
Example #2
0
        public Block(FileStream stream, DataStructure schema, long index)
        {
            _stream = stream;
            _schema = schema;

            this.Index = index;

            _stream.SeekToBlockHeader(index);
            this.BlockHeader  = _stream.ReadBlockHeader();
            _fieldTypeFactory = new Creation.FieldTypeFactory();
            _valueByteSize    = _schema.GetRecordSize();
        }
Example #3
0
        /// <summary>
        /// Inserts the record returns the location at which it was inserted.
        /// </summary>
        /// <param name="fields"></param>
        /// <returns></returns>
        public long Insert(params object[] fields)
        {
            _stream.SeekToRecord(_schema, BlockHeader.BlockSequence, BlockHeader.RecordCount);

            IDataFieldType[] fieldTypes = new IDataFieldType[fields.Length];

            for (int i = 0; i < fields.Length; i++)
            {
                var fieldDefinition = _schema.Fields[i];

                fieldTypes[i] = _fieldTypeFactory.GetFieldType(fieldDefinition.DataType);
            }

            for (int i = 0; i < fields.Length; i++)
            {
                var field = fields[i];
                if (!fieldTypes[i].IsValid(field))
                {
                    //Maybe i should collect all the failures and return them at once.
                    throw new Exception();
                }
            }

            long recordLocation = _stream.Position;

            //Only if there are no failures, we start writing to the file
            for (int i = 0; i < fields.Length; i++)
            {
                var field = fields[i];
                fieldTypes[i].WriteToStream(_stream, field);
            }


            BlockHeader.FreeBytes -= _schema.GetRecordSize();
            BlockHeader.RecordCount++;
            _stream.Flush();
            this.WriteBlockHeader();

            return(recordLocation);
        }
Example #4
0
        public static void SeekToRecord(this Stream fileStream, DataStructure schema, long blockIndex, long recordIndex)
        {
            var blockContents = ((Lengths.BlockLength + Lengths.BlockHeaderLength) * (blockIndex + 1)) + Lengths.FileHeaderLength + Lengths.BlockHeaderLength;

            fileStream.Position = blockContents + (recordIndex * schema.GetRecordSize());
        }