1
2
3 try:
4
5 from collections import deque
6 except ImportError:
7 deque = None
8 import os
9 import pickle
10
11 from zope.interface import implements, Interface
12
13
15 f = open(path, 'rb')
16 try:
17 return f.read()
18 finally:
19 f.close()
20
21
23 f = open(path, 'wb')
24 try:
25 f.write(buf)
26 finally:
27 f.close()
28
29
31 """Abstraction of a queue."""
33 """Adds an individual item to the end of the queue.
34
35 Returns an item if it was overflowed."""
36
38 """Adds a list of items as the oldest entries.
39
40 Normally called in case of failure to process the data, queue the data
41 back so it can be retrieved at a later time.
42
43 Returns a list of items if it was overflowed."""
44
46 """Pop many items at once. Defaults to self.maxItems()."""
47
49 """Save the queue to storage if implemented."""
50
52 """Returns items in the queue.
53
54 Warning: Can be extremely slow for queue on disk."""
55
57 """Returns the number of items in the queue."""
58
60 """Returns the maximum number of items this queue can hold."""
61
62
63
65 """Simple length bounded queue using list."""
66 implements(IQueue)
67
69 self._maxItems = maxItems
70 if self._maxItems is None:
71 self._maxItems = 10000
72 self._items = []
73
75 self._items.append(item)
76 if len(self._items) > self._maxItems:
77 return self._items.pop(0)
78
80 ret = None
81 excess = len(self._items) + len(chunk) - self._maxItems
82 if excess > 0:
83 ret = chunk[0:excess]
84 chunk = chunk[excess:]
85 self._items = chunk + self._items
86 return ret
87
98
101
104
106 return len(self._items)
107
109 return self._maxItems
110
111 if deque:
113 """Simple length bounded queue using deque.
114
115 list.pop(0) operation is O(n) so for a 10000 items list, it can start to
116 be real slow. On the contrary, deque.popleft() is O(1) most of the time.
117 See http://docs.python.org/library/collections.html for more
118 information.
119 """
120 implements(IQueue)
121
123 self._maxItems = maxItems
124 if self._maxItems is None:
125 self._maxItems = 10000
126 self._items = deque()
127
129 ret = None
130 if len(self._items) == self._maxItems:
131 ret = self._items.popleft()
132 self._items.append(item)
133 return ret
134
136 ret = None
137 excess = len(self._items) + len(chunk) - self._maxItems
138 if excess > 0:
139 ret = chunk[0:excess]
140 chunk = chunk[excess:]
141 self._items.extendleft(reversed(chunk))
142 return ret
143
145 if nbItems is None:
146 nbItems = self._maxItems
147 if nbItems > len(self._items):
148 items = list(self._items)
149 self._items = deque()
150 else:
151 items = []
152 for i in range(nbItems):
153 items.append(self._items.popleft())
154 return items
155
158
160 return list(self._items)
161
163 return len(self._items)
164
166 return self._maxItems
167
168 MemoryQueue = DequeMemoryQueue
169 else:
170 MemoryQueue = ListMemoryQueue
171
172
174 """Keeps a list of abstract items and serializes it to the disk.
175
176 Use pickle for serialization."""
177 implements(IQueue)
178
179 - def __init__(self, path, maxItems=None, pickleFn=pickle.dumps,
180 unpickleFn=pickle.loads):
181 """
182 @path: directory to save the items.
183 @maxItems: maximum number of items to keep on disk, flush the
184 older ones.
185 @pickleFn: function used to pack the items to disk.
186 @unpickleFn: function used to unpack items from disk.
187 """
188 self.path = path
189 self._maxItems = maxItems
190 if self._maxItems is None:
191 self._maxItems = 100000
192 if not os.path.isdir(self.path):
193 os.mkdir(self.path)
194 self.pickleFn = pickleFn
195 self.unpickleFn = unpickleFn
196
197
198 self._nbItems = 0
199
200 self.firstItemId = 0
201 self.lastItemId = 0
202 self._loadFromDisk()
203
205 ret = None
206 if self._nbItems == self._maxItems:
207 id = self._findNext(self.firstItemId)
208 path = os.path.join(self.path, str(id))
209 ret = self.unpickleFn(ReadFile(path))
210 os.remove(path)
211 self.firstItemId = id + 1
212 else:
213 self._nbItems += 1
214 self.lastItemId += 1
215 path = os.path.join(self.path, str(self.lastItemId))
216 if os.path.exists(path):
217 raise IOError('%s already exists.' % path)
218 WriteFile(path, self.pickleFn(item))
219 return ret
220
222 ret = None
223 excess = self._nbItems + len(chunk) - self._maxItems
224 if excess > 0:
225 ret = chunk[0:excess]
226 chunk = chunk[excess:]
227 for i in reversed(chunk):
228 self.firstItemId -= 1
229 path = os.path.join(self.path, str(self.firstItemId))
230 if os.path.exists(path):
231 raise IOError('%s already exists.' % path)
232 WriteFile(path, self.pickleFn(i))
233 self._nbItems += 1
234 return ret
235
237 if nbItems is None:
238 nbItems = self._maxItems
239 ret = []
240 for i in range(nbItems):
241 if self._nbItems == 0:
242 break
243 id = self._findNext(self.firstItemId)
244 path = os.path.join(self.path, str(id))
245 ret.append(self.unpickleFn(ReadFile(path)))
246 os.remove(path)
247 self._nbItems -= 1
248 self.firstItemId = id + 1
249 return ret
250
253
255 """Warning, very slow."""
256 ret = []
257 for id in range(self.firstItemId, self.lastItemId + 1):
258 path = os.path.join(self.path, str(id))
259 if os.path.exists(path):
260 ret.append(self.unpickleFn(ReadFile(path)))
261 return ret
262
265
267 return self._maxItems
268
269
270
272 while True:
273 path = os.path.join(self.path, str(id))
274 if os.path.isfile(path):
275 return id
276 id += 1
277 return None
278
280 """Loads the queue from disk upto self.maxMemoryItems items into
281 self.items.
282 """
283 def SafeInt(item):
284 try:
285 return int(item)
286 except ValueError:
287 return None
288
289 files = filter(None, [SafeInt(x) for x in os.listdir(self.path)])
290 files.sort()
291 self._nbItems = len(files)
292 if self._nbItems:
293 self.firstItemId = files[0]
294 self.lastItemId = files[-1]
295
296
298 """Keeps a list of abstract items and serializes it to the disk.
299
300 It has 2 layers of queue, normally an in-memory queue and an on-disk queue.
301 When the number of items in the primary queue gets too large, the new items
302 are automatically saved to the secondary queue. The older items are kept in
303 the primary queue.
304 """
305 implements(IQueue)
306
307 - def __init__(self, primaryQueue=None, secondaryQueue=None, path=None):
308 """
309 @primaryQueue: memory queue to use before buffering to disk.
310 @secondaryQueue: disk queue to use as permanent buffer.
311 @path: path is a shortcut when using default DiskQueue settings.
312 """
313 self.primaryQueue = primaryQueue
314 if self.primaryQueue is None:
315 self.primaryQueue = MemoryQueue()
316 self.secondaryQueue = secondaryQueue
317 if self.secondaryQueue is None:
318 self.secondaryQueue = DiskQueue(path)
319
320
321 if self.secondaryQueue.nbItems() < self.primaryQueue.maxItems():
322 self.primaryQueue.insertBackChunk(
323 self.secondaryQueue.popChunk(self.primaryQueue.maxItems()))
324
326
327
328
329 if (self.secondaryQueue.nbItems() or
330 self.primaryQueue.nbItems() == self.primaryQueue.maxItems()):
331 item = self.secondaryQueue.pushItem(item)
332 if item is None:
333 return item
334
335
336
337 return self.primaryQueue.pushItem(item)
338
340 ret = None
341
342 excess = self.nbItems() + len(chunk) - self.maxItems()
343 if excess > 0:
344 ret = chunk[0:excess]
345 chunk = chunk[excess:]
346
347 excess = (self.primaryQueue.nbItems() + len(chunk) -
348 self.primaryQueue.maxItems())
349 if excess > 0:
350 chunk2 = []
351 for i in range(excess):
352 chunk2.append(self.primaryQueue.items().pop())
353 chunk2.reverse()
354 x = self.primaryQueue.insertBackChunk(chunk)
355 assert x is None, "primaryQueue.insertBackChunk did not return None"
356 if excess > 0:
357 x = self.secondaryQueue.insertBackChunk(chunk2)
358 assert x is None, ("secondaryQueue.insertBackChunk did not return "
359 " None")
360 return ret
361
370
373
375 return self.primaryQueue.items() + self.secondaryQueue.items()
376
378 return self.primaryQueue.nbItems() + self.secondaryQueue.nbItems()
379
382
383
385 """Adds functionality to a IQueue object to track its usage.
386
387 Adds a new member function getIndex() and modify popChunk() and
388 insertBackChunk() to keep a virtual pointer to the queue's first entry
389 index."""
390 implements(IQueue)
391
393
394
395 assert IQueue.providedBy(queue)
396 def Filter(m):
397 return (m[0] != '_' and callable(getattr(queue, m))
398 and not hasattr(self, m))
399 for member in filter(Filter, dir(queue)):
400 setattr(self, member, getattr(queue, member))
401 self.queue = queue
402 self._index = 0
403
406
412
414 self._index -= len(items)
415 ret = self.queue.insertBackChunk(items)
416 if ret:
417 self._index += len(ret)
418 return ret
419
420
422 """If the IQueue wasn't already a IndexedQueue, makes it an IndexedQueue."""
423 if not IQueue.providedBy(queue):
424 raise TypeError("queue doesn't implement IQueue", queue)
425 if isinstance(queue, IndexedQueue):
426 return queue
427 return IndexedQueue(queue)
428
429
430