STSDB使用心得
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STSdb4.Database;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.IO;
using System.Diagnostics;
namespace STSDBDemo
{
class Program
{
static void Main(string[] args)
{
ExecuteTest();
Console.ReadKey();
}
private static void ExecuteTest()
{
try
{
string engineName = "msheng.msheng";
//File.Delete(engineName);
Console.WriteLine("Write Beginning");
Stopwatch sw = Stopwatch.StartNew();
using (IStorageEngine engine = STSdb.FromFile(engineName))
{
ITable<int, string> valueTable = engine.OpenXTable<int, string>("engineValueTable");//怎么知道这里面有哪些表呢
//Parallel.For(0, 1000000, (i) => valueTable[i] = i.ToString());//减少了1000倍,对于简单的操作,千万不要使用并发
//对于集合,该不该使用并发,我觉得主要的考虑因素是对于集合中单个项的操作是不是很复杂,如果是计算cpu bound那
//使用并发是很有必要的,如果是i/o bound还得使用task await。因为使用并发,容易造成线程数过多,损耗性能。
for (int i = 0; i < 100; i++)
{
valueTable[i] = i.ToString();
}
//commit是针对单个库中所有打开的表的,不需要对每个单开的表都进行commit操作
//engine.Commit();
//}
//可以打开一个数据库,对不同的表进行操作。而不需要在对每个表进行操作时,都使用一个using
//using (IStorageEngine engine = STSdb.FromFile(engineName))
//{
ITable<int, MyClass> refrenceTable = engine.OpenXTable<int, MyClass>("engineRefTable");
PersonInfo msheng = new PersonInfo()
{
Name = "shengmiao",
Age = 25,
Description = "workhard"
};
PersonInfo gjh = new PersonInfo
{
Name = "gjh",
Age = 25,
Description = "bt"
};
MyClass myClass = new MyClass();
myClass.TestConDic.Add(20180, msheng);
myClass.TestConDic.Add(20181, gjh);
refrenceTable[0] = myClass;
//此处提交是针对所有的打开的表的
engine.Commit();
//如果不适用using,可以显式调用close来关闭数据库engine
//engine.Close();
}
Console.WriteLine(sw.ElapsedMilliseconds.ToString());
Console.WriteLine("WriteDone!");
Console.WriteLine("ReadBeginning");
using (IStorageEngine engine = STSdb.FromFile(engineName))
{
ITable<int, string> valTable = engine.OpenXTable<int, string>("engineValueTable");
Console.WriteLine("valTable's count is " + valTable.Count());
foreach (var item in valTable)
{
Console.Write("\r{" + item.Key.ToString() + ":" + item.Value.ToString() + "}");
}
Console.WriteLine();
ITable<int, MyClass> refTable = engine.OpenXTable<int, MyClass>("engineRefTable");
Console.WriteLine("refTable's count is " + refTable.Count());
foreach (var item in refTable)
{
MyClass myClass = item.Value;
foreach (var pair in myClass.TestConDic)
{
Console.WriteLine(pair.Value.Name + "\r\n" + pair.Value.Age + "\r\n" + pair.Value.Description);
}
}
Console.WriteLine("ReadDone!");
}
}
catch (Exception ex)
{
Console.WriteLine("Error Occured!\r\n" + ex.ToString());
}
}
}
//是否使用可序列化特性,对STSDB影响不大
//[Serializable]
sealed class MyClass
{
//使用并发集合会造成“影响运行时稳定性”的错误
public Dictionary<int, PersonInfo> TestConDic = new Dictionary<int, PersonInfo>();
}
// [Serializable]
sealed class PersonInfo
{
public string Name { get; set; }
public int Age { get; set; }
public string Description { get; set; }
}
/*
使用并发加入数据的测试结果:
Write Beginning
4326
WriteDone!
ReadBeginning
valTable's count is 100000
{99999:99999}
refTable's count is 1
shengmiao
25
workhard
gjh
25
bt
ReadDone!
//执行数增加了10倍,耗时增加了20倍
Write Beginning
83662
WriteDone!
ReadBeginning
valTable's count is 1000000
{999999:999999}
refTable's count is 1
shengmiao
25
workhard
gjh
25
bt
ReadDone!
*/
}
相关文章