{//最简单、原始的、可能的查询计划 public Plan createPlan(QueryData data, Transaction tx) { //第一步:为每一个出现的表或者视图定义创建一个plan List <Plan> plans = new List <Plan>(); foreach (string tblname in data.tables()) { string viewdef = SimpleDB.mdMgr().getViewDef(tblname, tx); if (viewdef != null) { plans.Add(SimpleDB.planner().createQueryPlan(viewdef, tx)); } else { plans.Add(new TablePlan(tblname, tx)); } } //第二步:创建所有表计划的叉积运算 Plan p = plans[0]; plans.RemoveAt(0); foreach (Plan nextplan in plans) { p = new ProductPlan(p, nextplan); } //第三步:为predicate添加一个选择运算selection p = new SelectPlan(p, data.pred()); //第四步:做字段名称上的投影运算 p = new ProjectPlan(p, data.fields()); return(p); }
public int size(string filename) {//返回指定文件的block数目,在要求文件管理器返回数目之前,先加 S 锁到“end of the file”块上 Block dummyblk = new Block(filename, END_OF_FILE); concurMgr.sLock(dummyblk); return(SimpleDB.fileMgr().size(filename)); }
public void recover() { doRecover(); int lsn = new CheckpointRecord().writeToLog(); SimpleDB.logMgr().flush(lsn); }
//--------------------------------------------------------------------------------------- public IndexInfo(string idxname, string tblname, string fldname, Transaction tx) { this.idxname = idxname; this.fldname = fldname; this.tx = tx; ti = SimpleDB.mdMgr().getTableInfo(tblname, tx); si = SimpleDB.mdMgr().getStatInfo(tblname, ti, tx); }
public override void undo(int txnum) { BufferMgr buffMgr = SimpleDB.bufferMgr(); Buffer buff = buffMgr.pin(blk); buff.setString(offset, val, txnum, -1); buffMgr.unpin(buff); }
public void rollback() {//写一个ROLLBACK日志,然后将这个日志记录写回磁盘 //所有被当前该事务修改过的脏数据页的“修改日志”写回磁盘,再将脏页写回磁盘 SimpleDB.bufferMgr().flushAll(txnum); doRollback();//回滚当前事务直到看到START日志记录 int lsn = new RollbackRecord(txnum).writeToLog(); SimpleDB.logMgr().flush(lsn); }
internal void flush() {//将Page写回磁盘块,若当前Page为脏,那么先将它的日志记录写回磁盘块,再将page写回 if (modifiedBy >= 0) { SimpleDB.logMgr().flush(logSequenceNumber); contents.write(blk); modifiedBy = -1;//修改的事务id置为负数 } }
static void Main(string[] args) { SimpleDB sdb = new SimpleDB(); sdb.CreateMap(); sdb.printMap(); System.Console.WriteLine("---------------Retrive data-----------------------"); sdb.GetData(2); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSingleton <SimpleBotUser>(); SimpleDB.ConnectionString = Configuration.GetSection("ConnectionStrings:ConnectionString").Value; SimpleDB.Banco = Configuration.GetSection("ConnectionStrings:Banco").Value; SimpleDB.Collection = Configuration.GetSection("ConnectionStrings:Collection").Value; SimpleDB.Iniciar(); services.AddMvc(); }
public override void undo(int txnum) { BufferMgr buffMgr = SimpleDB.bufferMgr(); //将块绑定到(读入)缓冲区 Buffer buff = buffMgr.pin(blk); //LSN为负数,生成一条临时日志记录,不保存到磁盘 buff.setInt(offset, val, txnum, -1); buffMgr.unpin(buff); }
public void commit() {//写一个COMMIT记录到日志,然后将这个日志记录写回磁盘 //所有被当前该事务修改过的脏数据页,先写“修改日志”到磁盘,再将脏页写回磁盘 SimpleDB.bufferMgr().flushAll(txnum); //写一个COMMIT记录到日志 int lsn = new CommitRecord(txnum).writeToLog(); //将该日志写回磁盘 SimpleDB.logMgr().flush(lsn); }
static void doUpdate(string cmd) { Planner planner = SimpleDB.planner(); Transaction tx = new Transaction(); int num = planner.executeUpdate(cmd, tx); tx.commit(); System.Console.WriteLine(num + " records affected"); }
static void doQuery(string cmd) { int length; string format; Planner planner = SimpleDB.planner(); Transaction tx = new Transaction(); Plan p = planner.createQueryPlan(cmd, tx); Schema sch = p.schema(); foreach (string fldname in sch.fields()) { if (sch.type(fldname) == Schema.INTEGER) { length = Math.Max(5, fldname.Length) + 2; format = "{0," + length + "}"; Console.Write(format, fldname); } else { length = Math.Max(sch.length(fldname), fldname.Length) + 2; format = "{0," + length + "}"; Console.Write(format, fldname); } } Console.WriteLine(); Scan s = p.open(); while (s.next()) { foreach (string fldname in sch.fields()) { if (sch.type(fldname) == Schema.INTEGER) { length = Math.Max(5, fldname.Length) + 2; format = "{0," + length + "}"; Console.Write(format, s.getInt(fldname)); } else { length = Math.Max(sch.length(fldname), fldname.Length) + 2; format = "{0," + length + "}"; Console.Write(format, s.getString(fldname)); } } Console.WriteLine(); } s.close(); tx.commit(); }
static void Main(string[] args) { SimpleDB sdb = new SimpleDB(); System.Console.WriteLine("---------------Set data-----------------------"); sdb.SetData("1", "First data entry"); sdb.SetData(2, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }); sdb.SetData('3', "more regular text - key 3 entry"); sdb.SetData("4", "This value will soon be deleted in the hashmap"); sdb.SetData("weird_key", "Any type of key is acceptable"); sdb.SetData("4", 123456); System.Console.WriteLine("---------------Set data done-----------------------"); System.Console.WriteLine("---------------Print offSets-----------------------"); sdb.CreateMap(); sdb.printMap(); }
public LogMgr(string logfile) {/* 为制定日志文件创建的管理器,如果日志文件不存在,会用一个空的第一个块创建 * 这个构造函数依赖于一个 FileMgr 的 object 对象(来自server.SimpleDB.filemgr创建于系统初始化时) * 所以这个构造函数必须要通过先调用server.SimpleDB.initFileMgr之后才能调用*/ this.logfile = logfile; int logsize = SimpleDB.fileMgr().size(logfile); if (logsize == 0) { appendNewBlock(); } else { currentblk = new Block(logfile, logsize - 1); mypage.read(currentblk); currentpos = getLastRecordPosition() + Page.INT_SIZE; } }
static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); SimpleDALConfig.RegistConfigService(new XmlConfigurationService()); SimpleDALConfig.RegistLogService<DALLog4net>(); Console.WriteLine("初始化完成,按任意键下一步"); Console.ReadLine(); using(SimpleDB db = new SimpleDB("")) { Console.WriteLine("SimpleDB for MySql已经创建"); string sql = "select * from hb_oauth_sites"; SimpleCommand comm = db.CreateCommand(sql, CommandType.Text); DataTable table = new DataTable(); DataSet ds = new DataSet(); comm.Fill(ds); Console.WriteLine("The Final Result count is: {0}", ds.Tables[0].Rows.Count); } Console.ReadLine(); using(SimpleDB db = new SimpleDB("oracle")) { Console.WriteLine("SimpleDB for Oracle 已经创建"); string sql = "select CUBE, CUBE_Name, method from dual"; SimpleCommand commad = db.CreateCommand(sql, System.Data.CommandType.Text); Console.WriteLine(commad.CommandText); } Console.ReadLine(); }
public TablePlan(string tblname, Transaction tx) {//根据指定表在查询树种创建一个叶子节点 this.tx = tx; ti = SimpleDB.mdMgr().getTableInfo(tblname, tx); si = SimpleDB.mdMgr().getStatInfo(tblname, ti, tx); }
public void recover() {/* 将所有被该事务修改过的buffer页flush;从后往前遍历日志记录,回滚所有未提交的事务 * 最后写一个静态检查点记录到日志;这个函数只在系统启动时、在用户事务前调用*/ SimpleDB.bufferMgr().flushAll(txnum); recoveryMgr.recover(); }
static void Main(string[] args) { string cmd, cmdstr; help(); string dbname = "test-db"; Console.WriteLine("Current database is " + dbname); string homedir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string dbDirectory = Path.Combine(homedir, dbname); Console.WriteLine("Home directory is " + dbDirectory); Console.WriteLine("Use the following command to get metadata information;"); Console.WriteLine(" select tblname,reclength from tblcat;"); Console.WriteLine(" select tblname,fldname,type,length,offset from fldcat;"); SimpleDB.init(dbname); while (true) { Console.Write(dbname + ">"); cmd = Console.ReadLine().Trim(); if (cmd.EndsWith(";")) { cmd = cmd.Substring(0, cmd.Length - 1); } if (cmd.Length == 0) { continue; } cmdstr = cmd.ToLower(); if (cmdstr == "exit") { break; } else if (cmdstr == "help") { help(); } else if (cmdstr.StartsWith("select")) { try { doQuery(cmd); } catch (BadSyntaxException) { Console.WriteLine("Error SQL command!"); } } else if (cmdstr.StartsWith("insert") || cmdstr.StartsWith("delete") || cmdstr.StartsWith("update") || cmdstr.StartsWith("create")) { try { doUpdate(cmd); } catch (BadSyntaxException) { Console.WriteLine("Error SQL command!"); } } else { Console.WriteLine("Unknown command!"); } } }
public int executeCreateIndex(CreateIndexData data, Transaction tx) { SimpleDB.mdMgr().createIndex(data.indexName(), data.tableName(), data.fieldName(), tx); return(0); }
public int executeCreateView(CreateViewData data, Transaction tx) { SimpleDB.mdMgr().createView(data.viewName(), data.viewDef(), tx); return(0); }
public int executeCreateTable(CreateTableData data, Transaction tx) { SimpleDB.mdMgr().createTable(data.tableName(), data.newSchema(), tx); return(0); }