-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
256 lines (203 loc) · 6.91 KB
/
Program.cs
File metadata and controls
256 lines (203 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using FASTER.core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
[Serializable]
class MyType
{
public int i;
public override int GetHashCode()
{
return i.GetHashCode();
}
public override bool Equals(object obj)
{
if(!(obj is MyType))
{
return false;
}
return i.Equals(((MyType)obj).i);
}
}
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var x = new d<MyType, MyType>();
for (int i = 0; i < 1000000; i++)
//Parallel.For(0, 1000000, i =>
{
x.Add(new MyType() { i = i }, new MyType() { i = i });
}//);
//x.Flush();
for (int i = 0; i < 1000000; i++)
{
if (x[new MyType() { i = i }].i != i)
{
throw new Exception("mismatch!");
}
}
x.Dispose();
Console.WriteLine(sw.Elapsed);
}
class d<KEY, VALUE> : IDictionary<KEY, VALUE>, IDisposable
{
VALUE context;
private IDevice log;
private IDevice objlog;
private SerializerSettings<KEY, VALUE> serilizerSettings;
private FasterKV<KEY, VALUE> store;
private ClientSession<KEY, VALUE, VALUE, VALUE, Empty, IFunctions<KEY, VALUE, VALUE, VALUE, Empty>> s;
//private ClientSession<KEY, VALUE, Empty, VALUE, Empty, IFunctions<KEY, VALUE, Empty, VALUE, Empty>> s;
private bool disposed;
public d()
{
log = Devices.CreateLogDevice(@"C:\Users\merlynop\source\repos\ConsoleApp2\bin\Debug\hlog.log");
objlog = Devices.CreateLogDevice(@"C:\Users\merlynop\source\repos\ConsoleApp2\bin\Debug\hlog.obj.log");
serilizerSettings = new SerializerSettings<KEY, VALUE>
{
keySerializer = () => new MyValueSerializer<KEY>(),
valueSerializer = () => new MyValueSerializer<VALUE>()
};
store = new FasterKV<KEY, VALUE>(
1L << 20,
new LogSettings { LogDevice = log, ObjectLogDevice = objlog },
serializerSettings: serilizerSettings);
//s = store.NewSession(new MyFunctions<KEY, VALUE>());
s = store.NewSession(new SimpleFunctions<KEY, VALUE>());
}
public void Dispose()
{
if (!disposed)
{
store.Log.FlushAndEvict(true);
// end session
s.Dispose();
// dispose store
store.Dispose();
// close logs
log.Dispose();
objlog.Dispose();
disposed = true;
}
}
public VALUE this[KEY key]
{
get
{
VALUE ret = default;
TryGetValue(key, out ret);
return ret;
}
set
{
var r = s.Upsert(ref key, ref value);
if(r!= Status.OK)
{
throw new Exception("not okay");
}
}
}
public ICollection<KEY> Keys => throw new NotImplementedException();
public ICollection<VALUE> Values => throw new NotImplementedException();
public int Count => throw new NotImplementedException();
public bool IsReadOnly => false;
public void Add(KEY key, VALUE value)
{
this[key] = value;
}
public void Add(KeyValuePair<KEY, VALUE> item)
{
this[item.Key] = item.Value;
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(KeyValuePair<KEY, VALUE> item)
{
throw new NotImplementedException();
}
public bool ContainsKey(KEY key)
{
throw new NotImplementedException();
}
public void CopyTo(KeyValuePair<KEY, VALUE>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public IEnumerator<KeyValuePair<KEY, VALUE>> GetEnumerator()
{
throw new NotImplementedException();
}
public bool Remove(KEY key)
{
throw new NotImplementedException();
}
public bool Remove(KeyValuePair<KEY, VALUE> item)
{
throw new NotImplementedException();
}
public bool TryGetValue(KEY key, out VALUE value)
{
VALUE valuer = default;
VALUE input = default;
var r = s.Read(ref key, ref valuer);
//var r = s.Read(ref key, ref input, ref valuer, context, 0);
if (r == Status.OK)
{
value = (VALUE)valuer;
return true;
}
else
{
throw new Exception("not okay");
value = default;
return false;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
internal void Flush()
{
store.Log.FlushAndEvict(true);
}
}
//internal class MyFunctions<KEY, VALUE> : FunctionsBase<KEY, VALUE, Empty, VALUE, Empty>
//{
//}
}
public class MyValueSerializer<T> : BinaryObjectSerializer<T>, IFasterEqualityComparer<T>
{
public override void Serialize(ref T value)
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(writer.BaseStream, value);
}
public override void Deserialize(out T value)
{
BinaryFormatter bf = new BinaryFormatter();
value = (T)bf.Deserialize(reader.BaseStream);
}
public long GetHashCode64(ref T k)
{
// returns int hashcode, not as good as faster's expected long hashcode
return k.GetHashCode();
}
public bool Equals(ref T k1, ref T k2)
{
return k1.Equals(k2);
}
}
}