Beispiel #1
0
        public void SetValue(string fieldName, Constant value)
        {
            var type = _schema.FieldType(fieldName);

            switch (type)
            {
            case FieldType.Bool:
                _recordFile.SetBool(fieldName, (value as Constant <bool>).Value);
                return;

            case FieldType.Byte:
                _recordFile.SetByte(fieldName, (value as Constant <byte>).Value);
                return;

            case FieldType.Integer:
                _recordFile.SetInt(fieldName, (value as Constant <int>).Value);
                return;

            case FieldType.Blob:
                _recordFile.SetBlob(fieldName, (value as Constant <byte[]>).Value);
                return;

            case FieldType.Date:
                _recordFile.SetDate(fieldName, (value as Constant <DateTime>).Value);
                return;
            }
        }
Beispiel #2
0
        public void CanDeleteRecord()
        {
            var tableFile = RandomFilename;
            var schema    = new Schema();

            schema.AddIntField("field");
            _tableInfo = new TableInfo(tableFile, schema);

            _recordFile = new RecordFile(_tableInfo, _transaction);

            _recordFile.BeforeFirst();

            _recordFile.Insert();
            _recordFile.SetInt("field", 10);

            _recordFile.Delete();

            _transaction.Commit();

            var block = new IO.Primitives.Block(tableFile + ".tbl", 0);
            var page  = _fileManager.ResolvePage(block);

            page.Read(block);

            page.GetInt(0, out var used);
            Assert.AreEqual(0, used);
        }
Beispiel #3
0
        public void CanReadWrittenIntRecord()
        {
            var tableFile = RandomFilename;
            var schema    = new Schema();

            schema.AddIntField("field");
            _tableInfo = new TableInfo(tableFile, schema);

            _recordFile = new RecordFile(_tableInfo, _transaction);
            _recordFile.MoveToRID(new RID(0, 0));
            _recordFile.SetInt("field", 10);
            _recordFile.Close();

            _transaction.Commit();

            var cm    = new ConcurrencyManager();
            var newTr = new Transaction(_dispatcher, _bufferManager, cm, _fileManager, _logManager);

            var rf = new RecordFile(_tableInfo, newTr);

            rf.MoveToRID(new RID(0, 0));
            var value = rf.GetInt("field");

            Assert.AreEqual(10, value);
        }
Beispiel #4
0
        public void CanCreateIndexOnFilledTable()
        {
            var recordFile = new RecordFile(tableInfo, _transaction);

            recordFile.BeforeFirst();
            for (int i = 0; i < 10; ++i)
            {
                recordFile.Insert();
                recordFile.SetInt("Id", i - 1);
            }
            recordFile.Close();

            metadataManager = new MetadataManager(_fileManager, tableManager, viewManager, indexManager, statisticsManager);

            Assert.DoesNotThrow(() =>
            {
                metadataManager.CreateIndex(RandomFilename, tableName, "Id", _transaction);
            });

            var index = metadataManager.GetIndexInfo(tableName, _transaction);

            Assert.IsNotNull(index);
            Assert.AreEqual(0, index["Id"].BlocksAccessed());
            Assert.AreEqual(2, index["Id"].RecordsOutput());
        }
Beispiel #5
0
        public void CanGetStatisticsFromNewTableInTheSameTransaction()
        {
            manager = new StatisticsManager(tableManager, _transaction, tableCatalogName, fieldCatalogName, 1);


            var newTableName = RandomFilename;

            var schema = new Schema();

            schema.AddIntField("Id");
            schema.AddStringField("Guid", 40);

            var newTableInfo = new TableInfo(newTableName, schema);

            tableManager.CreateTable(newTableName, schema, _transaction);

            var recordFile = new RecordFile(newTableInfo, _transaction);

            recordFile.BeforeFirst();

            for (int i = 0; i < 50; ++i)
            {
                recordFile.Insert();
                recordFile.SetInt("Id", i + 10);
                recordFile.SetString("Guid", Guid.NewGuid().ToString());
            }

            var updatedData = manager.GetStatisticalInfo(newTableName, _transaction);

            Assert.AreEqual(50, updatedData.RecordsOutput);
            Assert.AreEqual(3, updatedData.BlocksAccessed);
            Assert.AreEqual(17, updatedData.DistinctValues("Id"));
        }
Beispiel #6
0
        public void CanGetStatisticsFromTableFilledWithValuesInTheSameTransaction()
        {
            manager = new StatisticsManager(tableManager, _transaction, tableCatalogName, fieldCatalogName, 1);

            var data = manager.GetStatisticalInfo(tableName, _transaction);

            var recordFile = new RecordFile(tableInfo, _transaction);

            recordFile.BeforeFirst();

            for (int i = 0; i < 30; ++i)
            {
                recordFile.Insert();
                recordFile.SetInt("Id", i + 10);
            }

            //Before update

            Assert.AreEqual(1, data.BlocksAccessed);
            Assert.AreEqual(0, data.RecordsOutput);

            Assert.AreEqual(1, data.DistinctValues("Id"));

            //After update
            var updatedData = manager.GetStatisticalInfo(tableName, _transaction);

            Assert.AreEqual(30, updatedData.RecordsOutput);
            Assert.AreEqual(1, updatedData.BlocksAccessed);
            Assert.AreEqual(11, updatedData.DistinctValues("Id"));
        }
