/// <summary> /// Adds a table to the database based on the passed in table schema /// </summary> /// <param name="schema">The table schema</param> public void AddTable(TableSchema2 schema) { var table = new Table2(_process, schema, this.Storage, _process.Cache); _tables.Add(table); HandleAddTable(table.Name); }
public StepResult GetResult(Process process, string databaseName) { var result = new StepResult(); if (process.HasDatabase(databaseName)) { var db = process.GetDatabase2(databaseName); var columns = new ColumnSchema[Columns.Count]; int i = 1; int x = 0; foreach (var column in Columns) { columns[x] = GetColumnSchema(column, i); x++; i++; } var schema = new TableSchema2(columns, db.GetNextTableId(), TableName, databaseName, db.DatabaseId); var table = new Table2(process, schema, db.Storage, process.Cache); db.AddTable(table); } else { result.IsValid = false; result.ErrorMessage = $"{databaseName} was not found"; } return(result); }
private void ParseLine(string line) { if (line.StartsWith("version")) { ParseVersion(line); } if (line.StartsWith("database")) { ParseDatabase(line); } if (line.StartsWith("table")) { if (_tableSchema is null) { _tableSchema = GetTableSchema(line); _currentColumn = 0; } else { _dbSchema.Tables.Add(_tableSchema); _tableSchema = GetTableSchema(line); _currentColumn = 0; } } if (line.StartsWith("column")) { var column = GetColumn(line); column.Ordinal = _currentColumn; _tableSchema.Columns[_currentColumn] = column; _currentColumn++; } }
/// <summary> /// Returns all the rows held in the page based on the specified schema /// </summary> /// <param name="schema">The table schema</param> /// <returns>A list of rows</returns> public RowStruct[] GetRows(TableSchema2 schema) { var rows = new RowStruct[_totalRows]; IterateOverData(ref rows); return(rows); }
public BTreeContainer(BTreeAddress address, TreeDictionary <int, Page> tree, DbStorage storage, TableSchema2 schema, Process process) { _tree = tree; _address = address; _storage = storage; _schema = schema; _process = process; }
public RowInsert(List <RowValue2> values, TableSchema2 table, Guid?participantId, bool isReferenceInsert, BTreeAddress address) { _values = values; _table = table; _participantId = participantId; _xactId = Guid.NewGuid(); SortByBinaryFormat(); _address = address; }
/// <summary> /// Creates a new table schema from the text line /// </summary> /// <param name="line">The line from the file</param> /// <returns>A tablre schema object</returns> private TableSchema2 GetTableSchema(string line) { // table tableId tableName numOfColumns TableSchema2 result; var items = line.Split(" "); { result = new TableSchema2(Convert.ToInt32(items[1]), items[2], _dbSchema.DatabaseName, _dbSchema.DatabaseId, Convert.ToInt32(items[3])); } return(result); }
/// <summary> /// Constructs a table based on the specified TableSchema. Used when loading FrostDB from disk. /// </summary> /// <param name="process">The FrostDB process.</param> /// <param name="schema">The table schema (loaded from disk, usually from a DBFill object.)</param> /// <param name="dbStorage">The backing storage object for the database this table is attached to</param> /// <param name="pager">The in memory cache for FrostDb</param> public Table2(Process process, TableSchema2 schema, DbStorage dbStorage, Cache pager) { _process = process; _schema = schema; _name = schema.Name; _databaseName = schema.DatabaseName; _columns = schema.Columns; _tableId = schema.TableId; _databaseId = schema.DatabaseId; _storage = dbStorage; _cache = pager; }
/// <summary> /// Loads the schema file from disk into memory for this database /// </summary> private void LoadFile() { _tableSchema = null; var file = FileName(); var lines = File.ReadAllLines(file); _numOfColumns = GetNumOfColumnsInFile(lines); Array.ForEach(lines, line => ParseLine(line)); // if there was only 1 table in the file if (_tableSchema != null && !_dbSchema.Tables.Any(t => t.Name == _tableSchema.Name)) { _dbSchema.Tables.Add(_tableSchema); } }
/// <summary> /// Returns all rows for the specified table (via tree address) /// </summary> /// <param name="treeAddress">The tree's address (dbId, tableId)</param> /// <returns>All rows for the table</returns> public List <RowStruct> GetAllRows(BTreeAddress treeAddress) { var result = new List <RowStruct>(); Database2 database = _process.GetDatabase2(treeAddress.DatabaseId); TableSchema2 schema = database.GetTable(treeAddress.TableId).Schema; if (CacheHasContainer(treeAddress)) { result.AddRange(GetContainerFromCache(treeAddress).GetAllRows(schema, false)); } else { AddContainerToCache(treeAddress); result.AddRange(GetContainerFromCache(treeAddress).GetAllRows(schema, false)); } return(result); }
/// <summary> /// Constructs a brand new binary Page object with a size based on the DatabaseConstants file. Use this constructor when creating /// a brand new page (i.e. not loading from disk) /// </summary> /// <param name="id">The id of this page</param> /// <param name="tableId">The table id that this page belongs to</param> /// <param name="dbId">The database id that this page belongs to</param> public Page(int id, int tableId, int dbId, TableSchema2 schema, Process process) { _id = id; _tableId = tableId; _dbId = dbId; _address = new PageAddress { DatabaseId = DbId, TableId = TableId, PageId = Id }; _data = new byte[DatabaseConstants.PAGE_SIZE]; _totalRows = 0; _schema = schema; _process = process; SetPreamble(true); InitalizeDataWithEndOfRowData(); _pendingXacts = new List <Guid>(); }
/// <summary> /// Returns all rows from this tree in this container /// </summary> /// <param name="schema">The table schema to convert the rows to</param> /// <param name="dirtyRead">true if unprotected read, otherwise false</param> /// <returns>A list of rows</returns> public RowStruct[] GetAllRows(TableSchema2 schema, bool dirtyRead) { RowStruct[] result; int totalRows = 0; lock (_treeLock) { _tree.ForEach(item => { totalRows += item.Value.TotalRows; }); } result = new RowStruct[totalRows]; var resultSpan = new Span <RowStruct>(result); int i = 0; if (dirtyRead) { TreeDictionary <int, Page> item = _tree.DeepCopy(); foreach (var x in item) { RowStruct[] rows = x.Value.GetRows(schema); i += rows.Length; Array.Copy(rows, 0, result, i, rows.Length); } } else { lock (_treeLock) { foreach (var item in _tree) { RowStruct[] rows = item.Value.GetRows(schema); i += rows.Length; Array.Copy(rows, 0, result, i, rows.Length); } } } return(result); }
/// <summary> /// Updates the local schema reference based on the current tables and columns in memory /// </summary> private void UpdateSchema() { var schema = new DbSchema2(_databaseId, _name); _tables.ForEach(table => { var tableSchema = new TableSchema2(table.TableId, table.Name, _name, _databaseId, table.Columns.Length); int colIndx = 0; table.Columns.ForEach(column => { var columnSchema = new ColumnSchema(column.Name, column.DataType); tableSchema.Columns[colIndx] = columnSchema; colIndx++; }); schema.Tables.Add(tableSchema); }); _schema = schema; }
/// <summary> /// Tries to load a container from disk file. If this is a fresh database, will return a new container. /// </summary> /// <param name="address">The address of the container to get</param> /// <returns>A container from disk</returns> private BTreeContainer GetContainerFromDisk(BTreeAddress address) { Database2 db = _process.GetDatabase2(address.DatabaseId); DbStorage storage = db.Storage; var tree = new TreeDictionary <int, Page>(); TableSchema2 schema = db.GetTable(address.TableId).Schema; // get the first page Page page = storage.GetPage(1, address); // if this is a brand new table if (page == null) { page = new Page(1, address.TableId, address.DatabaseId, schema, _process); } tree.Add(page.Id, page); return(new BTreeContainer(address, tree, storage, schema, _process)); }
public Table(Process process, TableSchema2 schema) { throw new NotImplementedException(); }