Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public void CanCreateViewDefinition()
        {
            var viewMetadataStorageFilename = RandomFilename;

            viewManager = new ViewManager(true, tableManager, _transaction, viewMetadataStorageFilename);

            viewManager.CreateView("view1", "viewDefinition1", _transaction);

            _transaction.Commit();

            var schema = new Schema();

            schema.AddStringField("viewname", ViewManager.MAX_VIEW_LENGTH);
            schema.AddStringField("viewdef", ViewManager.MAX_VIEW_LENGTH);

            var viewTableInfo = tableManager.GetTableInfo(viewMetadataStorageFilename, _transaction);
            var viewTableCatalogRecordPage = new RecordFile(viewTableInfo, _transaction);

            viewTableCatalogRecordPage.BeforeFirst();
            viewTableCatalogRecordPage.Next();

            var viewName       = viewTableCatalogRecordPage.GetString("viewname");
            var viewDefinition = viewTableCatalogRecordPage.GetString("viewdef");

            Assert.AreEqual("view1", viewName);
            Assert.AreEqual("viewDefinition1", viewDefinition);
        }
Ejemplo n.º 3
0
        public void CanInsertMoreThanForOnePage()
        {
            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();
            }

            // And this folk would go to the next page
            _recordFile.Insert();

            var rid = _recordFile.CurrentRID;

            Assert.AreEqual(1, rid.BlockNumber);
            Assert.AreEqual(0, rid.Id);
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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"));
        }
Ejemplo n.º 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"));
        }
Ejemplo n.º 7
0
        private StatisticalInfo RefreshTableStatistics(string tableName, Transaction transaction)
        {
            var recordsCount = 0;

            var tableInfo = _tableManager.GetTableInfo(tableName, transaction);

            if (tableInfo == null)
            {
                return(null);
            }

            var recordFile = new RecordFile(tableInfo, transaction);

            recordFile.BeforeFirst();

            while (recordFile.Next())
            {
                recordsCount++;
            }

            int blockNumber = recordFile.CurrentRID.BlockNumber;

            recordFile.Close();

            int blocksCount = 1 + blockNumber;
            var statInfo    = new StatisticalInfo(blocksCount, recordsCount);

            _tableStats.Add(tableName, statInfo);
            return(statInfo);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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();
            });
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        public void CanCreateTable()
        {
            var tableMetadataStorageFilename = RandomFilename;
            var fieldMetadataStorageFilename = RandomFilename;

            tableManager = new TableManager(true, _transaction, tableMetadataStorageFilename, fieldMetadataStorageFilename);
            var schema = new Schema();

            schema.AddIntField("field1");
            schema.AddBlobField("field2", 40);

            tableManager.CreateTable("table1", schema, _transaction);

            _transaction.Commit();

            _concurrencyManager = new ConcurrencyManager();
            _transaction        = new Transaction(_dispatcher, _bufferManager, _concurrencyManager, _fileManager, _logManager);

            var tableCatalogSchema = new Schema();

            tableCatalogSchema.AddStringField("tblname", TableManager.MAX_NAME_LENGTH);
            tableCatalogSchema.AddIntField("reclength");

            var tableCatalogInfo       = new TableInfo(tableMetadataStorageFilename, tableCatalogSchema);
            var tableCatalogRecordPage = new RecordFile(tableCatalogInfo, _transaction);

            tableCatalogRecordPage.BeforeFirst();
            tableCatalogRecordPage.Next();

            // skip all tables definition and all fields definition tables
            tableCatalogRecordPage.Next();
            tableCatalogRecordPage.Next();

            var tableName    = tableCatalogRecordPage.GetString("tblname");
            var recordLength = tableCatalogRecordPage.GetInt("reclength");

            Assert.AreEqual("table1", tableName);
            Assert.AreEqual(new TableInfo("table1", schema).RecordLength, recordLength);

            var fieldCatalogSchema = new Schema();

            fieldCatalogSchema.AddStringField("tblname", TableManager.MAX_NAME_LENGTH);
            fieldCatalogSchema.AddStringField("fldname", TableManager.MAX_NAME_LENGTH);
            fieldCatalogSchema.AddIntField("type");
            fieldCatalogSchema.AddIntField("length");
            fieldCatalogSchema.AddIntField("offset");
            var fieldCatalogInfo       = new TableInfo(fieldMetadataStorageFilename, fieldCatalogSchema);
            var fieldCatalogRecordPage = new RecordFile(fieldCatalogInfo, _transaction);

            fieldCatalogRecordPage.BeforeFirst();
            fieldCatalogRecordPage.Next();

            // 2 field for table metadata table, 5 fields for field metadata table
            fieldCatalogRecordPage.Next();
            fieldCatalogRecordPage.Next();
            fieldCatalogRecordPage.Next();
            fieldCatalogRecordPage.Next();
            fieldCatalogRecordPage.Next();
            fieldCatalogRecordPage.Next();
            fieldCatalogRecordPage.Next();

            var field1Table  = fieldCatalogRecordPage.GetString("tblname");
            var field1Name   = fieldCatalogRecordPage.GetString("fldname");
            var field1Type   = fieldCatalogRecordPage.GetInt("type");
            var field1Length = fieldCatalogRecordPage.GetInt("length");
            var field1Offset = fieldCatalogRecordPage.GetInt("offset");

            Assert.AreEqual("table1", field1Table);
            Assert.AreEqual("field1", field1Name);
            Assert.AreEqual(2, field1Type);
            Assert.AreEqual(sizeof(int), field1Length);
            Assert.AreEqual(0, field1Offset);

            fieldCatalogRecordPage.Next();

            var field2Table  = fieldCatalogRecordPage.GetString("tblname");
            var field2Name   = fieldCatalogRecordPage.GetString("fldname");
            var field2Type   = fieldCatalogRecordPage.GetInt("type");
            var field2Length = fieldCatalogRecordPage.GetInt("length");
            var field2Offset = fieldCatalogRecordPage.GetInt("offset");

            Assert.AreEqual("table1", field2Table);
            Assert.AreEqual("field2", field2Name);
            Assert.AreEqual(4, field2Type);
            Assert.AreEqual(40, field2Length);
            Assert.AreEqual(4, field2Offset);
        }
Ejemplo n.º 13
0
 public void BeforeFirst()
 => _recordFile.BeforeFirst();