Added capped collections support
This commit is contained in:
@@ -144,6 +144,8 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
||||
meta = {
|
||||
'collection': collection,
|
||||
'allow_inheritance': True,
|
||||
'max_documents': None,
|
||||
'max_size': None,
|
||||
}
|
||||
meta.update(attrs.get('meta', {}))
|
||||
# Only simple classes - direct subclasses of Document - may set
|
||||
|
||||
@@ -36,6 +36,14 @@ class Document(BaseDocument):
|
||||
though). To disable this behaviour and remove the dependence on the
|
||||
presence of `_cls` and `_types`, set :attr:`allow_inheritance` to
|
||||
``False`` in the :attr:`meta` dictionary.
|
||||
|
||||
A :class:`~mongoengine.Document` may use a **Capped Collection** by
|
||||
specifying :attr:`max_documents` and :attr:`max_size` in the :attr:`meta`
|
||||
dictionary. :attr:`max_documents` is the maximum number of documents that
|
||||
is allowed to be stored in the collection, and :attr:`max_size` is the
|
||||
maximum size of the collection in bytes. If :attr:`max_size` is not
|
||||
specified and :attr:`max_documents` is, :attr:`max_size` defaults to
|
||||
10000000 bytes (10MB).
|
||||
"""
|
||||
|
||||
__metaclass__ = TopLevelDocumentMetaclass
|
||||
|
||||
@@ -3,7 +3,7 @@ from connection import _get_db
|
||||
import pymongo
|
||||
|
||||
|
||||
__all__ = ['queryset_manager']
|
||||
__all__ = ['queryset_manager', 'InvalidQueryError', 'InvalidCollectionError']
|
||||
|
||||
|
||||
class InvalidQueryError(Exception):
|
||||
@@ -280,6 +280,10 @@ class QuerySet(object):
|
||||
return self.exec_js(freq_func, list_field, normalize=normalize)
|
||||
|
||||
|
||||
class InvalidCollectionError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class QuerySetManager(object):
|
||||
|
||||
def __init__(self, manager_func=None):
|
||||
@@ -296,7 +300,32 @@ class QuerySetManager(object):
|
||||
|
||||
if self._collection is None:
|
||||
db = _get_db()
|
||||
self._collection = db[owner._meta['collection']]
|
||||
collection = owner._meta['collection']
|
||||
|
||||
# Create collection as a capped collection if specified
|
||||
if owner._meta['max_size'] or owner._meta['max_documents']:
|
||||
# Get max document limit and max byte size from meta
|
||||
max_size = owner._meta['max_size'] or 10000000 # 10MB default
|
||||
max_documents = owner._meta['max_documents']
|
||||
|
||||
if collection in db.collection_names():
|
||||
self._collection = db[collection]
|
||||
# The collection already exists, check if its capped
|
||||
# options match the specified capped options
|
||||
options = self._collection.options()
|
||||
if options.get('max') != max_documents or \
|
||||
options.get('size') != max_size:
|
||||
msg = ('Cannot create collection "%s" as a capped '
|
||||
'collection as it already exists') % collection
|
||||
raise InvalidCollectionError(msg)
|
||||
else:
|
||||
# Create the collection as a capped collection
|
||||
opts = {'capped': True, 'size': max_size}
|
||||
if max_documents:
|
||||
opts['max'] = max_documents
|
||||
self._collection = db.create_collection(collection, opts)
|
||||
else:
|
||||
self._collection = db[collection]
|
||||
|
||||
# owner is the document that contains the QuerySetManager
|
||||
queryset = QuerySet(owner, self._collection)
|
||||
|
||||
Reference in New Issue
Block a user