Skip to content

Commit

Permalink
Added support for chunk read in Filestore
Browse files Browse the repository at this point in the history
  • Loading branch information
Jai Balani committed Jan 28, 2025
1 parent 3dd73ad commit eaa73d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 10 additions & 6 deletions ambry-store/src/main/java/com/github/ambry/store/FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,19 +64,20 @@ 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);
}
// 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)
Expand Down

0 comments on commit eaa73d2

Please sign in to comment.