Search in sources :

Example 1 with ChunkHandle

use of io.pravega.segmentstore.storage.chunklayer.ChunkHandle in project pravega by pravega.

the class FileSystemChunkStorageMockTest method doReadTest.

private void doReadTest(int index, int bufferSize) throws Exception {
    String chunkName = "test";
    // Set up mocks.
    FileChannel channel = mock(FileChannel.class);
    fixChannelMock(channel);
    FileSystemWrapper fileSystemWrapper = mock(FileSystemWrapper.class);
    when(fileSystemWrapper.getFileChannel(any(), any())).thenReturn(channel);
    when(fileSystemWrapper.getFileSize(any())).thenReturn(2L * bufferSize);
    @Cleanup FileSystemChunkStorage testStorage = new FileSystemChunkStorage(storageConfig, fileSystemWrapper, executorService());
    ChunkHandle handle = ChunkHandle.readHandle(chunkName);
    // Force two reads.
    ArgumentCaptor<Long> expectedArgs = ArgumentCaptor.forClass(Long.class);
    when(channel.read(any(), anyLong())).thenReturn(index, bufferSize - index);
    // Call method.
    byte[] buffer = new byte[bufferSize];
    testStorage.doRead(handle, 0, bufferSize, buffer, 0);
    // Verify.
    verify(channel, times(2)).read(any(), expectedArgs.capture());
    List<Long> actualArgs = expectedArgs.getAllValues();
    assertEquals(2, actualArgs.size());
    assertEquals(0, actualArgs.get(0).longValue());
    assertEquals(index, actualArgs.get(1).longValue());
}
Also used : FileChannel(java.nio.channels.FileChannel) Mockito.anyLong(org.mockito.Mockito.anyLong) Cleanup(lombok.Cleanup) ChunkHandle(io.pravega.segmentstore.storage.chunklayer.ChunkHandle)

Example 2 with ChunkHandle

use of io.pravega.segmentstore.storage.chunklayer.ChunkHandle in project pravega by pravega.

the class InMemoryChunkStorage method doCreateWithContent.

@Override
protected ChunkHandle doCreateWithContent(String chunkName, int length, InputStream data) throws ChunkStorageException {
    Preconditions.checkNotNull(chunkName);
    if (null != chunks.putIfAbsent(chunkName, new InMemoryChunk(chunkName))) {
        throw new ChunkAlreadyExistsException(chunkName, "InMemoryChunkStorage::doCreate");
    }
    ChunkHandle handle = new ChunkHandle(chunkName, false);
    int bytesWritten = doWriteInternal(handle, 0, length, data);
    if (bytesWritten < length) {
        doDelete(ChunkHandle.writeHandle(chunkName));
        throw new ChunkStorageException(chunkName, "doCreateWithContent - invalid length returned");
    }
    return handle;
}
Also used : ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException) ChunkHandle(io.pravega.segmentstore.storage.chunklayer.ChunkHandle) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)

Aggregations

ChunkHandle (io.pravega.segmentstore.storage.chunklayer.ChunkHandle)2 ChunkAlreadyExistsException (io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)1 ChunkStorageException (io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)1 FileChannel (java.nio.channels.FileChannel)1 Cleanup (lombok.Cleanup)1 Mockito.anyLong (org.mockito.Mockito.anyLong)1