121
122
return memo, start, finish
124
def _format_message(message):
126
'date': message.get('date'),
127
'from': message.get('from'),
128
'subject': message.get('subject'),
129
'message-id': message.get('message-id'),
125
LEGAL_HEADERS = set([
126
'date', 'from', 'subject', 'message-id',
130
def _format_message(message, headers=[], include_raw=False):
134
assert not set(headers).difference(LEGAL_HEADERS)
136
for header in headers:
137
hdict[header] = message.get(header)
138
data['headers'] = hdict
141
data['raw'] = message['raw']
133
146
class CassandraConnection(object):
135
def __init__(self, keyspace, host):
148
def __init__(self, keyspace, hosts):
136
149
self._keyspace = keyspace
138
151
self._connection = self._connect()
152
self._pool = self._connect()
139
153
self.messages = self._column_family('message')
140
154
self.archive_messages = self._column_family('archive_message')
142
156
def _connect(self):
143
return pycassa.connect(self._keyspace, self._host)
157
return pycassa.pool.ConnectionPool(self._keyspace, self._hosts)
145
159
def _column_family(self, name):
146
return pycassa.ColumnFamily(self._connection, name)
160
return pycassa.ColumnFamily(self._pool, name)
148
162
def add_message(self, archive_uuid, message):
149
163
message_uuid = uuid.uuid4()
150
164
message_date, message_dict = _parse_message(message)
151
message_dict['content'] = message
165
message_dict['raw'] = message
152
166
message_dict['date_created'] = (
153
167
datetime.datetime.utcnow().isoformat() + 'Z')
154
168
self.messages.insert(message_uuid, message_dict)
172
186
return sequence[1:]
174
188
def get_messages(self, archive_uuid, order, count, memo, backward=False,
175
start_date=None, finish_date=None):
189
start_date=None, finish_date=None, format='all',
190
headers=['from', 'date', 'subject', 'message-id']):
176
191
if order in ("date", "-date"):
177
192
reversed = order[0] == '-'
205
220
# We've narrowed down the message references. Fetch the messages.
206
221
ids = [v for k, v in pairs]
207
messages = self.messages.multiget(
208
ids, columns=['date', 'from', 'subject', 'message-id'])
222
formatter = functools.partial(
223
_format_message, headers=headers, include_raw=True)
224
# XXX: No need to get all columns. Restrict based on format.
225
messages = self.messages.multiget(ids)
211
228
str(pairs[0][0]),
212
[_format_message(messages[id]) for id in ids],
229
[formatter(messages[id]) for id in ids],
213
230
str(pairs[-1][0]),