1: <?php
2: namespace phpcassa;
3:
4: use phpcassa\ColumnFamily;
5: use phpcassa\ColumnSlice;
6:
7: use cassandra\Deletion;
8: use cassandra\ColumnParent;
9: use cassandra\ColumnPath;
10: use cassandra\CounterColumn;
11: use cassandra\CounterSuperColumn;
12: use cassandra\ColumnOrSuperColumn;
13: use cassandra\Column;
14: use cassandra\SuperColumn;
15:
16: 17: 18: 19: 20: 21: 22: 23:
24: class SuperColumnFamily extends AbstractColumnFamily {
25:
26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39:
40: public function get_super_column($key,
41: $super_column,
42: $column_slice=null,
43: $column_names=null,
44: $consistency_level=null) {
45:
46: $cp = $this->create_column_parent($super_column);
47: $slice = $this->create_slice_predicate($column_names, $column_slice, false);
48: return $this->_get($key, $cp, $slice, $consistency_level);
49: }
50:
51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68:
69: public function multiget_super_column($keys,
70: $super_column,
71: $column_slice=null,
72: $column_names=null,
73: $consistency_level=null,
74: $buffer_size=16) {
75:
76: $cp = $this->create_column_parent($super_column);
77: $slice = $this->create_slice_predicate($column_names, $column_slice, false);
78:
79: return $this->_multiget($keys, $cp, $slice, $consistency_level, $buffer_size);
80: }
81:
82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93:
94: public function get_subcolumn_count($key,
95: $super_column,
96: $column_slice=null,
97: $column_names=null,
98: $consistency_level=null) {
99:
100: $cp = $this->create_column_parent($super_column);
101: $slice = $this->create_slice_predicate(
102: $column_names, $column_slice, false, ColumnSlice::MAX_COUNT);
103:
104: return $this->_get_count($key, $cp, $slice, $consistency_level);
105: }
106:
107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119:
120: public function multiget_subcolumn_count($keys,
121: $super_column,
122: $column_slice=null,
123: $column_names=null,
124: $consistency_level=null) {
125:
126: $cp = $this->create_column_parent($super_column);
127: $slice = $this->create_slice_predicate(
128: $column_names, $column_slice, false, ColumnSlice::MAX_COUNT);
129:
130: return $this->_multiget_count($keys, $cp, $slice, $consistency_level);
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: public function get_super_column_range($super_column,
156: $key_start="",
157: $key_finish="",
158: $row_count=self::DEFAULT_ROW_COUNT,
159: $column_slice=null,
160: $column_names=null,
161: $consistency_level=null,
162: $buffer_size=null) {
163:
164: $cp = $this->create_column_parent($super_column);
165: $slice = $this->create_slice_predicate($column_names, $column_slice, false);
166:
167: return $this->_get_range($key_start, $key_finish, $row_count,
168: $cp, $slice, $consistency_level, $buffer_size);
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190:
191: public function add($key, $super_column, $column, $value=1,
192: $consistency_level=null) {
193: $packed_key = $this->pack_key($key);
194: $cp = $this->create_column_parent($super_column);
195: $counter = new CounterColumn();
196: $counter->name = $this->pack_name($column);
197: $counter->value = $value;
198: return $this->pool->call("add", $packed_key, $cp, $counter,
199: $this->wcl($consistency_level));
200: }
201:
202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214:
215: public function remove_super_column($key, $super_column, $subcolumns=null,
216: $consistency_level=null) {
217:
218: if ($subcolumns === null || count($subcolumns) == 1) {
219: $cp = new ColumnPath();
220: $cp->column_family = $this->column_family;
221: $cp->super_column = $this->pack_name($super_column, true);
222: if ($subcolumns !== null) {
223: $cp->column = $this->pack_name($subcolumns[0], false);
224: }
225: return $this->_remove_single($key, $cp, $consistency_level);
226: } else {
227: $deletion = new Deletion();
228: $deletion->super_column = $this->pack_name($super_column, true);
229: if ($subcolumns !== null) {
230: $predicate = $this->create_slice_predicate($subcolumns, null, false);
231: $deletion->predicate = $predicate;
232: }
233: return $this->_remove_multi($key, $deletion, $consistency_level);
234: }
235: }
236:
237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252:
253: public function remove_counter($key, $super_column, $column=null,
254: $consistency_level=null) {
255: $cp = new ColumnPath();
256: $packed_key = $this->pack_key($key);
257: $cp->column_family = $this->column_family;
258: $cp->super_column = $this->pack_name($super_column, true);
259: if ($column !== null)
260: $cp->column = $this->pack_name($column);
261: $this->pool->call("remove_counter", $packed_key, $cp,
262: $this->wcl($consistency_level));
263: }
264:
265: protected function dict_to_coscs($data, $timestamp, $ttl) {
266: $have_counters = $this->has_counters;
267: $ret = array();
268: foreach ($data as $name => $value) {
269: $cosc = new ColumnOrSuperColumn();
270: if($have_counters) {
271: $sub = new CounterSuperColumn();
272: $cosc->counter_super_column = $sub;
273: } else {
274: $sub = new SuperColumn();
275: $cosc->super_column = $sub;
276: }
277: $sub->name = $this->pack_name($name, true, self::NON_SLICE, true);
278: $sub->columns = $this->dict_to_columns($value, $timestamp, $ttl);
279: $ret[] = $cosc;
280: }
281:
282: return $ret;
283: }
284:
285: protected function array_to_coscs($data, $timestamp, $ttl) {
286: $have_counters = $this->has_counters;
287: $ret = array();
288: foreach ($data as $supercol) {
289: list($name, $columns) = $supercol;
290: $cosc = new ColumnOrSuperColumn();
291: if($have_counters) {
292: $sub = new CounterSuperColumn();
293: $cosc->counter_super_column = $sub;
294: } else {
295: $sub = new SuperColumn();
296: $cosc->super_column = $sub;
297: }
298: $sub->name = $this->pack_name($name, true, self::NON_SLICE, false);
299: $sub->columns = $this->array_to_columns($columns, $timestamp, $ttl);
300: $ret[] = $cosc;
301: }
302:
303: return $ret;
304: }
305:
306: protected function dict_to_columns($array, $timestamp, $ttl) {
307: $ret = array();
308: foreach($array as $name => $value) {
309: if($this->has_counters) {
310: $column = new CounterColumn();
311: } else {
312: $column = new Column();
313: $column->timestamp = $timestamp;
314: $column->ttl = $ttl;
315: }
316: $column->name = $this->pack_name(
317: $name, false, self::NON_SLICE, true);
318: $column->value = $this->pack_value($value, $column->name);
319: $ret[] = $column;
320: }
321: return $ret;
322: }
323:
324: protected function array_to_columns($array, $timestamp, $ttl) {
325: $ret = array();
326: foreach($array as $col) {
327: list($name, $value) = $col;
328: if($this->has_counters) {
329: $column = new CounterColumn();
330: } else {
331: $column = new Column();
332: $column->timestamp = $timestamp;
333: $column->ttl = $ttl;
334: }
335: $column->name = $this->pack_name(
336: $name, false, self::NON_SLICE, false);
337: $column->value = $this->pack_value($value, $column->name);
338: $ret[] = $column;
339: }
340: return $ret;
341: }
342:
343: protected function coscs_to_dict($array_of_coscs) {
344: $ret = array();
345: $first = $array_of_coscs[0];
346: if($first->column) {
347: foreach($array_of_coscs as $cosc) {
348: $name = $this->unpack_name($cosc->column->name, false);
349: $value = $this->unpack_value($cosc->column->value, $cosc->column->name);
350: $ret[$name] = $value;
351: }
352: } else if($first->super_column) {
353: foreach($array_of_coscs as $cosc) {
354: $name = $this->unpack_name($cosc->super_column->name, true);
355: $columns = $cosc->super_column->columns;
356: $ret[$name] = $this->columns_to_dict($columns, false);
357: }
358: } else if ($first->counter_column) {
359: foreach($array_of_coscs as $cosc) {
360: $name = $this->unpack_name($cosc->counter_column->name, false);
361: $ret[$name] = $cosc->counter_column->value;
362: }
363: } else {
364: foreach($array_of_coscs as $cosc) {
365: $name = $this->unpack_name($cosc->counter_super_column->name, true);
366: $columns = $cosc->counter_super_column->columns;
367: $ret[$name] = $this->columns_to_dict($columns, true);
368: }
369: }
370: return $ret;
371: }
372:
373: protected function coscs_to_array($array_of_coscs) {
374: $ret = array();
375: $first = $array_of_coscs[0];
376: if($first->column) {
377: foreach($array_of_coscs as $cosc) {
378: $name = $this->unpack_name($cosc->column->name, false, false);
379: $value = $this->unpack_value($cosc->column->value, $cosc->column->name);
380: $ret[] = array($name, $value);
381: }
382: } else if($first->super_column) {
383: foreach($array_of_coscs as $cosc) {
384: $name = $this->unpack_name($cosc->super_column->name, true, false);
385: $columns = $cosc->super_column->columns;
386: $ret[] = array($name, $this->columns_to_array($columns, false));
387: }
388: } else if ($first->counter_column) {
389: foreach($array_of_coscs as $cosc) {
390: $name = $this->unpack_name($cosc->counter_column->name, false, false);
391: $ret[] = array($name, $cosc->counter_column->value);
392: }
393: } else {
394: foreach($array_of_coscs as $cosc) {
395: $name = $this->unpack_name($cosc->counter_super_column->name, true, false);
396: $columns = $cosc->counter_super_column->columns;
397: $ret[] = array($name, $this->columns_to_array($columns, true));
398: }
399: }
400: return $ret;
401: }
402:
403: protected function unpack_coscs_attrs($array_of_coscs) {
404: $ret = array();
405: $first = $array_of_coscs[0];
406: if($first->column) {
407: foreach($array_of_coscs as $cosc) {
408: $col = $cosc->column;
409: $col->value = $this->unpack_value($col->value, $col->name);
410: $col->name = $this->unpack_name($col->name, false, false);
411: $ret[] = $col;
412: }
413: } else if($first->super_column) {
414: foreach($array_of_coscs as $cosc) {
415: $supercol = $cosc->super_column;
416: $supercol->name = $this->unpack_name(
417: $supercol->name, true, false);
418: $supercol->columns = $this->unpack_subcolumn_attrs(
419: $supercol->columns, false);
420: $ret[] = $supercol;
421: }
422: } else if ($first->counter_column) {
423: foreach($array_of_coscs as $cosc) {
424: $col = $cosc->counter_column;
425: $col->name = $this->unpack_name($col->name, false, false);
426: $ret[] = $col;
427: }
428: } else {
429: foreach($array_of_coscs as $cosc) {
430: $supercol = $cosc->super_column;
431: $supercol->name = $this->unpack_name(
432: $supercol->name, true, false);
433: $supercol->columns = $this->unpack_subcolumn_attrs(
434: $supercol->columns, true);
435: $ret[] = $supercol;
436: }
437: }
438: return $ret;
439: }
440:
441: protected function unpack_subcolumn_attrs($columns, $have_counters) {
442: $ret = array();
443: if (!$have_counters) {
444: foreach($columns as $c) {
445: $c->value = $this->unpack_value($c->value, $c->name);
446: $c->name = $this->unpack_name($c->name, false, false);
447: $ret[] = $c;
448: }
449: } else {
450: foreach($columns as $c) {
451: $c->name = $this->unpack_name($c->name, false, false);
452: $ret[] = $c;
453: }
454: }
455: return $ret;
456: }
457:
458: protected function columns_to_dict($columns, $have_counters) {
459: $ret = array();
460: if (!$have_counters) {
461: foreach($columns as $c) {
462: $value = $this->unpack_value($c->value, $c->name);
463: $name = $this->unpack_name($c->name, false);
464: $ret[$name] = $value;
465: }
466: } else {
467: foreach($columns as $c) {
468: $name = $this->unpack_name($c->name, false);
469: $ret[$name] = $c->value;
470: }
471: }
472: return $ret;
473: }
474:
475: protected function columns_to_array($columns, $have_counters) {
476: $ret = array();
477: if (!$have_counters) {
478: foreach($columns as $c) {
479: $value = $this->unpack_value($c->value, $c->name);
480: $name = $this->unpack_name($c->name, false, false);
481: $ret[] = array($name, $value);
482: }
483: } else {
484: foreach($columns as $c) {
485: $name = $this->unpack_name($c->name, false, false);
486: $ret[] = array($name, $c->value);
487: }
488: }
489: return $ret;
490: }
491:
492: }
493: