forked from hoan/phpcassa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphpcassa.php
590 lines (503 loc) · 21.4 KB
/
phpcassa.php
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
<?php
// Setting up nodes:
//
// CassandraConn::add_node('192.168.1.1', 9160);
// CassandraConn::add_node('192.168.1.2', 5000);
//
// Querying:
//
// $users = new CassandraCF('Keyspace1', 'Users');
// $users->insert('1', array('email' => '[email protected]', 'password' => 'test'));
// $users->get('1');
// $users->multiget(array(1, 2));
// $users->get_count('1');
// $users->get_range('1', '10');
// $users->remove('1');
// $users->remove('1', 'password');
//
class CassandraConn {
const DEFAULT_THRIFT_PORT = 9160;
static private $connections = array();
static private $last_error;
static public function add_node($host,
$port=self::DEFAULT_THRIFT_PORT,
$framed_transport=false,
$send_timeout=null,
$recv_timeout=null,
$persist=false) {
try {
// Create Thrift transport and binary protocol cassandra client
$socket = new TSocket($host, $port, $persist);
if($send_timeout) $socket->setSendTimeout($send_timeout);
if($recv_timeout) $socket->setRecvTimeout($recv_timeout);
if($framed_transport) {
$transport = new TFramedTransport($socket, true, true);
} else {
$transport = new TBufferedTransport($socket, 1024, 1024);
}
$client = new CassandraClient(new TBinaryProtocolAccelerated($transport));
// Store it in the connections
self::$connections[] = array(
'transport' => $transport,
'client' => $client
);
// Done
return TRUE;
} catch (TException $tx) {
self::$last_error = 'TException: '.$tx->getMessage() . "\n";
}
return FALSE;
}
// Default client
static public function get_client($write_mode = false) {
// * Try to connect to every cassandra node in order
// * Failed connections will be retried
// * Once a connection is opened, it stays open
// * TODO: add random and round robin order
// * TODO: add write-preferred and read-preferred nodes
shuffle(self::$connections);
foreach(self::$connections as $node => $connection) {
try {
$transport = $connection['transport'];
$client = $connection['client'];
if(!$transport->isOpen()) {
$transport->open();
}
$client = new CassandraClient(new TBinaryProtocolAccelerated($transport));
$connections[$node] = array(
'transport' => $transport,
'client' => $client
);
return $client;
} catch (TException $tx) {
self::$last_error = 'TException: '.$tx->getMessage() . "\n";
continue;
}
}
throw new Exception("Could not connect to a cassandra server");
}
}
class CassandraUtil {
// UUID
static public function uuid1($node="", $ns="") {
return UUID::generate(UUID::UUID_TIME,UUID::FMT_STRING, $node, $ns);
}
// Time
static public function get_time() {
// By Zach Buller ([email protected])
$time1 = microtime();
settype($time1, 'string'); //needs converted to string, otherwise will omit trailing zeroes
$time2 = explode(" ", $time1);
$time2[0] = preg_replace('/0./', '', $time2[0], 1);
$time3 = ($time2[1].$time2[0])/100;
return $time3;
}
}
class CassandraCF {
const DEFAULT_ROW_LIMIT = 1024; // default max # of rows for get_range()
const DEFAULT_COLUMN_LIMIT = 1024; // default max # of columns for get()
const DEFAULT_COLUMN_TYPE = "UTF8Type";
const DEFAULT_SUBCOLUMN_TYPE = null;
public $keyspace;
public $column_family;
public $is_super;
public $read_consistency_level;
public $write_consistency_level;
public $column_type; // CompareWith (TODO: actually use this)
public $subcolumn_type; // CompareSubcolumnsWith (TODO: actually use this)
public $parse_columns;
/*
BytesType: Simple sort by byte value. No validation is performed.
AsciiType: Like BytesType, but validates that the input can be parsed as US-ASCII.
UTF8Type: A string encoded as UTF8
LongType: A 64bit long
LexicalUUIDType: A 128bit UUID, compared lexically (by byte value)
TimeUUIDType: a 128bit version 1 UUID, compared by timestamp
*/
public function __construct($keyspace, $column_family,
$is_super=false,
$column_type=self::DEFAULT_COLUMN_TYPE,
$subcolumn_type=self::DEFAULT_SUBCOLUMN_TYPE,
$read_consistency_level=cassandra_ConsistencyLevel::ONE,
$write_consistency_level=cassandra_ConsistencyLevel::ZERO) {
// Vars
$this->keyspace = $keyspace;
$this->column_family = $column_family;
$this->is_super = $is_super;
$this->column_type = $column_type;
$this->subcolumn_type = $subcolumn_type;
$this->read_consistency_level = $read_consistency_level;
$this->write_consistency_level = $write_consistency_level;
// Toggles parsing columns
$this->parse_columns = true;
}
public function get($key,
$super_column=NULL,
$slice_start="",
$slice_finish="",
$column_reversed=False,
$column_count=self::DEFAULT_COLUMN_LIMIT) {
// If we do get($key, $x) on a non-super column
// we should just return that attribute
if(!$this->is_super && $super_column) {
$result = $this->get($key, NULL, array($super_column));
return $result[$super_column];
}
// Otherwise do a normal get query
$column_parent = new cassandra_ColumnParent();
$column_parent->column_family = $this->column_family;
$column_parent->super_column = $this->unparse_column_name($super_column, true);
$predicate = new cassandra_SlicePredicate();
if(is_array($slice_start)) {
// Treat this as a column_names query
$predicate->column_names = $slice_start;
} else {
// Treat this as a slice_range query
$slice_range = new cassandra_SliceRange();
$slice_range->count = $column_count;
$slice_range->reversed = $column_reversed;
$slice_range->start = $slice_start ? $this->unparse_column_name($slice_start, false) : "";
$slice_range->finish = $slice_finish ? $this->unparse_column_name($slice_finish, false) : "";
$predicate->slice_range = $slice_range;
}
$client = CassandraConn::get_client();
$resp = $client->get_slice($this->keyspace, $key, $column_parent, $predicate, $this->read_consistency_level);
if($super_column) {
return $this->supercolumns_or_columns_to_array($resp, false);
} else {
return $this->supercolumns_or_columns_to_array($resp);
}
}
public function multiget($keys, $slice_start="", $slice_finish="") {
$column_parent = new cassandra_ColumnParent();
$column_parent->column_family = $this->column_family;
$column_parent->super_column = NULL;
$predicate = new cassandra_SlicePredicate();
if(is_array($slice_start)) {
// Treat this as a column_names query
$predicate->column_names = $slice_start;
} else {
// Treat this as a slice_range query
$slice_range = new cassandra_SliceRange();
$slice_range->start = $slice_start ? $this->unparse_column_name($slice_start, false) : "";
$slice_range->finish = $slice_finish ? $this->unparse_column_name($slice_finish, false) : "";
$predicate->slice_range = $slice_range;
}
$client = CassandraConn::get_client();
$resp = $client->multiget_slice($this->keyspace, $keys, $column_parent, $predicate, $this->read_consistency_level);
$ret = null;
// foreach($keys as $sk => $k) {
// $ret[$k] = $this->supercolumns_or_columns_to_array($resp[$k]);
// }
foreach($resp as $key => $val) {
$ret[$key] = $this->supercolumns_or_columns_to_array($val);
}
return $ret;
}
public function get_count($key, $super_column=null) {
$column_path = new cassandra_ColumnPath();
$column_path->column_family = $this->column_family;
$column_path->super_column = $super_column;
$client = CassandraConn::get_client();
$resp = $client->get_count($this->keyspace, $key, $column_path, $this->read_consistency_level);
return $resp;
}
public function get_range($start_key="", $end_key="", $row_count=self::DEFAULT_ROW_LIMIT, $slice_start="", $slice_finish="") {
$column_parent = new cassandra_ColumnParent();
$column_parent->column_family = $this->column_family;
$column_parent->super_column = NULL;
$predicate = new cassandra_SlicePredicate();
if(is_array($slice_start)) {
// Treat this as a column_names query
$predicate->column_names = $slice_start;
} else {
// Treat this as a slice_range query
$slice_range = new cassandra_SliceRange();
$slice_range->start = $slice_start ? $this->unparse_column_name($slice_start, false) : "";
$slice_range->finish = $slice_finish ? $this->unparse_column_name($slice_finish, false) : "";
$predicate->slice_range = $slice_range;
}
$key_range = new cassandra_KeyRange();
$key_range->start_key = $start_key;
$key_range->end_key = $end_key;
$key_range->count = $row_count;
$client = CassandraConn::get_client();
$resp = $client->get_range_slices($this->keyspace, $column_parent, $predicate, $key_range, $this->read_consistency_level);
return $this->keyslices_to_array($resp);
}
public function get_range_iterator($start_key="", $end_key="", $row_count=self::DEFAULT_ROW_LIMIT, $slice_start="", $slice_finish="") {
return new CassandraIterator($this, $start_key, $end_key, $row_count, $slice_start, $slice_end);
}
public function insert($key, $columns=null) {
$timestamp = CassandraUtil::get_time();
if(is_array($key)) {
// We ignore $columns and convert $key
// to an array map of all mutations
$cfmap = array();
foreach($key as $_key => $_columns) {
$cfmap[$_key][$this->column_family] = $this->array_to_mutation($_columns, $timestamp);
}
} else {
$cfmap = array();
$cfmap[$key][$this->column_family] = $this->array_to_mutation($columns, $timestamp);
}
$client = CassandraConn::get_client();
$resp = $client->batch_mutate($this->keyspace, $cfmap, $this->write_consistency_level);
return $resp;
}
public function remove($key, $column_name=null) {
$timestamp = CassandraUtil::get_time();
$column_path = new cassandra_ColumnPath();
$column_path->column_family = $this->column_family;
if($this->is_super) {
$column_path->super_column = $this->unparse_column_name($column_name, true);
} else {
$column_path->column = $this->unparse_column_name($column_name, false);
}
$client = CassandraConn::get_client();
$resp = $client->remove($this->keyspace, $key, $column_path, $timestamp, $this->write_consistency_level);
return $resp;
}
// Wrappers
public function get_list($key, $key_name='key', $slice_start="", $slice_finish="") {
// Must be on supercols!
$resp = $this->get($key, NULL, $slice_start, $slice_finish);
$ret = array();
foreach($resp as $_key => $_value) {
$_value[$key_name] = $_key;
$ret[] = $_value;
}
return $ret;
}
public function get_range_list($key_name='key', $start_key="", $end_key="",
$row_count=self::DEFAULT_ROW_LIMIT, $slice_start="", $slice_finish="") {
$resp = $this->get_range($start_key, $end_key, $row_count, $slice_start, $slice_finish);
$ret = array();
foreach($resp as $_key => $_value) {
if(!empty($_value)) { // filter nulls
$_value[$key_name] = $_key;
$ret[] = $_value;
}
}
return $ret;
}
public function multiget_list($keys, $key_name='key', $slice_start="", $slice_finish="") {
$resp = $this->multiget($keys, $slice_start, $slice_finish);
$ret = array();
foreach($resp as $_key => $_value) {
$_value[$key_name] = $_key;
$ret[] = $_value;
}
return $ret;
}
// Helpers for parsing Cassandra's thrift objects into PHP arrays
public function keyslices_to_array($keyslices) {
$ret = null;
foreach($keyslices as $keyslice) {
$key = $keyslice->key;
$columns = $keyslice->columns;
$ret[$key] = $this->supercolumns_or_columns_to_array($columns);
}
return $ret;
}
public function supercolumns_or_columns_to_array($array_of_c_or_sc, $parse_as_columns=true) {
$ret = null;
foreach($array_of_c_or_sc as $c_or_sc) {
if($c_or_sc->column) { // normal columns
$name = $this->parse_column_name($c_or_sc->column->name, $parse_as_columns);
$value = $c_or_sc->column->value;
$ret[$name] = $value;
} else if($c_or_sc->super_column) { // super columns
$name = $this->parse_column_name($c_or_sc->super_column->name, $parse_as_columns);
$columns = $c_or_sc->super_column->columns;
$ret[$name] = $this->columns_to_array($columns);
}
}
return $ret;
}
public function columns_to_array($array_of_c) {
$ret = null;
foreach($array_of_c as $c) {
$name = $this->parse_column_name($c->name, false);
$value = $c->value;
$ret[$name] = $value;
}
return $ret;
}
// Helpers for turning PHP arrays into Cassandra's thrift objects
public function array_to_mutation($array, $timestamp=null) {
if(empty($timestamp)) $timestamp = CassandraUtil::get_time();
$c_or_sc = $this->array_to_supercolumns_or_columns($array, $timestamp);
$ret = null;
foreach($c_or_sc as $row) {
$mutation = new cassandra_Mutation();
$mutation->column_or_supercolumn = $row;
$ret[] = $mutation;
}
return $ret;
}
public function array_to_supercolumns_or_columns($array, $timestamp=null) {
if(empty($timestamp)) $timestamp = CassandraUtil::get_time();
$ret = null;
foreach($array as $name => $value) {
$c_or_sc = new cassandra_ColumnOrSuperColumn();
if(is_array($value)) {
$c_or_sc->super_column = new cassandra_SuperColumn();
$c_or_sc->super_column->name = $this->unparse_column_name($name, true);
$c_or_sc->super_column->columns = $this->array_to_columns($value, $timestamp);
$c_or_sc->super_column->timestamp = $timestamp;
} else {
$c_or_sc = new cassandra_ColumnOrSuperColumn();
$c_or_sc->column = new cassandra_Column();
$c_or_sc->column->name = $this->unparse_column_name($name, true);
$c_or_sc->column->value = $this->to_column_value($value);;
$c_or_sc->column->timestamp = $timestamp;
}
$ret[] = $c_or_sc;
}
return $ret;
}
public function array_to_columns($array, $timestamp=null) {
if(empty($timestamp)) $timestamp = CassandraUtil::get_time();
$ret = null;
foreach($array as $name => $value) {
$column = new cassandra_Column();
$column->name = $this->unparse_column_name($name, false);
$column->value = $this->to_column_value($value);
$column->timestamp = $timestamp;
$ret[] = $column;
}
return $ret;
}
public function to_column_value($thing) {
if($thing === null) return "";
return $thing;
}
// ARGH
public function parse_column_name($column_name, $is_column=true) {
if(!$this->parse_columns) return $column_name;
if(!$column_name) return NULL;
$type = $is_column ? $this->column_type : $this->subcolumn_type;
if($type == "LexicalUUIDType" || $type == "TimeUUIDType") {
return UUID::convert($column_name, UUID::FMT_BINARY, UUID::FMT_STRING);
} else if($type == "LongType") {
return $this->unpack_longtype($column_name);
} else {
return $column_name;
}
}
public function unparse_column_name($column_name, $is_column=true) {
if(!$this->parse_columns) return $column_name;
if(!$column_name) return NULL;
$type = $is_column ? $this->column_type : $this->subcolumn_type;
if($type == "LexicalUUIDType" || $type == "TimeUUIDType") {
return UUID::convert($column_name, UUID::FMT_STRING, UUID::FMT_BINARY);
} else if($type == "LongType") {
return $this->pack_longtype($column_name);
} else {
return $column_name;
}
}
// See http://webcache.googleusercontent.com/search?q=cache:9jjbeSy434UJ:wiki.apache.org/cassandra/FAQ+cassandra+php+%22A+long+is+exactly+8+bytes%22&cd=1&hl=en&ct=clnk&gl=us
public function pack_longtype($x) {
return pack('C8',
($x >> 56) & 0xff, ($x >> 48) & 0xff, ($x >> 40) & 0xff, ($x >> 32) & 0xff,
($x >> 24) & 0xff, ($x >> 16) & 0xff, ($x >> 8) & 0xff, $x & 0xff
);
}
public function unpack_longtype($x) {
$a = unpack('C8', $x);
return ($a[1] << 56) + ($a[2] << 48) + ($a[3] << 40) + ($a[4] << 32) + ($a[5] << 24) + ($a[6] << 16) + ($a[7] << 8) + $a[8];
}
}
class CassandraIterator implements Iterator {
const DEFAULT_BUFFER_SIZE = 1024; // default max # of rows for get_range()
// Options
public $column_family;
public $buffer_size;
public $start_key, $end_key;
public $start_slice, $end_slice;
// State
public $current_buffer;
public $next_start_key;
public $beyond_last_field;
public function __construct($column_family,
$start_key="",
$end_key="",
$buffer_size=self::DEFAULT_BUFFER_SIZE,
$start_slice="",
$end_slice="") {
// Lets go
$this->column_family = $column_family;
$this->start_key = $start_key;
$this->end_key = $end_key;
$this->buffer_size = $buffer_size;
$this->start_slice = $start_slice;
$this->end_slice = $end_slice;
}
// Interface
public function rewind() {
// Setup first buffer
$this->beyond_last_field = false;
$this->next_start_key = $this->start_key;
$this->current_buffer = $this->column_family->get_range(
$this->next_start_key,
$this->end_key,
$this->buffer_size,
$this->start_slice,
$this->end_slice
);
}
public function current() {
return current($this->current_buffer);
}
public function key() {
return key($this->current_buffer);
}
public function next() {
// See http://www.php.net/manual/en/function.current.php#81431
// for figuring if we are at the end
$next = next($this->current_buffer);
$key = key($this->current_buffer);
if(!isset($key)) {
$this->beyond_last_field = true;
return false;
} else {
return $next;
}
}
public function valid() {
if($this->beyond_last_field && count($this->current_buffer) < $this->buffer_size) {
// Stop if we were at the last buffer (we got less that $buffer_size elements returned)
return false;
} else if($this->beyond_last_field) {
// Set the next start key
end($this->current_buffer);
$this->next_start_key = key($this->current_buffer);
// Get the next buffer
$this->current_buffer = $this->column_family->get_range(
$this->next_start_key,
$this->end_key,
$this->buffer_size,
$this->start_slice,
$this->end_slice
);
// If the result set is 1, we can stop
// because the first item should always
// be skipped
if(count($this->current_buffer) == 1) {
return false;
} else {
// Skip 1st item (because it is the last buffer's last key)
next($this->current_buffer);
// Let us iterate again
$this->beyond_last_field = false;
return true;
}
} else {
// Normal iteration
return true;
}
}
}
?>