def insert(self, entry): """Inserts an entry tuple into the RecordIORecords. :param entry: An entry tuple :return: Returns True if the key existed before. """ pos = bisect_left(self.records_, entry, comperator=self.entry_comperator) if (pos < len(self.records_) and self.entry_comperator(self.records_[pos], entry) == 0): existing_entry = self.records_[pos] if len(existing_entry) == 5 and existing_entry[1] == 0: del_key = existing_entry[0] del_amount = existing_entry[2] del_hash = existing_entry[3] for i in xrange(1, del_amount): # POSSIBLE OPTIMIZATION: Do not do binary searches del_entry = (del_key, i, del_amount, del_hash) if not self.insert(del_entry): self.not_deleted_.add(del_entry) self.records_[pos] = entry return True else: self.records_.insert(pos, entry) return not self.is_entry_deleted(entry)
def read(self, start_key=("",), end_key=None): """Reads through the records from start_key to end_key (exclusive) :param start_key: An entry tuple (no value needed) :param end_key: An entry tuple (no value needed) :return: Yields all entry tuples within the range. """ pos = bisect_left(self.records_, start_key, comperator=self.entry_comperator) for entry in self.records_[pos:]: if end_key != None and self.entry_comperator(entry, end_key) >= 0: break yield entry
def read(self, start_key=("", ), end_key=None): """Reads through the records from start_key to end_key (exclusive) :param start_key: An entry tuple (no value needed) :param end_key: An entry tuple (no value needed) :return: Yields all entry tuples within the range. """ pos = bisect_left(self.records_, start_key, comperator=self.entry_comperator) for entry in self.records_[pos:]: if end_key != None and self.entry_comperator(entry, end_key) >= 0: break yield entry
def insert(self, entry): """Inserts an entry tuple into the RecordIORecords. :param entry: An entry tuple """ pos = bisect_left(self.zipped_chunks_, (entry, entry), comperator=self.zip_chunk_comperator) if ((pos < len(self.zipped_chunks_) and self.zip_chunk_comperator(self.zipped_chunks_[pos], (entry, entry)) == 0) or self.is_entry_deleted(entry)): RecordIORecords.insert(self, entry) else: self.zipped_chunks_.insert(pos, (entry[:-1], entry[:-1], zlib.compress(marshal.dumps([entry], MARSHAL_VERSION), COMPRESSION_LEVEL_MIN)))
def insert(self, entry): """Inserts an entry tuple into the RecordIORecords. :param entry: An entry tuple """ pos = bisect_left(self.zipped_chunks_, (entry, entry), comperator=self.zip_chunk_comperator) if ((pos < len(self.zipped_chunks_) and self.zip_chunk_comperator(self.zipped_chunks_[pos], (entry, entry)) == 0) or self.is_entry_deleted(entry)): RecordIORecords.insert(self, entry) else: self.zipped_chunks_.insert( pos, (entry[:-1], entry[:-1], zlib.compress(marshal.dumps([entry], MARSHAL_VERSION), COMPRESSION_LEVEL_MIN)))
def testBisectLeftFullEven(self): test_array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for i in xrange(0, 10): self.assertEqual(bisect_left(test_array, i), i) self.assertEqual(bisect_left(test_array, -1), 0) self.assertEqual(bisect_left(test_array, 10), 10)
def testBisectLeftOneElement(self): self.assertEqual(bisect_left([0], 0), 0) self.assertEqual(bisect_left([0], -1), 0) self.assertEqual(bisect_left([0], 1), 1)
def testBisectLeftEmpty(self): self.assertEqual(bisect_left([], 0), 0)
def testBisectLeftSparseOdd(self): test_array = [0, 2, 4, 6, 8] for i in xrange(0, 9): self.assertEqual(bisect_left(test_array, i), (i + 1) / 2)
def testBisectLeftSparseEven(self): test_array = [0, 2, 4, 6, 8, 10] for i in xrange(0, 10): self.assertEqual(bisect_left(test_array, i), (i + 1) / 2)
def testBisectLeftFullOdd(self): test_array = [0, 1, 2, 3, 4, 5, 6, 7, 8] for i in xrange(0, 9): self.assertEqual(bisect_left(test_array, i), i)
def testBisectLeftSparseEven(self): test_array = [0, 2, 4, 6, 8, 10] for i in xrange(0, 10): self.assertEqual(bisect_left(test_array, i), (i+1)/2)
def testBisectLeftSparseOdd(self): test_array = [0, 2, 4, 6, 8] for i in xrange(0, 9): self.assertEqual(bisect_left(test_array, i), (i+1)/2)