prolly-store-redis 0.3.0

Redis store adapter for prolly-map.
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#![doc = include_str!("../README.md")]

pub use prolly::{
    RemoteBatchOp, RemoteManifestUpdate, RemoteNamedRoot, RemoteProllyStore, RemoteRootCondition,
    RemoteRootWrite, RemoteStoreBackend, RemoteTransactionConflict, RemoteTransactionUpdate,
};

/// Redis adapter entry point.
pub mod redis {
    use redis_client::{ErrorKind, RedisError, Script, Value};

    use crate::{
        RemoteBatchOp, RemoteManifestUpdate, RemoteNamedRoot, RemoteRootCondition, RemoteRootWrite,
        RemoteStoreBackend, RemoteTransactionConflict, RemoteTransactionUpdate,
    };

    /// Store adapter for Redis-backed prolly nodes and roots.
    ///
    /// Redis should be treated as a cache or edge store unless persistence and
    /// durability are explicitly configured for the Redis deployment.
    pub type RedisStore = crate::RemoteProllyStore<RedisBackend>;

    /// Redis-backed prolly node/root backend.
    #[derive(Clone)]
    pub struct RedisBackend {
        connection: redis_client::aio::ConnectionManager,
        key_prefix: Vec<u8>,
        read_parallelism: usize,
    }

