1
# Copyright (c) 2012 Canonical Ltd
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU Affero General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Affero General Public License for more details.
13
# You should have received a copy of the GNU Affero General Public
14
# License along with this program. If not, see
15
# <http://www.gnu.org/licenses/>.
21
from pycassa.system_manager import (
27
from grackle.server.cassandra import workaround_1779
30
def create_schema(host, keyspace, clobber=False):
31
mgr = SystemManager(host)
34
for cf in mgr.get_keyspace_column_families(keyspace):
35
mgr.drop_column_family(keyspace, cf)
39
mgr.create_column_family, keyspace, 'message',
40
key_validation_class=LEXICAL_UUID_TYPE)
42
mgr.create_column_family, keyspace, 'archive_message',
43
comparator_type=TIME_UUID_TYPE,
44
default_validation_class=LEXICAL_UUID_TYPE)
50
class CassandraConnection(object):
52
def __init__(self, keyspace, host):
53
self._keyspace = keyspace
55
self._connection = self._connect()
56
self.messages = self._column_family('message')
57
self.archive_messages = self._column_family('archive_message')
60
return pycassa.connect(self._keyspace, self._host)
62
def _column_family(self, name):
63
return pycassa.ColumnFamily(self._connection, name)
65
def add_message(self, archive_uuid, message):
66
message_uuid = uuid.uuid4()
69
{'date_created': datetime.datetime.utcnow().isoformat() + 'Z',
72
self.archive_messages.insert(
73
archive_uuid, {datetime.datetime.now(): message_uuid})
76
def get_messages(self, archive_uuid):
77
ids = self.archive_messages.get(
78
archive_uuid, column_count=10, column_reversed=True).values()
79
messages = self.messages.multiget(ids)
80
return [messages[id]['content'] for id in ids]