31
from grackle.cassandra import workaround_1779
34
def create_schema(host, keyspace, clobber=False, create_keyspace=False):
31
from grackle.server.cassandra import workaround_1779
34
def create_schema(host, keyspace, clobber=False):
35
35
mgr = SystemManager(host)
38
mgr.create_keyspace(keyspace, replication_factor=1)
41
38
for cf in mgr.get_keyspace_column_families(keyspace):
42
39
mgr.drop_column_family(keyspace, cf)
57
def _parse_message(message):
58
"""Get a date and dict of an RFC822 message."""
59
parsed = email.parser.Parser().parsestr(message)
62
for key in ('from', 'to', 'subject', 'message-id'):
63
value = parsed.get(key, None)
65
message_dict[key] = value
67
date = parsed.get('date')
70
pdate = parsedate_tz(date)
71
date = datetime.datetime(
73
tzinfo=dateutil.tz.tzoffset('', pdate[9]))
76
message_dict['date'] = date.isoformat() if date is not None else None
78
return date, message_dict
81
54
class CassandraConnection(object):
83
56
def __init__(self, keyspace, host):
96
69
def add_message(self, archive_uuid, message):
97
70
message_uuid = uuid.uuid4()
98
message_date, message_dict = _parse_message(message)
99
message_dict['content'] = message
100
message_dict['date_created'] = (
101
datetime.datetime.utcnow().isoformat() + 'Z')
102
self.messages.insert(message_uuid, message_dict)
71
parsed = email.parser.Parser().parsestr(message)
72
date = parsed.get('date')
75
pdate = parsedate_tz(date)
76
date = datetime.datetime(
78
tzinfo=dateutil.tz.tzoffset('', pdate[9]))
83
{'date_created': datetime.datetime.utcnow().isoformat() + 'Z',
85
'from': parsed.get('From'),
86
'subject': parsed.get('Subject'),
87
'date': date.isoformat() if date is not None else None,
88
'message_id': parsed.get('Message-ID'),
103
90
self.archive_messages.insert(
105
{message_date.astimezone(dateutil.tz.tzutc()): message_uuid})
92
{date.astimezone(dateutil.tz.tzutc()): message_uuid})
107
'Imported %s into %s'
108
% (message_dict.get('message-id', None), archive_uuid))
94
'Imported %s into %s' % (parsed.get('Message-ID'), archive_uuid))
109
95
return message_uuid
111
97
def _format_message(self, message):
113
'date': message.get('date'),
114
'from': message.get('from'),
115
'subject': message.get('subject'),
116
'message-id': message.get('message-id'),
99
'date': message['date'],
100
'from': message['from'],
101
'subject': message['subject'],
119
def get_messages(self, archive_uuid, order, count, memo):
104
def get_messages(self, archive_uuid, count, order):
120
105
if order in ("date", "-date"):
121
106
reversed = order[0] == '-'
123
108
raise AssertionError("Unsupported order.")
125
memo = uuid.UUID(memo)
126
# Get up to n+1 messages from the memo: the last item of the
127
# previous batch (because that's where the memo starts) + this
129
pairs = self.archive_messages.get(
130
archive_uuid, column_count=count + 1,
131
column_start=memo, column_reversed=reversed).items()
133
if memo and len(pairs) and pairs[0][0] <= memo:
135
elif len(pairs) > count:
139
return (None, [], None)
141
assert 0 < len(pairs) <= count
143
ids = [v for k, v in pairs]
144
messages = self.messages.multiget(
145
ids, columns=['date', 'from', 'subject', 'message-id'])
149
[self._format_message(messages[id]) for id in ids],
109
ids = self.archive_messages.get(
110
archive_uuid, column_count=count,
111
column_reversed=reversed).values()
112
messages = self.messages.multiget(ids)
113
return [self._format_message(messages[id]) for id in ids]