Skip to content
Newer
Older
100644 304 lines (239 sloc) 14.9 KB
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
1 ;;; -*- Mode: lisp; Syntax: ansi-common-lisp; Base: 10; Package: de.setf.cassandra; -*-
2
3 (in-package :de.setf.cassandra)
4
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
5 ;;; This file defines column family operators for the 'de.setf.cassandra' library.
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
6 ;;; (c) 2010 james anderson
7 ;;;
8 ;;; 'de.setf.cassandra' is free software: you can redistribute it and/or modify
9 ;;; it under the terms of the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, either version 3 of the License, or
11 ;;; (at your option) any later version.
12 ;;;
13 ;;; 'de.setf.cassandra' is distributed in the hope that it will be useful,
14 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU Lesser General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU Lesser General Public License along
19 ;;; with 'de.setf.cassandra' as 'lgpl.txt'. If not, see the GNU [site](http://www.gnu.org/licenses/).
20
8e2c051 @lisp work on super columns
authored Jul 23, 2010
21 ;;;
22 ;;; interface
23
e56656d @lisp fill out package exports; key-slice -> keyslice
authored Jul 31, 2010
24 ;;; alternative:
25 ;;; cf-get : returns a column
26 ;;; cf-get-string : returns a string value
27 ;;; cf-get-value : returns the raw value
28 ;;; fc-get-as 'type :
29
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
30 (defgeneric get-column (column-family key column-name)
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
31 (:documentation "Given a COLUMN-FAMILY, KEY, and COLUMN-NAME, retrieve the designated attribute 'column'
32 from the family's store. This returns the column struct, which comprises the name, value, and timestamp.
33 A simple column-family requires atomic keys, while a super-column permits either the atomic family key or
34 a list of family-key and super-column key."))
8e2c051 @lisp work on super columns
authored Jul 23, 2010
35
36
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
37 (defgeneric get-attribute (column-family key column-name)
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
38 (:documentation "Given a COLUMN-FAMILY, KEY, and COLUMN-NAME, retrieve the designated attribute value
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
39 from the family's store. This retrieves the attribute 'column' and returns just its value."))
8e2c051 @lisp work on super columns
authored Jul 23, 2010
40
41
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 28, 2010
42 (defgeneric set-attribute (column-family key column-name value)
43 (:documentation "Given a COLUMN-FAMILY, KEY, COLUMN-NAME, VALUE, store the
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
44 designated attribute 'column' in the family's store. The timestamp defaults to the UUID-V1 timestamp.
45 A simple column-family requires atomic keys, while a super-column requires a list of family-key and
46 super-column key.") )
47
48
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
49 (defgeneric get-columns (column-family key &key start finish column-names reversed count)
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
50 (:documentation "Given a COLUMN-FAMILY, KEY, and a combination of START and FINISH column names, a
51 COLUMN-NAME list, REVERSED indicator, and a COUNT, retrieve the designated attribute 'columns' from the
52 family's store. (see cassandra:get-silce.) A simple column-family requires atomic an key, while a super-column
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
53 permits either the atomic family key or a list of family-key and super-column key.
54 In the former case, the respective elements are of the form ((key supercolumn-key) . column*)."))
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
55
56
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
57 (defgeneric get-attributes (column-family key &key start finish column-names reversed count)
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
58 (:documentation "Given a COLUMN-FAMILY, a KEY, and constraints as for get-attributes, retrieve the
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
59 attribute 'columns' and of those the respective values."))
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
60
61
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
62 (defgeneric map-columns (operator column-family key &key start finish column-names reversed count)
63 (:documentation "Given on OPERATOR, a COLUMN-FAMILY, KEY, and a combination of START and FINISH column names, a
64 COLUMN-NAME list, REVERSED indicator, and a COUNT, map the operator over the designated attribute 'columns' from the
65 family's store. (see get-slice and getrange-slices.) A simple column-family requires atomic an key and yields
66 elementary columns, while a super-column permits either the atomic family key or a list of family-key and super-column key.
67 In the former case, the respective elements are of the form ((key supercolumn-key) . column*)."))
68
69
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
70 (defgeneric set-attributes (column-family key &rest property-list)
71 (:documentation "Given a COLUMN-FAMILY, a key, and a property list of column names and values,
72 store the designated attribute 'columns' in the family's store. The first property can be :timestamp,
73 to specify the column timestamp, for which the default is the UUID-V1 timestamp."))
74
75
76 ;;;
77 ;;; classes
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
78 ;;; column-famiy
79 ;;; standard-column-family
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
80 ;;; super-column-family
81
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
82 (defclass column-family ()
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
83 ((keyspace
84 :initform (error "keyspace is required.") :initarg :keyspace
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
85 :reader column-family-keyspace
86 :type keyspace
87 :documentation "The column's keyspace instance. All operations are delegated through this keyspace
88 to the respective store.")
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
89 (name
90 :initform (error "name is required.") :initarg :name
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
91 :accessor column-family-name
92 :documentation "The column family name.[http://wiki.apache.org/cassandra/DataModel#Column_Families].")
cfaaa50 @lisp work on string/binary encoding variations
authored Jul 28, 2010
93 (type
94 :reader column-family-type)
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
95 (slice-size
96 :accessor column-family-slice-size
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
97 :type i32
98 :documentation "The default count to use for retrieval operations which return multiple values.
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
99 If no value is provided, the default is adopted from the column family's key space."))
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
100
101 (:documentation "The abstract class defines the slots shared by the column-family and super-column-family
102 classes to bind the store keyspace, the family name and the structs and parameters used in access operations."))
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
103
8e2c051 @lisp work on super columns
authored Jul 23, 2010
104
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
105 (defclass standard-column-family (column-family)
cfaaa50 @lisp work on string/binary encoding variations
authored Jul 28, 2010
106 ((type :initform :standard :allocation :class))
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
107 (:documentation "A standard-column-family represents a single-level cassandra hash."))
8e2c051 @lisp work on super columns
authored Jul 23, 2010
108
109
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
110 (defclass super-column-family (column-family)
cfaaa50 @lisp work on string/binary encoding variations
authored Jul 28, 2010
111 ((type :initform :super :allocation :class))
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
112 (:documentation "A super-column-family represents a two-level cassandra hash."))
8e2c051 @lisp work on super columns
authored Jul 23, 2010
113
114
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
115 ;;; generic operations
8e2c051 @lisp work on super columns
authored Jul 23, 2010
116
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
117 (defmethod initialize-instance :after ((instance column-family) &key
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
118 (slice-size (keyspace-slice-size (column-family-keyspace instance))))
cfaaa50 @lisp work on string/binary encoding variations
authored Jul 28, 2010
119 (setf (column-family-slice-size instance) slice-size)
120 (let* ((ks (column-family-keyspace instance))
121 (column-description (assoc (column-family-name instance) (keyspace-description ks) :test #'string-equal)))
122 (cond ((string-equal (column-family-type instance) (rest (assoc "Type" (rest column-description) :test #'string-equal))))
123 (column-description
124 (error "Invalid column type: ~s: ~s."
125 instance column-description))
126 (t
127 (error "Invalid column: ~s (~s): ~s ~s."
128 instance (column-family-name instance) ks (mapcar #'first (keyspace-description ks)))))))
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
129
e27dee3 @lisp renamed column exmaple readme.
authored Jul 29, 2010
130 (defmethod print-object ((object column-family) stream)
131 (print-unreadable-object (object stream :identity t :type t)
132 (format stream "~s" (when (slot-boundp object 'name) (column-family-name object)))))
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
133
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
134 (defmethod get-attribute ((column-family column-family) key column-name)
135 (column-value (get-column column-family key column-name)))
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
136
137
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
138 (defmethod get-attributes ((column-family column-family) key &rest args)
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
139 (declare (dynamic-extent args))
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
140 (labels ((walk-value (object)
21ba020 @lisp sketch of spoc-based indexing. fails due to non-unique relations.
authored Jul 26, 2010
141 (typecase object
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
142 (cons (cons (walk-value (first object)) (walk-value (rest object))))
cfaaa50 @lisp work on string/binary encoding variations
authored Jul 28, 2010
143 ((or cassandra_2.1.0:column cassandra_8.3.0:column)
144 (cons (column-name object) (column-value object)))
21ba020 @lisp sketch of spoc-based indexing. fails due to non-unique relations.
authored Jul 25, 2010
145 (t object))))
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
146 (mapcar #'walk-value (apply #'get-columns column-family key args))))
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
147
da5804c @lisp fix set-keyspace-column-family load order
authored Jul 31, 2010
148 (defmethod set-keyspace-column-family ((keyspace keyspace) (cf column-family) slot-name
149 &key (class 'standard-column-family) required)
150 (declare (ignore required))
151 (assert (typep cf class) () "Supplied column family is not the correct type: ~s: ~s: ~s." slot-name cf class)
152 (setf (slot-value keyspace slot-name) cf))
153
154
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
155
8e2c051 @lisp work on super columns
authored Jul 23, 2010
156 ;;;
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
157 ;;; standard column family operators expect the column key to map to a sequence of columns.
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
158
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
159 (defmethod get-column ((family standard-column-family) key column-name)
160 (columnorsupercolumn-column (get (column-family-keyspace family)
161 :column-family (column-family-name family) :key key :column column-name)))
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
162
163
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
164 (defmethod set-attribute ((family standard-column-family) key column-name column-value)
165 (insert (column-family-keyspace family)
166 :column-family (column-family-name family) :key key
167 :column column-name :value column-value))
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
168
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
169
170 (defmethod set-attribute ((family standard-column-family) key column-name (value null))
8e2c051 @lisp work on super columns
authored Jul 23, 2010
171 "Given a null value, delete the column"
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
172 (remove (column-family-keyspace family)
173 :column-family (column-family-name family) :key key
174 :column column-name))
175
176
177 (defmethod get-columns ((family standard-column-family) key &rest args
178 &key start finish column-names reversed (count (column-family-slice-size family)))
179 (declare (ignore start finish column-names reversed)
180 (dynamic-extent args))
181 (mapcar #'columnorsupercolumn-column
182 (apply #'get-slice (column-family-keyspace family)
183 :column-family (column-family-name family) :key key
184 :count count
185 args)))
186
187
188 (defmethod map-columns (op (family standard-column-family) key &rest args
189 &key start finish column-names reversed (count (column-family-slice-size family)))
190 (declare (ignore start finish column-names reversed)
191 (dynamic-extent args))
192 (flet ((coerce-cosc (cosc) (funcall op (columnorsupercolumn-column cosc))))
193 (declare (dynamic-extent #'coerce-cosc))
194 (apply #'map-slice #'coerce-cosc (column-family-keyspace family)
195 :column-family (column-family-name family) :key key
196 :count count
197 args)))
198
199
200 (defmethod set-attributes ((column-family standard-column-family) key &rest property-list)
201 (batch-mutate (column-family-keyspace column-family)
202 :mutation-map (apply #'make-standard-mutation-map (column-family-keyspace column-family)
203 (column-family-name column-family) key
204 property-list)))
205
8e2c051 @lisp work on super columns
authored Jul 23, 2010
206
207 ;;;
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
208 ;;; super-column family methods expect the first key to identify a sequence of super columns, from which the
209 ;;; second key selects a specific super-column which contains the column set
8e2c051 @lisp work on super columns
authored Jul 23, 2010
210
634e7a8 @lisp another api alternative which distinguishes acces to literal columns …
authored Jul 24, 2010
211 (defmethod get-column ((family super-column-family) (keys cons) column-name)
8e2c051 @lisp work on super columns
authored Jul 23, 2010
212 (destructuring-bind (key super-column) keys
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
213 (columnorsupercolumn-column (get (column-family-keyspace family)
214 :column-family (column-family-name family) :key key
215 :super-column super-column :column column-name))))
8e2c051 @lisp work on super columns
authored Jul 23, 2010
216
217
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
218 (defmethod set-attribute ((family super-column-family) (keys cons) column-name column-value)
8e2c051 @lisp work on super columns
authored Jul 23, 2010
219 (destructuring-bind (key super-column) keys
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
220 (insert (column-family-keyspace family)
221 :column-family (column-family-name family) :key key :super-column super-column
222 :column column-name :value column-value)))
223
224
225 (defmethod set-attribute ((family super-column-family) (keys cons) column-name (value null))
8e2c051 @lisp work on super columns
authored Jul 23, 2010
226 "Given a null value, delete the column"
227 (destructuring-bind (key super-column) keys
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
228 (remove (column-family-keyspace family)
229 :column-family (column-family-name family) :key key :super-column super-column
230 :column column-name)))
231
232
e56656d @lisp fill out package exports; key-slice -> keyslice
authored Jul 31, 2010
233 ;;;!!! combine the list v/s atomic key into one method which allows also a null row key with a supercolumn key
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
234 (defmethod get-columns ((family super-column-family) (keys cons) &rest args
235 &key start finish column-names reversed (count (column-family-slice-size family)))
236 "Where the key is a list, the first element is the family key and the second is the syper-column key."
237 (declare (ignore start finish column-names reversed)
238 (dynamic-extent args))
239 (destructuring-bind (key super-column) keys
240 (mapcar #'columnorsupercolumn-column
241 (apply #'get-slice (column-family-keyspace family)
242 :column-family (column-family-name family) :key key :super-column super-column
243 :count count
244 args))))
8e2c051 @lisp work on super columns
authored Jul 23, 2010
245
246
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
247 (defmethod map-columns (op (family super-column-family) (keys cons) &rest args
248 &key start finish column-names reversed (count (column-family-slice-size family)))
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
249 "Where the key is a list, the first element is the family key and the second is the syper-column key."
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
250 (declare (ignore start finish column-names reversed)
251 (dynamic-extent args))
8e2c051 @lisp work on super columns
authored Jul 23, 2010
252 (destructuring-bind (key super-column) keys
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
253 (flet ((coerce-cosc (cosc) (funcall op (columnorsupercolumn-column cosc))))
254 (declare (dynamic-extent #'coerce-cosc))
255 (apply #'map-slice #'coerce-cosc (column-family-keyspace family)
256 :column-family (column-family-name family) :key key :super-column super-column
257 :count count
258 args))))
259
260
261 (defmethod get-columns ((family super-column-family) key &rest args
262 &key start finish column-names reversed (count (column-family-slice-size family)))
263 "Where the key is atomic, it applies to all supercolumns. The result is then a list of super-column results
264 in which each entry has the form (supercolumn . columns)."
265 (declare (ignore start finish column-names reversed)
266 (dynamic-extent args))
267 (loop for key-slice in (apply #'get-range-slices (column-family-keyspace family)
268 :column-family (column-family-name family)
cfaaa50 @lisp work on string/binary encoding variations
authored Jul 28, 2010
269 :start-key (or key "") :finish-key (or key "")
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
270 :count count
271 args)
272 for key = (keyslice-key key-slice)
e56656d @lisp fill out package exports; key-slice -> keyslice
authored Jul 31, 2010
273 append (loop for cosc in (keyslice-columns key-slice)
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
274 for sc = (columnorsupercolumn-super-column cosc)
275 collect (cons (list key (supercolumn-name sc))
276 (supercolumn-columns sc)))))
277
278
279 (defmethod map-columns (op (family super-column-family) key &rest args
280 &key start finish column-names reversed (count (column-family-slice-size family)))
281 "Where the key is atomic, it applies to all supercolumns. The result is then a list of super-column results
282 in which each entry has the form (supercolumn . columns)."
283 (declare (ignore start finish column-names reversed)
284 (dynamic-extent args))
285 (flet ((do-key-slice (key-slice)
286 (let ((key (keyslice-key key-slice)))
e56656d @lisp fill out package exports; key-slice -> keyslice
authored Jul 31, 2010
287 (loop for cosc = (keyslice-columns key-slice)
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
288 for sc = (columnorsupercolumn-super-column cosc)
289 do (funcall op (cons (list key (supercolumn-name sc)) (supercolumn-columns sc)))))))
290 (declare (dynamic-extent #'do-key-slice))
291 (apply #'map-range-slices #'do-key-slice (column-family-keyspace family)
292 :column-family (column-family-name family)
cfaaa50 @lisp work on string/binary encoding variations
authored Jul 28, 2010
293 :start-key (or key "") :finish-key (or key "")
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
294 :count count
295 args)))
21ba020 @lisp sketch of spoc-based indexing. fails due to non-unique relations.
authored Jul 25, 2010
296
7a8b33b @lisp initial repository version for push; cassandra 0.6.3/2.1.0
authored Jul 21, 2010
297
a4edb6b @lisp docstrings; add timestamp to the interface
authored Jul 24, 2010
298 (defmethod set-attributes ((family super-column-family) (keys cons) &rest property-list)
299 (destructuring-bind (key super-column) keys
e400bdb @lisp change API to permit multiple simultaneous service/line protocol vers…
authored Jul 27, 2010
300 (batch-mutate (column-family-keyspace family)
301 :mutation-map (apply #'make-super-mutation-map (column-family-keyspace family)
302 (column-family-name family) key super-column
303 property-list))))
Something went wrong with that request. Please try again.