Last active
August 29, 2015 13:57
-
-
Save ishisaka/9522784 to your computer and use it in GitHub Desktop.
ESE InteropのPersistantDictionaryの使用方法。Dictionary<>の使用感覚でESEを使える大変な便利なライブラリ。当然Linqが使える
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
using Microsoft.Isam.Esent.Collections.Generic; | |
namespace ExampleOfEsentPersistantDictionary2 | |
{ | |
/// <summary> | |
/// PersistentDictionaryFileのサンプル。 | |
/// 保存するデータに構造体を用いる。 | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// データベースがすでに存在している場合には削除する | |
if (PersistentDictionaryFile.Exists("PlantLogs")) | |
{ | |
PersistentDictionaryFile.DeleteFiles("PlantLogs"); | |
} | |
// n件追加する。 | |
var logs = new PersistentDictionary<int, PlantLog>("PlantLogs"); | |
int n = 100000; | |
Console.WriteLine("{0}件データ追加", n); | |
var watch = Stopwatch.StartNew(); | |
Enumerable.Range(1,n).ToList().ForEach(i => | |
{ | |
var nowTime = DateTime.Now; | |
PlantLog log; | |
log.AleartTime = nowTime; | |
log.AlarmLevel = i; | |
log.Area = i + 1; | |
log.Description = "Message " + i; | |
//Addメソッドだと例外が発生するので、以下の構文で。 | |
logs[i] = log; | |
}); | |
Console.WriteLine("データ追加経過 {0}ms", watch.ElapsedMilliseconds); | |
watch.Stop(); | |
watch.Reset(); | |
// n件取得する。 | |
Console.WriteLine("{0}件データ取得", n); | |
watch.Start(); | |
var plantLogs = logs.Select(l => l.Value).ToList(); | |
Console.WriteLine("データ取得経過時間 {0}ms", watch.ElapsedMilliseconds); | |
watch.Stop(); | |
// 件数の確認。 | |
var q = logs.ToArray(); | |
Console.WriteLine("{0}件", q.Length); | |
Console.ReadLine(); | |
} | |
} | |
/// <summary> | |
/// 構造体はSerializable属性を付ける。 | |
/// 構造体も以下のデータ型とプリミティブなデータ型のみが許される | |
/// string, Uri, IPAddress | |
/// </summary> | |
[Serializable] | |
public struct PlantLog | |
{ | |
public DateTime AleartTime; | |
public int AlarmLevel; | |
public int Area; | |
public string Description; | |
// 以下のような配列も使用できない | |
// public byte[] Data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment