当前位置: 首页 > 工具软件 > STSdb > 使用案例 >

c stsdb 读取 mysql_STSdb

雍马鲁
2023-12-01

打开数据库并写入数据

读取数据

更多例子

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

XIndex 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 table1 = engine.OpenXIndex("table1");

IIndex table2 = engine.OpenXIndex("table2");

IIndex table3 = engine.OpenXIndex("table3");

Or even:

IIndex table3 = engine.OpenXIndex("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 and XIndex table. All transformers look like:

public interface IDataTransformer

{

IData ToIData(T item);

T FromIData(IData data);

DataType DataType { get; }

}

Each XIndex 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

IIndex table = engine.OpenXIndex("table");

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

Data

Data

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 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

Data

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 table = engine.OpenXIndex("table");

Then the backend XIndex will be with types:

Data

Data

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 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 

{

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 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 table = engine.OpenXIndex("table");

for (int i = 0; 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.

 类似资料: