no.ssb.sagalog.memory.MemorySagaLog Maven / Gradle / Ivy
package no.ssb.sagalog.memory;
import no.ssb.sagalog.SagaLog;
import no.ssb.sagalog.SagaLogEntry;
import no.ssb.sagalog.SagaLogEntryBuilder;
import no.ssb.sagalog.SagaLogEntryId;
import java.nio.ByteBuffer;
import java.util.Deque;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;
class MemorySagaLog implements SagaLog {
private final AtomicLong nextId = new AtomicLong(0);
private final Deque incompleteEntries = new ConcurrentLinkedDeque<>();
@Override
public Stream readEntries(String executionId) {
return incompleteEntries.stream().filter(e -> executionId.equals(e.getExecutionId()));
}
@Override
public CompletableFuture write(SagaLogEntryBuilder builder) {
synchronized (this) {
if (builder.id() == null) {
builder.id(new MemorySagaLogEntryId(nextId.getAndIncrement()));
}
SagaLogEntry entry = builder.build();
incompleteEntries.add(entry);
return CompletableFuture.completedFuture(entry);
}
}
@Override
public CompletableFuture truncate(SagaLogEntryId entryId) {
Set toBeRemoved = new LinkedHashSet<>();
Set toBeRemovedIds = new LinkedHashSet<>();
for (SagaLogEntry sle : incompleteEntries) {
toBeRemoved.add(sle);
toBeRemovedIds.add(sle.getId());
if (entryId.equals(sle.getId())) {
break;
}
}
if (toBeRemovedIds.contains(entryId)) {
incompleteEntries.removeAll(toBeRemoved);
}
return CompletableFuture.completedFuture(null);
}
@Override
public CompletableFuture truncate() {
SagaLogEntry lastEntry = incompleteEntries.peekLast();
if (lastEntry == null) {
return CompletableFuture.completedFuture(null);
}
return truncate(lastEntry.getId());
}
@Override
public Stream readIncompleteSagas() {
return incompleteEntries.stream();
}
@Override
public String toString(SagaLogEntryId id) {
return String.valueOf(((MemorySagaLogEntryId) id).id);
}
@Override
public SagaLogEntryId fromString(String idString) {
return new MemorySagaLogEntryId(Long.parseLong(idString));
}
@Override
public byte[] toBytes(SagaLogEntryId id) {
byte[] bytes = new byte[8];
ByteBuffer.wrap(bytes).putLong(((MemorySagaLogEntryId) id).id);
return bytes;
}
@Override
public SagaLogEntryId fromBytes(byte[] idBytes) {
return new MemorySagaLogEntryId(ByteBuffer.wrap(idBytes).getLong());
}
}