打开数据库并写入数据

using (StorageEngine engine = new StorageEngine("stsdb4.sys", "stsdb4.dat"))
{
    XIndex<int, string> table = engine.OpenXIndex<int, string>("table");
  
    for (int i = 0; i < 1000000; i++)
    {
        table[i] = i.ToString();
    }
  
    table.Flush();
    engine.Commit();
}

读取数据

using (StorageEngine engine = new StorageEngine("stsdb4.sys", "stsdb4.dat"))
{
    XIndex<int, string> table = engine.OpenXIndex<int, string>("table");
  
    foreach (var row in table) //table.Forward(), table.Backward()
    {
        Console.WriteLine("{0} {1}", row.Key, row.Value);
    }
}

更多例子

可以访问这里查看更多的例子,或者下载类库或代码,里面有pdf文档。

XIndex<TKey, TRecord> table;

The supported types for both TKey and TRecord are:

  • 1.1. Primitive types – Boolean, Char, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime, String, byte[];
  • 1.2. Classes and structures with default constructor, containing public read/write properties and/or fields with types from 1.1;
  • 1.3. Classes and structures with default constructor, containing public read/write properties and/or fields with types from 1.1 and/or from 1.2;
  • 1.4.Types that can be transformed to one of the IData successors, available in STSdb4 (described later).

For example, if we have the following two types:

    public class Key
    {
        public string Symbol { get; set; }
        public DateTime Timestamp { get; set; }
    }
    
    public class Tick
    {
        public double Bid { get; set; }
        public double Ask { get; set; }
        public long Volume { get; set; }
        public string Provider { get; set; }
    }

We can open different table types:

  
    IIndex<long, Tick> table1 = engine.OpenXIndex<long, Tick>("table1"); 
    IIndex<DateTime, Tick> table2 = engine.OpenXIndex<DateTime, Tick>("table2");
    
    IIndex<Key, Tick> table3 = engine.OpenXIndex<Key, Tick>("table3");

Or even:

    IIndex<Tick, Tick> table3 = engine.OpenXIndex<Tick, Tick>("table4");

In the case of composite keys the engine compares sub-keys in the order in which they are declared as properties.

Data Transformer

Data transformers are responsible for converting application types to and from IData types. They constitute the transformation layer between XIndex<TKey,TRecord> and XIndex table. All transformers look like:

    public interface IDataTransformer<T>
    {
        IData ToIData(T item);
        T FromIData(IData data);
        DataType DataType { get; }
    }

Each XIndex<TKey,TRecord> instance automatically generates two
data transformers – one for the keys and one for the records. In that
way every input TKey/TRecord instance is transformed to appropriate
IData instance. Similarly, every IData instance from the XIndex is
transformed back to TKey/TRecord. (For highly sophisticated types custom
transformers can be provided.)

Suppose again that we have the following class:

    public class Tick
    {
        public string Symbol { get; set; }
        public DateTime Timestamp { get; set; }
        public double Bid { get; set; }
        public double Ask { get; set; }
        public long Volume { get; set; }
        public string Provider { get; set; }
    }

If we have XIndex<long, Tick>

IIndex<long, Tick> table = engine.OpenXIndex<long, Tick>("table");

Then the backend XIndex table will be opened with the following two types for the keys and for the records:

Data<long>
Data<string, DateTime, double, double, long, string>

We can open simultaneously the backend XIndex table:

    DataType keyType = DataType.Int64;

    DataType recordType = DataType.Slotes(
        DataType.String,
        DataType.DateTime,
        DataType.Double,
        DataType.Double,
        DataType.Int64,
        DataType.String);     IIndex<IData, IData> table2 = engine.OpenXIndex(keyType, recordType, "table");

In this case table and table2 will refer to the same data.

If we decide to extend the Tick class with property of non-primitive type (Provider):

    public class Provider
    {
        public string Name { get; set; }
        public string Website { get; set; }
    }     public class Tick
    {
        public string Symbol { get; set; }
        public DateTime Timestamp { get; set; }
        public double Bid { get; set; }
        public double Ask { get; set; }
        public long Volume { get; set; }
        public Provider Provider { get; set; }
    }

