-
Notifications
You must be signed in to change notification settings - Fork 37
/
redis.go
378 lines (337 loc) · 10.9 KB
/
redis.go
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package gamestate
import (
"context"
"github.com/redis/go-redis/v9"
"github.com/rotisserie/eris"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"pkg.world.dev/world-engine/cardinal/codec"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/types"
)
var _ PrimitiveStorage[string] = &RedisStorage{}
type RedisStorage struct {
currentClient redis.Cmdable
}
func (r *RedisStorage) GetFloat64(ctx context.Context, key string) (float64, error) {
res, err := r.currentClient.Get(ctx, key).Float64()
if err != nil {
return 0, eris.Wrap(err, "")
}
return res, nil
}
func (r *RedisStorage) GetFloat32(ctx context.Context, key string) (float32, error) {
res, err := r.currentClient.Get(ctx, key).Float32()
if err != nil {
return 0, eris.Wrap(err, "")
}
return res, nil
}
func (r *RedisStorage) GetUInt64(ctx context.Context, key string) (uint64, error) {
res, err := r.currentClient.Get(ctx, key).Uint64()
if err != nil {
return 0, eris.Wrap(err, "")
}
return res, nil
}
func (r *RedisStorage) GetInt64(ctx context.Context, key string) (int64, error) {
res, err := r.currentClient.Get(ctx, key).Int64()
if err != nil {
return 0, eris.Wrap(err, "")
}
return res, nil
}
func (r *RedisStorage) GetInt(ctx context.Context, key string) (int, error) {
res, err := r.currentClient.Get(ctx, key).Int()
if err != nil {
return 0, eris.Wrap(err, "")
}
return res, nil
}
func (r *RedisStorage) GetBool(ctx context.Context, key string) (bool, error) {
res, err := r.currentClient.Get(ctx, key).Bool()
if err != nil {
return false, eris.Wrap(err, "")
}
return res, nil
}
func (r *RedisStorage) GetBytes(ctx context.Context, key string) ([]byte, error) {
bz, err := r.currentClient.Get(ctx, key).Bytes()
if err != nil {
return nil, eris.Wrap(err, "")
}
return bz, nil
}
func (r *RedisStorage) Set(ctx context.Context, key string, value any) error {
return eris.Wrap(r.currentClient.Set(ctx, key, value, 0).Err(), "")
}
// Underlying type is a string. Unfortunately this is the way redis works and this is the most generic return value.
func (r *RedisStorage) Get(ctx context.Context, key string) (any, error) {
var res any
var err error
res, err = r.currentClient.Get(ctx, key).Result()
return res, eris.Wrap(err, "")
}
func (r *RedisStorage) Incr(ctx context.Context, key string) error {
return eris.Wrap(r.currentClient.Incr(ctx, key).Err(), "")
}
func (r *RedisStorage) Decr(ctx context.Context, key string) error {
return eris.Wrap(r.currentClient.Decr(ctx, key).Err(), "")
}
func (r *RedisStorage) Delete(ctx context.Context, key string) error {
return eris.Wrap(r.currentClient.Del(ctx, key).Err(), "")
}
func (r *RedisStorage) Close(ctx context.Context) error {
return eris.Wrap(r.currentClient.Shutdown(ctx).Err(), "")
}
func (r *RedisStorage) Keys(ctx context.Context) ([]string, error) {
return r.currentClient.Keys(ctx, "*").Result()
}
func (r *RedisStorage) Clear(ctx context.Context) error {
return eris.Wrap(r.currentClient.FlushAll(ctx).Err(), "")
}
func (r *RedisStorage) StartTransaction(_ context.Context) (Transaction[string], error) {
pipeline := r.currentClient.TxPipeline()
redisTransaction := NewRedisPrimitiveStorage(pipeline)
return &redisTransaction, nil
}
func (r *RedisStorage) EndTransaction(ctx context.Context) error {
pipeline, ok := r.currentClient.(redis.Pipeliner)
if !ok {
return eris.New("current redis dbStorage is not a pipeline/transaction")
}
_, err := pipeline.Exec(ctx)
return eris.Wrap(err, "")
}
func NewRedisPrimitiveStorage(client redis.Cmdable) RedisStorage {
return RedisStorage{
currentClient: client,
}
}
// pipeFlushToRedis return a pipeliner with all pending state changes to redis ready to be committed in an atomic
// transaction. If an error is returned, no redis changes will have been made.
func (m *EntityCommandBuffer) makePipeOfRedisCommands(ctx context.Context) (PrimitiveStorage[string], error) {
pipe, err := m.dbStorage.StartTransaction(ctx)
if err != nil {
return nil, err
}
if m.typeToComponent == nil {
// component.ComponentID -> ComponentMetadata mappings are required to serialized data for the DB
return nil, eris.New("must call RegisterComponents before flushing to DB")
}
operations := []struct {
name string
method func(ctx context.Context, pipe PrimitiveStorage[string]) error
}{
{"component_changes", m.addComponentChangesToPipe},
{"next_entity_id", m.addNextEntityIDToPipe},
{"pending_arch_ids", m.addPendingArchIDsToPipe},
{"entity_id_to_arch_id", m.addEntityIDToArchIDToPipe},
{"active_entity_ids", m.addActiveEntityIDsToPipe},
}
for _, operation := range operations {
var pipeSpan tracer.Span
pipeSpan, ctx = tracer.StartSpanFromContext(ctx, "tick.span."+operation.name)
if err := operation.method(ctx, pipe); err != nil {
pipeSpan.Finish(tracer.WithError(err))
return nil, eris.Wrapf(err, "failed to run step %q", operation.name)
}
pipeSpan.Finish()
}
return pipe, nil
}
// addEntityIDToArchIDToPipe adds the information related to mapping an EntityID to its assigned archetype ArchetypeID.
func (m *EntityCommandBuffer) addEntityIDToArchIDToPipe(ctx context.Context, pipe PrimitiveStorage[string]) error {
ids, err := m.entityIDToOriginArchID.Keys()
if err != nil {
return err
}
for _, id := range ids {
originArchID, err := m.entityIDToOriginArchID.Get(id)
if err != nil {
return err
}
key := storageArchetypeIDForEntityID(id)
archID, err := m.entityIDToArchID.Get(id)
if err != nil {
// this entity has been removed
if err := pipe.Delete(ctx, key); err != nil {
return eris.Wrap(err, "")
}
continue
}
// This entity somehow ended up back at its original archetype. There's nothing to do.
if archID == originArchID {
continue
}
// Otherwise, the archetype actually needs to be updated
archIDAsNum := int(archID)
if err := pipe.Set(ctx, key, archIDAsNum); err != nil {
return eris.Wrap(err, "")
}
}
return nil
}
// addNextEntityIDToPipe adds any changes to the next available entity ArchetypeID to the given redis pipe.
func (m *EntityCommandBuffer) addNextEntityIDToPipe(ctx context.Context, pipe PrimitiveStorage[string]) error {
// There are no pending entity id creations, so there's nothing to commit
if m.pendingEntityIDs == 0 {
return nil
}
key := storageNextEntityIDKey()
nextID := m.nextEntityIDSaved + m.pendingEntityIDs
return eris.Wrap(pipe.Set(ctx, key, nextID), "")
}
// addComponentChangesToPipe adds updated component values for entities to the redis pipe.
func (m *EntityCommandBuffer) addComponentChangesToPipe(ctx context.Context, pipe PrimitiveStorage[string]) error {
keysToDelete, err := m.compValuesToDelete.Keys()
if err != nil {
return err
}
for _, key := range keysToDelete {
isMarkedForDeletion, err := m.compValuesToDelete.Get(key)
if err != nil {
return err
}
if !isMarkedForDeletion {
continue
}
redisKey := storageComponentKey(key.typeID, key.entityID)
if err := pipe.Delete(ctx, redisKey); err != nil {
return eris.Wrap(err, "")
}
}
if err = m.compValuesToDelete.Clear(); err != nil {
return eris.Wrap(err, "failed to clear to-be-deleted component values store")
}
keys, err := m.compValues.Keys()
if err != nil {
return err
}
for _, key := range keys {
cType, err := m.typeToComponent.Get(key.typeID)
if err != nil {
return err
}
value, err := m.compValues.Get(key)
if err != nil {
return err
}
bz, err := cType.Encode(value)
if err != nil {
return err
}
redisKey := storageComponentKey(key.typeID, key.entityID)
if err = pipe.Set(ctx, redisKey, bz); err != nil {
return eris.Wrap(err, "")
}
}
return nil
}
// preloadArchIDs loads the mapping of archetypes IDs to sets of IComponentTypes from dbStorage.
func (m *EntityCommandBuffer) loadArchIDs() error {
archIDToComps, ok, err := getArchIDToCompTypesFromRedis(m.dbStorage, m.typeToComponent)
if err != nil {
return err
}
if !ok {
// Nothing is saved in the DB. Leave the m.archIDToComps field unchanged
return nil
}
if m.archIDToComps.Len() > 0 {
return eris.New("assigned archetype ArchetypeID is about to be overwritten by something from dbStorage")
}
m.archIDToComps = archIDToComps
return nil
}
// addPendingArchIDsToPipe adds any newly created archetype IDs (as well as the associated sets of components) to the
// redis pipe.
func (m *EntityCommandBuffer) addPendingArchIDsToPipe(ctx context.Context, pipe PrimitiveStorage[string]) error {
if len(m.pendingArchIDs) == 0 {
return nil
}
bz, err := m.encodeArchIDToCompTypes()
if err != nil {
return err
}
return eris.Wrap(pipe.Set(ctx, storageArchIDsToCompTypesKey(), bz), "")
}
// addActiveEntityIDsToPipe adds information about which entities are assigned to which archetype IDs to the reids pipe.
func (m *EntityCommandBuffer) addActiveEntityIDsToPipe(ctx context.Context, pipe PrimitiveStorage[string]) error {
archIDs, err := m.activeEntities.Keys()
if err != nil {
return err
}
for _, archID := range archIDs {
active, err := m.activeEntities.Get(archID)
if err != nil {
return err
}
if !active.modified {
continue
}
bz, err := codec.Encode(active.ids)
if err != nil {
return err
}
key := storageActiveEntityIDKey(archID)
err = pipe.Set(ctx, key, bz)
if err != nil {
return eris.Wrap(err, "")
}
}
return nil
}
func (m *EntityCommandBuffer) encodeArchIDToCompTypes() ([]byte, error) {
forStorage := map[types.ArchetypeID][]types.ComponentID{}
archIDs, err := m.archIDToComps.Keys()
if err != nil {
return nil, err
}
for _, archID := range archIDs {
typeIDs := []types.ComponentID{}
comps, err := m.archIDToComps.Get(archID)
if err != nil {
return nil, err
}
for _, comp := range comps {
typeIDs = append(typeIDs, comp.ID())
}
forStorage[archID] = typeIDs
}
return codec.Encode(forStorage)
}
func getArchIDToCompTypesFromRedis(
storage PrimitiveStorage[string],
typeToComp VolatileStorage[types.ComponentID, types.ComponentMetadata],
) (m VolatileStorage[types.ArchetypeID, []types.ComponentMetadata], ok bool, err error) {
ctx := context.Background()
key := storageArchIDsToCompTypesKey()
bz, err := storage.GetBytes(ctx, key)
err = eris.Wrap(err, "")
if eris.Is(eris.Cause(err), redis.Nil) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
fromStorage, err := codec.Decode[map[types.ArchetypeID][]types.ComponentID](bz)
if err != nil {
return nil, false, err
}
// result is the mapping of Arch ArchetypeID -> IComponent sets
result := NewMapStorage[types.ArchetypeID, []types.ComponentMetadata]()
for archID, compTypeIDs := range fromStorage {
var currComps []types.ComponentMetadata
for _, compTypeID := range compTypeIDs {
currComp, err := typeToComp.Get(compTypeID)
if err != nil {
return nil, false, eris.Wrap(iterators.ErrComponentMismatchWithSavedState, "")
}
currComps = append(currComps, currComp)
}
err = result.Set(archID, currComps)
if err != nil {
return nil, false, err
}
}
return result, true, nil
}