1 from operator
import itemgetter
2 from collections
import namedtuple
4 from couchdbkit
.ext
.django
import loading
6 from couchdbkit
import *
9 def get_main_database():
10 return loading
.get_db('core')
13 def get_categories_database():
14 """ returns the database that contains Category documents """
15 return loading
.get_db('categories')
18 def get_pubsub_database():
19 return loading
.get_db('pubsub')
22 def get_userdata_database():
23 return loading
.get_db('userdata')
25 def get_database(user
=None):
26 return get_main_database()
29 class BulkException(Exception):
31 def __init__(self
, errors
):
35 BulkError
= namedtuple('BulkError', 'doc error reason')
38 def __default_reload(db
, obj
):
41 if isinstance(obj
, Document
):
42 return obj
.__class
__.get(_id
)
47 __get_obj
= itemgetter(0)
49 def bulk_save_retry(obj_funs
, db
=None, reload_f
=__default_reload
):
50 """ Saves multiple documents and retries failed ones
52 Objects to be saved are passed as (obj, mod_f), where obj is the CouchDB
53 and mod_f is the modification function that should be applied to it.
55 If saving a document fails, it is again fetched from the database, the
56 modification function is applied again and saving is retried. """
58 db
= db
or get_main_database()
63 # apply modification function (and keep funs)
64 obj_funs
= [(f(o
), f
) for (o
, f
) in obj_funs
]
66 # filter those with obj None
67 obj_funs
= filter(lambda of
: __get_obj(of
) is not None, obj_funs
)
70 objs
= map(__get_obj
, obj_funs
)
79 except BulkSaveError
as ex
:
82 for res
, (obj
, f
) in zip(ex
.results
, obj_funs
):
83 if res
.get('error', False) == 'conflict':
85 # reload conflicted object
86 obj
= reload_f(db
, obj
)
87 new_obj_funs
.append( (obj
, f
) )
89 elif res
.get('error', False):
90 # don't retry other errors
91 err
= BulkError(obj
, res
['error'], res
.get('reason', None))
94 obj_funs
= new_obj_funs
97 raise BulkException(errors
)