Exemple #1
0
 def a(self):
     s = BytesIO()
     for sample in ([], [0], [2, 1], range(7)):
         int_array = IntArray(file=s, number_of_ints=10, maximum_int=10)
         for j, x in enumerate(sample):
             int_array[j] = x
         non_blanks = set(int_array)
         non_blanks.discard(int_array.get_blank_value())
         assert set(sample) == non_blanks, (list(int_array), sample)
     assert raises(IndexError, int_array.__getitem__, 10)
     int_array2 = IntArray(file=BytesIO(s.getvalue()))
     int_array3 = IntArray(number_of_ints=10, maximum_int=300)
     for x in range(10):
         assert int_array3.get(x) == None
     assert int_array3[1] == int_array3.get_blank_value()
     int_array3[1] = 42
     assert int_array3.get(1)== 42
     assert len(int_array3) == 10
     raises(ValueError, int_array3.__setitem__, 2, 100000)
     int_array4 = IntArray(number_of_ints=10)
     assert int_array4.get(1, default=42) == 42
     assert int_array4.get(100, default=42) == 42
     assert list(iteritems(int_array4)) == []
     int_array4[3] = 4
     int_array4[8] = 9
     assert list(iteritems(int_array4)) == [(3, 4), (8, 9)]
Exemple #2
0
 def end(self, handle_invalidations=None):
     write(self.s, 'C')
     n = read_int4(self.s)
     oid_list = []
     if n != 0:
         packed_oids = read(self.s, n * 8)
         oid_list = split_oids(packed_oids)
         try:
             handle_invalidations(oid_list)
         except ConflictError:
             self.transaction_new_oids.reverse()
             self.oid_pool.extend(self.transaction_new_oids)
             assert len(self.oid_pool) == len(set(self.oid_pool))
             self.begin()  # clear out records and transaction_new_oids.
             write_int4(self.s, 0)  # Tell server we are done.
             raise
     tdata = []
     for oid, record in iteritems(self.records):
         tdata.append(int4_to_str(8 + len(record)))
         tdata.append(as_bytes(oid))
         tdata.append(record)
     tdata = join_bytes(tdata)
     write_int4_str(self.s, tdata)
     self.records.clear()
     if len(tdata) > 0:
         status = read(self.s, 1)
         if status == STATUS_OKAY:
             pass
         elif status == STATUS_INVALID:
             raise WriteConflictError()
         else:
             raise ProtocolError('server returned invalid status %r' %
                                 status)
Exemple #3
0
 def end(self, handle_invalidations=None):
     write(self.s, 'C')
     n = read_int4(self.s)
     oid_list = []
     if n != 0:
         packed_oids = read(self.s, n*8)
         oid_list = split_oids(packed_oids)
         try:
             handle_invalidations(oid_list)
         except ConflictError:
             self.transaction_new_oids.reverse()
             self.oid_pool.extend(self.transaction_new_oids)
             assert len(self.oid_pool) == len(set(self.oid_pool))
             self.begin() # clear out records and transaction_new_oids.
             write_int4(self.s, 0) # Tell server we are done.
             raise
     tdata = []
     for oid, record in iteritems(self.records):
         tdata.append(int4_to_str(8 + len(record)))
         tdata.append(as_bytes(oid))
         tdata.append(record)
     tdata = join_bytes(tdata)
     write_int4_str(self.s, tdata)
     self.records.clear()
     if len(tdata) > 0:
         status = read(self.s, 1)
         if status == STATUS_OKAY:
             pass
         elif status == STATUS_INVALID:
             raise WriteConflictError()
         else:
             raise ProtocolError('server returned invalid status %r' % status)
Exemple #4
0
 def iterindex(self):
     for n, position in iteritems(self.offset_map):
         if position < self.offset_map.get_start():
             name = int8_to_str(n)
             if name not in self.memory_index:
                 yield name, position
     for item in list(self.memory_index.items()):
         yield item
Exemple #5
0
 def __setstate__(self, state):
     if _hasattribute(self, '__dict__'):
         _getattribute(self, '__dict__').clear()
     for name in self._p_gen_data_slots():
         _delattribute(self, name)
     if state is not None:
         for key, value in iteritems(state):
             _setattribute(self, key, value)
Exemple #6
0
 def gen_oid_record(self, start_oid=None, batch_size=100):
     if start_oid is None:
         for oid, offset in iteritems(self.index):
             yield oid, self.load(oid)
     else:
         for item in Storage.gen_oid_record(
             self, start_oid=start_oid, batch_size=batch_size):
             yield item
Exemple #7
0
 def iterindex(self):
     for n, position in iteritems(self.offset_map):
         if position < self.offset_map.get_start():
             name = int8_to_str(n)
             if name not in self.memory_index:
                 yield name, position
     for item in list(self.memory_index.items()):
         yield item
Exemple #8
0
 def abort(self):
     """
     Abort uncommitted changes, sync, and try to shrink the cache.
     """
     for oid, obj in iteritems(self.changed):
         obj._p_set_status_ghost()
     self.changed.clear()
     self._sync()
     self.shrink_cache()
     self.transaction_serial += 1
Exemple #9
0
 def abort(self):
     """
     Abort uncommitted changes, sync, and try to shrink the cache.
     """
     for oid, obj in iteritems(self.changed):
         obj._p_set_status_ghost()
     self.changed.clear()
     self._sync()
     self.shrink_cache()
     self.transaction_serial += 1
