-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimplementation.lisp
More file actions
954 lines (801 loc) · 50 KB
/
Copy pathimplementation.lisp
File metadata and controls
954 lines (801 loc) · 50 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
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
;;; Copyright (c) 2022 Max-Gerd Retzlaff <mgr@matroid.org>, Datagraph GmbH.
;;; Distributed under the terms of the GNU General Public License, Version 2.0,
;;; see file LICENSE in the top level directory of this repository.
(in-package :ndbapi.implementation)
;;; errors
(defun %get-ndb-error (object &optional (getter #'ndbapi.ffi::ndb-get-ndb-error/swig-0))
(let ((pointer (if (typep object 'ndbapi.types::garbage-collected-class)
(ndbapi.types::foreign-pointer object)
object)))
(cffi:mem-aref (funcall getter pointer)
'(:struct ndbapi.ffi::ndberror-struct))))
(defun error-string (error-plist)
(format nil "Error with code ~a: ~a"
(getf error-plist :code)
(getf error-plist :message)))
(defun get-ndb-error (object &optional (getter #'ndbapi.ffi::ndb-get-ndb-error/swig-0))
(let ((error-plist (%get-ndb-error object getter)))
(values (error-string error-plist)
error-plist)))
(defun explicitly-check-for-error (object &optional (getter #'ndbapi.ffi::ndb-get-ndb-error/swig-0))
(let ((error-plist (%get-ndb-error object getter)))
(assert (eq (getf error-plist :status) :+NDBERROR-ST-SUCCESS+)
nil
"explicitly-check-for-error() reports: ~a"
(error-string error-plist))))
(defun explicitly-check-for-transaction-error (transaction)
"The MySQL NDB Cluster API Developer Guide says on
NdbTransaction::execute() that it returns 0 on success and -1 on
failure. But it reports failure \"if and only if the transaction was
aborted\". That is, NdbTransaction::execute() returning 0 mains only
that the transaction was not aborted, some operations might still have
not been successful.
In such cases, the transaction's error information will be set and this
is for what this function checks.
See https://dev.mysql.com/doc/ndbapi/en/ndb-ndbtransaction.html#ndb-ndbtransaction-execute
Note: ndbapi:ndb-transaction-execute already does this extra call,
so you do not need to repeat it."
(explicitly-check-for-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error))
(defun check-for-tuple-not-found-error (transaction)
(let ((error-plist (%get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
#+(or)
(break "~a" (list (getf error-plist :code)
(getf error-plist :classification)
(getf error-plist :message) ;; "Tuple did not exist"
error-plist))
(and ;; the documentation of ndb cluster on NdbTransaction::execute()
;; seems to imply that checking for:
;; (myTransaction->getNdbError().classification == NdbError:NoDataFound)
;; is enough; see:
;; https://dev.mysql.com/doc/ndbapi/en/ndb-ndbtransaction.html#ndb-ndbtransaction-execute
(eq (getf error-plist :classification) :+NDBERROR-CL-NO-DATA-FOUND+)
;; but lets check the error :code as well to be extra sure:
(eq (getf error-plist :code) 626))))
;;; test :ndbapi.types for validity
(defun valid-object-p (object)
(let ((pointer (if (typep object 'ndbapi.types::garbage-collected-class)
(let ((valid-cons (ndbapi.types::valid-cons object)))
(when (and valid-cons
(car valid-cons))
(ndbapi.types::foreign-pointer object)))
object)))
(and pointer
(not (cffi:null-pointer-p pointer)))))
(defun ndb-api-initialisation-is-initialized-p (object)
(and object
(typep object 'ndbapi.ffi::ndb-api-initialisation)
(let ((valid-cons (ndbapi.types::valid-cons object)))
(when (and valid-cons
(car valid-cons))
(ndbapi.types::initialized object)))))
(defun valid-connection-p (object)
(when (typep object 'ndbapi.ffi::ndb-cluster-connection)
(valid-object-p object)))
(defun valid-ndb-p (object)
(when (typep object 'ndbapi.ffi::ndb)
(valid-object-p object)))
;;; interface
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun remove-swig-postfix (string)
(subseq string 0 (search "/SWIG-" string))))
(defmacro make-interface-function ((function &rest fun-args) (&optional test datum &rest arguments) &body more-calls)
"FUNCTION can either be a direct symbol name, any \"/SWIG-*\" postfix will be removed
and it will be interned in the currecnt package;
or it can be an arglist (FUNCTION &KEY PACKAGE NAME), if NAME is supplied it is take as is as the new name,
if PACKAGE is supplied, the name treatment as above will be taken towards FUNCTION
but the new symbol will be interned to PACKAGE instead"
(destructuring-bind (function &key package
(name (apply #'intern (remove-swig-postfix (symbol-name function)) (when package (list package)))))
(if (consp function) function (list function))
`(progn
,(let* ((call (if (find '&rest fun-args)
(cons 'apply (cons `(symbol-function ',function) (remove '&rest fun-args)))
`(,function ,@ fun-args))))
`(defun ,name ,fun-args
,`(let ((value ,call))
,@(if test
`((assert (funcall (function ,test) value)
()
,datum
,@arguments)))
,@more-calls
value)))
,(let* ((fun-args-rest-pos (let ((pos (position '&rest fun-args)))
(when (and pos
(< (1+ pos) (length fun-args)))
pos)))
(fun-explicit-args (subseq fun-args 0 fun-args-rest-pos))
(macro-call (if fun-args-rest-pos
``(,',function ,@',(subseq fun-args 0 fun-args-rest-pos) ,@,(nth (1+ fun-args-rest-pos) fun-args))
``(,',function ,@',fun-args))))
`(define-compiler-macro ,name (,@fun-args)
`(let* (,,@(loop for var in fun-explicit-args collect ``(,',var ,,var))
(value ,,macro-call))
,@',(if test
`((assert (,test value)
()
,datum
,@arguments)))
,@',more-calls
value))))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun remove-ic-prefix (string)
(let* ((prefix "NDB-INTERPRETED-CODE-")
(prefix-length (length prefix)))
(if (and (>= (length string) prefix-length)
(string= (subseq string 0 prefix-length) prefix))
(subseq string prefix-length)
string))))
(defmacro make-ic-interface-function ((function code &rest args))
(destructuring-bind (function &key (package 'ndbapi.ic)
(name (intern (remove-swig-postfix (remove-ic-prefix (symbol-name function))) package)))
(if (consp function) function (list function))
`(make-interface-function ((,function :name ,name)
,code ,@args)
(zerop
,(format nil "~(~a:~a~)() failed: ~a"
package name "~a")
(get-ndb-error ,code #'ndbapi.ffi:ndb-interpreted-code-get-ndb-error)))))
(make-interface-function (ndbapi.ffi::ndb-api-begin%)
(ndb-api-initialisation-is-initialized-p
"ndb-api-initialisation failed"))
(make-interface-function (ndbapi.ffi::new-ndb-cluster-connection/swig-0 ndb-api-initialisation connection-string) ;; no unique arity (extension of RonDB)
(valid-object-p
"Create new ndb-cluster-connection object failed"))
(defvar *default-connection-name* "cl-ndbapi" "default 2nd argument for ndb-cluster-connection-set-name")
(defun ndb-cluster-connection-set-name (cluster-connection &optional name) ;; do not use default value here as name might be nil explicitly
(ndbapi.ffi::ndb-cluster-connection-set-name cluster-connection (or name *default-connection-name*))
;; returns void -> no test
)
(make-interface-function (ndbapi.ffi.o::ndb-cluster-connection-connect cluster-connection &rest args) ;; no-retries retry-delay-in-seconds verbose
;; defaults are specified in C++: no-retries=30 retry-delay-in-seconds=1 verbose=0
(zerop
"Cluster management server was not ready within expected time"))
(make-interface-function ((ndbapi.ffi::ndb-cluster-connection-wait-until-ready/swig-0 :name ndb-cluster-connection-wait-until-ready%)
cluster-connection timeout-for-first-alive timeout-after-first-alive)
(zerop
"Cluster was not ready within 30 secs."))
(defvar *timeout-for-first-alive* 30 "default for 2nd argument of ndb-cluster-connection-wait-until-ready ")
(defvar *timeout-after-first-alive* 0 "default for 3rd argument of ndb-cluster-connection-wait-until-ready ")
(defun ndb-cluster-connection-wait-until-ready (cluster-connection &optional timeout-for-first-alive timeout-after-first-alive) ;; do not use default value here
;; returns void
(ndb-cluster-connection-wait-until-ready% cluster-connection
(or timeout-for-first-alive *timeout-for-first-alive*)
(or timeout-after-first-alive *timeout-after-first-alive*)))
(make-interface-function (ndbapi.ffi.o::new-ndb cluster-connection &rest args)
(valid-object-p
"Create new NDB object failed"))
(make-interface-function (ndbapi.ffi::ndb-get-ndb-error/swig-0 ndb)
;; no error handling
())
(make-interface-function (ndbapi.ffi::ndb-get-database-name ndb)
;; no error handling, just returns a string and "" if not set
())
(make-interface-function (ndbapi.ffi::ndb-set-database-name ndb database-name)
;; returns void
())
(make-interface-function (ndbapi.ffi::ndb-get-database-schema-name ndb)
;; no error handling, just returns a string and "" if not set
())
(make-interface-function (ndbapi.ffi::ndb-set-database-schema-name ndb database-schema-name)
;; returns void
())
(make-interface-function (ndbapi.ffi.o::ndb-init ndb &rest args)
;; this is ndb::init(), the other ndb_init was
;; renamed to :ndb-api-begin to avoid conflict
(zerop
"Ndb.init() failed: ~a"
(get-ndb-error ndb #'ndb-get-ndb-error)))
(make-interface-function (ndbapi.ffi::ndb-start-transaction/swig-3 ndb)
(valid-object-p
"start-transaction() failed: ~a"
(get-ndb-error ndb)))
(make-interface-function (ndbapi.ffi::ndb-get-dictionary ndb)
(valid-object-p
"get-dictionary() failed: ~a"
(get-ndb-error ndb)))
(make-interface-function (ndbapi.ffi::dictionary-get-table/swig-0 dictionary name)
(valid-object-p
"get-table() failed: ~a"
(get-ndb-error dictionary #'ndbapi.ffi:dictionary-get-ndb-error)))
;; could also use make-interface:
(make-interface-function (ndbapi.ffi::dictionary-invalidate-table/swig-0 dictionary name)
;; returns void
())
;; could also use make-interface:
(make-interface-function (ndbapi.ffi::dictionary-remove-cached-table/swig-0 dictionary name)
;; returns void
())
(make-interface-function (ndbapi.ffi::dictionary-list-indexes/swig-0 dictionary list table-name)
(zerop
"list-indexes() failed: ~a"
(get-ndb-error dictionary #'ndbapi.ffi:dictionary-get-ndb-error)))
(make-interface-function (ndbapi.ffi::dictionary-get-index/swig-0 dictionary index-name table-name)
(valid-object-p
"get-index() failed: ~a"
(get-ndb-error dictionary #'ndbapi.ffi:dictionary-get-ndb-error)))
(make-interface-function (ndbapi.ffi::ndb-dictionary-set-null record row attr-id value)
(zerop ;; from the documentation: "Returns 0 on success; returns -1 if
;; the attrId is not part of the record, or is not nullable."
"set-null() failed: ~a"
;; from the documentation:
"the attrId is not part of the record, or is not nullable"))
;; could also use make-interface:
(make-interface-function (ndbapi.ffi::dictionary-invalidate-index/swig-0 dictionary index-name table-name)
;; returns void
())
;; could also use make-interface:
(make-interface-function (ndbapi.ffi::dictionary-remove-cached-index/swig-0 dictionary index-name table-name)
;; returns void
())
(make-interface-function (ndbapi.ffi::index-get-default-record index)
(valid-object-p
"get-default-record() of index ~a failed"
(ndbapi.ffi:index-get-name index)))
(make-interface-function (ndbapi.ffi::table-get-default-record table)
(valid-object-p
"get-default-record() of table ~a failed"
(ndbapi.ffi:table-get-name table)))
(make-interface-function (ndbapi.ffi::table-get-column/swig-3 table name)
(valid-object-p
"get-column(\"~a\") of table ~a failed"
name (ndbapi.ffi:table-get-name table)))
(make-interface-function ((ndbapi.ffi::table-get-column/swig-2 :name table-get-column-by-index) table name)
(valid-object-p
"get-column(\"~a\") of table ~a failed"
name (ndbapi.ffi:table-get-name table)))
(make-interface-function (ndbapi.ffi.o::ndb-transaction-scan-index transaction key-record result-record &rest args)
;;; all variants have the named three parameters
(valid-object-p
"transaction-scan-index() failed: ~a"
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function (ndbapi.ffi::ndb-index-scan-operation-set-bound/swig-6 index-scan key-record bound)
(zerop
"set-bound() failed: ~a"
(get-ndb-error (ndbapi.ffi:ndb-scan-operation-get-ndb-transaction index-scan) #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function ((ndbapi.ffi::ndb-index-scan-operation-set-bound/swig-4 :name ndb-index-scan-operation-set-bound/partition-pruned)
index-scan key-record bound partition-info size-of-partition-info)
(zerop
"set-bound() failed: ~a"
(get-ndb-error (ndbapi.ffi:ndb-scan-operation-get-ndb-transaction index-scan) #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function ((ndbapi.ffi::ndb-index-scan-operation-set-bound/swig-1 :name ndb-index-scan-operation-set-bound/recattr)
index-scan attr type value)
(zerop
"set-bound() failed: ~a"
(get-ndb-error (ndbapi.ffi:ndb-scan-operation-get-ndb-transaction index-scan) #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function (ndbapi.ffi::ndb-transaction-get-ndb-index-scan-operation/swig-2 transaction an-index)
(valid-object-p
"transaction-get-ndb-index-scan-operation() failed: ~a"
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function (ndbapi.ffi::ndb-index-scan-operation-read-tuples/swig-2 index-scan lock-mode scan-flags)
(zerop
"transaction-scan-index() failed: ~a"
(get-ndb-error (ndbapi.ffi:ndb-scan-operation-get-ndb-transaction index-scan) #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function (ndbapi.ffi.o::ndb-transaction-scan-table transaction result-record &rest args)
;;; all variants have the named two parameters
(valid-object-p
"transaction-scan-table() failed: ~a"
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function ((ndbapi.ffi::ndb-transaction-execute/swig-5 :name ndb-transaction-execute/no-explicit-check)
transaction exec-type)
(zerop
"transaction-execute() failed: ~a"
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function (ndbapi.ffi::ndb-transaction-execute/swig-5 transaction exec-type)
(zerop
"transaction-execute() failed: ~a"
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error))
;; need to explicitly check that there were no errors in addition to the ZEROP assert above
;; see documentation string on explicitly-check-for-transaction-error for details
(explicitly-check-for-transaction-error transaction))
(declaim (inline not-minusp))
(defun not-minusp (number)
(not (minusp number)))
(make-interface-function (ndbapi.ffi.o::ndb-scan-operation-next-result scan &rest args) ;; out-row-ptr fetch-allowed force-send
(not-minusp ;; only -1 indicates error, 0, 1, and 2 are valid and indicate different situations
"scan-operation-next-result() failed: ~a"
(get-ndb-error (ndbapi.ffi:ndb-scan-operation-get-ndb-transaction scan) #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function (ndbapi.ffi.o::ndb-scan-operation-close scan &rest args)
;; returns void
())
(make-interface-function (ndbapi.ffi.o::ndb-scan-operation-delete-current-tuple scan &rest args) ;; out-row-ptr fetch-allowed force-send
(valid-object-p
;; documentation indicates: (lambda (rc) (>= rc 0)) ;; only -1 indicates error, 0 means success
;; but it actually returns a "const NdbOperation*"
"scan-operation-delete-current-tuple() failed: ~a"
(get-ndb-error (ndbapi.ffi:ndb-scan-operation-get-ndb-transaction scan) #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function (ndbapi.ffi.o::ndb-scan-operation-update-current-tuple scan &rest args) ;; out-row-ptr fetch-allowed force-send
(valid-object-p
"scan-operation-update-current-tuple() failed: ~a"
(get-ndb-error (ndbapi.ffi:ndb-scan-operation-get-ndb-transaction scan) #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function ((ndbapi.ffi.o::new-ndb-interpreted-code/table :name ndbapi.ic:new)
ndb-api-initialisation &rest args)
(valid-object-p
"Create new ndb-interpreted-code object failed"))
(make-interface-function ((ndbapi.ffi.o::new-ndb-interpreted-code/record :name ndbapi.ic:new/record)
ndb-api-initialisation &rest args)
(valid-object-p
"Create new ndb-interpreted-code object failed"))
;;(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-get-words-used code))
(make-interface-function ((ndbapi.ffi::ndb-interpreted-code-get-words-used :name ndbapi.ic:get-words-used)
code)
;; no error handling, just returns the count
())
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-interpret-exit-last-row code))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-interpret-exit-ok code))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-interpret-exit-nok/swig-1 code))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-read-attr/swig-1 code reg-dest column))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-read-attr/swig-0 :name ndbapi.ic::read-attr/attr-id)
code reg-dest column))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-write-attr/swig-1 code column reg-source))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-write-from-mem/swig-1
code column reg-memory-offset reg-size))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-append-from-mem/swig-1
code column reg-memory-offset reg-size))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-read-partial/swig-1
code column reg-memory-offset reg-pos reg-size reg-dest))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-read-full/swig-1
code column reg-memory-offset reg-dest))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-load-const-u-16 :name ndbapi.ic::load-const-u16)
code reg-dest constant))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-load-const-u-32 :name ndbapi.ic::load-const-u32)
code reg-dest constant))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-load-const-u-64 :name ndbapi.ic::load-const-u64)
code reg-dest constant))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-load-const-null :name ndbapi.ic::load-const-null)
code reg-dest))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-load-const-mem :name ndbapi.ic::load-const-mem)
code reg-memory-offset reg-dest-size size-constant const-memory))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-def-label code label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-label code label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-ge code reg-l reg-r label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-gt code reg-l reg-r label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-le code reg-l reg-r label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-lt code reg-l reg-r label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-eq code reg-l reg-r label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-ne code reg-l reg-r label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-ge-const code reg-l constant label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-gt-const code reg-l constant label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-le-const code reg-l constant label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-lt-const code reg-l constant label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-eq-const code reg-l constant label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-ne-const code reg-l constant label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-ne-null code reg-l label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-branch-eq-null code reg-l label))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-call-sub code subroutine-number))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-def-sub code subroutine-number))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-ret-sub code))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-sub-val/swig-0 :name ndbapi.ic::sub-val-u32)
code attribute-id value))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-sub-val/swig-1 :name ndbapi.ic::sub-val-u64)
code attribute-id value))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-add-val/swig-0 :name ndbapi.ic::add-val-u32)
code attribute-id value))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-add-val/swig-1 :name ndbapi.ic::add-val-u64)
code attribute-id value))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-add-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-sub-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-finalise code))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-lshift-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-rshift-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-mul-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-div-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-and-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-or-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-xor-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-mod-reg code reg-dest reg-source-1 reg-source-2))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-not-reg code reg-dest reg-source))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-move-reg code reg-dest reg-source))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-add-const-reg code reg-dest reg-source constant))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-sub-const-reg code reg-dest reg-source constant))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-lshift-const-reg code reg-dest reg-source constant))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-rshift-const-reg code reg-dest reg-source constant))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-mul-const-reg code reg-dest reg-source constant))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-div-const-reg code reg-dest reg-source constant))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-and-const-reg code reg-dest reg-source constant))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-or-const-reg code reg-dest reg-source constant))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-xor-const-reg code reg-dest reg-source constant))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-mod-const-reg code reg-dest reg-source constant))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-read-uint-8-to-reg-const :name ndbapi.ic::read-u8-to-reg/const)
code reg-dest memory-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-read-uint-16-to-reg-const :name ndbapi.ic::read-u16-to-reg/const)
code reg-dest memory-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-read-uint-32-to-reg-const :name ndbapi.ic::read-u32-to-reg/const)
code reg-dest memory-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-read-int-64-to-reg-const :name ndbapi.ic::read-i64-to-reg/const)
code reg-dest memory-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-read-uint-8-to-reg-reg :name ndbapi.ic::read-u8-to-reg/reg)
code reg-dest reg-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-read-uint-16-to-reg-reg :name ndbapi.ic::read-u16-to-reg/reg)
code reg-dest reg-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-read-uint-32-to-reg-reg :name ndbapi.ic::read-u32-to-reg/reg)
code reg-dest reg-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-read-int-64-to-reg-reg :name ndbapi.ic::read-i64-to-reg/reg)
code reg-dest reg-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-write-uint-8-reg-to-mem-const :name ndbapi.ic::write-u8-reg-to-mem/const)
code reg-source memory-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-write-uint-16-reg-to-mem-const :name ndbapi.ic::write-u16-reg-to-mem/const)
code reg-source memory-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-write-uint-32-reg-to-mem-const :name ndbapi.ic::write-u32-reg-to-mem/const)
code reg-source memory-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-write-int-64-reg-to-mem-const :name ndbapi.ic::write-i64-reg-to-mem/const)
code reg-source memory-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-write-uint-8-reg-to-mem-reg :name ndbapi.ic::write-u8-reg-to-mem/reg)
code reg-source reg-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-write-uint-16-reg-to-mem-reg :name ndbapi.ic::write-u16-reg-to-mem/reg)
code reg-source reg-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-write-uint-32-reg-to-mem-reg :name ndbapi.ic::write-u32-reg-to-mem/reg)
code reg-source reg-offset))
(make-ic-interface-function ((ndbapi.ffi::ndb-interpreted-code-write-int-64-reg-to-mem-reg :name ndbapi.ic::write-i64-reg-to-mem/reg)
code reg-source reg-offset))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-search-interval-32
code reg-ordinal reg-offset reg-num-elems reg-result search-type))3
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-write-interpreter-output
code reg-value output-index))
(make-ic-interface-function (ndbapi.ffi::ndb-interpreted-code-read-interpreter-input
code reg-value input-index))
(make-interface-function (ndbapi.ffi::ndb-scan-operation-set-interpreted-code scan code)
(zerop
"ndb-scan-operation-set-interpreted-code() failed: ~a"
(get-ndb-error (ndbapi.ffi:ndb-scan-operation-get-ndb-transaction scan) #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function (ndbapi.ffi::ndb-operation-get-value/swig-4 ndb-operation a-column a-value)
(valid-object-p
"ndb-operation-get-value() failed: ~a"
(get-ndb-error (ndbapi.ffi:ndb-operation-get-ndb-transaction ndb-operation) #'ndbapi.ffi:ndb-transaction-get-ndb-error)))
(make-interface-function (ndbapi.ffi.o::ndb-transaction-update-tuple transaction key-record key-row attribute-record attribute-row &rest args)
(valid-object-p
"ndb-transaction-update-tuple() failed: ~a"
;; all examples do this:
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)
;; but the documentation seems to imply:
;; (get-ndb-error value #'ndbapi.ffi::ndb-operation-get-ndb-error)
;; this, howewer, must be wrong as in this case an erroneous value,
;; even a null-pointer, would be used for error checking!
))
;; as an insert needs all fields for us, we never need the mask,
;; and to insert with the combined-row/record is easiest, so this variant is enough:
(make-interface-function (ndbapi.ffi::ndb-transaction-insert-tuple/swig-7 transaction combined-record combined-row)
(valid-object-p
"ndb-transaction-insert-tuple() failed: ~a"
;; all examples do this:
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)
;; but the documentation seems to imply:
;; (get-ndb-error value #'ndbapi.ffi::ndb-operation-get-ndb-error)
;; this, howewer, must be wrong as in this case an erroneous value,
;; even a null-pointer, would be used for error checking!
))
(make-interface-function (ndbapi.ffi.o::ndb-transaction-write-tuple transaction key-record key-row attribute-record attribute-row &rest args)
(valid-object-p
"ndb-transaction-write-tuple() failed: ~a"
;; all examples do this:
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)
;; but the documentation seems to imply:
;; (get-ndb-error value #'ndbapi.ffi::ndb-operation-get-ndb-error)
;; this, howewer, must be wrong as in this case an erroneous value,
;; even a null-pointer, would be used for error checking!
))
(make-interface-function (ndbapi.ffi.o::ndb-transaction-delete-tuple transaction key-record key-row result-record &rest args)
(valid-object-p
"ndb-transaction-delete-tuple() failed: ~a"
;; all examples do this:
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)
;; but the documentation seems to imply:
;; (get-ndb-error value #'ndbapi.ffi::ndb-operation-get-ndb-error)
;; this, howewer, must be wrong as in this case an erroneous value,
;; even a null-pointer, would be used for error checking!
))
(make-interface-function (ndbapi.ffi.o::ndb-transaction-read-tuple transaction key-record key-row result-record result-row &rest args)
(valid-object-p
"ndb-transaction-read-tuple() failed: ~a"
;; all examples do this:
(get-ndb-error transaction #'ndbapi.ffi:ndb-transaction-get-ndb-error)
;; but the documentation seems to imply:
;; (get-ndb-error value #'ndbapi.ffi::ndb-operation-get-ndb-error)
;; this, howewer, must be wrong as in this case an erroneous value,
;; even a null-pointer, would be used for error checking!
))
;; begin of pseudo-columns
(defmacro make-pseudo-column-interface-function ((function))
`(make-interface-function (,function)
(valid-object-p
,(format nil "~(~a~)(): null pointer returned.
Note: The pseudo columns are only available in the context of a cluster connection,
as they are created as a side-effect of making the cluster connection."
(symbol-name function)))))
(make-pseudo-column-interface-function (ndbapi.ffi:column-fragment))
(make-pseudo-column-interface-function (ndbapi.ffi:column-fragment-fixed-memory))
(make-pseudo-column-interface-function (ndbapi.ffi:column-fragment-varsized-memory))
(make-pseudo-column-interface-function (ndbapi.ffi:column-row-count))
(make-pseudo-column-interface-function (ndbapi.ffi:column-commit-count))
(make-pseudo-column-interface-function (ndbapi.ffi:column-row-size))
(make-pseudo-column-interface-function (ndbapi.ffi:column-range-no))
(make-pseudo-column-interface-function (ndbapi.ffi:column-disk-ref))
(make-pseudo-column-interface-function (ndbapi.ffi:column-records-in-range))
(make-pseudo-column-interface-function (ndbapi.ffi:column-rowid))
(make-pseudo-column-interface-function (ndbapi.ffi:column-row-gci))
(make-pseudo-column-interface-function (ndbapi.ffi:column-row-gci-64))
(make-pseudo-column-interface-function (ndbapi.ffi:column-row-author))
(make-pseudo-column-interface-function (ndbapi.ffi:column-any-value))
(make-pseudo-column-interface-function (ndbapi.ffi:column-copy-rowid))
(make-pseudo-column-interface-function (ndbapi.ffi:column-lock-ref))
(make-pseudo-column-interface-function (ndbapi.ffi:column-op-id))
(make-pseudo-column-interface-function (ndbapi.ffi:column-optimize))
(make-pseudo-column-interface-function (ndbapi.ffi:column-fragment-extent-space))
(make-pseudo-column-interface-function (ndbapi.ffi:column-fragment-free-extent-space))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-0))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-1))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-2))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-3))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-4))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-5))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-6))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-7))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-8))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-9))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-10))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-11))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-12))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-13))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-14))
(make-pseudo-column-interface-function (ndbapi.ffi:column-read-interpreter-output-15))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-0))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-1))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-2))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-3))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-4))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-5))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-6))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-7))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-8))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-9))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-10))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-11))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-12))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-13))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-14))
(make-pseudo-column-interface-function (ndbapi.ffi:column-interpreter-input-15))
;; new
;; end of pseudo-columns
;;; low-level free
(setf (fdefinition 'ndb-free-object) (fdefinition 'ndbapi.types:free-foreign-object))
;;; with- macros
(defvar *ndb-api-initialisation* nil)
(defvar *ndb-api-initialisation-lock* (bt:make-lock "*ndb-api-initialisation* lock"))
(defun ndb-api-begin ()
"safely initializes NDB API (that is, only once)"
(bt:with-lock-held (*ndb-api-initialisation-lock*)
(if (ndb-api-initialisation-is-initialized-p *ndb-api-initialisation*)
*ndb-api-initialisation*
(progn
(load-ndbapi)
(setf *ndb-api-initialisation* (ndb-api-begin%))))))
;; NDB-API-BEGIN and ENSURE-NDB-API-INITIALISATION are identical, as there really should be only one
;; NDB-API-INITIALISATION object for the whole application. The NDB API must to be initialized once only.
(setf (fdefinition 'ensure-ndb-api-initialisation) (fdefinition 'ndb-api-begin))
(defun ndb-api-end ()
"WARNING: only call when you are sure that the NDB API is not used anymore at all"
(bt:with-lock-held (*ndb-api-initialisation-lock*)
;; no need to check with NDB-API-INITIALISATION-IS-INITIALIZED-P as NDB-FREE-OBJECT is safe to be called repeatedly
(ndb-free-object *ndb-api-initialisation*)))
(defmacro with-ndb-api-initialisation ((&key there-is-only-one) &body body)
"better just call ENSURE-NDB-API-INITIALISATION once and do not use WITH-NDB-API-INITIALISATION
If THERE-IS-ONLY-ONE is t, NDB-API-END is called at the end IFF WITH-NDB-API-INITIALISATION defined *NDB-API-INITIALISATION*
This initializes the ndb api not an individual ndb object, for that NDBAPI:NDB-INIT has to be called on an ndb object."
(let ((op (gensym "OP-")))
`(flet ((,op () ,@body))
(declare (dynamic-extent #',op))
(call-with-ndb-api-initialisation #',op :there-is-only-one ,there-is-only-one))))
(defun call-with-ndb-api-initialisation (op &key there-is-only-one)
;;(print 'ndb-api-begin *trace-output*) (time)
(if (ndb-api-initialisation-is-initialized-p *ndb-api-initialisation*)
(funcall op)
(progn
(ndb-api-begin)
(unwind-protect (funcall op)
;; explicit free of NDB-API-INITIALISATION object IFF we have introduced it,
;; that is *NDB-API-INITIALISATION* was previously unbound or not initialized.
;; Still this is quite unsafe. E.g., when two threads interleave WITH-NDB-API-INITIALISATION,
;; and the second thread uses the NDB-API-INITIALISATION of the first thread.
;;
;; Better use WITH-NDB-API-INITIALISATION just in simple example with one thread only,
;; and use ENSURE-NDB-API-INITIALISATION in all other cases. Call NDB-API-END then
;; at a time when the NDB API is not used at all, e.g. at program exit.
;;
;; Update: now you have to call as (with-ndb-api-initialisation (:there-is-only-one t) ...)
;; to get this behavior at all.
;;(print 'free/ndb-api-begin *trace-output*) (time)
(when there-is-only-one
#+ndbapi-verbose
(when ndbapi.types:*ndbapi-verbose*
(format *trace-output* "~&WITH-NDB-API-INITIALISATION: force freeing of *ndb-api-initialisation* as :THERE-IS-ONLY-ONE ~a"
there-is-only-one))
(ndb-api-end))))))
(defmacro with-ndb-cluster-connection ((var (connection-string) ;; do not support more args for now,
;;; as new-ndb-cluster-connection has
;;; no unique arity (extension of RonDB)
&key name
connect-args
wait-until-ready-args
(connect-and-wait-p t))
&body body)
"If VAR is bound, it will be reused;
(This only works for dynamic (special) variables, not for lexical ones.
If the variable is just lexical, a new connection will be made.)
It is suggested to use the special variable ndbapi:*connection* in most cases.
Also sets up the cluster connection with a name
(using *default-connection-name* when not explicitly specified),
and also calling connect and wait-until-ready
unless CONNECT-AND-WAIT-P is explicitly set to NIL"
(let ((op (gensym "OP-")))
`(flet ((,op (,var) ,@body))
(declare (dynamic-extent #',op))
(call-with-ndb-cluster-connection #',op
(when (boundp ',var) (symbol-value ',var))
(list ,connection-string)
,name
(list ,@connect-args)
(list ,@wait-until-ready-args)
,connect-and-wait-p))))
(defun call-with-ndb-cluster-connection (op value args &optional name connect-args wait-until-ready-args (connect-and-wait-p t))
(declare (dynamic-extent value args name connect-args wait-until-ready-args connect-and-wait-p))
;;(print 'new-ndb-cluster-connection *trace-output*) (time)
(ensure-ndb-api-initialisation)
(if (valid-connection-p value)
(funcall op value)
(let ((value (apply #'new-ndb-cluster-connection *ndb-api-initialisation* args)))
(unwind-protect
(progn
(ndb-cluster-connection-set-name value name)
(when connect-and-wait-p
(apply #'ndb-cluster-connection-connect value connect-args)
(apply #'ndb-cluster-connection-wait-until-ready value wait-until-ready-args))
(funcall op value))
;;(print 'free/new-ndb-cluster-connection *trace-output*) (time)
(ndb-free-object value)))))
;;; simple connection interace - begin
(defun connect (connection-string &key name
connect-args
wait-until-ready-args
connection)
"create new cluster connection and initialize it with name, connect and wait-until-ready;
optionally you can specify an existing connection with the keyword argument :connection,
if it refers to an existing and valid connection, it will be passed-through."
(ensure-ndb-api-initialisation)
(if (ndbapi.i::valid-connection-p connection)
connection
(let ((value (new-ndb-cluster-connection *ndb-api-initialisation* connection-string)))
(ndb-cluster-connection-set-name value name)
(apply #'ndb-cluster-connection-connect value connect-args)
(apply #'ndb-cluster-connection-wait-until-ready value wait-until-ready-args)
value)))
(defun disconnect (connection)
"it is safe to be called repeatedly"
(ndb-free-object connection))
(defvar *connection* nil)
(defvar *connection-lock* (bt:make-lock "*connection* lock"))
;; DISCONNECT doesn't hold the lock as it only invalidates the connection object,
;; but does not change the value of the *connection* variable.
(defun ensure-connection (connection-string &rest args
&key name
connect-args
wait-until-ready-args)
"safely initializes NDB API (that is, only once)
Works only with one connection in ndbapi:*connection* in total,
if you need more then one, use ndbapi:connect, which can also
pass-through an existing connection with the keyword argument :connection."
(declare (ignorable name connect-args wait-until-ready-args))
(bt:with-lock-held (*connection-lock*)
(if (ndbapi.i::valid-connection-p *connection*)
*connection*
(setf *connection* (apply #'connect connection-string args)))))
(setf (macro-function 'with-connection)
(macro-function 'with-ndb-cluster-connection))
;;; simple connection interace - end
(defvar *ndb* nil)
(defmacro with-ndb ((var (cluster-connection &rest more-new-ndb-args) &key max-no-of-transactions (ndb-init-p t)) &body body)
"If VAR is bound, it will be reused;
(This only works for dynamic (special) variables, not for lexical ones.
If the variable is just lexical, a new connection will be made.)
It is suggested to use the special variable ndbapi:*transaction* in most cases."
(let ((op (gensym "OP-")))
`(flet ((,op (,var) ,@body))
(declare (dynamic-extent #',op))
(call-with-ndb #',op
(when (boundp ',var) (symbol-value ',var))
,cluster-connection
(list ,@more-new-ndb-args) ;; need to convert (foo), a call form, to (list foo), a list form
,max-no-of-transactions
,ndb-init-p))))
(defun call-with-ndb (op value cluster-connection &optional more-new-ndb-args max-no-of-transactions (ndb-init-p t))
(declare (dynamic-extent value cluster-connection more-new-ndb-args max-no-of-transactions ndb-init-p))
;;(print 'new-ndb *trace-output*) (time)
(if (valid-ndb-p value)
(funcall op value)
(let ((value (apply #'new-ndb cluster-connection more-new-ndb-args)))
(unwind-protect
(progn
(when ndb-init-p
(apply #'ndb-init value (when max-no-of-transactions
(list max-no-of-transactions))))
(funcall op value))
;;(print 'free/new-ndb *trace-output*) (time)
(ndb-free-object value)))))
(defvar *transaction* nil)
(defmacro with-ndb-transaction ((var &rest args) &body body)
"If VAR is bound, it will be reused;
(This only works for dynamic (special) variables, not for lexical ones.
If the variable is just lexical, a new connection will be made.)
It is suggested to use the special variable ndbapi:*transaction* in most cases."
(let ((op (gensym "OP-")))
`(flet ((,op (,var) ,@body))
(declare (dynamic-extent #',op))
(call-with-ndb-transaction #',op (when (boundp ',var) (symbol-value ',var)) ,@args))))
(defun call-with-ndb-transaction (op value &rest args)
(declare (dynamic-extent args))
(if (valid-object-p value)
(funcall op value)
(let ((value (apply #'ndb-start-transaction args)))
(unwind-protect
(funcall op value)
;; returns no value
(ndbapi.ffi:ndb-close-transaction (ndbapi.ffi:ndb-transaction-get-ndb value) value)))))
(defmacro with-ndb-transaction-scan-index ((var (&rest open-args) (&rest close-args)) &body body)
(let ((op (gensym "OP-")))
`(flet ((,op (,var) ,@body))
(declare (dynamic-extent #',op))
(call-with-ndb-transaction-scan-index #',op
:open-args (list ,@open-args)
:close-args (list ,@close-args)))))
(defun call-with-ndb-transaction-scan-index (op &key open-args close-args)
(declare (dynamic-extent open-args close-args))
(let ((value (apply #'ndb-transaction-scan-index open-args)))
(unwind-protect (funcall op value)
;; returns no value
(apply #'ndb-scan-operation-close value close-args))))
(defmacro with-ndb-transaction-get-ndb-index-scan-operation ((var (&rest open-args) (&rest close-args)) &body body)
(let ((op (gensym "OP-")))
`(flet ((,op (,var) ,@body))
(declare (dynamic-extent #',op))
(call-with-ndb-transaction-get-ndb-index-scan-operation #',op
:open-args (list ,@open-args)
:close-args (list ,@close-args)))))
(defun call-with-ndb-transaction-get-ndb-index-scan-operation (op &key open-args close-args)
(declare (dynamic-extent open-args close-args))
(let ((value (apply #'ndb-transaction-get-ndb-index-scan-operation open-args)))
(unwind-protect (funcall op value)
;; returns no value
(apply #'ndb-scan-operation-close value close-args))))
(defmacro with-ndb-transaction-scan-table ((var (&rest open-args) (&rest close-args)) &body body)
(let ((op (gensym "OP-")))
`(flet ((,op (,var) ,@body))
(declare (dynamic-extent #',op))
(call-with-ndb-transaction-scan-table #',op
:open-args (list ,@open-args)
:close-args (list ,@close-args)))))
(defun call-with-ndb-transaction-scan-table (op &key open-args close-args)
(declare (dynamic-extent open-args close-args))
(let ((value (apply #'ndb-transaction-scan-table open-args)))
(unwind-protect (funcall op value)
;; returns no value
(apply #'ndb-scan-operation-close value close-args))))
(defmacro ndbapi.ic:with-code ((var &rest args) &body body)
(let ((op (gensym "OP-")))
`(flet ((,op (,var) ,@body))
(declare (dynamic-extent #',op))
(ndbapi.ic::call-with-code #',op ,@args))))
(defun ndbapi.ic::call-with-code (op &rest args)
(declare (dynamic-extent args))
(let ((value (apply #'ndbapi.ic:new *ndb-api-initialisation* args)))
(unwind-protect (funcall op value)
(ndb-free-object value))))
;;; more advanced commands
(defun get-index-names (database-name table-name)
(with-ndb (ndb (*connection* database-name))
(ndbapi.types::with-foreign-struct (list (list :count 0) '(:struct ndbapi.ffi::list))
(dictionary-list-indexes (ndb-get-dictionary ndb) list table-name)
(loop with count = (cffi:foreign-slot-value list '(:struct ndbapi.ffi::list) :count)
with elements = (cffi:foreign-slot-value list '(:struct ndbapi.ffi::list) :elements)
with field = :name
for i below count
for element = (cffi:mem-aptr elements '(:struct ndbapi.ffi::element) i)
collect (cffi:foreign-slot-value element '(:struct ndbapi.ffi::element) field)))))