Beispiel #1
0
 def handle_C(self, s):
     # commit
     client = self._find_client(s)
     s.sendall(p32(len(client.invalid)) + ''.join(client.invalid))
     client.invalid.clear()
     tlen = u32(recv(s, 4))
     if tlen == 0:
         return # client decided not to commit (e.g. conflict)
     tdata = recv(s, tlen)
     logging_debug = is_logging(10)
     logging_debug and log(10, 'Committing %s bytes', tlen)
     self.storage.begin()
     i = 0
     oids = []
     while i < len(tdata):
         rlen = u32(tdata[i:i+4])
         i += 4
         oid = tdata[i:i+8]
         record = tdata[i+8:i+rlen]
         i += rlen
         if logging_debug:
             class_name = extract_class_name(record)
             log(10, '  oid=%-6s rlen=%-6s %s', u64(oid), rlen, class_name)
         self.storage.store(oid, record)
         oids.append(oid)
     assert i == len(tdata)
     self.storage.end()
     log(20, 'Committed %3s objects %s bytes at %s',
         len(oids), tlen, datetime.now())
     s.sendall(STATUS_OKAY)
     for c in self.clients:
         if c is not client:
             c.invalid.update(oids)
Beispiel #2
0
 def sync(self):
     self.s.sendall('S')
     n = u32(recv(self.s, 4))
     if n == 0:
         packed_oids = ''
     else:
         packed_oids = recv(self.s, n*8)
     return split_oids(packed_oids)
Beispiel #3
0
def unpack_record(record):
    """(record:str) -> oid:str, data:str, refs:str
    The inverse of pack_record().
    """
    oid = record[:8]
    data_length = u32(record[8:12])
    data_end = 12 + data_length
    data = record[12:data_end]
    refs = record[data_end:]
    return oid, data, refs
Beispiel #4
0
 def _read_block(self):
     size_str = self.fp.read(4)
     if len(size_str) == 0:
         raise IOError, "eof"
     size = u32(size_str)
     if size == 0:
         return ''
     result = self.fp.read(size)
     if len(result) != size:
         raise IOError, "short read"
     return result
Beispiel #5
0
 def load(self, oid):
     self.s.sendall('L' + oid)
     status = recv(self.s, 1)
     if status == STATUS_OKAY:
         pass
     elif status == STATUS_INVALID:
         raise ReadConflictError([oid])
     elif status == STATUS_KEYERROR:
         raise DurusKeyError(oid)
     else:
         raise ProtocolError, 'server returned invalid status %r' % status
     rlen = u32(recv(self.s, 4))
     record = recv(self.s, rlen)
     return record
Beispiel #6
0
 def end(self, handle_invalidations=None):
     self.s.sendall('C')
     n = u32(recv(self.s, 4))
     if n != 0:
         packed_oids = recv(self.s, n*8)
         try:
             handle_invalidations(split_oids(packed_oids))
         except ConflictError:
             self.s.sendall(p32(0)) # Tell server we are done.
             raise
     tdata = []
     for oid, record in self.records.iteritems():
         tdata.append(p32(8 + len(record)))
         tdata.append(oid)
         tdata.append(record)
     tdata = ''.join(tdata)
     self.s.sendall(p32(len(tdata)))
     self.s.sendall(tdata)
     self.records.clear()
     status = recv(self.s, 1)
     if status != STATUS_OKAY:
         raise ProtocolError, 'server returned invalid status %r' % status