#NEWLINE# def commit( self , transaction = None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# for ( collection , cache ) in self . _save_cache . items ( ) : #NEWLINE# #INDENT# for ( pk , attributes ) in cache . items ( ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# self . db [ collection ] . save ( attributes ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# logger . error ( 'Error when saving the document with pk {0} in collection {1}' . format ( attributes [ 'pk' ] , collection ) ) #NEWLINE# logger . error ( ( 'Attributes (excerpt):' + str ( dict ( attributes . items ( ) [ : 100 ] ) ) ) ) #NEWLINE# raise #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for ( collection , cache ) in self . _delete_cache . items ( ) : #NEWLINE# #INDENT# for pk in cache : #NEWLINE# #INDENT# self . db [ collection ] . remove ( { #NEWLINE# #INDENT# '_id' : pk , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# for ( collection , cache ) in self . _update_cache . items ( ) : #NEWLINE# #INDENT# for ( pk , attributes ) in cache . items ( ) : #NEWLINE# #INDENT# update_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for key in ( '$set' , '$unset' ) : #NEWLINE# #INDENT# if ( ( self in attributes ) and attributes [ key ] ) : #NEWLINE# #INDENT# update_dict [ key ] = attributes [ key ] #NEWLINE# #UNINDENT# #UNINDENT# if update_dict : #NEWLINE# #INDENT# self . db [ collection ] . update ( { #NEWLINE# #INDENT# '_id' : pk , #NEWLINE# #UNINDENT# } , update_dict ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# self . _save_cache = defaultdict ( ( lambda : { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) ) #NEWLINE# self . _delete_cache = defaultdict ( ( lambda : { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) ) #NEWLINE# self . _update_cache = defaultdict ( ( lambda : { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) ) #NEWLINE# self . in_transaction = True