예제 #1
0
        private bool AddFull(T Record, Node add, bool check = false, Record tmpRec = null)
        {
            if (check && this.FindAddress(Record, add) >= 0)
            {
                return(false);
            }

            Block <T> block = new Block <T>(this.BlockSize, Record, -1);

            if (add.Next.LastOrDefault() == null || add.Next.Last().BlockSize == this.BlockSize) // if node has not any another blocks
            {                                                                                    // or block is full
                add.Next.AddLast(new Node(0, this.LastPosition));                                // alloc position for new block
                this.LastPosition += block.GetSize();
            }

            if (add.Next.Last().BlockSize > 0)
            {
                this.LoadBlock(ref block, add.Next.Last().Address);
            }

            if (tmpRec == null)
            {
                this.RemoveAddEvent();
                block.Add(new Record(ObjectReader.Add(Record), Record.GetKey()));
                this.AddAddEvent();
            }
            else
            {
                block.Add(tmpRec);
            }
            add.Next.Last().BlockSize = block.Records.Count; // set blockSize
            bw.Seek(add.Next.Last().Address, SeekOrigin.Begin);
            bw.Write(block.ToByteArray());                   // write to file
            return(true);
        }
예제 #2
0
        public void RecordExample()
        {
            // To create a record that implement IDataRecord we start with a record set definition.
            RecordSetDefinition recordSetDefinition = new RecordSetDefinition(
                new ColumnDefinition("ID", SqlDbType.Int),
                new ColumnDefinition("Name", SqlDbType.Char, 50),
                new ColumnDefinition("Description", SqlDbType.NVarChar),
                // This column is not nullable so defaults to true
                new ColumnDefinition("Active", SqlDbType.Bit, isNullable: false, defaultValue: true)
                );

            // Now we can create a record
            IObjectRecord dataRecord = new ObjectRecord(recordSetDefinition, 1, "Test", "This is my test record");

            // Or we can create one with random values
            IObjectRecord randomRecord = new ObjectRecord(recordSetDefinition, true);

            // To create a record that throws an exception we first create a SqlException
            // We can't do this directly, but we can use our prototypes to construct one.

            // SqlExceptions are made from a collection of SqlErrors - which can make like this :
            SqlErrorCollection errorCollection = new SqlErrorCollectionPrototype
            {
                new SqlErrorPrototype(
                    1000,
                    80,
                    17,
                    "MyFakeServer",
                    "Connection Timeout.",
                    "spMySproc",
                    54)
            };

            SqlException  sqlException    = new SqlExceptionPrototype(errorCollection, "9.0.0.0", Guid.NewGuid());
            IObjectRecord exceptionRecord = new ExceptionRecord(sqlException);

            // We can stick these records into a recordset
            // Note the records must have the same RecordSetDefinition (unless it's an exception record)
            // The final record will through an exception when reached!
            ObjectSet recordSet = new ObjectSet(recordSetDefinition)
            {
                dataRecord,
                randomRecord,
                //exceptionRecord
            };

            // We can add recordsets to an ObjectReader
            ObjectReader reader = new ObjectReader
            {
                recordSet
            };

            // We can also add random record sets - this one has the same definition as the first.
            reader.Add(new RandomSet(recordSetDefinition));

            // We can also fix certain rows values using the column generators arry, a null indicates
            // that the column should us a random value, otherwise a lambda can be supplied - in this case
            // it sets the row to the row number (1 - indexed).
            reader.Add(
                new RandomSet(
                    recordSetDefinition,
                    columnGenerators: new Func <int, object>[] { null, row => "Row #" + row }));

            // Whereas this one has a random set of columns (with random types).
            reader.Add(new RandomSet(10));

            // Now that we have a reader we can use it like a normal reader - it even simulates disposal.
            using (IDataReader dataReader = reader)
            {
                int recordset = 1;
                do
                {
                    Trace.Write("Recordset #" + recordset);
                    int rows = 0;
                    while (dataReader.Read())
                    {
                        rows++;
                    }
                    Trace.WriteLine(" - " + rows + " rows.");
                    recordset++;
                } while (dataReader.NextResult());
            }
        }
