Search in sources :

Example 1 with OutputBuffer

use of com.arjuna.ats.arjuna.state.OutputBuffer in project narayana by jbosstm.

the class HornetqJournalStore method write_committed.

/**
 * Write a new copy of the object's committed state.
 *
 * @param uid    The object to work on.
 * @param typeName   The type of the object to work on.
 * @param txData The state to write.
 * @return <code>true</code> if no errors occurred, <code>false</code>
 *         otherwise.
 * @throws ObjectStoreException if things go wrong.
 */
public boolean write_committed(Uid uid, String typeName, OutputObjectState txData) throws ObjectStoreException {
    RecordInfo previousRecord = null;
    try {
        OutputBuffer outputBuffer = new OutputBuffer();
        UidHelper.packInto(uid, outputBuffer);
        outputBuffer.packString(typeName);
        outputBuffer.packBytes(txData.buffer());
        byte[] data = outputBuffer.buffer();
        RecordInfo record = new RecordInfo(getId(uid, typeName), RECORD_TYPE, data, false, (short) 0);
        previousRecord = getContentForType(typeName).putIfAbsent(uid, record);
        if (previousRecord != null) {
            // the packed data may have changed so updated the map with the latest data
            getContentForType(typeName).replace(uid, record);
            journal.appendUpdateRecord(previousRecord.id, RECORD_TYPE, data, syncWrites);
        } else {
            journal.appendAddRecord(record.id, RECORD_TYPE, data, syncWrites);
        }
    } catch (Exception e) {
        if (previousRecord == null) {
            // if appendAddRecord() fails, remove record from map. Leave it there if appendUpdateRecord() fails.
            getContentForType(typeName).remove(uid);
        }
        throw new ObjectStoreException(e);
    }
    return true;
}
Also used : ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException) RecordInfo(org.apache.activemq.artemis.core.journal.RecordInfo) OutputBuffer(com.arjuna.ats.arjuna.state.OutputBuffer) IOException(java.io.IOException) ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException)

Example 2 with OutputBuffer

use of com.arjuna.ats.arjuna.state.OutputBuffer in project narayana by jbosstm.

the class ActionHierarchyUnitTest method test.

@Test
public void test() throws Exception {
    ActionHierarchy ah = new ActionHierarchy(5);
    Uid[] tx = new Uid[5];
    assertEquals(ah.getDeepestActionUid(), Uid.nullUid());
    for (int i = 0; i < tx.length; i++) {
        tx[i] = new Uid();
        ah.add(tx[i]);
    }
    assertEquals(ah.depth(), tx.length);
    assertEquals(ah.getActionUid(0), tx[0]);
    Uid deepest = new Uid();
    ah.add(deepest, ActionType.TOP_LEVEL);
    PrintWriter pw = new PrintWriter(System.err);
    ah.print(pw);
    assertEquals(ah.getDeepestActionUid(), deepest);
    ActionHierarchy cp = new ActionHierarchy(ah);
    assertTrue(cp.equals(ah));
    cp.copy(ah);
    ah.copy(ah);
    assertTrue(cp.equals(ah));
    OutputBuffer out = new OutputBuffer();
    cp.pack(out);
    InputBuffer in = new InputBuffer(out.buffer());
    ah.unpack(in);
    assertTrue(ah.equals(cp));
    assertTrue(ah.isAncestor(deepest));
    ah.forgetDeepest();
    assertTrue(ah.findCommonPrefix(cp) != 0);
}
Also used : Uid(com.arjuna.ats.arjuna.common.Uid) InputBuffer(com.arjuna.ats.arjuna.state.InputBuffer) ActionHierarchy(com.arjuna.ats.arjuna.coordinator.ActionHierarchy) OutputBuffer(com.arjuna.ats.arjuna.state.OutputBuffer) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 3 with OutputBuffer

use of com.arjuna.ats.arjuna.state.OutputBuffer in project narayana by jbosstm.

the class IOStateUnitTest method testIOObjectBuffer.

@Test
public void testIOObjectBuffer() throws Exception {
    PrintWriter pw = new PrintWriter(new StringWriter());
    OutputBuffer obuff = new OutputBuffer(1024);
    obuff.print(pw);
    assertTrue(obuff.toString() != null);
    OutputBuffer tobuff = new OutputBuffer(obuff);
    assertTrue(tobuff.valid());
    InputBuffer ibuff = new InputBuffer();
    ibuff.print(pw);
    InputBuffer tibuff = new InputBuffer(ibuff);
    assertEquals(tibuff.valid(), false);
}
Also used : StringWriter(java.io.StringWriter) InputBuffer(com.arjuna.ats.arjuna.state.InputBuffer) OutputBuffer(com.arjuna.ats.arjuna.state.OutputBuffer) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

OutputBuffer (com.arjuna.ats.arjuna.state.OutputBuffer)3 InputBuffer (com.arjuna.ats.arjuna.state.InputBuffer)2 PrintWriter (java.io.PrintWriter)2 Test (org.junit.Test)2 Uid (com.arjuna.ats.arjuna.common.Uid)1 ActionHierarchy (com.arjuna.ats.arjuna.coordinator.ActionHierarchy)1 ObjectStoreException (com.arjuna.ats.arjuna.exceptions.ObjectStoreException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 RecordInfo (org.apache.activemq.artemis.core.journal.RecordInfo)1