Then the backend XIndex will be opened with the types:

 Data<long>
 Data<string, DateTime, double, double, long, bool, string, string>

We can decide to exclude Symbol and Timestamp and use them as composite key:

public class Key
    {
        public string Symbol { get; set; }
        public DateTime Timestamp { get; set; }
    }
    
    public class Provider
    {
        public string Name { get; set; }
        public string Website { get; set; }
    }     public class Tick
    {
        //public string Symbol { get; set; }
        //public DateTime Timestamp { get; set; }
        public double Bid { get; set; }
        public double Ask { get; set; }
        public long Volume { get; set; }
        public Provider Provider { get; set; }
    }
IIndex<Key, Tick> table = engine.OpenXIndex<Key, Tick>("table");

Then the backend XIndex will be with types:

Data<string, DateTime>
Data<double, double, long, bool, string, string>

Because data slots can be from primitive types only, an additional
Boolean slot is added to indicate whether the Provider property of the
current object is null. Thus we decompose user types.

Note: In the future releases slots will be extended to support IData recursively, so the additional slots will be eliminated.

If a XIndex uses composite keys with more than one slot in its Data,
the engine compares sub-keys in BigEndian order – from small slot
indexes to big one. (The engine automatically generates appropriate
comparers for the relevant Data type.)

Transformed to IData user data is very suitable for compression. The
engine again automatically generates compression classes for each XIndex
type. These classes use parallel vertical compressions for each slot,
depending on its primitive type. In that way the compressions are fast,
while keeping high compression ratio. By default
XIndex<TKey,TRecord> and XIndex keeps the data in compressed
format.

XFile

STSdb 4.0 supports sparse files called XFile. We can work with XFile as we are with a standard .NET stream.

using (IStorageEngine engine = STSdb.FromFile("stsdb4.sys", "stsdb4.dat"))
    {
        XFile file = engine.OpenXFile("file");         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();             //writes some data on random positions
            file.Seek(position, SeekOrigin.Begin);
            file.Write(buffer, 0, buffer.Length);
        }         file.Flush();
        engine.Commit();
    }

XFile uses special XIndex<long, byte[]> implementation and
provides effective sparse file functionality. Thus, in one storage
engine developers can combine using of XIndex tables and of XFile sparse
files.

Client/Server

From the client side, creating a client connection:

using (IStorageEngine engine = STSdb.FromNetwork("localhost", 7182))
    {
        IIndex<int, string> table = engine.OpenXIndex<int, string>("table");         for (int i = 0; i < 1000; i++)
        {
            table[i] = i.ToString();
        }         table.Flush();
        engine.Commit();
    }