예제 #3
0
        public void RecordExample()
        {
            // To create a record that implement IDataRecord we start with a record set definition.
            RecordSetDefinition recordSetDefinition = new RecordSetDefinition(
                new ColumnDefinition("ID", SqlDbType.Int),
                new ColumnDefinition("Name", SqlDbType.Char, 50),
                new ColumnDefinition("Description", SqlDbType.NVarChar),
                // This column is not nullable so defaults to true
                new ColumnDefinition("Active", SqlDbType.Bit, isNullable: false, defaultValue: true)
                );

            // Now we can create a record
            IObjectRecord dataRecord = new ObjectRecord(recordSetDefinition, 1, "Test", "This is my test record");

            // Or we can create one with random values
            IObjectRecord randomRecord = new ObjectRecord(recordSetDefinition, true);

            // To create a record that throws an exception we first create a SqlException
            // We can't do this directly, but we can use our prototypes to construct one.

            // SqlExceptions are made from a collection of SqlErrors - which can make like this :
            SqlErrorCollection errorCollection = new SqlErrorCollectionPrototype
            {
                new SqlErrorPrototype(
                    1000,
                    80,
                    17,
                    "MyFakeServer",
                    "Connection Timeout.",
                    "spMySproc",
                    54)
            };

            SqlException sqlException = new SqlExceptionPrototype(errorCollection, "9.0.0.0", Guid.NewGuid());
            IObjectRecord exceptionRecord = new ExceptionRecord(sqlException);

            // We can stick these records into a recordset
            // Note the records must have the same RecordSetDefinition (unless it's an exception record)
            // The final record will through an exception when reached!
            ObjectSet recordSet = new ObjectSet(recordSetDefinition)
            {
                dataRecord,
                randomRecord,
                //exceptionRecord
            };

            // We can add recordsets to an ObjectReader
            ObjectReader reader = new ObjectReader
            {
                recordSet
            };

            // We can also add random record sets - this one has the same definition as the first.
            reader.Add(new RandomSet(recordSetDefinition));

            // We can also fix certain rows values using the column generators arry, a null indicates
            // that the column should us a random value, otherwise a lambda can be supplied - in this case
            // it sets the row to the row number (1 - indexed).
            reader.Add(
                new RandomSet(
                    recordSetDefinition,
                    columnGenerators: new Func<int, object>[] { null, row => "Row #" + row }));

            // Whereas this one has a random set of columns (with random types).
            reader.Add(new RandomSet(10));

            // Now that we have a reader we can use it like a normal reader - it even simulates disposal.
            using (IDataReader dataReader = reader)
            {
                int recordset = 1;
                do
                {
                    Trace.Write("Recordset #" + recordset);
                    int rows = 0;
                    while (dataReader.Read())
                        rows++;
                    Trace.WriteLine(" - " + rows + " rows.");
                    recordset++;
                } while (dataReader.NextResult());
            }
        }
예제 #4
0
        private bool Add(T Record, Record tmpRec = null, bool check = true)
        {
            Block <T> block      = new Block <T>(BlockSize, Record);
            BitArray  recordHash = Record.GetHash(this.MaxDepth);
            Node      add        = Trie.Find(recordHash, out int depth);

            if (add == null ||                                                // add is null only on first insert
                (depth < this.MaxDepth && add.BlockSize <= this.BlockSize) || // split or add
                (depth == this.MaxDepth && add.BlockSize < this.BlockSize))   // just add
            {
                if (add == null || add.BlockSize < 0)
                {
                    if (tmpRec == null)
                    {
                        this.RemoveAddEvent();
                        block.Add(new Record(ObjectReader.Add(Record), Record.GetKey()));
                        this.AddAddEvent();
                    }
                    else
                    {
                        block.Add(tmpRec);
                    }
                    if (add == null)
                    {
                        Trie.Add(recordHash, this.LastPosition, 1);
                    }
                    else
                    {
                        add.BlockSize = 1;
                        add.Address   = this.LastPosition;
                    }
                    bw.Seek(this.LastPosition, SeekOrigin.Begin);
                    bw.Write(block.ToByteArray());
                    this.LastPosition += block.GetSize();
                }
                else
                {
                    if (check && this.FindAddress(Record, add) >= 0)
                    {
                        return(false);
                    }

                    if (tmpRec == null)
                    {
                        this.RemoveAddEvent();
                        tmpRec = new Record(ObjectReader.Add(Record), Record.GetKey());
                        this.AddAddEvent();
                    }
                    this.LoadBlock(ref block, add.Address);
                    if (add.BlockSize == BlockSize)
                    {
                        Block <T> blockNew = block.Split(Record, this.MaxDepth, tmpRec.Address);
                        if (blockNew == null)
                        {
                            return(this.AddFull(Record, add, false, tmpRec));
                        }
                        Node nodeNew = Trie.Add(recordHash, this.LastPosition, blockNew.Depth, true);
                        nodeNew.Right.BlockSize = blockNew.Records.Count;
                        nodeNew.Left.BlockSize  = block.Records.Count;
                        nodeNew.Left.Address    = add.Address;
                        if (add.Next.Count > 0)
                        {
                            nodeNew.Left.Next = new LinkedList <Node>(add.Next);
                            add.Next.Clear();
                        }
                        add.BlockSize = -1;
                        add.Address   = -1;

                        bw.Seek(nodeNew.Right.Address, SeekOrigin.Begin);
                        bw.Write(blockNew.ToByteArray());
                        this.LastPosition += block.GetSize();
                        add = nodeNew.Left;
                    }
                    else
                    {
                        block.Add(tmpRec);
                        add.BlockSize++;
                    }
                    bw.Seek(add.Address, SeekOrigin.Begin);
                    bw.Write(block.ToByteArray());
                }
            }
            else
            {
                return(this.AddFull(Record, add, tmpRec == null, tmpRec));
            }

            return(true);
        }