changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/ffi/rocksdb/merge.lisp

changeset 597: 5b2ca5b2a9db
parent: 5bd0eb9fa1fa
child: c7f9bfc9570f
author: Richard Westhaver <ellis@rwest.io>
date: Thu, 15 Aug 2024 23:36:34 -0400
permissions: -rw-r--r--
description: rocksdb merge-op progress
1 ;;; rocksdb/merge.lisp --- RocksDB Merge Operators
2 
3 ;; RocksDB Lisp Merge Operator API
4 
5 ;;; Commentary:
6 
7 ;; When to use built-in ROCKSDB-MERGE:
8 
9 ;; - You have data that needs to be incrementally updated.
10 
11 ;; - You would usually need to read the data before knowing what the new value would be.
12 
13 ;; Oterwise as far as the FFI is concerned - which doesn't support
14 ;; AssociateMerge, you should use the Generic Merge API.
15 
16 ;; When to use Associative Merge (unavailable in C/LISP API):
17 
18 ;; - merge operands are formatted the same as Put values AND
19 
20 ;; - it is okay to combine multiple operands into one
21 
22 ;; When to use Generic Merge (this API):
23 
24 ;; - you are unable to use Associate Merge
25 
26 ;; - it is possible to combine multiple operands
27 
28 ;;; Refs:
29 
30 ;; impl: https://github.com/facebook/rocksdb/wiki/Merge-Operator-Implementation
31 
32 ;; wiki: https://github.com/facebook/rocksdb/wiki/merge-operator
33 
34 ;;; Code:
35 (in-package :rocksdb)
36 
37 #|
38 Gives the client a way to express the read -> modify -> write semantics
39 key: (IN) The key that's associated with this merge operation.
40 existing: (IN) null indicates that the key does not exist before this op
41 operand_list:(IN) the sequence of merge operations to apply, front() first.
42 new_value: (OUT) Client is responsible for filling the merge result here
43 logger: (IN) Client could use this to log errors during merge.
44 
45 Return true on success. Return false failure / error / corruption.
46 |#
47 ;; FullMerge() is used when a Put/Delete is the *existing_value (or null)
48 (define-alien-type rocksdb-full-merge-function
49  (function (* t)
50  (array unsigned-char)
51  size-t
52  (array (array unsigned-char))
53  (array size-t)
54  int
55  (array unsigned-char)
56  (* size-t)))
57 
58 #|
59 This function performs merge(left_op, right_op)
60 when both the operands are themselves merge operation types.
61 Save the result in *new_value and return true. If it is impossible
62 or infeasible to combine the two operations, return false instead.
63 |#
64 ;; PartialMerge() is used to combine two-merge operands (if possible)
65 (define-alien-type rocksdb-partial-merge-function
66  (function (* t)
67  (array unsigned-char)
68  size-t
69  (array (array unsigned-char))
70  (array size-t)
71  int
72  (array unsigned-char)
73  (* size-t)))
74 
75 (define-alien-type rocksdb-delete-value-function
76  (function (* t)
77  (array unsigned-char)
78  size-t))
79 
80 (define-alien-type rocksdb-destructor-function
81  (function void (* t)))
82 
83 #|
84 The name of the MergeOperator. Used to check for MergeOperator
85 mismatches (i.e., a DB created with one MergeOperator is
86 accessed using a different MergeOperator)
87 |#
88 (define-alien-type rocksdb-name-function
89  (function c-string))
90 
91 (deftype rocksdb-merge-operands () '(array (octet-vector)))
92 
93 ;; (sb-alien::define-alien-callable mangle int () 0)
94 
95 (define-alien-routine rocksdb-mergeoperator-create (* rocksdb-mergeoperator)
96  (state (* t))
97  (destructor (* rocksdb-destructor-function))
98  (full-merge (* rocksdb-full-merge-function))
99  (partial-merge (* rocksdb-partial-merge-function))
100  (delete-value (* rocksdb-delete-value-function))
101  (name (* rocksdb-name-function)))
102 
103 #| [[file:~/dev/comp/core/c/rocksdb.h::/* Merge Operator */]] |#
104 
105 (define-alien-routine rocksdb-mergeoperator-destroy void (self (* rocksdb-mergeoperator)))
106 
107 (export '(rocksdb-mergeoperator-create rocksdb-mergeoperator-destroy
108  rocksdb-full-merge-function rocksdb-partial-merge-function
109  rocksdb-delete-value-function rocksdb-destructor-function))
110 
111 ;; TODO 2023-12-11:
112 (deftype rocksdb-mergeoperator-function ()
113  '(function (octet-vector (or octet-vector null) &rest t) (or null octet-vector)))
114 
115 (define-alien-callable rocksdb-delete-value (* t)
116  ((val (array unsigned-char))
117  (vlen size-t))
118  (declare (ignore val vlen))
119  nil)
120 
121 (define-alien-callable rocksdb-destructor void ((self (* t)))
122  (free-alien self))
123 
124 (define-alien-callable rocksdb-name c-string () (make-alien-string (symbol-name (gensym "rocksdb:"))))
125 
126 (define-alien-callable rocksdb-concat-full-merge boolean
127  ((key (array unsigned-char))
128  (klen size-t)
129  (existing-val (array unsigned-char))
130  (existing-vlen size-t)
131  (ops (array (array unsigned-char)))
132  (ops-length (* size-t))
133  (num-ops size-t)
134  (success (array unsigned-char))
135  (new-vlen (* size-t)))
136  (log:debug! (list key klen existing-val existing-vlen ops ops-length num-ops success new-vlen))
137  1)
138 
139 (define-alien-callable rocksdb-concat-partial-merge boolean
140  ((key (array unsigned-char))
141  (klen size-t)
142  (ops (array (array unsigned-char)))
143  (ops-length (* size-t))
144  (num-ops size-t)
145  (success (array unsigned-char))
146  (new-vlen (* size-t)))
147  (log:debug! (list key klen ops ops-length num-ops success new-vlen))
148  0)