<s> from threading import Lock <EOL> class ResponsibleGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , gen , cleanup ) : <EOL> self . cleanup = cleanup <EOL> self . gen = gen <EOL> def __del__ ( self ) : <EOL> self . cleanup ( ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> return self . gen . next ( ) <EOL> class ConcurrentStore ( object ) : <EOL> def __init__ ( self , store ) : <EOL> self . store = store <EOL> self . __visit_count = <NUM_LIT:0> <EOL> self . __lock = Lock ( ) <EOL> self . __pending_removes = [ ] <EOL> self . __pending_adds = [ ] <EOL> def add ( self , triple ) : <EOL> ( s , p , o ) = triple <EOL> if self . __visit_count == <NUM_LIT:0> : <EOL> self . store . add ( ( s , p , o ) ) <EOL> else : <EOL> self . __pending_adds . append ( ( s , p , o ) ) <EOL> def remove ( self , triple ) : <EOL> ( s , p , o ) = triple <EOL> if self . __visit_count == <NUM_LIT:0> : <EOL> self . store . remove ( ( s , p , o ) ) <EOL> else : <EOL> self . __pending_removes . append ( ( s , p , o ) ) <EOL> def triples ( self , triple ) : <EOL> ( su , pr , ob ) = triple <EOL> g = self . store . triples ( ( su , pr , ob ) ) <EOL> pending_removes = self . __pending_removes <EOL> self . __begin_read ( ) <EOL> for s , p , o in ResponsibleGenerator ( g , self . __end_read ) : <EOL> if not ( s , p , o ) in pending_removes : <EOL> yield s , p , o <EOL> for ( s , p , o ) in self . __pending_adds : <EOL> if ( su is None or su == s ) and ( pr is None or pr == p ) and ( ob is None or ob == o ) : <EOL> yield s , p , o <EOL> def __len__ ( self ) : <EOL> return self . store . __len__ ( ) <EOL> def __begin_read ( self ) : <EOL> lock = self . __lock <EOL> lock . acquire ( ) <EOL> self . __visit_count = self . __visit_count + <NUM_LIT:1> <EOL> lock . release ( ) <EOL> def __end_read ( self ) : <EOL> lock = self . __lock <EOL> lock . acquire ( ) <EOL> self . __visit_count = self . __visit_count - <NUM_LIT:1> <EOL> if self . __visit_count == <NUM_LIT:0> : <EOL> pending_removes = self . __pending_removes <EOL> while pending_removes : <EOL> ( s , p , o ) = pending_removes . pop ( ) <EOL> try : <EOL> self . store . remove ( ( s , p , o ) ) <EOL> except : <EOL> print s , p , o , "<STR_LIT>" <EOL> pending_adds = self . __pending_adds <EOL> while pending_adds : <EOL> ( s , p , o ) = pending_adds . pop ( ) <EOL> self . store . add ( ( s , p , o ) ) <EOL> lock . release ( ) </s>
