2009-05-23 22:24:02 +02:00
|
|
|
package se.scalablesolutions.akka.api;
|
|
|
|
|
|
2009-09-23 09:02:14 +02:00
|
|
|
import se.scalablesolutions.akka.annotation.inittransactionalstate;
|
2009-07-03 17:15:36 +02:00
|
|
|
import se.scalablesolutions.akka.annotation.transactionrequired;
|
2010-03-11 09:50:15 +01:00
|
|
|
import se.scalablesolutions.akka.persistence.common.*;
|
|
|
|
|
import se.scalablesolutions.akka.persistence.cassandra.*;
|
2009-05-23 22:24:02 +02:00
|
|
|
|
2009-07-03 17:15:36 +02:00
|
|
|
@transactionrequired
|
2009-05-23 22:24:02 +02:00
|
|
|
public class PersistentStateful {
|
2009-09-23 09:02:14 +02:00
|
|
|
private PersistentMap mapState;
|
|
|
|
|
private PersistentVector vectorState;
|
|
|
|
|
private PersistentRef refState;
|
|
|
|
|
|
|
|
|
|
@inittransactionalstate
|
|
|
|
|
public void init() {
|
2009-12-05 20:59:15 +01:00
|
|
|
mapState = CassandraStorage.newMap();
|
|
|
|
|
vectorState = CassandraStorage.newVector();
|
|
|
|
|
refState = CassandraStorage.newRef();
|
2009-09-23 09:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
public String getMapState(String key) {
|
2009-12-05 20:59:15 +01:00
|
|
|
byte[] bytes = (byte[]) mapState.get(key.getBytes()).get();
|
|
|
|
|
return new String(bytes, 0, bytes.length);
|
2009-05-23 22:24:02 +02:00
|
|
|
}
|
2009-07-03 17:15:36 +02:00
|
|
|
|
2009-06-11 13:47:07 +02:00
|
|
|
public String getVectorState(int index) {
|
2009-12-05 20:59:15 +01:00
|
|
|
byte[] bytes = (byte[]) vectorState.get(index);
|
|
|
|
|
return new String(bytes, 0, bytes.length);
|
2009-05-23 22:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-03 17:53:33 +02:00
|
|
|
public int getVectorLength() {
|
|
|
|
|
return vectorState.length();
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-11 13:47:07 +02:00
|
|
|
public String getRefState() {
|
|
|
|
|
if (refState.isDefined()) {
|
2009-12-05 20:59:15 +01:00
|
|
|
byte[] bytes = (byte[]) refState.get().get();
|
|
|
|
|
return new String(bytes, 0, bytes.length);
|
2009-06-11 13:47:07 +02:00
|
|
|
} else throw new IllegalStateException("No such element");
|
|
|
|
|
}
|
2009-07-03 17:15:36 +02:00
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
public void setMapState(String key, String msg) {
|
2009-12-05 20:59:15 +01:00
|
|
|
mapState.put(key.getBytes(), msg.getBytes());
|
2009-06-10 20:04:33 +02:00
|
|
|
}
|
2009-07-03 17:15:36 +02:00
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
public void setVectorState(String msg) {
|
2009-12-05 20:59:15 +01:00
|
|
|
vectorState.add(msg.getBytes());
|
2009-06-10 20:04:33 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-11 13:47:07 +02:00
|
|
|
public void setRefState(String msg) {
|
2009-12-05 20:59:15 +01:00
|
|
|
refState.swap(msg.getBytes());
|
2009-06-11 13:47:07 +02:00
|
|
|
}
|
2009-06-10 20:04:33 +02:00
|
|
|
|
2009-05-23 22:24:02 +02:00
|
|
|
public void success(String key, String msg) {
|
2009-12-05 20:59:15 +01:00
|
|
|
mapState.put(key.getBytes(), msg.getBytes());
|
|
|
|
|
vectorState.add(msg.getBytes());
|
|
|
|
|
refState.swap(msg.getBytes());
|
2009-05-23 22:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-25 13:07:58 +02:00
|
|
|
public String failure(String key, String msg, PersistentFailer failer) {
|
2009-12-05 20:59:15 +01:00
|
|
|
mapState.put(key.getBytes(), msg.getBytes());
|
|
|
|
|
vectorState.add(msg.getBytes());
|
|
|
|
|
refState.swap(msg.getBytes());
|
2009-05-23 22:24:02 +02:00
|
|
|
failer.fail();
|
2009-06-25 13:07:58 +02:00
|
|
|
return msg;
|
2009-05-23 22:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-07 22:11:27 +02:00
|
|
|
public String success(String key, String msg, PersistentStatefulNested nested) {
|
2009-12-05 20:59:15 +01:00
|
|
|
mapState.put(key.getBytes(), msg.getBytes());
|
|
|
|
|
vectorState.add(msg.getBytes());
|
|
|
|
|
refState.swap(msg.getBytes());
|
2009-06-29 17:33:38 +02:00
|
|
|
nested.success(key, msg);
|
2009-07-07 22:11:27 +02:00
|
|
|
return msg;
|
2009-06-29 17:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String failure(String key, String msg, PersistentStatefulNested nested, PersistentFailer failer) {
|
2009-12-05 20:59:15 +01:00
|
|
|
mapState.put(key.getBytes(), msg.getBytes());
|
|
|
|
|
vectorState.add(msg.getBytes());
|
|
|
|
|
refState.swap(msg.getBytes());
|
2009-06-29 17:33:38 +02:00
|
|
|
nested.failure(key, msg, failer);
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
2009-05-23 22:24:02 +02:00
|
|
|
}
|
|
|
|
|
|