    impl std::fmt::Debug for RedisBackend {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("RedisBackend")
                .field("key_prefix", &self.key_prefix)
                .field("read_parallelism", &self.read_parallelism)
                .finish_non_exhaustive()
        }
    }

    impl RedisBackend {
        /// Create a backend from an existing Redis connection manager.
        pub fn new(connection: redis_client::aio::ConnectionManager) -> Self {
            Self {
                connection,
                key_prefix: DEFAULT_KEY_PREFIX.to_vec(),
                read_parallelism: DEFAULT_READ_PARALLELISM,
            }
        }

        /// Connect to Redis using `redis_url`.
        pub async fn connect(redis_url: &str) -> Result<Self, RedisError> {
            let client = redis_client::Client::open(redis_url)?;
            Self::from_client(client).await
        }

        /// Create a backend from an existing Redis client.
        pub async fn from_client(client: redis_client::Client) -> Result<Self, RedisError> {
            Ok(Self::new(client.get_connection_manager().await?))
        }

        /// Borrow the underlying connection manager.
        pub fn connection(&self) -> &redis_client::aio::ConnectionManager {
            &self.connection
        }

        /// Return the namespace prefix prepended to all Redis keys.
        pub fn key_prefix(&self) -> &[u8] {
            &self.key_prefix
        }

        /// Set the namespace prefix prepended to all Redis keys.
        ///
        /// Use a unique prefix when running tests or sharing a Redis database.
        pub fn with_key_prefix(mut self, key_prefix: impl Into<Vec<u8>>) -> Self {
            self.key_prefix = key_prefix.into();
            self
        }

        /// Set the read parallelism advertised to async prolly traversals.
        pub fn with_read_parallelism(mut self, read_parallelism: usize) -> Self {
            self.read_parallelism = read_parallelism.max(1);
            self
        }

        /// Delete every key under this backend's namespace prefix.
        ///
        /// This is primarily intended for isolated integration tests.
        pub async fn clear_namespace(&self) -> Result<(), RedisError> {
            if self.key_prefix.is_empty() {
                return Err(redis_type_error(
                    "refusing to clear an empty Redis key prefix",
                ));
            }

            let mut pattern = self.key_prefix.clone();
            pattern.push(b'*');
            let keys = self.scan_keys(&pattern).await?;
            self.delete_keys(&keys).await
        }

        fn node_key(&self, key: &[u8]) -> Vec<u8> {
            self.family_key(NODE_FAMILY, key)
        }

        fn root_key(&self, name: &[u8]) -> Vec<u8> {
            self.family_key(ROOT_FAMILY, name)
        }

        fn hint_key(&self, namespace: &[u8], key: &[u8]) -> Vec<u8> {
            let mut redis_key = self.family_key(HINT_FAMILY, &[]);
            redis_key.extend_from_slice(&(namespace.len() as u64).to_be_bytes());
            redis_key.extend_from_slice(namespace);
            redis_key.extend_from_slice(key);
            redis_key
        }

        fn family_key(&self, family: &[u8], suffix: &[u8]) -> Vec<u8> {
            let mut key = Vec::with_capacity(self.key_prefix.len() + family.len() + suffix.len());
            key.extend_from_slice(&self.key_prefix);
            key.extend_from_slice(family);
            key.extend_from_slice(suffix);
            key
        }

        fn family_prefix(&self, family: &[u8]) -> Vec<u8> {
            self.family_key(family, &[])
        }

        fn family_pattern(&self, family: &[u8]) -> Vec<u8> {
            let mut pattern = self.family_prefix(family);
            pattern.push(b'*');
            pattern
        }

        async fn scan_keys(&self, pattern: &[u8]) -> Result<Vec<Vec<u8>>, RedisError> {
            let mut connection = self.connection.clone();
            let mut cursor = 0_u64;
            let mut keys = Vec::new();

            loop {
                let (next_cursor, batch): (u64, Vec<Vec<u8>>) = redis_client::cmd("SCAN")
                    .arg(cursor)
                    .arg("MATCH")
                    .arg(pattern)
                    .arg("COUNT")
                    .arg(SCAN_COUNT)
                    .query_async(&mut connection)
                    .await?;
                keys.extend(batch);
                if next_cursor == 0 {
                    break;
                }
                cursor = next_cursor;
            }

            Ok(keys)
        }

        async fn delete_keys(&self, keys: &[Vec<u8>]) -> Result<(), RedisError> {
            if keys.is_empty() {
                return Ok(());
            }

            let mut connection = self.connection.clone();
            for chunk in keys.chunks(DELETE_CHUNK_SIZE) {
                let mut command = redis_client::cmd("DEL");
                for key in chunk {
                    command.arg(key.as_slice());
                }
                command.query_async::<()>(&mut connection).await?;
            }
            Ok(())
        }
    }

    impl RemoteStoreBackend for RedisBackend {
        type Error = RedisError;

        async fn get_node(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
            let mut connection = self.connection.clone();
            redis_client::cmd("GET")
                .arg(self.node_key(key))
                .query_async(&mut connection)
                .await
        }

        async fn put_node(&self, key: &[u8], value: &[u8]) -> Result<(), Self::Error> {
            let mut connection = self.connection.clone();
            redis_client::cmd("SET")
                .arg(self.node_key(key))
                .arg(value)
                .query_async::<()>(&mut connection)
                .await
        }

        async fn delete_node(&self, key: &[u8]) -> Result<(), Self::Error> {
            let mut connection = self.connection.clone();
            redis_client::cmd("DEL")
                .arg(self.node_key(key))
                .query_async::<()>(&mut connection)
                .await
        }

        async fn batch_nodes(&self, ops: &[RemoteBatchOp<'_>]) -> Result<(), Self::Error> {
            if ops.is_empty() {
                return Ok(());
            }

            let mut pipeline = redis_client::pipe();
            pipeline.atomic();
            for op in ops {
                match op {
                    RemoteBatchOp::Upsert { key, value } => {
                        pipeline
                            .cmd("SET")
                            .arg(self.node_key(key))
                            .arg(*value)
                            .ignore();
                    }
                    RemoteBatchOp::Delete { key } => {
                        pipeline.cmd("DEL").arg(self.node_key(key)).ignore();
                    }
                }
            }

            let mut connection = self.connection.clone();
            pipeline.query_async::<()>(&mut connection).await
        }

        async fn batch_get_nodes_ordered(
            &self,
            keys: &[&[u8]],
        ) -> Result<Vec<Option<Vec<u8>>>, Self::Error> {
            if keys.is_empty() {
                return Ok(Vec::new());
            }

            let mut command = redis_client::cmd("MGET");
            for key in keys {
                command.arg(self.node_key(key));
            }

            let mut connection = self.connection.clone();
            command.query_async(&mut connection).await
        }

        async fn batch_put_nodes(&self, entries: &[(&[u8], &[u8])]) -> Result<(), Self::Error> {
            if entries.is_empty() {
                return Ok(());
            }

            let mut pipeline = redis_client::pipe();
            pipeline.atomic();
            for (key, value) in entries {
                pipeline
                    .cmd("SET")
                    .arg(self.node_key(key))
                    .arg(*value)
                    .ignore();
            }

            let mut connection = self.connection.clone();
            pipeline.query_async::<()>(&mut connection).await
        }

        async fn list_node_cids(&self) -> Result<Vec<Vec<u8>>, Self::Error> {
            let prefix = self.family_prefix(NODE_FAMILY);
            let pattern = self.family_pattern(NODE_FAMILY);
            let mut cids = self
                .scan_keys(&pattern)
                .await?
                .into_iter()
                .filter_map(|key| {
                    key.strip_prefix(prefix.as_slice())
                        .filter(|cid| cid.len() == 32)
                        .map(<[u8]>::to_vec)
                })
                .collect::<Vec<_>>();
            cids.sort();
            Ok(cids)
        }

        fn prefers_batch_reads(&self) -> bool {
            true
        }

        fn read_parallelism(&self) -> usize {
            self.read_parallelism
        }

        fn supports_hints(&self) -> bool {
            true
        }

        async fn get_hint(
            &self,
            namespace: &[u8],
            key: &[u8],
        ) -> Result<Option<Vec<u8>>, Self::Error> {
            let mut connection = self.connection.clone();
            redis_client::cmd("GET")
                .arg(self.hint_key(namespace, key))
                .query_async(&mut connection)
                .await
        }

        async fn put_hint(
            &self,
            namespace: &[u8],
            key: &[u8],
            value: &[u8],
        ) -> Result<(), Self::Error> {
            let mut connection = self.connection.clone();
            redis_client::cmd("SET")
                .arg(self.hint_key(namespace, key))
                .arg(value)
                .query_async::<()>(&mut connection)
                .await
        }

        async fn batch_put_nodes_with_hint(
            &self,
            entries: &[(&[u8], &[u8])],
            namespace: &[u8],
            key: &[u8],
            value: &[u8],
        ) -> Result<(), Self::Error> {
            let mut pipeline = redis_client::pipe();
            pipeline.atomic();
            for (key, value) in entries {
                pipeline
                    .cmd("SET")
                    .arg(self.node_key(key))
                    .arg(*value)
                    .ignore();
            }
            pipeline
                .cmd("SET")
                .arg(self.hint_key(namespace, key))
                .arg(value)
                .ignore();

            let mut connection = self.connection.clone();
            pipeline.query_async::<()>(&mut connection).await
        }

        async fn get_root_manifest(&self, name: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
            let mut connection = self.connection.clone();
            redis_client::cmd("GET")
                .arg(self.root_key(name))
                .query_async(&mut connection)
                .await
        }

        async fn put_root_manifest(&self, name: &[u8], manifest: &[u8]) -> Result<(), Self::Error> {
            let mut connection = self.connection.clone();
            redis_client::cmd("SET")
                .arg(self.root_key(name))
                .arg(manifest)
                .query_async::<()>(&mut connection)
                .await
        }

        async fn delete_root_manifest(&self, name: &[u8]) -> Result<(), Self::Error> {
            let mut connection = self.connection.clone();
            redis_client::cmd("DEL")
                .arg(self.root_key(name))
                .query_async::<()>(&mut connection)
                .await
        }

        async fn compare_and_swap_root_manifest(
            &self,
            name: &[u8],
            expected: Option<&[u8]>,
            new: Option<&[u8]>,
        ) -> Result<RemoteManifestUpdate, Self::Error> {
            let script = Script::new(ROOT_CAS_LUA);
            let mut invocation = script.prepare_invoke();
            invocation
                .key(self.root_key(name))
                .arg(if expected.is_some() { b"1" } else { b"0" }.as_slice())
                .arg(expected.unwrap_or_default())
                .arg(if new.is_some() { b"1" } else { b"0" }.as_slice())
                .arg(new.unwrap_or_default());

            let mut connection = self.connection.clone();
            let response: Value = invocation.invoke_async(&mut connection).await?;
            parse_root_cas_response(response)
        }

        async fn list_root_manifests(&self) -> Result<Vec<RemoteNamedRoot>, Self::Error> {
            let prefix = self.family_prefix(ROOT_FAMILY);
            let pattern = self.family_pattern(ROOT_FAMILY);
            let mut names = self
                .scan_keys(&pattern)
                .await?
                .into_iter()
                .filter_map(|key| key.strip_prefix(prefix.as_slice()).map(<[u8]>::to_vec))
                .collect::<Vec<_>>();
            names.sort();

            let mut roots = Vec::with_capacity(names.len());
            for name in names {
                if let Some(manifest) = self.get_root_manifest(&name).await? {
                    roots.push(RemoteNamedRoot::new(name, manifest));
                }
            }
            Ok(roots)
        }

        fn supports_transactions(&self) -> bool {
            true
        }

        async fn commit_transaction(
            &self,
            node_writes: &[RemoteBatchOp<'_>],
            root_conditions: &[RemoteRootCondition],
            root_writes: &[RemoteRootWrite],
        ) -> Result<RemoteTransactionUpdate, Self::Error> {
            let script = Script::new(TRANSACTION_COMMIT_LUA);
            let mut invocation = script.prepare_invoke();
            for condition in root_conditions {
                invocation.key(self.root_key(&condition.name));
            }
            for write in node_writes {
                match write {
                    RemoteBatchOp::Upsert { key, .. } | RemoteBatchOp::Delete { key } => {
                        invocation.key(self.node_key(key));
                    }
                }
            }
            for write in root_writes {
                match write {
                    RemoteRootWrite::Put { name, .. } | RemoteRootWrite::Delete { name } => {
                        invocation.key(self.root_key(name));
                    }
                }
            }

            invocation
                .arg(root_conditions.len())
                .arg(node_writes.len())
                .arg(root_writes.len());
            for condition in root_conditions {
                invocation
                    .arg(
                        if condition.expected.is_some() {
                            b"1"
                        } else {
                            b"0"
                        }
                        .as_slice(),
                    )
                    .arg(condition.expected.as_deref().unwrap_or_default());
            }
            for write in node_writes {
                match write {
                    RemoteBatchOp::Upsert { value, .. } => {
                        invocation.arg("upsert").arg(*value);
                    }
                    RemoteBatchOp::Delete { .. } => {
                        invocation.arg("delete");
                    }
                }
            }
            for write in root_writes {
                match write {
                    RemoteRootWrite::Put { manifest, .. } => {
                        invocation.arg("put").arg(manifest);
                    }
                    RemoteRootWrite::Delete { .. } => {
                        invocation.arg("delete");
                    }
                }
            }

            let mut connection = self.connection.clone();
            let response: Value = invocation.invoke_async(&mut connection).await?;
            parse_transaction_response(response, root_conditions)
        }
    }

    fn parse_root_cas_response(response: Value) -> Result<RemoteManifestUpdate, RedisError> {
        let Value::Array(values) = response else {
            return Err(redis_type_error("root CAS script returned a non-array"));
        };
        let [applied, current] = values
            .try_into()
            .map_err(|_| redis_type_error("root CAS script returned wrong arity"))?;

        if value_to_bool(applied)? {
            return Ok(RemoteManifestUpdate::Applied);
        }

        Ok(RemoteManifestUpdate::Conflict {
            current: value_to_optional_bytes(current)?,
        })
    }

    fn value_to_bool(value: Value) -> Result<bool, RedisError> {
        match value {
            Value::Int(0) => Ok(false),
            Value::Int(1) => Ok(true),
            Value::Boolean(value) => Ok(value),
            other => Err(redis_type_error(format!(
                "root CAS script returned invalid applied flag: {other:?}"
            ))),
        }
    }

    fn value_to_usize(value: Value) -> Result<usize, RedisError> {
        match value {
            Value::Int(value) if value >= 0 => Ok(value as usize),
            other => Err(redis_type_error(format!(
                "transaction script returned invalid conflict index: {other:?}"
            ))),
        }
    }

    fn value_to_optional_bytes(value: Value) -> Result<Option<Vec<u8>>, RedisError> {
        match value {
            Value::Nil => Ok(None),
            Value::Boolean(false) => Ok(None),
            Value::BulkString(bytes) => Ok(Some(bytes)),
            other => Err(redis_type_error(format!(
                "root CAS script returned invalid current manifest: {other:?}"
            ))),
        }
    }

    fn parse_transaction_response(
        response: Value,
        root_conditions: &[RemoteRootCondition],
    ) -> Result<RemoteTransactionUpdate, RedisError> {
        let Value::Array(values) = response else {
            return Err(redis_type_error("transaction script returned a non-array"));
        };
        let [applied, conflict_index, current] = values
            .try_into()
            .map_err(|_| redis_type_error("transaction script returned wrong arity"))?;

        if value_to_bool(applied)? {
            return Ok(RemoteTransactionUpdate::Applied);
        }

        let index = value_to_usize(conflict_index)?;
        if index == 0 || index > root_conditions.len() {
            return Err(redis_type_error(format!(
                "transaction script returned out-of-range conflict index: {index}"
            )));
        }
        let condition = &root_conditions[index - 1];
        Ok(RemoteTransactionUpdate::Conflict(
            RemoteTransactionConflict::new(
                condition.name.clone(),
                condition.expected.clone(),
                value_to_optional_bytes(current)?,
            ),
        ))
    }

    fn redis_type_error(detail: impl Into<String>) -> RedisError {
        (
            ErrorKind::TypeError,
            "unexpected Redis adapter response",
            detail.into(),
        )
            .into()
    }

    const DEFAULT_KEY_PREFIX: &[u8] = b"prolly:";
    const DEFAULT_READ_PARALLELISM: usize = 16;
    const SCAN_COUNT: usize = 1024;
    const DELETE_CHUNK_SIZE: usize = 512;

    const NODE_FAMILY: &[u8] = b"node:";
    const ROOT_FAMILY: &[u8] = b"root:";
    const HINT_FAMILY: &[u8] = b"hint:";

    /// Recommended key prefix for immutable node values.
    pub const NODE_KEY_PREFIX: &str = "prolly:node:";
    /// Recommended key prefix for named root manifests.
    pub const ROOT_KEY_PREFIX: &str = "prolly:root:";
    /// Recommended key prefix for hints.
    pub const HINT_KEY_PREFIX: &str = "prolly:hint:";

    const ROOT_CAS_LUA: &str = r#"
local current = redis.call('GET', KEYS[1])
local has_expected = ARGV[1]
local expected = ARGV[2]
local has_new = ARGV[3]
local new_value = ARGV[4]

if has_expected == '1' then
  if current == false or current ~= expected then
    return {0, current}
  end
else
  if current ~= false then
    return {0, current}
  end
end

if has_new == '1' then
  redis.call('SET', KEYS[1], new_value)
else
  redis.call('DEL', KEYS[1])
end

return {1, false}
"#;

    const TRANSACTION_COMMIT_LUA: &str = r#"
local condition_count = tonumber(ARGV[1])
local node_write_count = tonumber(ARGV[2])
local root_write_count = tonumber(ARGV[3])
local arg_index = 4

for i = 1, condition_count do
  local current = redis.call('GET', KEYS[i])
  local has_expected = ARGV[arg_index]
  local expected = ARGV[arg_index + 1]
  arg_index = arg_index + 2

  if has_expected == '1' then
    if current == false or current ~= expected then
      return {0, i, current}
    end
  else
    if current ~= false then
      return {0, i, current}
    end
  end
end

local node_key_offset = condition_count
for i = 1, node_write_count do
  local kind = ARGV[arg_index]
  arg_index = arg_index + 1
  local key = KEYS[node_key_offset + i]

  if kind == 'upsert' then
    redis.call('SET', key, ARGV[arg_index])
    arg_index = arg_index + 1
  elseif kind == 'delete' then
    redis.call('DEL', key)
  else
    error('unknown transaction node op: ' .. tostring(kind))
  end
end

local root_key_offset = condition_count + node_write_count
for i = 1, root_write_count do
  local kind = ARGV[arg_index]
  arg_index = arg_index + 1
  local key = KEYS[root_key_offset + i]

  if kind == 'put' then
    redis.call('SET', key, ARGV[arg_index])
    arg_index = arg_index + 1
  elseif kind == 'delete' then
    redis.call('DEL', key)
  else
    error('unknown transaction root op: ' .. tostring(kind))
  end
end

return {1, 0, false}
"#;
}

pub use redis::*;