Beispiel #7
0
        public void CanNavigateThourghExistingRecordsMoreRecords()
        {
            var tableFile = RandomFilename;
            var schema    = new Schema();

            schema.AddIntField("field");
            _tableInfo = new TableInfo(tableFile, schema);

            _recordFile = new RecordFile(_tableInfo, _transaction);

            _recordFile.BeforeFirst();

            for (int i = 0; i < 12; ++i)
            {
                _recordFile.Insert();
                _recordFile.SetInt("field", i);
            }

            // And this folk would go to the next page
            _recordFile.Insert();
            _recordFile.SetInt("field", 123);

            _transaction.Commit();

            var cm    = new ConcurrencyManager();
            var newTr = new Transaction(_dispatcher, _bufferManager, cm, _fileManager, _logManager);

            var rf = new RecordFile(_tableInfo, newTr);

            rf.BeforeFirst();

            for (int i = 0; i < 12; ++i)
            {
                rf.Next();
                var value = rf.GetInt("field");
                Assert.AreEqual(i, value);
            }

            rf.Next();
            var lastValue = rf.GetInt("field");

            Assert.AreEqual(123, lastValue);

            var canMoveNext = rf.Next();

            Assert.IsFalse(canMoveNext);
        }
Beispiel #8
0
        public void CanGetBeforeFirstAndInsertValues()
        {
            var tableFile = RandomFilename;
            var schema    = new Schema();

            schema.AddIntField("field");
            _tableInfo = new TableInfo(tableFile, schema);

            _recordFile = new RecordFile(_tableInfo, _transaction);

            _recordFile.BeforeFirst();

            _recordFile.Insert();
            _recordFile.SetInt("field", 10);

            _recordFile.Insert();
            _recordFile.SetInt("field", 20);

            _transaction.Commit();

            var block = new IO.Primitives.Block(tableFile + ".tbl", 0);
            var page  = _fileManager.ResolvePage(block);

            page.Read(block);

            page.GetInt(0, out var used1);
            page.GetInt(8, out var used2);


            page.GetInt(4, out var value1);
            page.GetInt(12, out var value2);

            Assert.AreEqual(1, used1);
            Assert.AreEqual(1, used2);

            Assert.AreEqual(10, value1);
            Assert.AreEqual(20, value2);
        }
Beispiel #9
0
        public void CreateTable(string tableName, Schema schema, Transaction transaction)
        {
            var tableInfo = new TableInfo(tableName, schema);

            var tableCatalogFile = new RecordFile(_tableCatalogInfo, transaction);

            tableCatalogFile.Insert();
            tableCatalogFile.SetString("tblname", tableName);
            tableCatalogFile.SetInt("reclength", tableInfo.RecordLength);
            tableCatalogFile.Close();

            var fieldCatalogFile = new RecordFile(_fieldCatalogInfo, transaction);

            foreach (var fieldName in schema.Fields)
            {
                fieldCatalogFile.Insert();
                fieldCatalogFile.SetString("tblname", tableName);
                fieldCatalogFile.SetString("fldname", fieldName.Key);
                fieldCatalogFile.SetInt("type", (int)fieldName.Value.Type);
                fieldCatalogFile.SetInt("length", fieldName.Value.Length);
                fieldCatalogFile.SetInt("offset", tableInfo.Offset(fieldName.Key));
            }
            fieldCatalogFile.Close();
        }
Beispiel #10
0
        public void CanCreatePlanFromTableWithData()
        {
            var recordFile = new RecordFile(tableInfo, _transaction);

            recordFile.BeforeFirst();

            for (int i = 0; i < 30; ++i)
            {
                recordFile.Insert();
                recordFile.SetInt("Id", i + 10);
            }
            _transaction.Commit();

            tablePlan = new TablePlan(tableName, metadataManager, _transaction);

            Assert.DoesNotThrow(() =>
            {
                var scan = tablePlan.Open();
            });
        }
Beispiel #11
0
        public void CanWriteIntOnARecord()
        {
            var tableFile = RandomFilename;
            var schema    = new Schema();

            schema.AddIntField("field");
            _tableInfo = new TableInfo(tableFile, schema);

            _recordFile = new RecordFile(_tableInfo, _transaction);
            _recordFile.MoveToRID(new RID(0, 0));
            _recordFile.SetInt("field", 10);
            _recordFile.Close();

            _transaction.Commit();

            var block = new IO.Primitives.Block(tableFile + ".tbl", 0);
            var page  = _fileManager.ResolvePage(block);

            page.Read(block);

            _ = page.GetInt(4, out var value);
            Assert.AreEqual(10, value);
        }
Beispiel #12
0
        public void CanGetRecordsOutputOnMoreThanOneBlock()
        {
            var recordFile = new RecordFile(tableInfo, _transaction);

            recordFile.BeforeFirst();

            for (int i = 0; i < 1024; ++i)
            {
                recordFile.Insert();
                recordFile.SetInt("Id", i * i);
            }
            recordFile.Close();
            _transaction.Commit();
            _concurrencyManager = new ConcurrencyManager();
            _transaction        = new Transaction(_dispatcher, _bufferManager, _concurrencyManager, _fileManager, _logManager);


            indexInfo = new IndexInfo("index", tableName, "Id", tableManager, statisticsManager, _transaction, 1024);

            var recordsOutput = indexInfo.RecordsOutput();

            Assert.AreEqual(2, recordsOutput);
        }