-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathSetObject.cs
More file actions
234 lines (202 loc) · 6.67 KB
/
SetObject.cs
File metadata and controls
234 lines (202 loc) · 6.67 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Garnet.common;
using Tsavorite.core;
namespace Garnet.server
{
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
/// <summary>
/// Operations on Set
/// </summary>
public enum SetOperation : byte
{
SADD,
SREM,
SPOP,
SMEMBERS,
SCARD,
SSCAN,
SMOVE,
SRANDMEMBER,
SISMEMBER,
SMISMEMBER,
SUNION,
SUNIONSTORE,
SDIFF,
SDIFFSTORE,
SINTER,
SINTERSTORE
}
/// <summary>
/// Set Object Class
/// </summary>
public partial class SetObject : GarnetObjectBase
{
public HashSet<byte[]> Set { get; }
#if NET9_0_OR_GREATER
private readonly HashSet<byte[]>.AlternateLookup<ReadOnlySpan<byte>> setLookup;
#endif
/// <summary>
/// Constructor
/// </summary>
public SetObject()
: base(MemoryUtils.HashSetOverhead)
{
Set = new HashSet<byte[]>(ByteArrayComparer.Instance);
#if NET9_0_OR_GREATER
setLookup = Set.GetAlternateLookup<ReadOnlySpan<byte>>();
#endif
}
/// <summary>
/// Construct from binary serialized form
/// </summary>
public SetObject(BinaryReader reader)
: base(reader, MemoryUtils.HashSetOverhead)
{
var count = reader.ReadInt32();
Set = new HashSet<byte[]>(count, ByteArrayComparer.Instance);
for (var i = 0; i < count; i++)
{
var item = reader.ReadBytes(reader.ReadInt32());
Set.Add(item);
UpdateSize(item);
}
#if NET9_0_OR_GREATER
setLookup = Set.GetAlternateLookup<ReadOnlySpan<byte>>();
#endif
}
/// <summary>
/// Copy constructor
/// </summary>
public SetObject(HashSet<byte[]> set, long heapMemorySize)
: base(heapMemorySize)
{
Set = set;
#if NET9_0_OR_GREATER
setLookup = Set.GetAlternateLookup<ReadOnlySpan<byte>>();
#endif
}
/// <inheritdoc />
public override byte Type => (byte)GarnetObjectType.Set;
/// <inheritdoc />
public override void DoSerialize(BinaryWriter writer)
{
base.DoSerialize(writer);
var count = Set.Count;
writer.Write(count);
foreach (var item in Set)
{
writer.Write(item.Length);
writer.Write(item);
count--;
}
Debug.Assert(count == 0);
}
/// <inheritdoc />
public override void Dispose() { }
/// <inheritdoc />
public override GarnetObjectBase Clone() => new SetObject(Set, HeapMemorySize);
/// <inheritdoc />
public override bool Operate(ref ObjectInput input, ref ObjectOutput output,
byte respProtocolVersion)
{
if (input.header.type != GarnetObjectType.Set)
{
// Indicates an incorrect type of key
output.OutputFlags |= ObjectOutputFlags.WrongType;
output.SpanByteAndMemory.Length = 0;
return true;
}
switch (input.header.SetOp)
{
case SetOperation.SADD:
SetAdd(ref input, ref output);
break;
case SetOperation.SMEMBERS:
SetMembers(ref input, ref output, respProtocolVersion);
break;
case SetOperation.SISMEMBER:
SetIsMember(ref input, ref output, respProtocolVersion);
break;
case SetOperation.SMISMEMBER:
SetMultiIsMember(ref input, ref output, respProtocolVersion);
break;
case SetOperation.SREM:
SetRemove(ref input, ref output);
break;
case SetOperation.SCARD:
SetLength(ref output);
break;
case SetOperation.SPOP:
SetPop(ref input, ref output, respProtocolVersion);
break;
case SetOperation.SRANDMEMBER:
SetRandomMember(ref input, ref output, respProtocolVersion);
break;
case SetOperation.SSCAN:
Scan(ref input, ref output, respProtocolVersion);
break;
default:
throw new GarnetException($"Unsupported operation {input.header.SetOp} in SetObject.Operate");
}
if (Set.Count == 0)
output.OutputFlags |= ObjectOutputFlags.RemoveKey;
return true;
}
internal void UpdateSize(ReadOnlySpan<byte> item, bool add = true)
{
var memorySize = Utility.RoundUp(item.Length, IntPtr.Size) + MemoryUtils.ByteArrayOverhead + MemoryUtils.HashSetEntryOverhead;
if (add)
HeapMemorySize += memorySize;
else
{
HeapMemorySize -= memorySize;
Debug.Assert(HeapMemorySize >= MemoryUtils.HashSetOverhead);
}
}
/// <inheritdoc />
public override unsafe void Scan(long start, out List<byte[]> items, out long cursor, int count = 10, byte* pattern = default, int patternLength = 0, bool isNoValue = false)
{
cursor = start;
items = [];
if (Set.Count < start)
{
cursor = 0;
return;
}
var index = 0;
foreach (var item in Set)
{
if (index < start)
{
index++;
continue;
}
if (patternLength == 0)
{
items.Add(item);
}
else
{
fixed (byte* keyPtr = item)
{
if (GlobUtils.Match(pattern, patternLength, keyPtr, item.Length))
{
items.Add(item);
}
}
}
cursor++;
if (items.Count == count)
break;
}
// Indicates end of collection has been reached.
if (cursor == Set.Count)
cursor = 0;
}
}
}