/// <summary> /// Re load the schema structure of the database from disk /// </summary> /// <returns>true if loaded successfully</returns> public bool Load() { try { var reader = new io.BinaryReader(io.File.OpenRead(SchemaFilePath)); Schema = DbSchemaConfig.Load(reader); reader.Dispose(); //link foreach (var table in Tables) { table.Database = this; foreach (var column in table.Columns) { //update if (!column.Indexed) { column.NodePages = 0; column.ItemPages = 0; } column.Table = table; } } ; SaveKeyIndexedKeyOffsets(); return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }
/// <summary> /// Loads a database schema /// </summary> /// <param name="reader">binary reader</param> /// <returns></returns> public static DbSchemaConfig Load(io.BinaryReader reader) { var schema = new DbSchemaConfig() { Flags = (DbSchemaConfigType)reader.ReadUInt32(), Version = reader.BinaryRead(), Description = reader.BinaryRead(), Name = reader.BinaryRead(), PageSize = reader.ReadInt32(), _tables = new Dictionary <string, DbTable>() //Tables = new List<DbTable>() }; //tables var pageCount = reader.ReadInt32(); for (var t = 0; t < pageCount; t++) { var table = new DbTable() { Name = reader.BinaryRead(), FileName = reader.BinaryRead(), Generate = reader.ReadBoolean(), Multikey = reader.ReadBoolean(), Rows = reader.ReadInt32(), RowMask = reader.ReadUInt64(), RowMaskLength = reader.ReadInt32(), Pager = DbTablePager.Load(reader), Count = reader.ReadInt32(), _columns = new Dictionary <string, DbColumn>() }; //read columns var columnNameList = new List <string>(); for (var c = 0; c < table.Count; c++) { var col = new DbColumn() { Indexer = reader.BinaryRead(), Unique = reader.ReadBoolean(), Name = reader.BinaryRead(), Index = reader.ReadInt32(), Type = reader.BinaryRead(), Key = reader.ReadBoolean(), Indexed = reader.ReadBoolean(), // NodePages = reader.ReadInt32(), ItemPages = reader.ReadInt32() }; if (!table.Add(col)) { throw new ArgumentException($"duplicated column: {col.Name} on table {table.Name}"); } columnNameList.Add(col.Name); } //check count if (table.Count != table.Columns.Count()) { throw new ArgumentException($"invalid table count on: {table.Name}"); } table._columnNameList = columnNameList.ToArray(); //get key table.Keys = table.Columns.Where(c => c.Key).ToArray(); var keyCount = table.Keys.Length; if (table.Multikey) { //must have more than one key table.Key = null; if (keyCount < 2) { throw new ArgumentException($"table: {table} is multi-key and has less than 2 keys"); } } else { //must have just one key if (keyCount != 1) { throw new ArgumentException($"table: {table} must have one key"); } table.Key = table.Keys[0]; } //schema.Tables.Add(table); schema._tables.Add(table.Name, table); } return(schema); }