/
__init__.py
143 lines (111 loc) · 4.32 KB
/
__init__.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
"""
Storage containers for durable queues and (planned) durable topics.
"""
import logging
import threading
from coilmq.util.concurrency import synchronized
__authors__ = ['"Hans Lellelid" <hans@xmpl.org>']
__copyright__ = "Copyright 2009 Hans Lellelid"
__license__ = """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."""
class QueueStore(object):
"""
Abstract base class for queue storage.
Extensions/implementations of this class must be thread-safe.
@ivar log: A logger for this class.
@type log: C{logging.Logger}
"""
def __init__(self):
"""
A base constructor that sets up logging and the lock used by synchronized decorator.
If you extend this class, you should either call this method or at minimum make sure these values
get set.
"""
self.log = logging.getLogger('%s.%s' % (self.__module__, self.__class__.__name__))
self._lock = threading.RLock()
@synchronized
def enqueue(self, destination, frame):
raise NotImplementedError()
@synchronized
def dequeue(self, destination):
raise NotImplementedError()
@synchronized
def requeue(self, destination, frame):
self.enqueue(destination, frame)
@synchronized
def size(self, destination):
"""
Size of the queue for specified destination.
@param destination: The queue destination (e.g. /queue/foo)
@type destination: C{str}
@return: The number of frames in specified queue.
@rtype: C{int}
"""
raise NotImplementedError()
@synchronized
def has_frames(self, destination):
"""
Whether specified destination has any frames.
Default implementation uses L{QueueStore.size} to determine if there
are any frames in queue. Subclasses may choose to optimize this.
@param destination: The queue destination (e.g. /queue/foo)
@type destination: C{str}
@return: The number of frames in specified queue.
@rtype: C{int}
"""
return self.size(destination) > 0
@synchronized
def close(self):
"""
May be implemented to perform any necessary cleanup operations when store is closed.
"""
pass
# This is intentionally not synchronized, since it does not directly
# expose any shared data.
def frames(self, destination):
"""
Returns an iterator for frames in specified queue.
The iterator simply wraps calls to L{dequeue} method, so the order of the
frames from the iterator will be the reverse of the order in which the
frames were enqueued.
@param destination: The queue destination (e.g. /queue/foo)
@type destination: C{str}
"""
return QueueFrameIterator(self, destination)
class QueueFrameIterator(object):
"""
Provides an C{iterable} over the frames for a specified destination in a queue.
@ivar store: The queue store.
@type store: L{coilmq.store.QueueStore}
@ivar destination: The destination for this iterator.
@type destination: C{str}
"""
def __init__(self, store, destination):
self.store = store
self.destination = destination
def __iter__(self):
return self
def next(self):
frame = self.store.dequeue(self.destination)
if not frame:
raise StopIteration()
return frame
def __len__(self):
return self.store.size(self.destination)
def __nonzero__(self):
return self.store.has_frames(self.destination)
class TopicStore(object):
"""
Abstract base class for non-durable topic storage.
"""
class DurableTopicStore(TopicStore):
"""
Abstract base class for durable topic storage.
"""