-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtokyo-tyrant.cl
More file actions
926 lines (852 loc) · 47.8 KB
/
Copy pathtokyo-tyrant.cl
File metadata and controls
926 lines (852 loc) · 47.8 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
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
(in-package "TOKYO-TYRANT")
(defconstant *ESUCCESS* 0 "error code: success")
(defconstant *EINVALID* 1 "error code: invalid operation")
(defconstant *ENOHOST* 2 "error code: host not found")
(defconstant *EREFUSED* 3 "error code: connection refused")
(defconstant *ESEND* 4 "error code: send error")
(defconstant *ERECV* 5 "error code: recv error")
(defconstant *EKEEP* 6 "error code: existing record")
(defconstant *ENOREC* 7 "error code: no record found")
(defconstant *EMISC* 9999 "error code: miscellaneous error")
(defconstant *XOLCKREC* 1 "scripting extension option: record locking")
(defconstant *XOLCKGLB* 2 "scripting extension option: global locking")
(defconstant *MONOULOG* 1 "versatile function option: omission of the update log")
(defclass RDB ()
((ecode :accessor ecode :initform *ESUCCESS* :documentation "the last happened error code")
;; (enc :accessor enc :initform nil)
(sock :accessor sock :initform nil)
(tout :accessor tout :initform 0)
))
(defmethod tt-recv ((rdb RDB) length)
(let ((buf (make-string length)))
(format t "~&tt-recv: length:~D" length)
(read-sequence buf (sock rdb))
(format t ",buf:~S~%" buf)
buf))
(defmethod tt-recv-int32 ((rdb RDB))
(unpack-N (tt-recv rdb 4)))
(defmethod tt-recv-int64 ((rdb RDB))
(unpack-Q (tt-recv rdb 8)))
(defmethod tt-recv-code ((rdb RDB))
(handler-case (read-byte (sock rdb))
(error (condition)
(declare (ignore condition))
(setf (ecode rdb) *ERECV*)
nil)))
(defmacro tt-recv-code-cond (rdb code &rest clauses)
`(let ((,code (tokyo-tyrant::tt-recv-code ,rdb)))
(when ,code
(cond ,@clauses))))
(defmethod tt-send ((rdb RDB) fmt &rest fmt-args)
(handler-case (progn
(apply #'format `(t ,(string+ "~&tt-send:" fmt "~%") ,@fmt-args))
(apply #'format `(,(sock rdb) ,fmt ,@fmt-args))
(force-output (sock rdb))
t)
(error (condition)
(declare (ignore condition))
(setf (ecode rdb) *ESEND*)
nil)))
(defmacro when-sock (rdb &rest then-forms)
`(if (tokyo-tyrant::sock ,rdb)
(progn
,@then-forms)
(progn
(setf (tokyo-tyrant::ecode ,rdb) tokyo-tyrant::*EINVALID*)
nil)))
(defmacro unless-sock (rdb &rest else-forms)
`(if (tokyo-tyrant::sock ,rdb)
(progn
(setf (tokyo-tyrant::ecode ,rdb) tokyo-tyrant::*EINVALID*)
nil)
(progn
,@else-forms)))
(defmethod tt-errmsg ((rdb RDB) &optional (ecode nil))
"Get the message string corresponding to an error code.\
`<i>ecode</i>' specifies the error code. If it is not defined or negative, the last happened error code is specified.\
The return value is the message string of the error code."
(let ((e (or ecode (ecode rdb))))
(cond
((eq e *ESUCCESS*) "success")
((eq e *EINVALID*) "invalid operation")
((eq e *ENOHOST*) "host not found")
((eq e *EREFUSED*) "connection refused")
((eq e *ESEND*) "send error")
((eq e *ERECV*) "recv error")
((eq e *EKEEP*) "existing record")
((eq e *ENOREC*) "no record found")
((eq e *EMISC*) "miscellaneous error")
(t "unknown"))))
(defmethod tt-open ((rdb RDB) host &optional (port 0) (timeout 0))
"Open a remote database connection.\
`<i>host</i>' specifies the name or the address of the server.\
`<i>port</i>' specifies the port number. If it is not defined or not more than 0, UNIX domain socket is used and the path of the socket file is specified by the host parameter.\
`<i>timeout</i>' specifies the timeout of each query in seconds. If it is not defined or not more than 0, the timeout is not specified.\
If successful, the return value is true, else, it is false."
(unless-sock rdb
(handler-case (let ((sock (if (> port 0)
(make-socket :remote-host host :remote-port port :address-family :internet :format :bivalent)
(make-socket :remote-filename host :format :bivalent))))
(socket-connect sock)
(setf (sock rdb) sock)
(setf (tout rdb) timeout)
(setf (stream-external-format sock) 'ascii)
t)
(excl:socket-error (condition)
(setf (ecode rdb) (case (stream-error-identifier condition)
((:host-unreachable) *ENOHOST*)
((:connection-refused) *EREFUSED*)
(t *EREFUSED*)))
nil)
(error (condition)
(declare (ignore condition))
(setf (ecode rdb) *EMISC*)
(top-level.debug:zoom *standard-output*)
nil))))
(defmethod tt-close ((rdb RDB))
"Close the database connection.\
If successful, the return value is true, else, it is false."
(when-sock rdb
(handler-case (progn
(close (sock rdb))
(setf (sock rdb) nil)
t)
(error (condition)
(declare (ignore condition))
(setf (ecode rdb) *EMISC*)
(setf (sock rdb) nil)
nil))))
(defmethod tt-put ((rdb RDB) key value)
"Store a record.\
`<i>key</i>' specifies the key.\
`<i>value</i>' specifies the value.\
If successful, the return value is true, else, it is false.\
If a record with the same key exists in the database, it is overwritten."
(when-sock rdb
(when (multiple-value-bind (key-ascii key-length) (string-ascii key)
(multiple-value-bind (value-ascii value-length) (string-ascii value)
(tt-send rdb "~C~C~A~A~A~A" (code-char #xC8) (code-char #x10) (pack-N key-length) (pack-N value-length) key-ascii value-ascii)))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-putkeep ((rdb RDB) key value)
"Store a new record.\
`<i>key</i>' specifies the key.\
`<i>value</i>' specifies the value.\
If successful, the return value is true, else, it is false.\
If a record with the same key exists in the database, this method has no effect."
(when-sock rdb
(when (multiple-value-bind (key-ascii key-length) (string-ascii key)
(multiple-value-bind (value-ascii value-length) (string-ascii value)
(tt-send rdb "~C~C~A~A~A~A" (code-char #xC8) (code-char #x11) (pack-N key-length) (pack-N value-length) key-ascii value-ascii)))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *EKEEP*)
nil)))))
(defmethod tt-putcat ((rdb RDB) key value)
"Concatenate a value at the end of the existing record.\
`<i>key</i>' specifies the key.\
`<i>value</i>' specifies the value.\
If successful, the return value is true, else, it is false.\
If there is no corresponding record, a new record is created."
(when-sock rdb
(when (multiple-value-bind (key-ascii key-length) (string-ascii key)
(multiple-value-bind (value-ascii value-length) (string-ascii value)
(tt-send rdb "~C~C~A~A~A~A" (code-char #xC8) (code-char #x12) (pack-N key-length) (pack-N value-length) key-ascii value-ascii)))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-putshl ((rdb RDB) key value &optional (width 0))
"Concatenate a value at the end of the existing record and shift it to the left.\
`<i>key</i>' specifies the key.\
`<i>value</i>' specifies the value.\
`<i>width</i>' specifies the width of the record.\
If successful, the return value is true, else, it is false.\
If there is no corresponding record, a new record is created."
(when-sock rdb
(when (multiple-value-bind (key-ascii key-length) (string-ascii key)
(multiple-value-bind (value-ascii value-length) (string-ascii value)
(tt-send rdb "~C~C~A~A~A~A~A" (code-char #xC8) (code-char #x13) (pack-N key-length) (pack-N value-length) (pack-N width) key-ascii value-ascii)))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-putnr ((rdb RDB) key value)
"Store a record without response from the server.\
`<i>key</i>' specifies the key.\
`<i>value</i>' specifies the value.\
If successful, the return value is true, else, it is false.\
If a record with the same key exists in the database, it is overwritten."
(when-sock rdb
(multiple-value-bind (key-ascii key-length) (string-ascii key)
(multiple-value-bind (value-ascii value-length) (string-ascii value)
(tt-send rdb "~C~C~A~A~A~A" (code-char #xC8) (code-char #x18) (pack-N key-length) (pack-N value-length) key-ascii value-ascii)))))
(defmethod tt-out ((rdb RDB) key)
"Remove a record.\
`<i>key</i>' specifies the key.\
If successful, the return value is true, else, it is false."
(when-sock rdb
(when (multiple-value-bind (key-ascii key-length) (string-ascii key)
(tt-send rdb "~C~C~A~A" (code-char #xC8) (code-char #x20) (pack-N key-length) key-ascii))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *ENOREC*)
nil)))))
(defmethod tt-get ((rdb RDB) key)
"Retrieve a record.\
`<i>key</i>' specifies the key.\
If successful, the return value is the value of the corresponding record. `nil' is returned if no record corresponds."
(when-sock rdb
(when (multiple-value-bind (key-ascii key-length) (string-ascii key)
(tt-send rdb "~C~C~A~A" (code-char #xC8) (code-char #x30) (pack-N key-length) key-ascii))
(tt-recv-code-cond rdb code
((zerop code)
(or (let ((vsiz (tt-recv-int32 rdb)))
(when (>= vsiz 0)
(let ((vbuf (tt-recv rdb vsiz)))
(when vbuf
(string-external vbuf)))))
(progn
(setf (ecode rdb) *ERECV*)
nil)))
(t
(setf (ecode rdb) *ENOREC*)
nil)))))
(defmethod tt-mget ((rdb RDB) recs)
"Retrieve records.\
`<i>recs</i>' specifies a hash containing the retrieval keys. As a result of this method, keys existing in the database have the corresponding values and keys not existing in the database are removed.\
If successful, the return value is the number of retrieved records or nil on failure."
(unless (hash-table-p recs)
(error "~S is not a valid argument to TT-MGET." recs))
(when-sock rdb
(when (tt-send rdb "~C~C~A~A"
(code-char #xC8)
(code-char #x31)
(pack-N (hash-table-count recs))
(loop for key being the hash-key of recs
collect (multiple-value-bind (key-ascii key-length) (string-ascii key)
(format nil "~A~A" (pack-N key-length) key-ascii)) into keys
finally (return (apply #'string+ keys))))
(tt-recv-code-cond rdb code
((not (zerop code))
(setf (ecode rdb) *ENOREC*)
nil)
(t
(or (let ((rnum (tt-recv-int32 rdb)))
(when (>= rnum 0)
(clrhash recs)
(loop for i from 1 to rnum
for ksiz = (tt-recv-int32 rdb)
for vsiz = (tt-recv-int32 rdb)
when (or (< ksiz 0) (< vsiz 0)) return nil
for kbuf = (tt-recv rdb ksiz)
for vbuf = (tt-recv rdb vsiz)
unless (and kbuf vbuf) return nil
do (setf (gethash (string-external kbuf) recs) (string-external vbuf))
finally (return rnum))))
(progn
(setf (ecode rdb) *ERECV*)
nil)))))))
(defmethod tt-vsiz ((rdb RDB) key)
"Get the size of the value of a record.\
`<i>key</i>' specifies the key.\
If successful, the return value is the size of the value of the corresponding record, else, it is nil."
(when-sock rdb
(when (multiple-value-bind (key-ascii key-length) (string-ascii key)
(tt-send rdb "~C~C~A~A" (code-char #xC8) (code-char #x38) (pack-N key-length) key-ascii))
(tt-recv-code-cond rdb code
((zerop code)
(tt-recv-int32 rdb))
(t
(setf (ecode rdb) *ENOREC*)
nil)))))
(defmethod tt-iterinit ((rdb RDB))
"Initialize the iterator.\
If successful, the return value is true, else, it is false.\
The iterator is used in order to access the key of every record stored in a database."
(when-sock rdb
(when (tt-send rdb "~C~C" (code-char #xC8) (code-char #x50))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-iternext ((rdb RDB))
"Get the next key of the iterator.\
If successful, the return value is the next key, else, it is `nil'. `nil' is returned when no record is to be get out of the iterator.\
It is possible to access every record by iteration of calling this method. It is allowed to update or remove records whose keys are fetched while the iteration. However, it is not assured if updating the database is occurred while the iteration. Besides, the order of this traversal access method is arbitrary, so it is not assured that the order of storing matches the one of the traversal access."
(when-sock rdb
(when (tt-send rdb "~C~C" (code-char #xC8) (code-char #x51))
(tt-recv-code-cond rdb code
((zerop code)
(or (let ((vsiz (tt-recv-int32 rdb)))
(when (>= vsiz 0)
(let ((vbuf (tt-recv rdb vsiz)))
(when vbuf
(string-external vbuf)))))
(progn
(setf (ecode rdb) *ERECV*)
nil)))
(t
(setf (ecode rdb) *ENOREC*)
nil)))))
(defmethod tt-fwmkeys ((rdb RDB) prefix &optional (max -1))
"Get forward matching keys.\
`<i>prefix</i>' specifies the prefix of the corresponding keys.\
`<i>max</i>' specifies the maximum number of keys to be fetched. If it is not defined or negative, no limit is specified.\
The return value is an array of the keys of the corresponding records. This method does never fail. It returns an empty array even if no record corresponds.\
Note that this method may be very slow because every key in the database is scanned."
(when-sock rdb
(when (multiple-value-bind (prefix-ascii prefix-length) (string-ascii prefix)
(tt-send rdb "~C~C~A~A~A" (code-char #xC8) (code-char #x58) (pack-N prefix-length) (pack-N max) prefix-ascii))
(tt-recv-code-cond rdb code
((zerop code)
(let ((knum (tt-recv-int32 rdb)))
(when (>= knum 0)
(loop for i from 1 to knum
for ksiz = (tt-recv-int32 rdb)
when (< ksiz 0) return (progn
(setf (ecode rdb) *ERECV*)
nil)
for kbuf = (tt-recv rdb ksiz)
unless kbuf return (progn
(setf (ecode rdb) *ERECV*)
nil)
collect (string-external kbuf)))))
(t
(setf (ecode rdb) *ENOREC*)
nil)))))
(defmethod tt-addint ((rdb RDB) key &optional (num 0))
"Add an integer to a record.\
`<i>key</i>' specifies the key.\
`<i>num</i>' specifies the additional value. If it is not defined, 0 is specified.\
If successful, the return value is the summation value, else, it is `nil'.\
If the corresponding record exists, the value is treated as an integer and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the `unpack' function with the `i' operator after retrieval."
(when-sock rdb
(when (multiple-value-bind (key-ascii key-length) (string-ascii key)
(tt-send rdb "~C~C~A~A~A" (code-char #xC8) (code-char #x60) (pack-N key-length) (pack-N num) key-ascii))
(tt-recv-code-cond rdb code
((zerop code)
(tt-recv-int32 rdb))
(t
(setf (ecode rdb) *EKEEP*)
nil)))))
(defmethod tt-adddouble ((rdb RDB) key &optional (num 0))
"Add a real number to a record.\
`<i>key</i>' specifies the key.\
`<i>num</i>' specifies the additional value. If it is not defined, 0 is specified.\
If successful, the return value is the summation value, else, it is `nil'.\
If the corresponding record exists, the value is treated as a real number and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the `unpack' function with the `d' operator after retrieval."
(when-sock rdb
(let* ((integ (truncate num))
(fract (truncate (* (- num integ) 1000000000000))))
(when (multiple-value-bind (key-ascii key-length) (string-ascii key)
(tt-send rdb "~C~C~A~A~A~A" (code-char #xC8) (code-char #x61) (pack-N key-length) (pack-Q integ) (pack-Q fract) key-ascii))
(tt-recv-code-cond rdb code
((zerop code)
(let ((integ (tt-recv-int64 rdb))
(fract (tt-recv-int64 rdb)))
(+ integ (/ fract 1000000000000.0))))
(t
(setf (ecode rdb) *EKEEP*)
nil))))))
(defmethod tt-ext ((rdb RDB) name &optional (key "") (value "") (opts 0))
"Call a function of the script language extension.\
`<i>name</i>' specifies the function name.\
`<i>key</i>' specifies the key. If it is not defined, an empty string is specified.\
`<i>value</i>' specifies the value. If it is not defined, an empty string is specified.\
`<i>opts</i>' specifies options by bitwise-or: `*XOLCKREC*' for record locking, `*XOLCKGLB*' for global locking. If it is not defined, no option is specified.\
If successful, the return value is the value of the response or `nil' on failure."
(when-sock rdb
(when (multiple-value-bind (name-ascii name-length) (string-ascii name)
(multiple-value-bind (key-ascii key-length) (string-ascii key)
(multiple-value-bind (value-ascii value-length) (string-ascii value)
(tt-send rdb "~C~C~A~A~A~A~A~A~A" (code-char #xC8) (code-char #x68) (pack-N name-length) (pack-N opts) (pack-N key-length) (pack-N value-length) name-ascii key-ascii value-ascii))))
(tt-recv-code-cond rdb code
((zerop code)
(or (let ((vsiz (tt-recv-int32 rdb)))
(when (>= vsiz 0)
(let ((vbuf (tt-recv rdb vsiz)))
(when vbuf
(string-external vbuf)))))
(progn
(setf (ecode rdb) *ERECV*)
nil)))
(t
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-sync ((rdb RDB))
"Synchronize updated contents with the file and the device.\
If successful, the return value is true, else, it is false."
(when-sock rdb
(when (tt-send rdb "~C~C" (code-char #xC8) (code-char #x70))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-optimize ((rdb RDB) &optional (params ""))
"Optimize the storage.\
`<i>params</i>' specifies the string of the tuning parameters. If it is not defined, it is not used.\
If successful, the return value is true, else, it is false."
(when-sock rdb
(when (multiple-value-bind (params-ascii params-length) (string-ascii params)
(tt-send rdb "~C~C~A~A" (code-char #xC8) (code-char #x71) (pack-N params-length) params-ascii))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-vanish ((rdb RDB))
"Remove all records.\
If successful, the return value is true, else, it is false."
(when-sock rdb
(when (tt-send rdb "~C~C" (code-char #xC8) (code-char #x72))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-copy ((rdb RDB) path)
"Copy the database file.
`<i>path</i>' specifies the path of the destination file. If it begins with `@', the trailing substring is executed as a command line.\
If successful, the return value is true, else, it is false. False is returned if the executed command returns non-zero code.\
The database file is assured to be kept synchronized and not modified while the copying or executing operation is in progress. So, this method is useful to create a backup file of the database file."
(when-sock rdb
(when (multiple-value-bind (path-ascii path-length) (string-ascii path)
(tt-send rdb "~C~C~A~A" (code-char #xC8) (code-char #x73) (pack-N path-length) path-ascii))
(tt-recv-code-cond rdb code
((zerop code)
t)
(t
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-rnum ((rdb RDB))
"Get the number of records.\
The return value is the number of records or 0 if the object does not connect to any database server."
(or (when-sock rdb
(when (tt-send rdb "~C~C" (code-char #xC8) (code-char #x80))
(tt-recv-code-cond rdb code
((zerop code)
(or (let ((rv (tt-recv-int64 rdb)))
(when (>= rv 0)
rv))
(progn
(setf (ecode rdb) *ERECV*)
nil)))
(t
(setf (ecode rdb) *EMISC*)
nil))))
0))
(defmethod tt-size ((rdb RDB))
"Get the size of the database.\
The return value is the size of the database or 0 if the object does not connect to any database server."
(or (when-sock rdb
(when (tt-send rdb "~C~C" (code-char #xC8) (code-char #x81))
(tt-recv-code-cond rdb code
((zerop code)
(or (let ((rv (tt-recv-int64 rdb)))
(when (>= rv 0)
rv))
(progn
(setf (ecode rdb) *ERECV*)
nil)))
(t
(setf (ecode rdb) *EMISC*)
nil))))
0))
(defmethod tt-stat ((rdb RDB))
"Get the status string of the database server.\
The return value is the status message of the database or `nil' if the object does not connect to any database server. The message format is TSV. The first field of each line means the parameter name and the second field means the value."
(when-sock rdb
(when (tt-send rdb "~C~C" (code-char #xC8) (code-char #x88))
(tt-recv-code-cond rdb code
((zerop code)
(or (let ((ssiz (tt-recv-int32 rdb)))
(when (>= ssiz 0)
(let ((sbuf (tt-recv rdb ssiz)))
(when sbuf
(string-external sbuf)))))
(progn
(setf (ecode rdb) *ERECV*)
nil)))
(t
(setf (ecode rdb) *EMISC*)
nil)))))
;;; NOTE: if successful, and the result is empty, the return value is t,
;;; if successful, and the result is not empty, the return value is an array of the result,
;;; `nil' is returned on failure.
(defmethod tt-misc ((rdb RDB) name &optional (args nil) (opts 0))
"Call a versatile function for miscellaneous operations.\
`<i>name</i>' specifies the name of the function. All databases support 'putlist', 'outlist', and 'getlist'. 'putlist' is to store records. It receives keys and values one after the other, and returns an empty list. 'outlist' is to remove records. It receives keys, and returns an empty array. 'getlist' is to retrieve records. It receives keys, and returns keys and values of corresponding records one after the other. Table database supports 'setindex', 'search', and 'genuid'.\
`<i>args</i>' specifies an array containing arguments. If it is not defined, no argument is specified.\
`<i>opts</i>' specifies options by bitwise-or: `*MONOULOG*' for omission of the update log. If it is not defined, no option is specified.\
If successful, the return value is an array of the result. `nil' is returned on failure."
(unless (listp args)
(error "~S is not a valid argument to TT-MISC." args))
(when-sock rdb
(when (multiple-value-bind (name-ascii name-length) (string-ascii name)
(tt-send rdb "~C~C~A~A~A~A~A"
(code-char #xC8)
(code-char #x90)
(pack-N name-length)
(pack-N opts)
(pack-N (length args))
name-ascii
(loop for arg in args
collect (multiple-value-bind (arg-ascii arg-length) (string-ascii arg)
(format nil "~A~A" (pack-N arg-length) arg-ascii)) into strings
finally (return (apply #'string+ strings)))))
(tt-recv-code-cond rdb code
((zerop code)
(or (let ((rnum (tt-recv-int32 rdb)))
(if (zerop rnum)
t
(loop for i from 1 to rnum
for esiz = (tt-recv-int32 rdb)
when (< esiz 0) return nil
for ebuf = (tt-recv rdb esiz)
unless ebuf return nil
collect (string-external ebuf))))
(progn
(setf (ecode rdb) *ERECV*)
nil)))
(t
(tt-recv-int32 rdb) ;; NOTE: This tt-recv-int32 is must.
(setf (ecode rdb) *EMISC*)
nil)))))
(defmethod tt-store ((rdb RDB) key value)
"Hash-compatible method.\
Alias of `put'."
(tt-put rdb key value))
(defmethod tt-delete ((rdb RDB) key)
"Hash-compatible method.\
Alias of `out'."
(tt-out rdb key))
(defmethod tt-fetch ((rdb RDB) key)
"Hash-compatible method.\
Alias of `get'."
(tt-get rdb key))
(defmethod tt-has-keyp ((rdb RDB) key)
"Hash-compatible method.\
Check existence of a key."
(>= (tt-vsiz rdb key) 0))
(defmethod tt-has-valuep ((rdb RDB) value)
"Hash-compatible method.\
Check existence of a value."
(when (tt-iterinit rdb)
(loop for tkey = (tt-iternext rdb)
while tkey
for tvalue = (tt-get rdb tkey)
while tvalue
thereis (equal value tvalue))))
(defmethod tt-clear ((rdb RDB))
"Hash-compatible method.\
Alias of `vanish'."
(tt-vanish rdb))
(defmethod tt-length ((rdb RDB))
"Hash-compatible method.\
Alias of `rnum'."
(tt-rnum rdb))
(defmethod tt-emptyp ((rdb RDB))
"Hash-compatible method.\
Alias of `rnum < 1'."
(< (tt-rnum rdb) 1))
(defmethod tt-each ((rdb RDB) proc)
"Hash-compatible method.\
Iterator of pairs of the key and the value."
(when (tt-iterinit rdb)
(loop for key = (tt-iternext rdb)
while key
for value = (tt-get rdb key)
while value
do (funcall proc key value))))
(defmethod tt-each-keys ((rdb RDB) proc)
"Hash-compatible method.\
Iterator of the keys."
(when (tt-iterinit rdb)
(loop for key = (tt-iternext rdb)
while key
do (funcall proc key))))
(defmethod tt-each-values ((rdb RDB) proc)
"Hash-compatible method.\
Iterator of the values."
(when (tt-iterinit rdb)
(loop for key = (tt-iternext rdb)
while key
for value = (tt-get rdb key)
while value
do (funcall proc value))))
(defmethod tt-keys ((rdb RDB))
"Hash-compatible method.\
Get an array of all keys."
(when (tt-iterinit rdb)
(loop for key = (tt-iternext rdb)
while key
collect key)))
(defmethod tt-values ((rdb RDB))
"Hash-compatible method.\
Get an array of all keys."
(when (tt-iterinit rdb)
(loop for key = (tt-iternext rdb)
while key
for value = (tt-get rdb key)
while value
collect value)))
(defconstant *ITLEXICAL* 0 "index type: lexical string")
(defconstant *ITDECIMAL* 1 "index type: decimal string")
(defconstant *ITTOKEN* 2 "index type: token inverted index")
(defconstant *ITQGRAM* 3 "index type: q-gram inverted index")
(defconstant *ITOPT* 9998 "index type: optimize")
(defconstant *ITVOID* 9999 "index type: void")
(defconstant *ITKEEP* (ash 1 24) "index type: keep existing index")
;;; This class inherits the class 'RDB'. All methods are specific to servers of the table database.
(defclass RDBTBL (RDB)
()
)
(defmethod tt-put ((rdbtbl RDBTBL) pkey cols)
"Store a record.\
`<i>pkey</i>' specifies the primary key.\
`<i>cols</i>' specifies a hash containing columns.\
If successful, the return value is true, else, it is false.\
If a record with the same key exists in the database, it is overwritten."
(unless (hash-table-p cols)
(error "~S is not a valid argument to TT-PUT." cols))
(if (tt-misc rdbtbl "put" (cons pkey (hash-table-to-key-value-list cols)) 0)
t
nil))
(defmethod tt-putkeep ((rdbtbl RDBTBL) pkey cols)
"Store a new record.\
`<i>pkey</i>' specifies the primary key.\
`<i>cols</i>' specifies a hash containing columns.\
If successful, the return value is true, else, it is false.\
If a record with the same key exists in the database, this method has no effect."
(unless (hash-table-p cols)
(error "~S is not a valid argument to TT-PUTKEEP." cols))
(if (tt-misc rdbtbl "putkeep" (cons pkey (hash-table-to-key-value-list cols)) 0)
t
(when (eq (ecode rdbtbl) *EMISC*)
(setf (ecode rdbtbl) *EKEEP*)
nil)))
(defmethod tt-putcat ((rdbtbl RDBTBL) pkey cols)
"Concatenate columns of the existing record.\
`<i>pkey</i>' specifies the primary key.\
`<i>cols</i>' specifies a hash containing columns.\
If successful, the return value is true, else, it is false.\
If there is no corresponding record, a new record is created."
(unless (hash-table-p cols)
(error "~S is not a valid argument to TT-PUTCAT." cols))
(if (tt-misc rdbtbl "putcat" (cons pkey (hash-table-to-key-value-list cols)) 0)
t
nil))
(defmethod tt-out ((rdbtbl RDBTBL) pkey)
"Remove a record.\
`<i>pkey</i>' specifies the primary key.\
If successful, the return value is true, else, it is false."
(if (tt-misc rdbtbl "out" (list pkey) 0)
t
(when (eq (ecode rdbtbl) *EMISC*)
(setf (ecode rdbtbl) *ENOREC*)
nil)))
(defmethod tt-get ((rdbtbl RDBTBL) pkey)
"Retrieve a record.\
`<i>pkey</i>' specifies the primary key.\
If successful, the return value is a hash of the columns of the corresponding record. `nil' is returned if no record corresponds."
(let ((rv (tt-misc rdbtbl "get" (list pkey))))
(if rv
(loop with cols = (make-hash-table :test 'equal)
for l on rv by #'cddr
do (setf (gethash (first l) cols) (second l))
finally (return cols))
(when (eq (ecode rdbtbl) *EMISC*)
(setf (ecode rdbtbl) *ENOREC*)
nil))))
(defmethod tt-mget ((rdbtbl RDBTBL) recs)
"Retrieve records.\
`<i>recs</i>' specifies a hash containing the retrieval keys. As a result of this method, keys existing in the database have the corresponding columns and keys not existing in the database are removed.\
If successful, the return value is the number of retrieved records or -1 on failure.\
Due to the protocol restriction, this method can not handle records with binary columns including the '\0' chracter."
(let ((rv (call-next-method)))
(if (< rv 0)
-1
(progn
(loop for pkey being the hash-key using (hash-value value) of recs
for cols = (make-hash-table :test 'equal)
for cary = (excl:split-re "