您好,欢迎访问一九零五行业门户网

nosql数据库STSDB的一般性使用

接着上一篇这里罗列下stsdb的一般性使用 以下内容基于stsdb4.dll(4.0.3.0版本)库(百度分享资源:http://pan.baidu.com/s/1jgxhe3k),截止本文发布,官方最新版本是4.0.5.0,官方地址:http://stsdb.com/ using system;using system.collections.generic;
接着上一篇这里罗列下stsdb的一般性使用
以下内容基于stsdb4.dll(4.0.3.0版本)库(百度分享资源:http://pan.baidu.com/s/1jgxhe3k),截止本文发布,官方最新版本是4.0.5.0,官方地址:http://stsdb.com/
using system;using system.collections.generic;namespace stsdb{ [serializable] public class tstudent { public tstudent() { } public string name { get; set; } public int age { get; set; } public int groupnumber { get; set; } public list courselist { get; set; } }}
using system;namespace stsdb{ [serializable] public class tcourse { public string coursename { get; set; } public string teacher { get; set; } public int score { get; set; } }}
演示代码:
/* * 1. stsdb 4.0 是一个开源的nosql 数据库和虚拟文件系统,支持实时索引,完全用c#开发的。 * 引擎原理基于waterfalltree(瀑布树)数据结构搭建 * * * 2.特性 * 支持几十亿级别的数据存取 * 支持tb级别文件大小 * 实时索引 * 内置压缩 * 内置序列化 * 支持稀疏分散的文件(byte[]) * 存储内存可控 * 支持多线程,且线程安全 * storage engine instance is thread-safe. creating (opening) xtable and xfile instances in one storage engine from different threads is thread-safe. xtable and xfile instances are also thread-safe. manipulating different xtable/xfile instances from different threads is thread-safe. * * 3.缺点 * 不支持事务 * 同时处理所有打开的表 * * 支持多种情况下的数据引擎连接 istorageengine engine = stsdb.frommemory(); //从内存中读取 istorageengine engine = stsdb.fromstream(stream); //从数据流中读取 istorageengine engine = stsdb.fromheap(heap); //从堆栈中读取 istorageengine engine = stsdb.fromnetwork(host, port); //从远程地址读取 * * */using system;using system.io;using system.linq;using system.collections.generic;namespace stsdb{ using newtonsoft.json; using stsdb4.data; using stsdb4.database; using stsdb4.storage; using stsdb4.waterfalltree; using stsdb4.remote.heap; class program { static void main(string[] args) { executecode(writedata); executecode(readdata); //executecode(databaseschemeinfo); //executecode(readitem); //executecode(deleteitems); //executecode(readitem); //executecode(getrecord); //executecode(pagerecord); //executecode(others); //executecode(renametable); //executecode(existstable); //executecode(deletetable); //executecode(existstable); #region test //bool quit = false; //while (!quit) //{ // console.write(get item data: ); // string demo = console.readline(); // switch (demo) // { // case y: // break; // case q: // quit = true; // break; // default: // console.writeline(choose a word between y and q(to quit)); // break; // } //} #endregion console.readkey(); } /// 执行方法 static void executecode(action act) { system.diagnostics.stopwatch stopwatch = new system.diagnostics.stopwatch(); stopwatch.start(); act(); stopwatch.stop(); timespan timespan = stopwatch.elapsed; console.writeline(运行{0}秒, timespan.totalseconds); } /// /// 数据库名 /// /// 文件名和扩展名不限制 protected static string database = classdb.db; /// /// 学生表名 /// protected static string tablename = tb_student; /// /// 【新】学生表名 /// protected static string newtablename = new_tb_student; /// /// xfile /// protected static string xfilename = tb_file; #region 基本操作 /// /// 创建库,写入数据 /// static void writedata() { /* * ①:没有数据库会自动创建的,默认目录和应用程序目录一致; * ②:打开表,key支持组合结构 => openxtable */ using (istorageengine engine = stsdb.fromfile(database)) //① { var table = engine.openxtable(tablename); //② //var table2 = engine.openxtable(table2); //支持key嵌套 for (int i = 0; i x.key = 10); //key是5的记录 //table[10]; var item = table.firstordefault(x => x.key == 5); //key是5的记录 if (item.value != null) console.writeline(jsonconvert.serializeobject(item, newtonsoft.json.formatting.indented)); else console.writeline(key = 5 的记录不存在!); //console.writeline(10{0}, exists.tostring()); } } /// /// 重命名表名 /// static void renametable() { using (istorageengine engine = stsdb.fromfile(database)) { //判断表存在与否 bool exists = engine.exists(tablename); console.writeline(tablename + exist? =>{0}, exists.tostring()); //表重命名 engine.rename(tablename, newtablename); console.writeline(表 + tablename + 被重命名为: + newtablename); if (engine.exists(tablename)) console.writeline(old table name \ + tablename + \ exist); if (engine.exists(newtablename)) console.writeline(new table name \ + newtablename + \ exist); } } /// /// 删除表 /// static void deletetable() { using (istorageengine engine = stsdb.fromfile(database)) { //删除表 engine.delete(tablename); //engine.delete(newtablename); engine.commit(); } } #endregion #region xfile static void testxfile() { using (istorageengine engine = stsdb.fromfile(database)) { xfile file = engine.openxfile(xfilename); random random = new random(); byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int i = 0; i < 100; i++) { long position = random.next(); file.seek(position, seekorigin.begin); file.write(buffer, 0, buffer.length); } engine.commit(); } } //xfile uses special xtable implementation to provide effective sparse file functionality. //one storage engine can have many files #endregion #region client/server static void clientupdatedata() { using (istorageengine engine = stsdb.fromnetwork(localhost, 7182)) { itable table = engine.openxtable(table); for (int i = 0; i stsdb4.waterfalltree; stsdb4.storage; stsdb4.remote.heap; */ static void heaperengine() { //server端 iheap heap = new heap(new filestream(heap.db, filemode.openorcreate)); heapserver server = new heapserver(heap, 7183); //监听堆服务器 server.start(); //开始监听 //从远程堆中创建 istorageengine 引擎,并处理数据 //using (istorageengine engine = stsdb.fromheap(new remoteheap(host, 7183))) //{ // itable table = engine.openxtable(table); // for (int i = 0; i < 100000; i++) // { // table[i] = i.tostring(); // } // engine.commit(); //} } #endregion //... }}
其它类似信息

推荐信息