-
Notifications
You must be signed in to change notification settings - Fork 13
/
base.py
191 lines (159 loc) · 7.31 KB
/
base.py
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
# Copyright 2010 BSN, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from djangotoolbox.db.base import NonrelDatabaseFeatures, \
NonrelDatabaseOperations, NonrelDatabaseWrapper, NonrelDatabaseClient, \
NonrelDatabaseValidation, NonrelDatabaseIntrospection, \
NonrelDatabaseCreation
from thrift import Thrift
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol import TBinaryProtocol
from cassandra import Cassandra
from cassandra.ttypes import *
import time
from .creation import DatabaseCreation
from .introspection import DatabaseIntrospection
class DatabaseFeatures(NonrelDatabaseFeatures):
string_based_auto_field = True
class DatabaseOperations(NonrelDatabaseOperations):
compiler_module = __name__.rsplit('.', 1)[0] + '.compiler'
def sql_flush(self, style, tables, sequence_list):
for table_name in tables:
self.connection.creation.flush_table(table_name)
return ""
class DatabaseClient(NonrelDatabaseClient):
pass
class DatabaseValidation(NonrelDatabaseValidation):
pass
# TODO: Maybe move this somewhere else? db.utils.py maybe?
class CassandraConnection(object):
def __init__(self, host, port, keyspace, user, password):
self.host = host
self.port = port
self.keyspace = keyspace
self.user = user
self.password = password
self.transport = None
self.client = None
self.keyspace_set = False
self.logged_in = False
def commit(self):
pass
def set_keyspace(self):
if not self.keyspace_set:
self.client.set_keyspace(self.keyspace)
self.keyspace_set = True
def login(self):
# TODO: This user/password auth code hasn't been tested
if not self.logged_in:
if self.user:
credentials = {'username': self.user, 'password': self.password}
self.client.login(AuthenticationRequest(credentials))
self.logged_in = True
def open(self, set_keyspace=False, login=False):
if self.transport == None:
# Create the client connection to the Cassandra daemon
socket = TSocket.TSocket(self.host, int(self.port))
transport = TTransport.TFramedTransport(TTransport.TBufferedTransport(socket))
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
transport.open()
self.transport = transport
self.client = Cassandra.Client(protocol)
if login:
self.login()
if set_keyspace:
self.set_keyspace()
def close(self):
if self.transport != None:
self.transport.close()
self.transport = None
self.client = None
self.keyspace_set = False
self.logged_in = False
def is_connected(self):
return self.transport != None
def reopen(self):
self.close()
self.open(True, True)
class DatabaseWrapper(NonrelDatabaseWrapper):
def __init__(self, *args, **kwds):
super(DatabaseWrapper, self).__init__(*args, **kwds)
# Set up the associated backend objects
self.features = DatabaseFeatures(self)
self.ops = DatabaseOperations(self)
self.client = DatabaseClient(self)
self.creation = DatabaseCreation(self)
self.validation = DatabaseValidation(self)
self.introspection = DatabaseIntrospection(self)
self.read_consistency_level = self.settings_dict.get('CASSANDRA_READ_CONSISTENCY_LEVEL')
if self.read_consistency_level is None:
self.read_consistency_level = ConsistencyLevel.ONE
self.write_consistency_level = self.settings_dict.get('CASSANDRA_WRITE_CONSISTENCY_LEVEL')
if self.write_consistency_level is None:
self.write_consistency_level = ConsistencyLevel.ONE
self.max_key_count = self.settings_dict.get('CASSANDRA_MAX_KEY_COUNT')
if self.max_key_count is None:
self.max_key_count = 10000
self.max_column_count = self.settings_dict.get('CASSANDRA_MAX_COLUMN_COUNT')
if self.max_column_count is None:
self.max_column_count = 1000
self._db_connection = None
def get_db_connection(self, set_keyspace=False, login=False):
if not self._db_connection:
# Get the host and port specified in the database backend settings.
# Default to the standard Cassandra settings.
host = self.settings_dict.get('HOST')
if not host or host == '':
host = 'localhost'
port = self.settings_dict.get('PORT')
if not port or port == '':
port = 9160
keyspace = self.settings_dict.get('NAME')
if keyspace == None:
keyspace = 'django'
user = self.settings_dict.get('USER')
password = self.settings_dict.get('PASSWORD')
# Create our connection wrapper
self._db_connection = CassandraConnection(host, port, keyspace, user, password)
if not self._db_connection.is_connected():
self._db_connection.open(False, False)
#version = client.describe_version()
# FIXME: Should do some version check here to make sure that we're
# talking to a cassandra daemon that supports the operations we require
if login:
try:
self._db_connection.login()
except Exception, e:
# FIXME: Better handling of auth error
raise e
if set_keyspace:
try:
self._db_connection.set_keyspace()
except Exception, e:
replication_factor = self.settings_dict.get('CASSANDRA_REPLICATION_FACTOR')
if not replication_factor:
replication_factor = 1
replication_strategy_class = self.settings_dict.get('CASSANDRA_REPLICATION_STRATEGY')
if not replication_strategy_class:
replication_strategy_class = 'org.apache.cassandra.locator.SimpleStrategy'
keyspace_def = KsDef(name=self._db_connection.keyspace,
strategy_class=replication_strategy_class,
replication_factor=replication_factor,
cf_defs=[])
self._db_connection.client.system_add_keyspace(keyspace_def)
self._db_connection.set_keyspace()
return self._db_connection
@property
def db_connection(self):
return self.get_db_connection(True, True)