Skip to content

Commit

Permalink
Add Java object file storage utility
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Aug 19, 2024
1 parent da3eb4a commit bbc9903
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import javax.servlet.http.HttpServletRequest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
Expand All @@ -28,10 +28,9 @@
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.ServletRequestPathUtils;

import edu.tamu.scholars.middleware.config.model.IndexConfig;
import edu.tamu.scholars.middleware.utility.JavaObjectStorageFileUtility;

public class CachingSolrClient<C extends SolrClient> extends SolrClient {

Expand All @@ -53,9 +52,15 @@ public class CachingSolrClient<C extends SolrClient> extends SolrClient {

private final Map<String, File> lookup;

// TODO: take the average of 5 attempts on cache, repeat until 5 attempts on solr, select more performant and fallback

// add threshold to solr response to avoid network glitches
// add threshold to cached response to avoid unexpected file objects

private final Map<String, Map<String, String>> map;

// there are different type of SolrClient
// this provides only interface methods
C client;

JwtTokenService jwtTokenService;
Expand Down Expand Up @@ -186,18 +191,14 @@ public NamedList<Object> request(SolrRequest<?> request, String collection)

File file = new File(filename);

long startFoundCache = System.currentTimeMillis();
long startQueryToSolr = System.currentTimeMillis();
long startFoundCache, startQueryToSolr;

if (file.exists()) {
startFoundCache = System.currentTimeMillis();

try (
FileInputStream fileIn = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fileIn);
) {
response = (NamedList<Object>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
try {
response = JavaObjectStorageFileUtility.readObject(filename);
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}

Expand All @@ -206,14 +207,7 @@ public NamedList<Object> request(SolrRequest<?> request, String collection)
startQueryToSolr = System.currentTimeMillis();
response = client.request(request, collection);

try (
FileOutputStream fileOut = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
) {
out.writeObject(response);
} catch (IOException e) {
e.printStackTrace();
}
JavaObjectStorageFileUtility.writeObject(response, filename);

logger.info("{}:{}: {} seconds", uuid, "QUERY RESPONSE", (System.currentTimeMillis() - startQueryToSolr) / (double) 1000);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package edu.tamu.scholars.middleware.utility;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

// https://claude.ai/chat/38a46bde-f2c6-48b8-9a88-c4e286f55776
public class JavaObjectStorageFileUtility {

private JavaObjectStorageFileUtility() {

}

public static <T extends Serializable> void writeObject(T object, String fileName) throws IOException {
Path path = Paths.get(fileName);
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos)) {

// Serialize the object
oos.writeObject(object);
oos.flush();

// Write the serialized object to the file
channel.write(ByteBuffer.wrap(baos.toByteArray()));
}
}

public static <T extends Serializable> T readObject(String fileName) throws IOException, ClassNotFoundException {
Path path = Paths.get(fileName);
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ);
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

// Read the file contents into a ByteArrayOutputStream
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (channel.read(buffer) > 0) {
buffer.flip();
baos.write(buffer.array(), 0, buffer.limit());
buffer.clear();
}

// Deserialize the object
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
@SuppressWarnings("unchecked")
T object = (T) ois.readObject();
return object;
}
}
}

}

0 comments on commit bbc9903

Please sign in to comment.