Next, the general usage of STSDB is listed here. The following content is based on the stsdb4.dll (version 4.0.3.0) Library (Baidu shared resource: pan. baidu. coms1jGxHE3k), as of the end of this article, the latest official version is 4.0.5.0, official address: stsdb. comusingSystem; usingSystem. collections. generic;
Next, the previous article lists the general use of STSDB Based on stsdb4.dll (4.0.3.0 version) Library (Baidu share resources: http://pan.baidu.com/s/1jGxHE3k), as of this article published, the latest official version is 4.0.5.0, the official address: http://stsdb.com/using System; using System. collections. generic;
Next, the general use of STSDB is listed in the previous article.
The following content based on stsdb4.dll (4.0.3.0 version) Library (Baidu share resources: http://pan.baidu.com/s/1jGxHE3k), as of this release, the latest official version is 4.0.5.0, official address: 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; } }}
DEMO code:
/** 1. STSdb 4.0 is an open-source NoSQL database and virtual file system that supports real-time indexing and is fully developed using c. * The engine principle is based on the WaterfallTree data structure. *** 2. features * Support for billions of data access * Support for TB-level file sizes * Real-Time indexing * built-in compression * built-in serialization * Support for sparse and scattered files (byte []) * controllable Storage memory * supports multithreading and thread security * 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 thre Ads is thread-safe. ** 3. disadvantage: * transactions are not supported * all opened tables are processed at the same time ** data engines can be connected to IStorageEngine = STSdb in multiple cases. fromMemory (); // read IStorageEngine engine = STSdb from memory. fromStream (stream); // read IStorageEngine engine = STSdb from the data stream. fromHeap (heap); // read IStorageEngine engine = STSdb from the stack. fromNetwork (host, port); // read from a remote address ***/using System; using System. IO; using System. linq; using System. collections. generic; namespace ST SDB {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 (Page Record); // 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 ();}///Execution MethodStatic void ExecuteCode (Action act) {System. diagnostics. stopwatch stopwatch = new System. diagnostics. stopwatch (); stopwatch. start (); act (); stopwatch. stop (); TimeSpan timespan = stopwatch. elapsed; Console. writeLine ("Running {0} seconds", timespan. totalSeconds );}////// Database name //////
The file name and extension are not limited.
Protected static string DataBase = "ClassDB. db ";////// Student table name ///Protected static string TableName = "tb_student ";////// [New] student table name ///Protected static string NewTableName = "new_tb_student ";////// XFile ///Protected static string XFileName = "tb_file"; # region basic operations ////// Create a database and write data ///Static void WriteData () {/** ①: no database is automatically created. The default directory is the same as the application directory. * ②: Open the table. The Key supports the combination structure => OpenXTable
*/Using (IStorageEngine engine = STSdb. FromFile (DataBase) // ① {var table = engine. OpenXTable
(TableName); // ② // var table2 = engine. OpenXTable
("Table2"); // supports key nesting 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
() {New TCourse {CourseName = "C # advanced programming" + I. toString (), Teacher = "Chen" + I. toString (), Score = 80}, new TCourse {CourseName = "C # functional programming" + I. toString (), Teacher = "Lao Li" + I. toString (), Score = 90}, new TCourse {CourseName = "multi-thread practical application" + I. toString (), Teacher = "Lao Zhang" + I. toString (), Score = 95 },};} engine. commit ();}}///
/// Read data ///Static void ReadData () {using (IStorageEngine engine = STSdb. FromFile (DataBase) {var table = engine. OpenXTable
(TableName); // ITable: IEnumerable object foreach (var item in table) Console. writeLine (JsonConvert. serializeObject (item, Newtonsoft. json. formatting. indented); Console. writeLine (table. count (); // The TableName table contains 100 rows of Data }}///
//////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 is the unique id of the table. Once a table is created, it is created. Once the table is created, it will not be modified as long as it is present. // The Console will be allocated to the table again. writeLine (descriptor. ID );//...}} ///
/// Read a single piece of data ///Static void ReadItem () {using (IStorageEngine engine = STSdb. FromFile (DataBase) {var table = engine. OpenXTable
(TableName); // ITable: IEnumerable object // var item = table. firstOrDefault (x => x. key <= 15 & x. key> = 10); // The key is 5 Records // table [10]; var item = table. firstOrDefault (x => x. key = 5); // The key is 5 records if (item. value! = Null) Console. WriteLine (JsonConvert. SerializeObject (item, Newtonsoft. Json. Formatting. Indented); else Console. WriteLine ("key = 5 records do not exist! "); // Console. WriteLine (" 10 <= key <= 15 records do not exist! ") ;}} Static void AddItems () {using (IStorageEngine engine = STSdb. FromFile (DataBase) {var table = engine. OpenXTable
(TableName); // table [100] = new TStudent (){....}; table. insertOrIgnore (2, new TStudent (); engine. commit ();}}///
/// Delete table data ///Static void DeleteItems () {using (IStorageEngine engine = STSdb. FromFile (DataBase) {var table = engine. OpenXTable
(TableName); // ITable: IEnumerable object if (table! = Null) {// table. clear (); // Clear table data. delete (5); // Delete records whose key is 5 // table. delete (10, 15); // Delete the record engine from 10 to 15. commit (); // submit the operation, not less }}}///
/// Obtain data as needed ///Static void GetRecord () {using (IStorageEngine engine = STSdb. fromFile (DataBase) {/* Forward reads Forward and Backward reads Backward. Both of them have two reloads. The second overload * Forward (TKey from, bool hasFrom, TKey to, bool hasTo); * Backward (TKey to, bool hasTo, TKey from, bool hasFrom); * excluded if the range is exceeded. In addition, the query range is exceeded, but note that the relationship between formkey and endkey is ** 0 <---------- [(S)] ---------------- [(E)]. -------------> N **/var table = engine. openXTable
(TableName); var fiterTB = table. forward (2, true, 9, true); // index from 2 to 9 // var fiterTB = table. forward (2, false, 9, true); // index from 0 to 9 // var fiterTB = table. forward (2, false, 9, false); // The index ranges from 0 to the end of the table. // var fiterTB = table. forward (2, true, 9, false); // The index ranges from 2 to the end of the table // the opposite is true for Backward (var item in fiterTB) Console. writeLine (JsonConvert. serializeObject (item, Newtonsoft. json. formatting. indented ));}}///
/// Data paging ///Static void PageRecord () {using (IStorageEngine engine = STSdb. FromFile (DataBase) {int pageIndex = 2; int pageSize = 10; var table = engine. OpenXTable
(TableName); var fiterTB = table. skip (pageSize * (pageIndex-1 )). take (pageSize); foreach (var item in fiterTB) Console. writeLine (JsonConvert. serializeObject (item, Newtonsoft. json. formatting. indented ));}}///
/// Number of files and records ///Static void Others () {using (IStorageEngine engine = STSdb. fromFile (DataBase) {// number of tables and virtual files on the Console. writeLine ("DataBase" + DataBase + "contains {0} tables: {1}", engine. count, TableName); // Number of table records var table = engine. openXTable
(TableName); Console. WriteLine ("table" + TableName + "contains" + table. Count () + "record ");}}///
/// Whether the table exists ///Static void ExistsTable () {using (IStorageEngine engine = STSdb. fromFile (DataBase) {// determines whether the table exists or not // bool exists = engine. exists (NewTableName); // Console. writeLine (NewTableName + "exist? ==>{ 0} ", exists. ToString (); bool exists = engine. Exists (TableName); Console. WriteLine (TableName +" exist? ==>{ 0} ", exists. ToString ());}}///
/// Rename the table name ///Static void ReNameTable () {using (IStorageEngine engine = STSdb. fromFile (DataBase) {// determines whether the table exists or not bool exists = engine. exists (TableName); Console. writeLine (TableName + "exist? => {0} ", exists. toString (); // rename the table engine. rename (TableName, NewTableName); Console. writeLine ("table" + TableName + "renamed to:" + NewTableName); if (engine. exists (TableName) Console. writeLine ("old table name \" "+ TableName +" \ "exist"); if (engine. exists (NewTableName) Console. writeLine ("new table name \" "+ NewTableName +" \ "exist ");}}///
/// Delete a table ///Static void DeleteTable () {using (IStorageEngine engine = STSdb. fromFile (DataBase) {// Delete the table 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 valid sparse file functionality. // One storage engine can have program 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 <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) {// The following demos are all STS. set the default value of 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); // listen to the heap server. start (); // Start listening // create the IStorageEngine engine from the remote heap and process the data // 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 //...}}