def get_changes(self, filter_func=lambda x: True): storage = MemcacheSlice(NAMESPACE) storage_version = storage.get_index() client_version = self.get_client_version() if client_version != storage_version: self.set_client_version(storage_version) pass_two = [] if client_version < storage_version: # Generate entries without duplicate add or update event pass_one = [] updated_entries = set() added_entries = set() for entry in storage[client_version+1:storage_version+1]: if not filter_func(entry): continue if entry['action'] != 'remove': if entry['content'] in updated_entries: continue updated_entries.add(entry['content']) if entry['action'] == 'add': added_entries.add(entry['content']) pass_one.append(entry) # Remove add and update that have a delete event, and # delete that have an add. deleted_entries = set() for entry in reversed(pass_one): if entry['action'] != 'remove': if entry['content'] in deleted_entries: continue else: deleted_entries.add(entry['content']) if entry['content'] in added_entries: continue pass_two.append(entry) # Return result in the correct order return reversed(pass_two)
class TestMemoryImplementation(unittest.TestCase): def setUp(self): self.cache = LRU(1000) self.mutex = threading.Lock() self.memslice = MemcacheSlice( 'test', client=MemcacheFakeClient(self.cache, self.mutex)) def test_empty(self): self.assertEquals(-1, self.memslice.get_index()) self.assertEquals(0, len(self.memslice)) self.assertEquals([], self.memslice[:]) self.assertEquals([], self.memslice[0:1]) def test_simple_add(self): self.memslice.push('first') self.assertEquals(0, self.memslice.get_index()) self.assertEquals(['first'], self.memslice[:]) self.assertEquals(['first'], self.memslice[0:1]) self.assertEquals(['first'], self.memslice[:1]) def test_multiple_add(self): self.memslice.push('first') self.memslice.push('second') self.memslice.push('third') self.assertEquals(2, self.memslice.get_index()) self.assertEquals(3, len(self.memslice)) self.assertEquals(['first', 'second', 'third'], self.memslice[:]) self.assertEquals(['first', 'second', 'third'], self.memslice[0:3]) self.assertEquals(['second', 'third'], self.memslice[1:]) def test_threaded_add(self): for run in range(0, 10): value_count = 40 thread_count = 5 threads = [] cache = LRU((value_count + 1) * thread_count) memslice = MemcacheSlice( 'test', client=MemcacheFakeClient(cache, self.mutex)) def async_add(): for i in range(0, value_count): memslice.push(i) for i in range(0, thread_count): th = threading.Thread(target=async_add) threads.append(th) for thread in threads: thread.start() for thread in threads: thread.join() self.assertEquals(thread_count * value_count, len(memslice), "run %d : %d/%d" % ( run + 1, len(memslice), thread_count * value_count))
def tpc_finish(self, transaction): memcache = MemcacheSlice(NAMESPACE) for entry in self._entries: memcache.push(entry) self.clear_entries()
def setUp(self): self.cache = LRU(1000) self.mutex = threading.Lock() self.memslice = MemcacheSlice( 'test', client=MemcacheFakeClient(self.cache, self.mutex))