From the server side, starting the server:

  using (IStorageEngine engine = STSdb.FromFile("stsdb4.sys", "stsdb4.dat"))
    {
        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.

STSdb的更多相关文章

  1. STSDB、NDataBase 对象数据库在不同.net framework下无法读取的解决办法

    STSDB.NDataBase 等对象数据库将对象保存在文件中后,如果在不同的windows平台.不同的.net frameWork下总是无法读取,原因是对象模式已经不同了. 解决的办法也很简单,就是 ...

  2. 基于STSdb和fastJson的磁盘/内存缓存

    更新 1. 增加了对批量处理的支持,写操作速度提升5倍,读操作提升100倍 2. 增加了对并发的支持 需求 业务系统用的是数据库,数据量大,部分只读或相对稳定业务查询复杂,每次页面加载都要花耗不少时间 ...

  3. STSdb,最强纯C#开源NoSQL和虚拟文件系统 4.0 RC2 支持C/S架构

    STSdb是什么 再来说明一下STSdb是什么:STSdb是C#写的开源嵌入式数据库和虚拟文件系统,支持实时索引,性能是同类产品的几倍到几十倍,访问官方网站. 温故知新 之前发了文章<STSdb ...

  4. stsdb开发指南

    摘自:http://www.iopenworks.com/Products/ProductDetails/DevelopmentGuide?proID=387 多线程问题,请参见线程安全小结 1 ST ...

  5. 一个基于STSdb和fastJson的磁盘/内存缓存

    一个基于STSdb和fastJson的磁盘/内存缓存 需求 业务系统用的是数据库,数据量大,部分只读或相对稳定业务查询复杂,每次页面加载都要花耗不少时间(不讨论异步),觉得可以做一下高速缓存,譬如用n ...

  6. STSdb数据库的实现使用类

    STSdb 3.5是一个开源的key-value存储形式的数据库,它是用微软.net框架C#语言编写的.STSdb 3.5尤其使用于紧急任务或实时系统,如:股市交易,电子通信,实验室数据等,它的主要功 ...

  7. STSDB 一

    STSdb 4.0 是一个开源的NoSQL 数据库和虚拟文件系统,支持实时索引,完全用c#开发的. 引擎原理基于WaterfallTree(瀑布树)数据结构搭建 以下内容基于stsdb4.dll(4. ...

  8. ENode 2.0 - 整体架构介绍

    前言 今天是个开心的日子,又是周末,可以轻轻松松的写写文章了.去年,我写了ENode 1.0版本,那时我也写了一个分析系列.经过了大半年的时间,我对第一个版本做了很多架构上的改进,最重要的就是让ENo ...

  9. WaterfallTree(瀑布树) 详细技术分析系列

    前言 WaterfallTree(瀑布树) 是最强纯C#开源NoSQL和虚拟文件系统-STSdb专有的(版权所有/专利)算法/存储结构. 参考 关于STSdb,我之前写过几篇文章,譬如: STSdb, ...

随机推荐

  1. Ubuntu 14.04为浏览器添加Flash插件

    在刚安装好到Ubuntu操作系统中默认是没有flash支持到,因此,当我们使用浏览器查看很多视频网页到时候,会导致网页上到视频无法播放.然而,这个问题我们也不能够通过“软件中心”来解决,这时候需要我们 ...

  2. jxl和poi处理excel之比较

    功能需求是根据客户提供的excel模板,程序动态填充其中的一些数据.该模板包含大量的宏,刚拿到的时候头都要晕了,本人虽天天和电脑打交道,但是excel咱不是高手啊,什么宏,之前光听过,听着都觉得是高级 ...

  3. Oracle if else if for case

    ------------------游标+for+if else if DECLARE cursor s_cursor is SELECT * from emp;--定义游标 begin for r ...

  4. C#序列化/反序列化

    序列化:将实体类以二进制或者XML的形式保存到磁盘或者内存中. 反序列化:将序列化的二进制文件和XML文件解析成实体类. 例如下面的二进制序列化与反系列化: using System; using S ...

  5. PHP引用传值规范问题

    在我上一篇:  shopnc 商城源码阅读笔记--开篇概述   中,遇到了一个PHP引用传值导致的错误,情况大致如下: 在我查阅PHP官方文档  的中文版的时候   http://php.net/ma ...

  6. Python复杂多重排序

    1. cmp函数是python自带的函数,用于比较两个参数哪个大哪个小 print cmp(2, 3) # -1 如果第一个参数比第二个小,就返回-1,两个元素相等,返回0,否则返回1 2.所以就可以 ...

  7. spring beans源码解读

    spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类.org.springframework.beans.anno ...

  8. ExtJS4中initComponent和constructor的区别

    Ext的define方法参数类型define( String className, Object data, Function createdFn ) 创建自定义类时,先构造(constructor) ...

  9. 2014年50个程序员最适用的免费JQuery插件

    有用的jQuery库是设计师和开发者之间一个非常熟悉的短语.这是现在互联网中最流行的JavaScript函数库之一.每个设计师和开发人员都应该知道它的重要性,而且熟悉它的功能和特点. jQuery几乎 ...

  10. hdu 3717

    思路:二分答案,然后模拟消灭石头的过程: 如果单纯的暴力模拟的话,肯定会T的: 所以要用到一定的技巧来维护: 在网上看到大神们用O(n)的复杂度来优化,真心orz: 原理是这样的:用一个变量sum_2 ...