-
Notifications
You must be signed in to change notification settings - Fork 4
/
redis.pl
1442 lines (1270 loc) · 47.5 KB
/
redis.pl
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Part of SWI-Prolog
Author: Jan Wielemaker and Sean Charles
E-mail: [email protected] and <sean at objitsu dot com>
WWW: http://www.swi-prolog.org
Copyright (c) 2013-2024, Sean Charles
SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
NOTE
The original code was subject to the MIT licence and written by
Sean Charles. Re-licenced to standard SWI-Prolog BSD-2 with
permission from Sean Charles.
*/
:- module(redis,
[ redis_server/3, % +Alias, +Address, +Options
redis_connect/1, % -Connection
redis_connect/3, % -Connection, +Host, +Port
redis_disconnect/1, % +Connection
redis_disconnect/2, % +Connection, +Options
% Queries
redis/1, % +Request
redis/2, % +Connection, +Request
redis/3, % +Connection, +Request, -Reply
% High level queries
redis_get_list/3, % +Redis, +Key, -List
redis_get_list/4, % +Redis, +Key, +ChunkSize, -List
redis_set_list/3, % +Redis, +Key, +List
redis_get_hash/3, % +Redis, +Key, -Data:dict
redis_set_hash/3, % +Redis, +Key, +Data:dict
redis_scan/3, % +Redis, -LazyList, +Options
redis_sscan/4, % +Redis, +Set, -LazyList, +Options
redis_hscan/4, % +Redis, +Hash, -LazyList, +Options
redis_zscan/4, % +Redis, +Set, -LazyList, +Options
% Publish/Subscribe
redis_subscribe/4, % +Redis, +Channels, -Id, +Options
redis_subscribe/2, % +Id, +Channels
redis_unsubscribe/2, % +Id, +Channels
redis_current_subscription/2, % ?Id,?Channels
redis_write/2, % +Redis, +Command
redis_read/2, % +Redis, -Reply
% Building blocks
redis_array_dict/3, % ?Array, ?Tag, ?Dict
% Admin stuff
redis_property/2, % +Reply, ?Property
redis_current_command/2, % +Redis,?Command
redis_current_command/3, % +Redis, +Command, -Properties
sentinel_slave/4 % +ServerId, +Pool, -Slave, +Options
]).
:- autoload(library(socket), [tcp_connect/3]).
:- autoload(library(apply), [maplist/2, convlist/3, maplist/3, maplist/5]).
:- autoload(library(broadcast), [broadcast/1]).
:- autoload(library(error),
[ must_be/2,
type_error/2,
instantiation_error/1,
uninstantiation_error/1,
existence_error/2,
existence_error/3
]).
:- autoload(library(lazy_lists), [lazy_list/2]).
:- autoload(library(lists), [append/3, member/2]).
:- autoload(library(option), [merge_options/3, option/2,
option/3, select_option/4]).
:- autoload(library(pairs), [group_pairs_by_key/2]).
:- autoload(library(time), [call_with_time_limit/2]).
:- use_module(library(debug), [debug/3, assertion/1]).
:- use_module(library(settings), [setting/4, setting/2]).
:- if(exists_source(library(ssl))).
:- autoload(library(ssl), [ssl_context/3, ssl_negotiate/5]).
:- endif.
:- use_foreign_library(foreign(redis4pl)).
:- setting(max_retry_count, nonneg, 8640, % one day
"Max number of retries").
:- setting(max_retry_wait, number, 10,
"Max time to wait between recovery attempts").
:- setting(sentinel_timeout, number, 0.2,
"Time to wait for a sentinel").
:- predicate_options(redis_server/3, 3,
[ pass_to(redis:redis_connect/3, 3)
]).
:- predicate_options(redis_connect/3, 3,
[ reconnect(boolean),
user(atom),
password(atomic),
version(between(2,3))
]).
:- predicate_options(redis_disconnect/2, 2,
[ force(boolean)
]).
:- predicate_options(redis_scan/3, 3,
[ match(atomic),
count(nonneg),
type(atom)
]).
% Actually not passing, but the same
:- predicate_options(redis_sscan/4, 4, [pass_to(redis:redis_scan/3, 3)]).
:- predicate_options(redis_hscan/4, 4, [pass_to(redis:redis_scan/3, 3)]).
:- predicate_options(redis_zscan/4, 4, [pass_to(redis:redis_scan/3, 3)]).
/** <module> Redis client
This library is a client to [Redis](https://redis.io), a popular key
value store to deal with caching and communication between micro
services.
In the typical use case we register the details of one or more Redis
servers using redis_server/3. Subsequenly, redis/2-3 is used to issue
commands on the server. For example:
```
?- redis_server(default, redis:6379, [password("secret")]).
?- redis(default, set(user, "Bob")).
?- redis(default, get(user), User).
User = "Bob"
```
*/
:- dynamic server/3.
:- dynamic ( connection/2, % ServerName, Stream
sentinel/2 % Pool, Address
) as volatile.
%! redis_server(+ServerName, +Address, +Options) is det.
%
% Register a redis server without connecting to it. The ServerName
% acts as a lazy connection alias. Initially the ServerName `default`
% points at `localhost:6379` with no connect options. The `default`
% server is used for redis/1 and redis/2 and may be changed using this
% predicate. Options are described with redis_connect/3.
%
% Connections established this way are by default automatically
% reconnected if the connection is lost for some reason unless a
% reconnect(false) option is specified.
redis_server(Alias, Address, Options) :-
must_be(ground, Alias),
retractall(server(Alias, _, _)),
asserta(server(Alias, Address, Options)).
server(default, localhost:6379, []).
%! redis_connect(-Connection) is det.
%! redis_connect(+Address, -Connection, +Options) is det.
%! redis_connect(-Connection, +Host, +Port) is det.
%
% Connect to a redis server. The main mode is redis_connect(+Address,
% -Connection, +Options). redis_connect/1 is equivalent to
% redis_connect(localhost:6379, Connection, []). Options:
%
% - reconnect(+Boolean)
% If `true`, try to reconnect to the service when the connection
% seems lost. Default is `true` for connections specified using
% redis_server/3 and `false` for explictly opened connections.
% - user(+User)
% If version(3) and password(Password) are specified, these
% are used to authenticate using the `HELLO` command.
% - password(+Password)
% Authenticate using Password
% - version(+Version)
% Specify the connection protocol version. Initially this is
% version 2. Redis 6 also supports version 3. When specified
% as `3`, the `HELLO` command is used to upgrade the protocol.
% - tls(true)
% When specified, initiate a TLS connection. If this option is
% specified we must also specify the `cacert`, `key` and `cert`
% options.
% - cacert(+File)
% CA Certificate file to verify with.
% - cert(+File)
% Client certificate to authenticate with.
% - key(+File)
% Private key file to authenticate with.
% - sentinels(+ListOfAddresses)
% Used together with an Address of the form sentinel(MasterName)
% to enable contacting a network of Redis servers guarded by a
% sentinel network.
% - sentinel_user(+User)
% - sentinel_password(+Password)
% Authentication information for the senitels. When omitted we
% try to connect withour authentication.
%
% Instead of using these predicates, redis/2 and redis/3 are normally
% used with a _server name_ argument registered using redis_server/3.
% These predicates are meant for creating a temporary paralel
% connection or using a connection with a _blocking_ call.
%
% @compat redis_connect(-Connection, +Host, +Port) provides
% compatibility to the original GNU-Prolog interface and is equivalent
% to redis_connect(Host:Port, Connection, []).
%
% @arg Address is a term Host:Port, unix(File) or the name of a server
% registered using redis_server/3. The latter realises a _new_
% connection that is typically used for blocking redis commands such
% as listening for published messages, waiting on a list or stream.
redis_connect(Conn) :-
redis_connect(default, Conn, []).
redis_connect(Conn, Host, Port) :-
var(Conn),
ground(Host), ground(Port),
!, % GNU-Prolog compatibility
redis_connect(Host:Port, Conn, []).
redis_connect(Server, Conn, Options) :-
atom(Server),
!,
( server(Server, Address, DefaultOptions)
-> merge_options(Options, DefaultOptions, Options2),
do_connect(Server, Address, Conn, [address(Address)|Options2])
; existence_error(redis_server, Server)
).
redis_connect(Address, Conn, Options) :-
do_connect(Address, Address, Conn, [address(Address)|Options]).
%! do_connect(+Id, +Address, -Conn, +Options)
%
% Open the connection. A connection is a compound term of the shape
%
% redis_connection(Id, Stream, Failures, Options)
do_connect(Id, sentinel(Pool), Conn, Options) =>
sentinel_master(Id, Pool, Conn, Options).
do_connect(Id, Address0, Conn, Options) =>
tcp_address(Address0, Address),
tcp_connect(Address, Stream0, Options),
tls_upgrade(Address, Stream0, Stream, Options),
Conn = redis_connection(Id, Stream, 0, Options),
hello(Conn, Options).
tcp_address(unix(Path), Path) :-
!. % Using an atom is ambiguous
tcp_address(Address, Address).
%! tls_upgrade(+Address, +Raw, -Stream, +Options) is det.
%
% Upgrade to a TLS connection when tls(true) is specified.
:- if(current_predicate(ssl_context/3)).
tls_upgrade(Host:_Port, Raw, Stream, Options) :-
option(tls(true), Options),
!,
must_have_option(cacert(CacertFile), Options),
must_have_option(key(KeyFile), Options),
must_have_option(cert(CertFile), Options),
ssl_context(client, SSL,
[ host(Host),
certificate_file(CertFile),
key_file(KeyFile),
cacerts([file(CacertFile)]),
cert_verify_hook(tls_verify),
close_parent(true)
]),
stream_pair(Raw, RawRead, RawWrite),
ssl_negotiate(SSL, RawRead, RawWrite, Read, Write),
stream_pair(Stream, Read, Write).
:- endif.
tls_upgrade(_, Stream, Stream, _).
:- if(current_predicate(ssl_context/3)).
%! tls_verify(+SSL, +ProblemCert, +AllCerts, +FirstCert, +Status) is semidet.
%
% Accept or reject the certificate verification. Similar to the
% Redis command line client (``redis-cli``), we accept the
% certificate as long as it is signed, not verifying the hostname.
:- public tls_verify/5.
tls_verify(_SSL, _ProblemCert, _AllCerts, _FirstCert, verified) :-
!.
tls_verify(_SSL, _ProblemCert, _AllCerts, _FirstCert, hostname_mismatch) :-
!.
tls_verify(_SSL, _ProblemCert, _AllCerts, _FirstCert, _Error) :-
fail.
:- endif.
%! sentinel_master(+ServerId, +SentinelPool, -Connection, +Options) is det.
%
% Discover the master and connect to it.
sentinel_master(Id, Pool, Master, Options) :-
sentinel_connect(Id, Pool, Conn, Options),
setting(sentinel_timeout, TMO),
call_cleanup(
query_sentinel(Pool, Conn, MasterAddr),
redis_disconnect(Conn)),
debug(redis(sentinel), 'Sentinel claims master is at ~p', [MasterAddr]),
do_connect(Id, MasterAddr, Master, Options),
debug(redis(sentinel), 'Connected to claimed master', []),
redis(Master, role, Role),
( Role = [master|_Slaves]
-> debug(redis(sentinel), 'Verified role at ~p', [MasterAddr])
; redis_disconnect(Master),
debug(redis(sentinel), '~p is not the master: ~p', [MasterAddr, Role]),
sleep(TMO),
sentinel_master(Id, Pool, Master, Options)
).
sentinel_connect(Id, Pool, Conn, Options) :-
must_have_option(sentinels(Sentinels), Options),
sentinel_auth(Options, Options1),
setting(sentinel_timeout, TMO),
( sentinel(Pool, Sentinel)
; member(Sentinel, Sentinels)
),
catch(call_with_time_limit(
TMO,
do_connect(Id, Sentinel, Conn,
[sentinel(true)|Options1])),
Error,
(print_message(warning, Error),fail)),
!,
debug(redis(sentinel), 'Connected to sentinel at ~p', [Sentinel]),
redis(Conn, sentinel(sentinels, Pool), Peers),
transaction(update_known_sentinels(Pool, Sentinel, Peers)).
sentinel_auth(Options0, Options) :-
option(sentinel_user(User), Options0),
option(sentinel_password(Passwd), Options0),
!,
merge_options([user(User), password(Passwd)], Options0, Options).
sentinel_auth(Options0, Options) :-
select_option(password(_), Options0, Options, _).
query_sentinel(Pool, Conn, Host:Port) :-
redis(Conn, sentinel('get-master-addr-by-name', Pool), MasterData),
MasterData = [Host,Port].
update_known_sentinels(Pool, Sentinel, Peers) :-
retractall(sentinel(Pool, _)),
maplist(update_peer_sentinel(Pool), Peers),
asserta(sentinel(Pool, Sentinel)).
update_peer_sentinel(Pool, Attrs),
memberchk(ip-Host, Attrs),
memberchk(port-Port, Attrs) =>
asserta(sentinel(Pool, Host:Port)).
must_have_option(Opt, Options) :-
option(Opt, Options),
!.
must_have_option(Opt, Options) :-
existence_error(option, Opt, Options).
%! sentinel_slave(+ServerId, +Pool, -Slave, +Options) is nondet.
%
% True when Slave is a slave server in the sentinel cluster. Slave is
% a dict holding the keys and values as described by the Redis command
%
% SENTINEL SLAVES mastername
sentinel_slave(ServerId, Pool, Slave, Options) :-
sentinel_connect(ServerId, Pool, Conn, Options),
redis(Conn, sentinel(slaves, Pool), Slaves),
member(Pairs, Slaves),
dict_create(Slave, redis, Pairs).
%! hello(+Connection, +Option)
%
% Initialize the connection. This is used to upgrade to the RESP3
% protocol and/or to authenticate.
hello(Con, Options) :-
option(version(V), Options),
V >= 3,
!,
( option(user(User), Options),
option(password(Password), Options)
-> redis(Con, hello(3, auth, User, Password))
; redis(Con, hello(3))
).
hello(Con, Options) :-
option(password(Password), Options),
!,
redis(Con, auth(Password)).
hello(_, _).
%! redis_stream(+Spec, --Stream, +DoConnect) is det.
%
% Get the stream to a Redis server from Spec. Spec is either the name
% of a registered server or a term
% redis_connection(Id,Stream,Failures,Options). If the stream is
% disconnected it will be reconnected.
redis_stream(Var, S, _) :-
( var(Var)
-> !, instantiation_error(Var)
; nonvar(S)
-> !, uninstantiation_error(S)
).
redis_stream(ServerName, S, Connect) :-
atom(ServerName),
!,
( connection(ServerName, S0)
-> S = S0
; Connect == true,
server(ServerName, Address, Options)
-> redis_connect(Address, Connection, Options),
redis_stream(Connection, S, false),
asserta(connection(ServerName, S))
; existence_error(redis_server, ServerName)
).
redis_stream(redis_connection(_,S0,_,_), S, _) :-
S0 \== (-),
!,
S = S0.
redis_stream(Redis, S, _) :-
Redis = redis_connection(Id,-,_,Options),
option(address(Address), Options),
do_connect(Id,Address,Redis2,Options),
arg(2, Redis2, S0),
nb_setarg(2, Redis, S0),
S = S0.
has_redis_stream(Var, _) :-
var(Var),
!,
instantiation_error(Var).
has_redis_stream(Alias, S) :-
atom(Alias),
!,
connection(Alias, S).
has_redis_stream(redis_connection(_,S,_,_), S) :-
S \== (-).
%! redis_disconnect(+Connection) is det.
%! redis_disconnect(+Connection, +Options) is det.
%
% Disconnect from a redis server. The second form takes one option,
% similar to close/2:
%
% - force(Force)
% When `true` (default `false`), do not raise any errors if
% Connection does not exist or closing the connection raises
% a network or I/O related exception. This version is used
% internally if a connection is in a broken state, either due
% to a protocol error or a network issue.
redis_disconnect(Redis) :-
redis_disconnect(Redis, []).
redis_disconnect(Redis, Options) :-
option(force(true), Options),
!,
( Redis = redis_connection(_Id, S, _, _Opts)
-> ( S == (-)
-> true
; close(S, [force(true)]),
nb_setarg(2, Redis, -)
)
; has_redis_stream(Redis, S)
-> close(S, [force(true)]),
retractall(connection(_,S))
; true
).
redis_disconnect(Redis, _Options) :-
redis_stream(Redis, S, false),
close(S),
retractall(connection(_,S)).
%! redis(+Connection, +Request) is semidet.
%
% This predicate is overloaded to handle two types of requests. First,
% it is a shorthand for `redis(Connection, Command, _)` and second, it
% can be used to exploit Redis _pipelines_ and _transactions_. The
% second form is acticated if Request is a _list_. In that case, each
% element of the list is either a term `Command -> Reply` or a simple
% `Command`. Semantically this represents a sequence of redis/3 and
% redis/2 calls. It differs in the following aspects:
%
% - All commands are sent in one batch, after which all replies are
% read. This reduces the number of _round trips_ and typically
% greatly improves performance.
% - If the first command is `multi` and the last `exec`, the
% commands are executed as a Redis _transaction_, i.e., they
% are executed _atomically_.
% - If one of the commands returns an error, the subsequent commands
% __are still executed__.
% - You can not use variables from commands earlier in the list for
% commands later in the list as a result of the above execution
% order.
%
% Procedurally, the process takes the following steps:
%
% 1. Send all commands
% 2. Read all replies and push messages
% 3. Handle all callbacks from push messages
% 4. Check whether one of the replies is an error. If so,
% raise this error (subsequent errors are lost)
% 5. Bind all replies for the `Command -> Reply` terms.
%
% Examples
%
% ```
% ?- redis(default,
% [ lpush(li,1),
% lpush(li,2),
% lrange(li,0,-1) -> List
% ]).
% List = ["2", "1"].
% ```
redis(Redis, PipeLine) :-
is_list(PipeLine),
!,
redis_pipeline(Redis, PipeLine).
redis(Redis, Req) :-
redis(Redis, Req, _).
%! redis(+Connection, +Command, -Reply) is semidet.
%
% Execute a redis Command on Connnection. Next, bind Reply to the
% returned result. Command is a callable term whose functor is the
% name of the Redis command and whose arguments are translated to
% Redis arguments according to the rules below. Note that all text is
% always represented using UTF-8 encoding.
%
% - Atomic values are emitted verbatim
% - A term A:B:... where all arguments are either atoms,
% strings or integers (__no floats__) is translated into
% a string `"A:B:..."`. This is a common shorthand for
% representing Redis keys.
% - A term Term as prolog is emitted as "\u0000T\u0000" followed
% by Term in canonical form.
% - Any other term is emitted as write/1.
%
% Reply is either a plain term (often a variable) or a term `Value as
% Type`. In the latter form, `Type` dictates how the Redis _bulk_
% reply is translated to Prolog. The default equals to `auto`, i.e.,
% as a number of the content satisfies the Prolog number syntax and
% as an atom otherwise.
%
% - status(Atom)
% Returned if the server replies with ``+ Status``. Atom
% is the textual value of `Status` converted to lower case,
% e.g., status(ok) or status(pong).
% - `nil`
% This atom is returned for a NIL/NULL value. Note that if
% the reply is only `nil`, redis/3 _fails_. The `nil` value
% may be embedded inside lists or maps.
% - A number
% Returned if the server replies an integer (":Int"), double
% (",Num") or big integer ("(Num")
% - A string
% Returned on a _bulk_ reply. Bulk replies are supposed to be
% in UTF-8 encoding. The the bulk reply starts with
% "\u0000T\u0000" it is supposed to be a Prolog term.
% Note that this intepretation means it is __not__ possible
% to read arbitrary binary blobs.
% - A list of replies. A list may also contain `nil`. If Reply
% as a whole would be `nil` the call fails.
% - A list of _pairs_. This is returned for the redis version 3
% protocol "%Map". Both the key and value respect the same
% rules as above.
%
% Redis _bulk_ replies are translated depending on the `as` `Type` as
% explained above.
%
% - string
% - string(Encoding)
% Create a SWI-Prolog string object interpreting the blob as
% following Encoding. Encoding is a restricted set of SWI-Prolog's
% encodings: `bytes` (`iso_latin_1`), `utf8` and `text` (the
% current locale translation).
% - atom
% - atom(Encoding)
% As above, producing an atom.
% - codes
% - codes(Encoding)
% As above, producing a list of integers (Unicode code points)
% - chars
% - chars(Encoding)
% As above, producing a list of one-character atoms.
% - integer
% - float
% - rational
% - number
% Interpret the bytes as a string representing a number. If
% the string does not represent a number of the requested type
% a type_error(Type, String) is raised.
% - tagged_integer
% Same as integer, but demands the value to be between the Prolog
% flags `min_tagged_integer` and `max_tagged_integer`, allowing
% the value to be used as a dict key.
% - auto
% Same as auto(atom, number)
% - auto(AsText,AsNumber)
% If the bulk string confirms the syntax of AsNumber, convert
% the value to the requested numberical type. Else convert
% the value to text according to AsText. This is similar to
% the Prolog predicate name/2.
% - dict_key
% Alias for auto(atom,tagged_integer). This allows the value
% to be used as a key for a SWI-Prolog dict.
% - pairs(AsKey, AsValue)
% Convert a map or array of even length into pairs for which the
% key satisfies AsKey and the value AsValue. The `pairs` type
% can also be applied to a Redis array. In this case the array
% length must be even. This notably allows fetching a Redis
% _hash_ as pairs using ``HGETALL`` using version 2 of the
% Redis protocol.
% - dict(AsKey, AsValue)
% Similar to pairs(AsKey, AsValue), but convert the resulting
% pair list into a SWI-Prolog dict. AsKey must convert to a
% valid dict key, i.e., an atom or tagged integer. See `dict_key`.
% - dict(AsValue)
% Shorthand for dict(dict_key, AsValue).
%
% Here are some simple examples
%
% ```
% ?- redis(default, set(a, 42), X).
% X = status("OK").
% ?- redis(default, get(a), X).
% X = "42".
% ?- redis(default, get(a), X as integer).
% X = 42.
% ?- redis(default, get(a), X as float).
% X = 42.0.
% ?- redis(default, set(swipl:version, 8)).
% true.
% ?- redis(default, incr(swipl:version), X).
% X = 9.
% ```
%
% @error redis_error(Code, String)
redis(Redis, Req, Out) :-
out_val(Out, Val),
redis1(Redis, Req, Out),
Val \== nil.
out_val(Out, Val) :-
( nonvar(Out),
Out = (Val as _)
-> true
; Val = Out
).
redis1(Redis, Req, Out) :-
Error = error(Formal, _),
catch(redis2(Redis, Req, Out), Error, true),
( var(Formal)
-> true
; recover(Error, Redis, redis1(Redis, Req, Out))
).
redis2(Redis, Req, Out) :-
atom(Redis),
!,
redis_stream(Redis, S, true),
with_mutex(Redis,
( redis_write_msg(S, Req),
redis_read_stream(Redis, S, Out)
)).
redis2(Redis, Req, Out) :-
redis_stream(Redis, S, true),
redis_write_msg(S, Req),
redis_read_stream(Redis, S, Out).
%! redis_pipeline(+Redis, +PipeLine)
redis_pipeline(Redis, PipeLine) :-
Error = error(Formal, _),
catch(redis_pipeline2(Redis, PipeLine), Error, true),
( var(Formal)
-> true
; recover(Error, Redis, redis_pipeline(Redis, PipeLine))
).
redis_pipeline2(Redis, PipeLine) :-
atom(Redis),
!,
redis_stream(Redis, S, true),
with_mutex(Redis,
redis_pipeline3(Redis, S, PipeLine)).
redis_pipeline2(Redis, PipeLine) :-
redis_stream(Redis, S, true),
redis_pipeline3(Redis, S, PipeLine).
redis_pipeline3(Redis, S, PipeLine) :-
maplist(write_pipeline(S), PipeLine),
flush_output(S),
read_pipeline(Redis, S, PipeLine).
write_pipeline(S, Command -> _Reply) :-
!,
redis_write_msg_no_flush(S, Command).
write_pipeline(S, Command) :-
redis_write_msg_no_flush(S, Command).
read_pipeline(Redis, S, PipeLine) :-
E = error(Formal,_),
catch(read_pipeline2(Redis, S, PipeLine), E, true),
( var(Formal)
-> true
; reconnect_error(E)
-> redis_disconnect(Redis, [force(true)]),
throw(E)
; resync(Redis),
throw(E)
).
read_pipeline2(Redis, S, PipeLine) :-
maplist(redis_read_msg3(S), PipeLine, Replies, Errors, Pushed),
maplist(handle_push(Redis), Pushed),
maplist(handle_error, Errors),
maplist(bind_reply, PipeLine, Replies).
redis_read_msg3(S, _Command -> ReplyIn, Reply, Error, Push) :-
!,
redis_read_msg(S, ReplyIn, Reply, Error, Push).
redis_read_msg3(S, Var, Reply, Error, Push) :-
redis_read_msg(S, Var, Reply, Error, Push).
handle_push(Redis, Pushed) :-
handle_push_messages(Pushed, Redis).
handle_error(Error) :-
( var(Error)
-> true
; throw(Error)
).
bind_reply(_Command -> Reply0, Reply) :-
!,
Reply0 = Reply.
bind_reply(_Command, _).
%! recover(+Error, +Redis, :Goal)
%
% Error happened while running Goal on Redis. If this is a recoverable
% error (i.e., a network or disconnected peer), wait a little and try
% running Goal again.
:- meta_predicate recover(+, +, 0).
recover(Error, Redis, Goal) :-
Error = error(Formal, _),
reconnect_error(Formal),
auto_reconnect(Redis),
!,
debug(redis(recover), '~p: got error ~p; trying to reconnect',
[Redis, Error]),
redis_disconnect(Redis, [force(true)]),
( wait_to_retry(Redis, Error)
-> call(Goal),
retractall(failure(Redis, _))
; throw(Error)
).
recover(Error, _, _) :-
throw(Error).
auto_reconnect(redis_connection(_,_,_,Options)) :-
!,
option(reconnect(true), Options).
auto_reconnect(Server) :-
ground(Server),
server(Server, _, Options),
option(reconnect(true), Options, true).
reconnect_error(io_error(_Action, _On)).
reconnect_error(socket_error(_Code, _)).
reconnect_error(syntax_error(unexpected_eof)).
reconnect_error(existence_error(stream, _)).
%! wait(+Redis, +Error)
%
% Wait for some time after a failure. First we wait for 10ms. This is
% doubled on each failure upto the setting `max_retry_wait`. If the
% setting `max_retry_count` is exceeded we fail and the called signals
% an exception.
:- dynamic failure/2 as volatile.
wait_to_retry(Redis, Error) :-
redis_failures(Redis, Failures),
setting(max_retry_count, Count),
Failures < Count,
Failures2 is Failures+1,
redis_set_failures(Redis, Failures2),
setting(max_retry_wait, MaxWait),
Wait is min(MaxWait*100, 1<<Failures)/100.0,
debug(redis(recover), ' Sleeping ~p seconds', [Wait]),
retry_message_level(Failures, Level),
print_message(Level, redis(retry(Redis, Failures, Wait, Error))),
sleep(Wait).
redis_failures(redis_connection(_,_,Failures0,_), Failures) :-
!,
Failures = Failures0.
redis_failures(Server, Failures) :-
atom(Server),
( failure(Server, Failures)
-> true
; Failures = 0
).
redis_set_failures(Connection, Count) :-
compound(Connection),
!,
nb_setarg(3, Connection, Count).
redis_set_failures(Server, Count) :-
atom(Server),
retractall(failure(Server, _)),
asserta(failure(Server, Count)).
retry_message_level(0, warning) :- !.
retry_message_level(_, silent).
%! redis(+Request)
%
% Connect to the default redis server, call redist/3 using Request,
% disconnect and print the result. This predicate is intended for
% interactive usage.
redis(Req) :-
setup_call_cleanup(
redis_connect(default, C, []),
redis1(C, Req, Out),
redis_disconnect(C)),
print(Out).
%! redis_write(+Redis, +Command) is det.
%! redis_read(+Redis, -Reply) is det.
%
% Write command and read replies from a Redis server. These are
% building blocks for subscribing to event streams.
redis_write(Redis, Command) :-
redis_stream(Redis, S, true),
redis_write_msg(S, Command).
redis_read(Redis, Reply) :-
redis_stream(Redis, S, true),
redis_read_stream(Redis, S, Reply).
/*******************************
* HIGH LEVEL ACCESS *
*******************************/
%! redis_get_list(+Redis, +Key, -List) is det.
%! redis_get_list(+Redis, +Key, +ChunkSize, -List) is det.
%
% Get the content of a Redis list in List. If ChunkSize is given and
% smaller than the list length, List is returned as a _lazy list_. The
% actual values are requested using redis ``LRANGE`` requests. Note
% that this results in O(N^2) complexity. Using a lazy list is most
% useful for relatively short lists holding possibly large items.
%
% Note that values retrieved are _strings_, unless the value was added
% using `Term as prolog`.
%
% It seems possible for ``LLEN`` to return ``OK``. I don't know why.
% As a work-around we return the empty list rather than an error.
%
% @see lazy_list/2 for a discussion on the difference between lazy
% lists and normal lists.
redis_get_list(Redis, Key, List) :-
redis_get_list(Redis, Key, -1, List).
redis_get_list(Redis, Key, Chunk, List) :-
redis(Redis, llen(Key), Len),
( Len == status(ok)
-> List = []
; ( Chunk >= Len
; Chunk == -1
)
-> ( Len == 0
-> List = []
; End is Len-1,
list_range(Redis, Key, 0, End, List)
)
; lazy_list(rlist_next(s(Redis,Key,0,Chunk,Len)), List)
).
rlist_next(State, List, Tail) :-
State = s(Redis,Key,Offset,Slice,Len),
End is min(Len-1, Offset+Slice-1),
list_range(Redis, Key, Offset, End, Elems),
( End =:= Len-1
-> List = Elems,
Tail = []
; Offset2 is Offset+Slice,
nb_setarg(3, State, Offset2),
append(Elems, Tail, List)
).
% Redis LRANGE demands End > Start and returns inclusive.
list_range(DB, Key, Start, Start, [Elem]) :-
!,
redis(DB, lindex(Key, Start), Elem).
list_range(DB, Key, Start, End, List) :-
!,
redis(DB, lrange(Key, Start, End), List).
%! redis_set_list(+Redis, +Key, +List) is det.
%
% Associate a Redis key with a list. As Redis has no concept of an
% empty list, if List is `[]`, Key is _deleted_. Note that key values
% are always strings in Redis. The same conversion rules as for
% redis/1-3 apply.
redis_set_list(Redis, Key, List) :-
redis(Redis, del(Key), _),
( List == []
-> true
; Term =.. [rpush,Key|List],
redis(Redis, Term, _Count)
).
%! redis_get_hash(+Redis, +Key, -Data:dict) is det.
%! redis_set_hash(+Redis, +Key, +Data:dict) is det.
%
% Put/get a Redis hash as a Prolog dict. Putting a dict first deletes
% Key. Note that in many cases applications will manage Redis hashes
% by key. redis_get_hash/3 is notably a user friendly alternative to
% the Redis ``HGETALL`` command. If the Redis hash is not used by
% other (non-Prolog) applications one may also consider using the
% `Term as prolog` syntax to store the Prolog dict as-is.
redis_get_hash(Redis, Key, Dict) :-
redis(Redis, hgetall(Key), Dict as dict(auto)).
redis_set_hash(Redis, Key, Dict) :-
redis_array_dict(Array, _, Dict),
Term =.. [hset,Key|Array],
redis(Redis, del(Key), _),
redis(Redis, Term, _Count).
%! redis_array_dict(?Array, ?Tag, ?Dict) is det.
%
% Translate a Redis reply representing hash data into a SWI-Prolog
% dict. Array is either a list of alternating keys and values or a
% list of _pairs_. When translating to an array, this is always a list
% of alternating keys and values.
%
% @arg Tag is the SWI-Prolog dict tag.
redis_array_dict(Array, Tag, Dict) :-
nonvar(Array),
!,
array_to_pairs(Array, Pairs),
dict_pairs(Dict, Tag, Pairs).
redis_array_dict(TwoList, Tag, Dict) :-
dict_pairs(Dict, Tag, Pairs),
pairs_to_array(Pairs, TwoList).
array_to_pairs([], []) :-
!.
array_to_pairs([NameS-Value|T0], [Name-Value|T]) :-
!, % RESP3 returns a map as pairs.
atom_string(Name, NameS),
array_to_pairs(T0, T).
array_to_pairs([NameS,Value|T0], [Name-Value|T]) :-
atom_string(Name, NameS),
array_to_pairs(T0, T).