Exemple #10
0
 def end(self, handle_invalidations=None):
     self.shelf.store(iteritems(self.pending_records))
     if is_logging(10):
         shelf_file = self.shelf.get_file()
         shelf_file.seek_end()
         pos = shelf_file.tell()
         log(10, "Transaction at [%s] end=%s" % (datetime.now(), pos))
     if self.pack_extra is not None:
         self.pack_extra.update(self.pending_records)
     self.allocated_unused_oids -= set(self.pending_records)
     self.begin()
Exemple #11
0
 def end(self, handle_invalidations=None):
     self.shelf.store(iteritems(self.pending_records))
     if is_logging(20):
         shelf_file = self.shelf.get_file()
         shelf_file.seek_end()
         pos = shelf_file.tell()
         log(20, "Transaction at [%s] end=%s" % (datetime.now(), pos))
     if self.pack_extra is not None:
         self.pack_extra.update(self.pending_records)
     self.allocated_unused_oids -= set(self.pending_records)
     self.begin()
Exemple #12
0
 def commit(self):
     """
     If there are any changes, try to store them, and
     raise WriteConflictError if there are any invalid oids saved
     or if there are any invalid oids for non-ghost objects.
     """
     if not self.changed:
         self._sync()
     else:
         assert not self.invalid_oids, "still conflicted: missing abort()"
         self.storage.begin()
         new_objects = {}
         for oid, changed_object in iteritems(self.changed):
             writer = ObjectWriter(self)
             try:
                 for obj in writer.gen_new_objects(changed_object):
                     oid = obj._p_oid
                     if oid in new_objects:
                         continue
                     elif oid not in self.changed:
                         new_objects[oid] = obj
                         self.cache[oid] = obj
                     data, refs = writer.get_state(obj)
                     self.storage.store(oid, pack_record(oid, data, refs))
                     obj._p_set_status_saved()
             finally:
                 writer.close()
         try:
             self.storage.end(self._handle_invalidations)
         except ConflictError:
             for oid, obj in iteritems(new_objects):
                 obj._p_oid = None
                 del self.cache[oid]
                 obj._p_set_status_unsaved()
                 obj._p_connection = None
             raise
         self.changed.clear()
     self.shrink_cache()
     self.transaction_serial += 1
Exemple #13
0
 def commit(self):
     """
     If there are any changes, try to store them, and
     raise WriteConflictError if there are any invalid oids saved
     or if there are any invalid oids for non-ghost objects.
     """
     if not self.changed:
         self._sync()
     else:
         assert not self.invalid_oids, "still conflicted: missing abort()"
         self.storage.begin()
         new_objects = {}
         for oid, changed_object in iteritems(self.changed):
             writer = ObjectWriter(self)
             try:
                 for obj in writer.gen_new_objects(changed_object):
                     oid = obj._p_oid
                     if oid in new_objects:
                         continue
                     elif oid not in self.changed:
                         new_objects[oid] = obj
                         self.cache[oid] = obj
                     data, refs = writer.get_state(obj)
                     self.storage.store(oid, pack_record(oid, data, refs))
                     obj._p_set_status_saved()
             finally:
                 writer.close()
         try:
             self.storage.end(self._handle_invalidations)
         except ConflictError:
             for oid, obj in iteritems(new_objects):
                 obj._p_oid = None
                 del self.cache[oid]
                 obj._p_set_status_unsaved()
                 obj._p_connection = None
             raise
         self.changed.clear()
     self.shrink_cache()
     self.transaction_serial += 1
Exemple #14
0
 def gen_oid_record(self, start_oid=None, **other):
     if start_oid is None:
         for item in iteritems(self.shelf):
             yield item
     else:
         todo = [start_oid]
         seen = IntSet()  # This eventually contains them all.
         while todo:
             oid = todo.pop()
             if str_to_int8(oid) in seen:
                 continue
             seen.add(str_to_int8(oid))
             record = self.load(oid)
             record_oid, data, refdata = unpack_record(record)
             assert oid == record_oid
             todo.extend(split_oids(refdata))
             yield oid, record
Exemple #15
0
 def gen_oid_record(self, start_oid=None, **other):
     if start_oid is None:
         for item in iteritems(self.shelf):
             yield item
     else:
         todo = [start_oid]
         seen = IntSet() # This eventually contains them all.
         while todo:
             oid = todo.pop()
             if str_to_int8(oid) in seen:
                 continue
             seen.add(str_to_int8(oid))
             record = self.load(oid)
             record_oid, data, refdata = unpack_record(record)
             assert oid == record_oid
             todo.extend(split_oids(refdata))
             yield oid, record
Exemple #16
0
 def gen_oid_record(self, start_oid=None, seen=None, **other):
     if start_oid is None:
         for item in iteritems(self.shelf):
             yield item
     else:
         todo = [start_oid]
         if seen is None:
             seen = IntSet() # This eventually contains them all.
         while todo:
             oid = heapq.heappop(todo)
             if str_to_int8(oid) in seen:
                 continue
             seen.add(str_to_int8(oid))
             record = self.load(oid)
             record_oid, data, refdata = unpack_record(record)
             assert oid == record_oid
             for ref_oid in split_oids(refdata):
                 heapq.heappush(todo, ref_oid)
             yield oid, record
Exemple #17
0
 def iteritems(self):
     return iteritems(self.data)
Exemple #18
0
 def _generate_pending_records(self):
     for oid, record in iteritems(self.pending_records):
         yield oid, record
Exemple #19
0
 def iteritems(self):
     return iteritems(self.data)