搜索
首页数据库mysql教程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<TCourse> 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();
        }
        /// <summary>执行方法</summary>
        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);
        }

        /// <summary>
        /// 数据库名
        /// </summary>
        /// <remarks>文件名和扩展名不限制</remarks>
        protected static string DataBase = "ClassDB.db";
        /// <summary>
        /// 学生表名
        /// </summary>
        protected static string TableName = "tb_student";
        /// <summary>
        /// 【新】学生表名
        /// </summary>
        protected static string NewTableName = "new_tb_student";
        /// <summary>
        /// XFile
        /// </summary>
        protected static string XFileName = "tb_file";

        #region 基本操作
        /// <summary>
        /// 创建库,写入数据
        /// </summary>
        static void WriteData()
        {
            /*
             * ①:没有数据库会自动创建的,默认目录和应用程序目录一致;
             * ②:打开表,Key支持组合结构 => OpenXTable<TKey, TRecord>
             */
            using (IStorageEngine engine = STSdb.FromFile(DataBase)) //①
            {
                var table = engine.OpenXTable<int, TStudent>(TableName); //②
                //var table2 = engine.OpenXTable<TKey, TTick>("table2"); //支持key嵌套
                for (int i = 0; i < 1000; i++)
                {
                    table[i] = new TStudent
                    {
                        Name = "Jon_" + i.ToString(),
                        Age = new Random().Next(25, 30),
                        GroupNumber = i + (new Random().Next(310, 399)),
                        CourseList = new List<TCourse>()
                        {
                            new TCourse{
                                CourseName="C#高级编程"+i.ToString(),
                                Teacher="老陈"+i.ToString(), 
                                Score=80
                            },
                            new TCourse{
                                CourseName="C#函数式程序设计"+i.ToString(),
                                Teacher="老李"+i.ToString(),
                                Score=90
                            },
                            new TCourse{
                                CourseName="多线程实战应用"+i.ToString(),
                                Teacher="老张"+i.ToString(),
                                Score=95
                            },
                        }
                    };
                }
                engine.Commit();
            }
        }
        /// <summary>
        /// 读取数据
        /// </summary>
        static void ReadData()
        {
            using (IStorageEngine engine = STSdb.FromFile(DataBase))
            {
                var table = engine.OpenXTable<int, TStudent>(TableName); //ITable:IEnumerable对象
                foreach (var item in table)
                    Console.WriteLine(JsonConvert.SerializeObject(item, Newtonsoft.Json.Formatting.Indented));

                Console.WriteLine(table.Count());   //TableName表中有100行数据
            }
        }

        /// <summary>
        /// 
        /// </summary>
        static void DatabaseSchemeInfo()
        {
            using (IStorageEngine engine = STSdb.FromFile(DataBase)) //①
            {
                IDescriptor descriptor = engine[TableName];
                Console.WriteLine(descriptor.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine(descriptor.ModifiedTime.ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine(descriptor.Name);
                //ID是表的唯一标识id,表一旦创建,它就创建了,后面只要表在就不会修改
                //重建表它会从新分配
                Console.WriteLine(descriptor.ID);

                //...
            }
        }

        /// <summary>
        /// 读取单条数据
        /// </summary>
        static void ReadItem()
        {
            using (IStorageEngine engine = STSdb.FromFile(DataBase))
            {
                var table = engine.OpenXTable<int, TStudent>(TableName); //ITable: IEnumerable对象
                //var item = table.FirstOrDefault(x => x.Key <= 15 && 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<= key <= 15 的记录不存在!");
            }
        }

        static void AddItems()
        {
            using (IStorageEngine engine = STSdb.FromFile(DataBase))
            {
                var table = engine.OpenXTable<int, TStudent>(TableName);
                //table[100] = new TStudent(){....};
                table.InsertOrIgnore(2, new TStudent());

                engine.Commit();
            }
        }

        /// <summary>
        /// 删除表数据
        /// </summary>
        static void DeleteItems()
        {
            using (IStorageEngine engine = STSdb.FromFile(DataBase))
            {
                var table = engine.OpenXTable<int, TStudent>(TableName); //ITable:IEnumerable对象
                if (table != null)
                {
                    //table.Clear();          //清空表数据
                    table.Delete(5);        //删掉key是5的记录
                    //table.Delete(10, 15);  //删掉key从10到15的记录
                    engine.Commit();        //提交操作,不能少
                }
            }
        }

        /// <summary>
        /// 按需获取数据
        /// </summary>
        static void GetRecord()
        {
            using (IStorageEngine engine = STSdb.FromFile(DataBase))
            {
                /*
                   Forward向前读取, Backward向后读取, 它们都有2个重载,下面重点说明第二个重载
                 * Forward(TKey from, bool hasFrom, TKey to, bool hasTo);
                 * Backward(TKey to, bool hasTo, TKey from, bool hasFrom);
                 * 超出范围的都不会排除,另外,查询范围超出也不会有影响,但是要注意一点,formkey和endkey的大小关系
                 * 
                 * 0<----------[(S)]----------------[(E)]------------->N
                 * 
                 */
                var table = engine.OpenXTable<int, TStudent>(TableName);
                var fiterTB = table.Forward(2, true, 9, true);    //索引从2到9
                //var fiterTB = table.Forward(2, false, 9, true);   //索引从0到9
                //var fiterTB = table.Forward(2, false, 9, false);  //索引从0到表结尾
                //var fiterTB = table.Forward(2, true, 9, false);   //索引从2到表结尾
                //Backward刚好相反
                foreach (var item in fiterTB)
                    Console.WriteLine(JsonConvert.SerializeObject(item, Newtonsoft.Json.Formatting.Indented));
            }
        }
        /// <summary>
        /// 数据分页
        /// </summary>
        static void PageRecord()
        {
            using (IStorageEngine engine = STSdb.FromFile(DataBase))
            {
                int pageIndex = 2;
                int pageSize = 10;

                var table = engine.OpenXTable<int, TStudent>(TableName);
                var fiterTB = table.Skip(pageSize * (pageIndex - 1)).Take(pageSize);
                foreach (var item in fiterTB)
                    Console.WriteLine(JsonConvert.SerializeObject(item, Newtonsoft.Json.Formatting.Indented));
            }
        }
        /// <summary>
        /// 文件数和记录数
        /// </summary>
        static void Others()
        {
            using (IStorageEngine engine = STSdb.FromFile(DataBase))
            {
                //表和虚拟文件的数量
                Console.WriteLine("数据库 " + DataBase + " 中有 {0} 张表:{1}", engine.Count, TableName);

                //表记录数
                var table = engine.OpenXTable<int, TStudent>(TableName);
                Console.WriteLine("表" + TableName + "中有" + table.Count() + "条记录");
            }
        }

        /// <summary>
        /// 表是否存在
        /// </summary>
        static void ExistsTable()
        {
            using (IStorageEngine engine = STSdb.FromFile(DataBase))
            {
                //判断表存在与否
                //bool exists = engine.Exists(NewTableName);
                //Console.WriteLine(NewTableName + " exist?=>{0}", exists.ToString());

                bool exists = engine.Exists(TableName);
                Console.WriteLine(TableName + " exist?=>{0}", exists.ToString());
            }
        }

        /// <summary>
        /// 重命名表名
        /// </summary>
        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");
            }
        }
        /// <summary>
        /// 删除表
        /// </summary>
        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<long, byte[]> 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<int, string> table = engine.OpenXTable<int, string>("table");
                for (int i = 0; i < 100000; i++)
                {
                    table[i] = i.ToString();
                }
                engine.Commit();
            }
        }

        static void ServerHandleData()
        {
            string _dbname = "test.stsdb4";
            using (IStorageEngine engine = STSdb.FromFile(_dbname))
            {
                var server = STSdb.CreateServer(engine, 7182);
                server.Start();
                //server is ready for connections
                //server.Stop();
            }
        }
        //The created server instance will listen on the specified port 
        //and receive/send data from/to the clients

        #endregion

        #region Memory Usage
        /*
         min/max children (branches) in each internal (non-leaf) node
         max operations in the root node
         min/max operations in each internal node
         min/max records in each leaf node
         number of cached nodes in the memory
         */
        static void MemoryUsageHandle()
        {
            using (StorageEngine engine = (StorageEngine)STSdb.FromFile(DataBase))
            {
                //下面的demo都是STS.DB的默认值设置

                //min/max children (branches) in each internal node
                engine.INTERNAL_NODE_MIN_BRANCHES = 2;
                engine.INTERNAL_NODE_MAX_BRANCHES = 4;
                //max operations in the root node
                engine.INTERNAL_NODE_MAX_OPERATIONS_IN_ROOT = 8 * 1024;
                //min/max operations in each internal node
                engine.INTERNAL_NODE_MIN_OPERATIONS = 64 * 1024;
                engine.INTERNAL_NODE_MAX_OPERATIONS = 128 * 1024;
                //min/max records in each leaf node
                engine.LEAF_NODE_MIN_RECORDS = 16 * 1024;
                engine.LEAF_NODE_MAX_RECORDS = 128 * 1024; //at least 2 x MIN_RECORDS
                //number of cached nodes in memory
                engine.CacheSize = 32;
            }
        }

        #endregion

        #region Heap

        /*using =>
            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<int, string> table = engine.OpenXTable<int, string>("table");
            //    for (int i = 0; i < 100000; i++)
            //    {
            //        table[i] = i.ToString();
            //    }
            //    engine.Commit();
            //}
        }
        #endregion

        //...

    }
}


 

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
深入理解MySQL索引优化器工作原理深入理解MySQL索引优化器工作原理Nov 09, 2022 pm 02:05 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

Spring Boot与NoSQL数据库的整合使用Spring Boot与NoSQL数据库的整合使用Jun 22, 2023 pm 10:34 PM

随着互联网的发展,大数据分析和实时信息处理成为了企业的一个重要需求。为了满足这样的需求,传统的关系型数据库已经不再满足业务和技术发展的需要。相反,使用NoSQL数据库已经成为了一个重要的选择。在这篇文章中,我们将讨论SpringBoot与NoSQL数据库的整合使用,以实现现代应用程序的开发和部署。什么是NoSQL数据库?NoSQL是notonlySQL

数据库系统的构成包括哪些数据库系统的构成包括哪些Jul 15, 2022 am 11:58 AM

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

PHP和NoSQL数据库的应用PHP和NoSQL数据库的应用Jun 19, 2023 pm 03:25 PM

在现代的网络应用程序开发中,PHP和NoSQL数据库已经成为了非常受欢迎的技术选择。在过去,PHP曾被广泛应用于开发动态网站和Web应用程序,而NoSQL数据库则是最近才出现的全新的数据存储技术,它提供了更加灵活和可扩展的解决方案。在这篇文章中,我们将会探讨PHP和NoSQL数据库在实际应用中的情况。PHP是一种服务器端编程语言,最初

access数据库的结构层次是什么access数据库的结构层次是什么Aug 26, 2022 pm 04:45 PM

结构层次是“数据库→数据表→记录→字段”;字段构成记录,记录构成数据表,数据表构成了数据库。数据库是一个完整的数据的记录的整体,一个数据库包含0到N个表,一个表包含0到N个字段,记录是表中的行。

mysql查询慢的因素除了索引,还有什么?mysql查询慢的因素除了索引,还有什么?Jul 19, 2022 pm 08:22 PM

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?

使用PHP和MongoDB实现NoSQL数据库,满足不同用户需求使用PHP和MongoDB实现NoSQL数据库,满足不同用户需求Jun 26, 2023 pm 11:39 PM

NoSQL(NotOnlySQL)数据库是近年来快速发展的一类数据库,与传统关系型数据库相比,其具有更好的可扩展性和性能,并支持更多的数据类型和数据存储方式。其中,MongoDB是一款使用文档数据库模型的NoSQL数据库,被广泛应用于Web应用、移动应用、物联网设备等领域。本文将介绍如何使用PHP编写MongoDB数据库的基本操作,并通过实例演示如何满足

数据库的什么是指数据的正确性和相容性数据库的什么是指数据的正确性和相容性Jul 04, 2022 pm 04:59 PM

数据库的“完整性”是指数据的正确性和相容性。完整性是指数据库中数据在逻辑上的一致性、正确性、有效性和相容性。完整性对于数据库系统的重要性:1、数据库完整性约束能够防止合法用户使用数据库时向数据库中添加不合语义的数据;2、合理的数据库完整性设计,能够同时兼顾数据库的完整性和系统的效能;3、完善的数据库完整性有助于尽早发现应用软件的错误。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。