Skip to content

Commit

Permalink
WIP on PayloadStore
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-v committed Aug 5, 2024
1 parent a96c239 commit 0fc52d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.UUID;
import org.agrona.CloseHelper;
import org.agrona.DirectBuffer;
import org.agrona.collections.Object2ObjectHashMap;

public class PayloadStore {
public class PayloadStore implements AutoCloseable {

private FileChannel storeChannel;
private RandomAccessFile storeFile;
private final RandomAccessFile storeFile;
private final FileChannel storeChannel;
private final ByteBuffer dstBuffer = ByteBuffer.allocateDirect(64 * 1024);
private final Object2ObjectHashMap<UUID, PayloadInfo> payloadIndex = new Object2ObjectHashMap<>();

Expand Down Expand Up @@ -40,10 +41,6 @@ public void removeGeneration(UUID memberId) {
payloadIndex.remove(memberId);
}

public int size() {
return payloadIndex.size();
}

public boolean putPayload(
UUID memberId, int payloadOffset, DirectBuffer chunk, int chunkOffset, int chunkLength)
throws IOException {
Expand Down Expand Up @@ -92,4 +89,13 @@ public ByteBuffer readPayload(UUID memberId) throws IOException {
//noinspection RedundantCast
return (ByteBuffer) readBuffer.flip();
}

public int size() {
return payloadIndex.size();
}

@Override
public void close() {
CloseHelper.quietCloseAll(storeFile, storeChannel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,32 @@
import java.nio.file.Path;
import java.util.Random;
import java.util.UUID;
import org.agrona.CloseHelper;
import org.agrona.concurrent.UnsafeBuffer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

class PayloadStoreTest {

@TempDir private Path tempDir;
private PayloadStore payloadStore;
private File storeFile;

@BeforeEach
void beforeEach() {
storeFile = tempDir.resolve("" + System.currentTimeMillis()).toFile();
payloadStore = new PayloadStore(storeFile);
}

@AfterEach
void afterEach() {
CloseHelper.quietClose(payloadStore);
}

@Test
void testAddGeneration() throws IOException {
final File storeFile = tempDir.resolve("" + System.currentTimeMillis()).toFile();
final PayloadStore payloadStore = new PayloadStore(storeFile);
final int n = 100;
final int payloadLength = 128;

Expand All @@ -33,8 +47,6 @@ void testAddGeneration() throws IOException {

@Test
void testRemoveGeneration() throws IOException {
final File storeFile = tempDir.resolve("" + System.currentTimeMillis()).toFile();
final PayloadStore payloadStore = new PayloadStore(storeFile);
final int n = 100;
final int payloadLength = 128;

Expand All @@ -52,9 +64,6 @@ void testRemoveGeneration() throws IOException {

@Test
void testReadPayload() throws IOException {
final File storeFile = tempDir.resolve("" + System.currentTimeMillis()).toFile();
final PayloadStore payloadStore = new PayloadStore(storeFile);

final UUID memberId = UUID.randomUUID();
final int payloadLength = 1032;

Expand All @@ -81,9 +90,6 @@ void testReadPayload() throws IOException {

@Test
void testPutPayloadPayloadNotFound() throws IOException {
final File storeFile = tempDir.resolve("" + System.currentTimeMillis()).toFile();
final PayloadStore payloadStore = new PayloadStore(storeFile);

final UUID memberId = UUID.randomUUID();
final int payloadLength = 1032;

Expand All @@ -101,9 +107,6 @@ void testPutPayloadPayloadNotFound() throws IOException {

@Test
void testPutPayloadInvalidPayloadOffset() throws IOException {
final File storeFile = tempDir.resolve("" + System.currentTimeMillis()).toFile();
final PayloadStore payloadStore = new PayloadStore(storeFile);

final UUID memberId = UUID.randomUUID();
final int payloadLength = 1032;

Expand All @@ -121,9 +124,6 @@ void testPutPayloadInvalidPayloadOffset() throws IOException {

@Test
void testPutPayloadInvalidChunk() throws IOException {
final File storeFile = tempDir.resolve("" + System.currentTimeMillis()).toFile();
final PayloadStore payloadStore = new PayloadStore(storeFile);

final UUID memberId = UUID.randomUUID();
final int payloadLength = 1032;

Expand Down

0 comments on commit 0fc52d9

Please sign in to comment.