1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace lroman242\LaravelCassandra; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Connectors\Connector; |
8
|
|
|
use Illuminate\Queue\Connectors\ConnectorInterface; |
9
|
|
|
|
10
|
|
|
class CassandraConnector extends Connector implements ConnectorInterface |
11
|
|
|
{ |
12
|
|
|
const DEFAULT_PAGE_SIZE = 5000; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Cassandra Cluster Builder instance |
16
|
|
|
* |
17
|
|
|
* @var \Cassandra\Cluster\Builder |
18
|
|
|
*/ |
19
|
|
|
protected $builder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Establish a database connection. |
23
|
|
|
* |
24
|
|
|
* @param array $config |
25
|
|
|
* |
26
|
|
|
* @return \Cassandra\Cluster |
27
|
|
|
*/ |
28
|
48 |
|
public function connect(array $config) |
29
|
|
|
{ |
30
|
48 |
|
$this->builder = \Cassandra::cluster(); |
31
|
|
|
|
32
|
48 |
|
$this->setConnectionOptions($config); |
33
|
|
|
|
34
|
48 |
|
$this->setDefaultQueryOptions($config); |
35
|
|
|
|
36
|
48 |
|
return $this->builder->build(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set username and password to cluster connection. |
41
|
|
|
* Set cluster contact points (IP addresses) |
42
|
|
|
* Set connection communication port |
43
|
|
|
* |
44
|
|
|
* @param array $config |
45
|
|
|
*/ |
46
|
48 |
|
protected function setConnectionOptions(array $config) |
47
|
|
|
{ |
48
|
|
|
// Authentication |
49
|
|
|
list($username, $password) = [ |
50
|
48 |
|
$config['username'] ?? null, $config['password'] ?? null, |
51
|
|
|
]; |
52
|
|
|
|
53
|
48 |
|
$this->builder->withCredentials($username, $password); |
54
|
|
|
|
55
|
48 |
|
call_user_func_array([$this->builder, 'withContactPoints'], $this->getContactPoints($config)); |
56
|
|
|
|
57
|
48 |
|
$this->builder->withPort($this->getPort($config)); |
58
|
48 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Parse contact points list from config |
62
|
|
|
* |
63
|
|
|
* @param array $config |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
48 |
|
protected function getContactPoints(array $config) |
68
|
|
|
{ |
69
|
48 |
|
$contactPoints = []; |
70
|
|
|
|
71
|
48 |
|
if (!empty($config['host'])) { |
72
|
48 |
|
$contactPoints = $config['host']; |
73
|
|
|
|
74
|
48 |
|
if (is_string($contactPoints)) { |
75
|
48 |
|
$contactPoints = explode(',', $contactPoints); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
48 |
|
return (array) $contactPoints; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Parse communication port or set default |
84
|
|
|
* |
85
|
|
|
* @param array $config |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
48 |
|
protected function getPort(array $config) |
90
|
|
|
{ |
91
|
48 |
|
return !empty($config['port']) ? (int)$config['port'] : 9042; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set default consistency level |
96
|
|
|
* Set default timeouts to queries |
97
|
|
|
* Set default response size to queries |
98
|
|
|
* |
99
|
|
|
* @param array $config |
100
|
|
|
*/ |
101
|
48 |
|
protected function setDefaultQueryOptions(array $config) |
102
|
|
|
{ |
103
|
48 |
|
if (isset($config['consistency']) && in_array($config['consistency'], [ |
104
|
48 |
|
\Cassandra::CONSISTENCY_ANY, \Cassandra::CONSISTENCY_ONE, \Cassandra::CONSISTENCY_TWO, |
105
|
|
|
\Cassandra::CONSISTENCY_THREE, \Cassandra::CONSISTENCY_QUORUM, \Cassandra::CONSISTENCY_ALL, |
106
|
|
|
\Cassandra::CONSISTENCY_SERIAL, \Cassandra::CONSISTENCY_QUORUM, \Cassandra::CONSISTENCY_LOCAL_QUORUM, |
107
|
|
|
\Cassandra::CONSISTENCY_EACH_QUORUM, \Cassandra::CONSISTENCY_LOCAL_SERIAL, \Cassandra::CONSISTENCY_LOCAL_ONE, |
108
|
|
|
])) { |
109
|
|
|
|
110
|
48 |
|
$this->builder->withDefaultConsistency($config['consistency']); |
111
|
|
|
} |
112
|
|
|
|
113
|
48 |
|
if (!empty($config['timeout'])) { |
114
|
48 |
|
$this->builder->withDefaultTimeout(intval($config['timeout'])); |
115
|
|
|
} |
116
|
|
|
|
117
|
48 |
|
if (!empty($config['connect_timeout'])) { |
118
|
48 |
|
$this->builder->withConnectTimeout(floatval($config['connect_timeout'])); |
119
|
|
|
} |
120
|
|
|
|
121
|
48 |
|
if (!empty($config['request_timeout'])) { |
122
|
48 |
|
$this->builder->withRequestTimeout(floatval($config['request_timeout'])); |
123
|
|
|
} |
124
|
|
|
|
125
|
48 |
|
$this->builder->withDefaultPageSize(intval(!empty($config['page_size']) ? $config['page_size'] : self::DEFAULT_PAGE_SIZE)); |
126
|
48 |
|
} |
127
|
|
|
|
128
|
|
|
} |