From eaa73d212013d3a3e29d42a169335efc479eb4e5 Mon Sep 17 00:00:00 2001 From: Jai Balani Date: Tue, 28 Jan 2025 14:37:52 +0530 Subject: [PATCH] Added support for chunk read in Filestore --- .../com/github/ambry/server/AmbryServer.java | 11 ++++++----- .../java/com/github/ambry/store/FileStore.java | 16 ++++++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ambry-server/src/main/java/com/github/ambry/server/AmbryServer.java b/ambry-server/src/main/java/com/github/ambry/server/AmbryServer.java index 5cea124295..881dce70ad 100644 --- a/ambry-server/src/main/java/com/github/ambry/server/AmbryServer.java +++ b/ambry-server/src/main/java/com/github/ambry/server/AmbryServer.java @@ -100,6 +100,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -504,11 +506,10 @@ public void startup() throws InstantiationException { System.err.println("An error occurred: " + e.getMessage()); } - FileInputStream inputStream = fileStore.getStreamForFileRead("/tmp/0/", "0_log"); - long fileSize = inputStream.available(); - byte[] content = new byte[(int) fileSize]; // Read the content of the source file into a byte array - inputStream.read(content); // Read bytes into the array - System.out.println("Parsed log file contents read: " + new String(content)); + int offset = 10; + int size = 10; + ByteBuffer byteBuffer = fileStore.getStreamForFileRead("/tmp/0/", "0_log", offset, size); + System.out.println("Parsed log file contents read for offset=" + offset + ", size=" + size + " is: " + StandardCharsets.UTF_8.decode(byteBuffer)); } catch (Exception e) { diff --git a/ambry-store/src/main/java/com/github/ambry/store/FileStore.java b/ambry-store/src/main/java/com/github/ambry/store/FileStore.java index 95b03e0668..34c8aabecd 100644 --- a/ambry-store/src/main/java/com/github/ambry/store/FileStore.java +++ b/ambry-store/src/main/java/com/github/ambry/store/FileStore.java @@ -22,10 +22,13 @@ import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; @@ -61,7 +64,7 @@ public void stop() { } - public FileInputStream getStreamForFileRead(String mountPath, String fileName) + public ByteBuffer getStreamForFileRead(String mountPath, String fileName, int offset, int size) throws IOException { if(!isRunning){ throw new FileStoreException("FileStore is not running", FileStoreErrorCode.FileStoreRunningFailure); @@ -69,11 +72,12 @@ public FileInputStream getStreamForFileRead(String mountPath, String fileName) // TODO: Handle edge cases and validations String filePath = mountPath + "/" + fileName; File file = new File(filePath); - // Check if file exists and is readable - if (!file.exists() || !file.canRead()) { - throw new IOException("File doesn't exist or cannot be read: " + filePath); - } - return new FileInputStream(file); + RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); + randomAccessFile.seek(offset); + ByteBuffer buf = ByteBuffer.allocate(size); + randomAccessFile.getChannel().read(buf); + buf.flip(); + return buf; } public void putChunkToFile(String outputFilePath, FileInputStream fileInputStream)