Beispiel #1
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 #2
0
        public void CanReadWrittenDateRecord()
        {
            var tableFile = RandomFilename;
            var schema    = new Schema();

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

            _recordFile = new RecordFile(_tableInfo, _transaction);
            _recordFile.MoveToRID(new RID(0, 0));
            _recordFile.SetDate("field", new DateTime(2020, 1, 1));
            _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.GetDate("field");

            Assert.AreEqual(new DateTime(2020, 1, 1), value);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public void CanCreateAndCloseRecordFile()
        {
            _tableInfo = new TableInfo(RandomFilename, new Schema());

            Assert.DoesNotThrow(() =>
            {
                _recordFile = new RecordFile(_tableInfo, _transaction);
                _recordFile.Close();
            });
        }
Beispiel #5
0
        public void CreateIndex(string indexName, string tableName, string fieldName, Transaction transaction)
        {
            var recordFile = new RecordFile(_tableInfo, transaction);

            recordFile.Insert();
            recordFile.SetString("idxname", indexName);
            recordFile.SetString("tblname", tableName);
            recordFile.SetString("fldname", fieldName);
            recordFile.Close();
        }
Beispiel #6
0
        public void CreateView(string viewName, string viewDefinition, Transaction transaction)
        {
            var tableInfo  = _tableManager.GetTableInfo(_viewTableName, transaction);
            var recordFile = new RecordFile(tableInfo, transaction);

            recordFile.Insert();
            recordFile.SetString("viewname", viewName);
            recordFile.SetString("viewdef", viewDefinition);
            recordFile.Close();
        }
Beispiel #7
0
        public string GetViewDefinition(string viewName, Transaction transaction)
        {
            var result     = string.Empty;
            var tableInfo  = _tableManager.GetTableInfo(_viewTableName, transaction);
            var recordFile = new RecordFile(tableInfo, transaction);

            while (recordFile.Next())
            {
                if (recordFile.GetString("viewname") == viewName)
                {
                    result = recordFile.GetString("viewdef");
                    break;
                }
            }

            recordFile.Close();
            return(result);
        }
Beispiel #8
0
        public TableInfo GetTableInfo(string tableName, Transaction transaction)
        {
            var tableCatalogFile = new RecordFile(_tableCatalogInfo, transaction);
            var recordLength     = -1;

            while (tableCatalogFile.Next())
            {
                if (tableCatalogFile.GetString("tblname") == tableName)
                {
                    recordLength = tableCatalogFile.GetInt("reclength");
                    break;
                }
            }
            tableCatalogFile.Close();

            if (recordLength == -1)
            {
                return(null);
            }

            var fieldCatalogFile = new RecordFile(_fieldCatalogInfo, transaction);
            var schema           = new Schema();
            var offsets          = new Dictionary <string, int>();

            while (fieldCatalogFile.Next())
            {
                if (fieldCatalogFile.GetString("tblname") == tableName)
                {
                    var fieldName   = fieldCatalogFile.GetString("fldname");
                    int fieldType   = fieldCatalogFile.GetInt("type");
                    int fieldLength = fieldCatalogFile.GetInt("length");
                    int offset      = fieldCatalogFile.GetInt("offset");
                    offsets.Add(fieldName, offset);
                    schema.AddField(fieldName, (FieldType)fieldType, fieldLength);
                }
            }
            fieldCatalogFile.Close();

            return(new TableInfo(tableName, schema, offsets, recordLength));
        }
Beispiel #9
0
        public Dictionary <string, IndexInfo> GetIndexInfo(string tableName, Transaction transaction)
        {
            var result     = new Dictionary <string, IndexInfo>();
            var recordFile = new RecordFile(_tableInfo, transaction);

            while (recordFile.Next())
            {
                if (recordFile.GetString("tblname") == tableName)
                {
                    var indexName = recordFile.GetString("idxname");
                    var fieldName = recordFile.GetString("fldname");

                    var indexInfo = new IndexInfo(indexName, tableName, fieldName, _tableManager, _statisticsManager, transaction, _blockSize);

                    result.Add(fieldName, indexInfo);
                }
            }

            recordFile.Close();

            return(result);
        }
Beispiel #10
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 #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);
        }
Beispiel #12
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 #13
0
 public void Close()
 => _recordFile.Close();