78
80
return date, message_dict
83
def _utc_datetime(dt):
84
return dt.astimezone(dateutil.tz.tzutc())
87
def _utc_timestamp(dt):
88
return time.mktime(_utc_datetime(dt).timetuple()) - time.timezone
91
def _utc_timeuuid(dt, lowest_val=True):
92
return convert_time_to_uuid(_utc_timestamp(dt), lowest_val)
95
def _cmp_timeuuid(a, b):
97
return cmp(a.time, b.time)
101
def _bound_timeuuid(a, b, max=False):
102
if a == '' or _cmp_timeuuid(b, a) == (1 if max else -1):
81
107
class CassandraConnection(object):
83
109
def __init__(self, keyspace, host):
116
142
'message-id': message.get('message-id'),
119
def get_messages(self, archive_uuid, order, count, memo):
145
def _trim(self, sequence, end):
146
"""Return the sequence with one of the ends trimmed.
148
:param end: if true, remove the last element. otherwise remove
156
def get_messages(self, archive_uuid, order, count, memo, backward=False,
157
start_date=None, finish_date=None):
120
158
if order in ("date", "-date"):
121
159
reversed = order[0] == '-'
123
161
raise AssertionError("Unsupported order.")
125
163
memo = uuid.UUID(memo)
170
if start_date is not None:
171
start = _bound_timeuuid(
172
start, _utc_timeuuid(start_date, lowest_val=False), max=True)
173
if finish_date is not None:
174
finish = _bound_timeuuid(
175
finish, _utc_timeuuid(finish_date, lowest_val=False))
126
177
# Get up to n+1 messages from the memo: the last item of the
127
178
# previous batch (because that's where the memo starts) + this
129
180
pairs = self.archive_messages.get(
130
archive_uuid, column_count=count + 1,
131
column_start=memo, column_reversed=reversed).items()
181
archive_uuid, column_count=count + 1, column_start=start,
182
column_finish=finish, column_reversed=reversed).items()
133
if memo and len(pairs) and pairs[0][0] <= memo:
184
if len(pairs) and memo and pairs[0][0] <= memo:
185
# The memo (from the previous batch) was included in the result.
187
pairs = self._trim(pairs, False ^ backward)
135
188
elif len(pairs) > count:
189
# There was no memo in the result, so the n+1th element is
190
# unnecessary. Kill it.
191
pairs = self._trim(pairs, True ^ backward)
138
193
if len(pairs) == 0:
139
194
return (None, [], None)
141
196
assert 0 < len(pairs) <= count
198
# We've narrowed down the message references. Fetch the messages.
143
199
ids = [v for k, v in pairs]
144
200
messages = self.messages.multiget(
145
201
ids, columns=['date', 'from', 'subject', 'message-id'])