Ejemplo n.º 1
0
    def test_deserialize_logindex_write(self):
        log_index = LogIndexEntrySerializer.deserialize(
            decodehexstr("03020a5f8201"))

        self.assertEquals(
            log_index,
            LogIndexEntry(LogIndexEntry.WRITE, 34234242, needs_commit=True))
Ejemplo n.º 2
0
    def test_deserialize_logindex_begin_tx(self):
        log_index = LogIndexEntrySerializer.deserialize(
            decodehexstr("010000000001"))

        self.assertEquals(
            log_index, LogIndexEntry(LogIndexEntry.BEGIN_TX,
                                     needs_commit=True))
Ejemplo n.º 3
0
 def write(self, chunkid, address, data):
     size = len(data)
     log = Log(LogHeader(chunkid, address, len(data)), data,
               self.chunkfile.read(chunkid, address, size))
     bufferpos = self.logbuffer.find_empty_location_for_log(log)
     self.logbuffer.write_entry(bufferpos, log)
     self.logindex.log(LogIndexEntry(LogIndexEntry.WRITE, bufferpos))
Ejemplo n.º 4
0
    def test_load_WhenCalledWithLogIndexAndBufferReader_WriteLogsAreCorrectlyAssigned(
            self):
        logbuffer_reader = mock.Mock(read_entry=lambda pos: {
            0: ("log1", 1),
            10: ("log2", 5)
        }[pos])
        logindex = mock.Mock(tocommit=[
            LogIndexEntry(LogIndexEntry.WRITE, 0),
            LogIndexEntry(LogIndexEntry.WRITE, 10)
        ])

        logbuff = LogBuffer.load(logindex, logbuffer_reader)

        self.assertEquals(logbuff.writelogs, {
            (0, 1): "log1",
            (10, 15): "log2"
        })
Ejemplo n.º 5
0
 def deserialize(data):
     if len(data) != LogIndexEntrySerializer.SERIALIZED_LENGTH:
         raise DeserializationException("log index incorrect length: %s" % (len(data)))
     cmdval, argument, needs_commit = struct.unpack(">BIB", data)
     if cmdval not in LogIndexEntrySerializer.COMMANDS_FOR_VALUES:
         raise DeserializationException("log index unknown command value: %s" % (cmdval))
     if needs_commit > 1:
         raise DeserializationException("needs_commit not in [0,1]")
     return (LogIndexEntry(LogIndexEntrySerializer.COMMANDS_FOR_VALUES[cmdval],
                           argument,
                           bool(needs_commit)))
Ejemplo n.º 6
0
    def test_deserialize_logindex_end_tx(self):
        log_index = LogIndexEntrySerializer.deserialize(
            decodehexstr("020000000000"))

        self.assertEquals(
            log_index, LogIndexEntry(LogIndexEntry.END_TX, needs_commit=False))
Ejemplo n.º 7
0
    def test_SerializedLogIndex_recover_ErasesLastUnfinishedTransaction(self):
        COUNT = 10
        io = IoHandle.using_stringio(COUNT)
        reader = LogIndexReader(io, COUNT)
        logindex = SerializedLogIndex.new(reader)
        logindex.log(LogIndexEntry(LogIndexEntry.BEGIN_TX))
        logindex.log(LogIndexEntry(LogIndexEntry.WRITE, 1))
        logindex.log(LogIndexEntry(LogIndexEntry.WRITE, 2))
        logindex.log(LogIndexEntry(LogIndexEntry.END_TX))
        logindex.log(LogIndexEntry(LogIndexEntry.BEGIN_TX))
        logindex.log(LogIndexEntry(LogIndexEntry.WRITE, 3))
        logindex.log(LogIndexEntry(LogIndexEntry.WRITE, 4))
        logindex2 = SerializedLogIndex.load(reader)

        logindex2.recover()

        self.assertEquals(
            logindex2.tocommit,
            deque([
                LogIndexEntry(LogIndexEntry.BEGIN_TX),
                LogIndexEntry(LogIndexEntry.WRITE, 1),
                LogIndexEntry(LogIndexEntry.WRITE, 2),
                LogIndexEntry(LogIndexEntry.END_TX)
            ]))
Ejemplo n.º 8
0
 def new(logindex_reader):
     for i in range(logindex_reader.nbentries):
         logindex_reader.write_entry(
             i, LogIndexEntry(LogIndexEntry.WRITE, needs_commit=False))
     return SerializedLogIndex(logindex_reader)
Ejemplo n.º 9
0
 def end_transaction(self):
     self.logindex.log(LogIndexEntry(LogIndexEntry.END_TX))
Ejemplo n.º 10
0
 def start_transaction(self):
     self.logindex.log(LogIndexEntry(LogIndexEntry.BEGIN_TX))