-
Notifications
You must be signed in to change notification settings - Fork 559
/
Types.cs
144 lines (122 loc) · 4.5 KB
/
Types.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Linq;
using FASTER.core;
namespace ClassRecoveryDurablity
{
public class Types
{
public class StoreKey : IFasterEqualityComparer<StoreKey>
{
public byte[] key;
public string tableType;
public virtual long GetHashCode64(ref StoreKey key)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(key.tableType);
byte[] b = bytes.ToArray().Concat(key.key).ToArray();
var hash256 = Program.Hash256(b);
long res = 0;
foreach (byte bt in hash256)
res = res * 31 * 31 * bt + 17;
return res;
}
public virtual bool Equals(ref StoreKey k1, ref StoreKey k2)
{
return k1.key.SequenceEqual(k2.key) && k1.tableType == k2.tableType;
}
}
public class StoreKeySerializer : BinaryObjectSerializer<StoreKey>
{
public override void Deserialize(out StoreKey obj)
{
obj = new StoreKey();
var bytesr = new byte[4];
reader.Read(bytesr, 0, 4);
var sizet = BitConverter.ToInt32(bytesr);
var bytes = new byte[sizet];
reader.Read(bytes, 0, sizet);
obj.tableType = System.Text.Encoding.UTF8.GetString(bytes);
bytesr = new byte[4];
reader.Read(bytesr, 0, 4);
var size = BitConverter.ToInt32(bytesr);
obj.key = new byte[size];
reader.Read(obj.key, 0, size);
}
public override void Serialize(ref StoreKey obj)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(obj.tableType);
var len = BitConverter.GetBytes(bytes.Length);
writer.Write(len);
writer.Write(bytes);
len = BitConverter.GetBytes(obj.key.Length);
writer.Write(len);
writer.Write(obj.key);
}
}
public class StoreValue
{
public byte[] value;
public StoreValue()
{
}
}
public class StoreValueSerializer : BinaryObjectSerializer<StoreValue>
{
public override void Deserialize(out StoreValue obj)
{
obj = new StoreValue();
var bytesr = new byte[4];
reader.Read(bytesr, 0, 4);
int size = BitConverter.ToInt32(bytesr);
obj.value = reader.ReadBytes(size);
}
public override void Serialize(ref StoreValue obj)
{
var len = BitConverter.GetBytes(obj.value.Length);
writer.Write(len);
writer.Write(obj.value);
}
}
public class StoreInput
{
public byte[] value;
}
public class StoreOutput
{
public StoreValue value;
}
public class StoreContext
{
private Status status;
private StoreOutput output;
internal void Populate(ref Status status, ref StoreOutput output)
{
this.status = status;
this.output = output;
}
internal void FinalizeRead(ref Status status, ref StoreOutput output)
{
status = this.status;
output = this.output;
}
}
public sealed class StoreFunctions : FunctionsBase<StoreKey, StoreValue, StoreInput, StoreOutput, StoreContext>
{
public override bool SingleReader(ref StoreKey key, ref StoreInput input, ref StoreValue value, ref StoreOutput dst, ref ReadInfo readInfo)
{
dst.value = value;
return true;
}
public override bool ConcurrentReader(ref StoreKey key, ref StoreInput input, ref StoreValue value, ref StoreOutput dst, ref ReadInfo readInfo)
{
dst.value = value;
return true;
}
public override void ReadCompletionCallback(ref StoreKey key, ref StoreInput input, ref StoreOutput output, StoreContext ctx, Status status, RecordMetadata recordMetadata)
{
ctx.Populate(ref status, ref output);
}
}
}
}