|
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
|
49 |
|
public function connect(array $config) |
|
29
|
|
|
{ |
|
30
|
49 |
|
$this->builder = \Cassandra::cluster(); |
|
31
|
|
|
|
|
32
|
49 |
|
$this->setConnectionOptions($config); |
|
33
|
|
|
|
|
34
|
49 |
|
$this->setTimeouts($config); |
|
35
|
|
|
|
|
36
|
49 |
|
$this->setDefaultQueryOptions($config); |
|
37
|
|
|
|
|
38
|
49 |
|
return $this->builder->build(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Set username and password to cluster connection. |
|
43
|
|
|
* Set cluster contact points (IP addresses) |
|
44
|
|
|
* Set connection communication port |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $config |
|
47
|
|
|
*/ |
|
48
|
49 |
|
protected function setConnectionOptions(array $config) |
|
49
|
|
|
{ |
|
50
|
|
|
// Authentication |
|
51
|
|
|
list($username, $password) = [ |
|
52
|
49 |
|
$config['username'] ?? null, $config['password'] ?? null, |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
49 |
|
$this->builder->withCredentials($username, $password); |
|
56
|
|
|
|
|
57
|
49 |
|
call_user_func_array([$this->builder, 'withContactPoints'], $this->getContactPoints($config)); |
|
58
|
|
|
|
|
59
|
49 |
|
$this->builder->withPort($this->getPort($config)); |
|
60
|
49 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Parse contact points list from config |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $config |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
49 |
|
protected function getContactPoints(array $config) |
|
70
|
|
|
{ |
|
71
|
49 |
|
$contactPoints = []; |
|
72
|
|
|
|
|
73
|
49 |
|
if (!empty($config['host'])) { |
|
74
|
49 |
|
$contactPoints = $config['host']; |
|
75
|
|
|
|
|
76
|
49 |
|
if (is_string($contactPoints)) { |
|
77
|
49 |
|
$contactPoints = explode(',', $contactPoints); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
49 |
|
return (array) $contactPoints; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Parse communication port or set default |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $config |
|
88
|
|
|
* |
|
89
|
|
|
* @return int |
|
90
|
|
|
*/ |
|
91
|
49 |
|
protected function getPort(array $config) |
|
92
|
|
|
{ |
|
93
|
49 |
|
return !empty($config['port']) ? (int)$config['port'] : 9042; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set default consistency level |
|
98
|
|
|
* Set default response size to queries |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $config |
|
101
|
|
|
*/ |
|
102
|
49 |
|
protected function setDefaultQueryOptions(array $config) |
|
103
|
|
|
{ |
|
104
|
49 |
|
if (isset($config['consistency']) && in_array($config['consistency'], [ |
|
105
|
49 |
|
\Cassandra::CONSISTENCY_ANY, \Cassandra::CONSISTENCY_ONE, \Cassandra::CONSISTENCY_TWO, |
|
106
|
|
|
\Cassandra::CONSISTENCY_THREE, \Cassandra::CONSISTENCY_QUORUM, \Cassandra::CONSISTENCY_ALL, |
|
107
|
|
|
\Cassandra::CONSISTENCY_SERIAL, \Cassandra::CONSISTENCY_QUORUM, \Cassandra::CONSISTENCY_LOCAL_QUORUM, |
|
108
|
|
|
\Cassandra::CONSISTENCY_EACH_QUORUM, \Cassandra::CONSISTENCY_LOCAL_SERIAL, \Cassandra::CONSISTENCY_LOCAL_ONE, |
|
109
|
|
|
])) { |
|
110
|
|
|
|
|
111
|
49 |
|
$this->builder->withDefaultConsistency($config['consistency']); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
49 |
|
$this->builder->withDefaultPageSize(intval(!empty($config['page_size']) ? $config['page_size'] : self::DEFAULT_PAGE_SIZE)); |
|
115
|
49 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set timeouts for query execution |
|
119
|
|
|
* and cluster connection |
|
120
|
|
|
* |
|
121
|
|
|
* @param array $config |
|
122
|
|
|
*/ |
|
123
|
49 |
|
protected function setTimeouts(array $config) |
|
124
|
|
|
{ |
|
125
|
49 |
|
if (!empty($config['timeout'])) { |
|
126
|
49 |
|
$this->builder->withDefaultTimeout(intval($config['timeout'])); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
49 |
|
if (!empty($config['connect_timeout'])) { |
|
130
|
49 |
|
$this->builder->withConnectTimeout(floatval($config['connect_timeout'])); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
49 |
|
if (!empty($config['request_timeout'])) { |
|
134
|
49 |
|
$this->builder->withRequestTimeout(floatval($config['request_timeout'])); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |