From bbc99033a8f243e3be64e51d83c6285564fab02a Mon Sep 17 00:00:00 2001 From: William Welling Date: Mon, 19 Aug 2024 15:58:53 -0500 Subject: [PATCH] Add Java object file storage utility --- .../middleware/service/CachingSolrClient.java | 34 +++++------ .../utility/JavaObjectStorageFileUtility.java | 59 +++++++++++++++++++ 2 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 src/main/java/edu/tamu/scholars/middleware/utility/JavaObjectStorageFileUtility.java diff --git a/src/main/java/edu/tamu/scholars/middleware/service/CachingSolrClient.java b/src/main/java/edu/tamu/scholars/middleware/service/CachingSolrClient.java index e460fa8b7..a5420098a 100644 --- a/src/main/java/edu/tamu/scholars/middleware/service/CachingSolrClient.java +++ b/src/main/java/edu/tamu/scholars/middleware/service/CachingSolrClient.java @@ -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; @@ -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 extends SolrClient { @@ -53,9 +52,15 @@ public class CachingSolrClient extends SolrClient { private final Map 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> map; // there are different type of SolrClient + // this provides only interface methods C client; JwtTokenService jwtTokenService; @@ -186,18 +191,14 @@ public NamedList 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) in.readObject(); - } catch (IOException | ClassNotFoundException e) { + try { + response = JavaObjectStorageFileUtility.readObject(filename); + } catch (ClassNotFoundException | IOException e) { e.printStackTrace(); } @@ -206,14 +207,7 @@ public NamedList 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); diff --git a/src/main/java/edu/tamu/scholars/middleware/utility/JavaObjectStorageFileUtility.java b/src/main/java/edu/tamu/scholars/middleware/utility/JavaObjectStorageFileUtility.java new file mode 100644 index 000000000..e0c8b6e7b --- /dev/null +++ b/src/main/java/edu/tamu/scholars/middleware/utility/JavaObjectStorageFileUtility.java @@ -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 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 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; + } + } + } + +} \ No newline at end of file