<s> from couchdbkit import ResourceNotFound <EOL> from dimagi . utils . couch . database import iter_docs <EOL> from . interface import DocumentStore <EOL> from pillowtop . dao . exceptions import DocumentNotFoundError <EOL> ID_CHUNK_SIZE = <NUM_LIT> <EOL> class CouchDocumentStore ( DocumentStore ) : <EOL> def __init__ ( self , couch_db , domain = None , doc_type = None ) : <EOL> self . _couch_db = couch_db <EOL> self . domain = domain <EOL> self . doc_type = doc_type <EOL> def get_document ( self , doc_id ) : <EOL> try : <EOL> return self . _couch_db . get ( doc_id ) <EOL> except ResourceNotFound : <EOL> raise DocumentNotFoundError ( ) <EOL> def save_document ( self , doc_id , document ) : <EOL> document [ '<STR_LIT>' ] = doc_id <EOL> self . _couch_db . save_doc ( document ) <EOL> def delete_document ( self , doc_id ) : <EOL> try : <EOL> return self . _couch_db . delete_doc ( doc_id ) <EOL> except ResourceNotFound : <EOL> raise DocumentNotFoundError ( ) <EOL> def iter_document_ids ( self , last_id = None ) : <EOL> from corehq . apps . domain . dbaccessors import iterate_doc_ids_in_domain_by_type <EOL> if not ( self . domain and self . doc_type ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> start_key = None <EOL> if last_id : <EOL> last_doc = self . get_document ( last_id ) <EOL> start_key = [ self . domain , self . doc_type ] <EOL> if self . doc_type in _DATE_MAP . keys ( ) : <EOL> start_key . append ( last_doc [ _DATE_MAP [ self . doc_type ] ] ) <EOL> return iterate_doc_ids_in_domain_by_type ( <EOL> self . domain , <EOL> self . doc_type , <EOL> chunk_size = ID_CHUNK_SIZE , <EOL> database = self . _couch_db , <EOL> startkey = start_key , <EOL> startkey_docid = last_id <EOL> ) <EOL> def iter_documents ( self , ids ) : <EOL> return iter_docs ( self . _couch_db , ids , chunksize = <NUM_LIT> ) <EOL> _DATE_MAP = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } </s>
