Example #1
0
 static void test_db_open(string[] words)
 {
     try
     {
         Console.WriteLine("open db");
         db = new LightDB();
         //打开一个数据库,打开时如果不存在会创建一个
         db.Open("d:\\db001");
     }
     catch
     {
         //try
         {
             Console.WriteLine("create db");
             db.Open("d:\\db001", new DBCreateOption()
             {
                 MagicStr = "hello world."
             });
         }
         //catch (Exception err)
         //{
         //    Console.WriteLine("error:" + err.Message);
         //}
     }
 }
Example #2
0
 static void test_db_close(string[] words)
 {
     try
     {
         Console.WriteLine("close db");
         db.Close();
         db = null;
     }
     catch (Exception err)
     {
         Console.WriteLine("error:" + err.Message);
     }
 }
Example #3
0
        static void test_db_checkpoint(string[] words)
        {
            //建立当前数据库的副本
            db.CheckPoint("d:\\db_cp001");
            //根据rocksdb的说明,这个建立副本贼鸡儿高效

            //然后就从这个数据库读了,这是作为历史记录的数据库,对他只读,别去瞎几把改
            var db2 = new LightDB();

            db2.OpenRead("d:\\db_cp001");

            var snap = db2.UseSnapShot();

            var table = snap.GetTableInfo(new byte[] { 01, 02, 03 });

            Console.WriteLine("db2 table=" + table.tablename);

            Console.WriteLine("db2 dataheight=" + snap.DataHeight);
        }
        public void Init()
        {
            Console.CursorLeft = 0;
            Console.WriteLine(" == Open DB ==");


            maindb = new LightDB();

            state_DBOpen = false;
            string fullpath = System.IO.Path.GetFullPath(Program.config.server_storage_path);

            if (System.IO.Directory.Exists(fullpath) == false)
            {
                System.IO.Directory.CreateDirectory(fullpath);
            }
            string pathDB = System.IO.Path.Combine(fullpath, "maindb");

            try
            {
                maindb.Open(pathDB);
                state_DBOpen = true;
                Console.WriteLine("db opened in:" + pathDB);
            }
            catch (Exception err)
            {
                Console.WriteLine("error msg:" + err.Message);
            }
            if (state_DBOpen == false)
            {
                Console.WriteLine("open database fail. try to create it.");
                try
                {
                    DBCreateOption createop = new DBCreateOption();
                    createop.MagicStr  = Program.config.storage_maindb_magic;
                    createop.FirstTask = new WriteTask();

                    createop.FirstTask.CreateTable(new TableInfo(tableID_Writer, "_writeraddress_", "", DBValue.Type.String));

                    createop.FirstTask.Put(tableID_Writer, Program.config.storage_maindb_firstwriter_address.ToBytes_UTF8Encode(), DBValue.FromValue(DBValue.Type.BOOL, true));

                    createop.FirstTask.CreateTable(new TableInfo(tableID_BlockID2Hash, "_block:index->hash_", "", DBValue.Type.String));
                    createop.FirstTask.CreateTable(new TableInfo(tableID_BlockID2Verifiy, "_block:index->hash_", "", DBValue.Type.String));

                    var srcdata = createop.FirstTask.ToBytes();
                    var hash    = Helper.Sha256.ComputeHash(srcdata);

                    //用后处理写入hash
                    createop.afterparser = (_task, _data, _wb) =>
                    {
                        var keyindexzero = new byte[8];

                        //填充创世块hash
                        _wb.Put(StorageService.tableID_BlockID2Hash, keyindexzero, DBValue.FromValue(DBValue.Type.Bytes, hash));

                        //填充一个空字,校验数据
                        _wb.Put(StorageService.tableID_BlockID2Verifiy, keyindexzero, DBValue.FromValue(DBValue.Type.Bytes, new byte[1]));
                    };

                    maindb.Open(pathDB, createop);
                    Console.WriteLine("db created in:" + pathDB);
                    state_DBOpen = true;
                }
                catch (Exception err)
                {
                    Console.WriteLine("error msg:" + err.Message);
                }
            }
        }