跳转到主要内容

未知

项目描述

目录队列提供目录索引的队列。基本思想是排队目录操作,以便

  • 操作可以批量处理以提高效率

  • 应用程序请求不需要等待索引完成

当使用文本索引时,排队的好处尤为显著。

详细文档

使用队列

通过实例化zc.catalogqueue.queue.CatalogQueue对象来创建队列

>>> import zc.catalogqueue.queue
>>> queue = zc.catalogqueue.queue.CatalogQueue()

我们可以传递队列大小。它应该是一个素数。默认值为1009,略大。

>>> queue = zc.catalogqueue.queue.CatalogQueue(11)

通常,队列作为zc.catalogqueue.interfaces.ICatalogQueue实用工具注册。

>>> import zope.interface, pprint
>>> pprint.pprint(sorted(zope.interface.providedBy(queue)), width=1)
[<InterfaceClass zc.catalogqueue.interfaces.ICatalogQueue>,
 <InterfaceClass persistent.interfaces.IPersistent>]

队列维护有关其自身处理状态的一些信息。最后处理时间和处理的目录事件总数是可用的。由于此队列尚未处理,因此这些有一些初始值

>>> print queue.lastProcessedTime
None
>>> queue.totalProcessed
0

队列长度提供了访问待处理目录事件数量的途径

>>> len(queue)
0

队列以两种方式使用。当内容被修改时,我们在队列上调用add、update和remove方法

>>> queue.add(1)
>>> queue.update(1)
>>> queue.remove(1)
>>> queue.update(2)
>>> queue.update(2)
>>> queue.add(3)
>>> queue.update(3)
>>> queue.add(3)
Traceback (most recent call last):
...
TypeError: Attempt to add an object that is already in the catalog
>>> queue.update(4)
>>> queue.update(4)
>>> queue.update(4)
>>> queue.remove(5)
>>> queue.update(5)
Traceback (most recent call last):
...
TypeError: Attempt to change an object that has been removed
>>> queue.update(0)
>>> queue.update(0)

在这种情况下,我们已经添加了几个事件,但尚未处理队列,因此我们预计lastProcessedTimetotalProcessed将保持不变,但队列长度将反映待处理任务

>>> print queue.lastProcessedTime
None
>>> queue.totalProcessed
0
>>> len(queue)
6

定期,我们在队列上调用process。我们需要传递一个ids对象和一个注入(目录)对象的集合

>>> class Ids:
...     def queryObject(self, id, default=None):
...         if not id:
...             return default
...         return "object %s" % id
>>> class Injection:
...     def __init__(self, name):
...         self.name = name
...     def index_doc(self, docid, value):
...         print self.name, 'indexing', docid, value
...     def unindex_doc(self, docid):
...         print self.name, 'unindexing', docid
>>> queue.process(Ids(), [Injection('cat1'), Injection('cat2')], 10)
cat1 unindexing 1
cat2 unindexing 1
cat1 indexing 2 object 2
cat2 indexing 2 object 2
cat1 indexing 3 object 3
cat2 indexing 3 object 3
cat1 indexing 4 object 4
cat2 indexing 4 object 4
cat1 unindexing 5
cat2 unindexing 5
6

关于此示例有一些需要注意的事项

  • 每个对象只处理一次。

  • 发生的事情取决于最后的事件。

  • 对象0未索引,因为queryObject返回了None。我们忽略来自intid实用程序已删除的对象的事件。

  • 返回处理的对象数量。

处理信息已在队列中更新。

>>> queue.lastProcessedTime  # doctest: +ELLIPSIS
datetime.datetime(... tzinfo=<UTC>)
>>> queue.totalProcessed
6
>>> previous_time = queue.lastProcessedTime

队列的长度现在表示没有更多事件待处理。

>>> len(queue)
0

如果我们处理队列而不添加额外事件,我们只会返回0。

>>> queue.process(Ids(), [Injection('cat1'), Injection('cat2')], 10)
0

历史处理信息已更新。

>>> queue.lastProcessedTime  # doctest: +ELLIPSIS
datetime.datetime(... tzinfo=<UTC>)
>>> queue.lastProcessedTime > previous_time
True
>>> queue.totalProcessed
6
>>> len(queue)
0

当然,limit参数限制了我们可以处理的事件数量。

>>> for i in range(10):
...     queue.update(i)
>>> len(queue)
10
>>> queue.process(Ids(), [Injection('cat1')], 5)
cat1 indexing 1 object 1
cat1 indexing 2 object 2
cat1 indexing 3 object 3
cat1 indexing 4 object 4
5
>>> queue.totalProcessed
11
>>> len(queue)
5
>>> queue.process(Ids(), [Injection('cat1')], 5)
cat1 indexing 5 object 5
cat1 indexing 6 object 6
cat1 indexing 7 object 7
cat1 indexing 8 object 8
cat1 indexing 9 object 9
5
>>> queue.totalProcessed
16
>>> len(queue)
0

(请记住,0不处理,因为它找不到。)

当找不到对象时,会记录一条警告信息。

>>> import zope.testing.loggingsupport
>>> handler = zope.testing.loggingsupport.InstalledHandler('zc')
>>> queue.update(0)
>>> queue.process(Ids(), [Injection('cat1')], 5)
1
>>> print handler
zc.catalogqueue.queue WARNING
  Couldn't find object for 0
>>> handler.uninstall()

边缘情况

如果一个“旧”状态有两个“添加”事件,并且提交状态处理队列,而“新”状态修改了一个标记为添加的对象,则代码会将另一个标记为删除。

>>> from zc.catalogqueue.CatalogEventQueue import (
...     CatalogEventQueue, ADDED, REMOVED, CHANGED, CHANGED_ADDED)
>>> cq = CatalogEventQueue()
>>> def resolve(old, committed, new):
...     return sorted(cq._p_resolveConflict(
...        {'_conflict_policy': 0, '_data': old},
...        {'_conflict_policy': 0, '_data': committed},
...        {'_conflict_policy': 0, '_data': new}
...        )['_data'].items())
>>> resolve({1: (1, ADDED), 2: (1, ADDED)}, {},
...         {1: (1, ADDED), 2: (3, REMOVED), 3: (1, ADDED)})
[(2, (3, 0)), (3, (1, 1))]

下载

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

zc.catalogqueue-0.3.1.tar.gz (12.0 kB 查看哈希值)

上传时间

支持