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); }
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); }
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)); }
public Constant GetValue(string fieldName) { var type = _schema.FieldType(fieldName); switch (type) { case FieldType.Bool: return(new Constant <bool>(_recordFile.GetBool(fieldName))); case FieldType.Byte: return(new Constant <byte>(_recordFile.GetByte(fieldName))); case FieldType.Integer: return(new Constant <int>(_recordFile.GetInt(fieldName))); case FieldType.Blob: return(new Constant <byte[]>(_recordFile.GetBlob(fieldName))); case FieldType.Date: return(new Constant <DateTime>(_recordFile.GetDate(fieldName))); } return(null); }
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); }