diff --git a/libraries/jdb/VERSION b/libraries/jdb/VERSION index e6d5cb83..e4c0d46e 100644 --- a/libraries/jdb/VERSION +++ b/libraries/jdb/VERSION @@ -1 +1 @@ -1.0.2 \ No newline at end of file +1.0.3 \ No newline at end of file diff --git a/libraries/jdb/src/main/java/de/oliver/jdb/JDB.java b/libraries/jdb/src/main/java/de/oliver/jdb/JDB.java index 8297eb75..cd919e4f 100644 --- a/libraries/jdb/src/main/java/de/oliver/jdb/JDB.java +++ b/libraries/jdb/src/main/java/de/oliver/jdb/JDB.java @@ -16,7 +16,7 @@ import java.util.Map; * The JDB class provides a simple JSON document-based storage system in a specified directory. */ public class JDB { - private final static Gson GSON = new GsonBuilder() + public final static Gson GSON = new GsonBuilder() .setPrettyPrinting() .disableHtmlEscaping() .create(); @@ -24,6 +24,7 @@ public class JDB { private static final String FILE_EXTENSION = ".json"; private final @NotNull String basePath; private final @NotNull File baseDirectory; + private final JIndex index; /** * Constructs a new JDB instance with the specified base path. @@ -33,6 +34,8 @@ public class JDB { public JDB(@NotNull String basePath) { this.basePath = basePath; this.baseDirectory = new File(basePath); + + this.index = JIndex.load("jdb_index", basePath); } /** @@ -47,6 +50,13 @@ public class JDB { public T get(@NotNull String path, @NotNull Class clazz) throws IOException { File documentFile = new File(baseDirectory, createFilePath(path)); if (!documentFile.exists()) { + + // Check index for alternative path + if (index.indexMap().containsKey(path)) { + String indexPath = index.indexMap().get(path); + return get(indexPath, clazz); + } + return null; } BufferedReader bufferedReader = Files.newBufferedReader(documentFile.toPath()); @@ -133,12 +143,35 @@ public class JDB { Files.write(documentFile.toPath(), json.getBytes()); } + /** + * Saves the given value as a document at the specified path and indexes it under additional paths. + */ + public void set(@NotNull String path, @NotNull T value, String... indexPaths) throws IOException { + set(path, value); + for (String indexPath : indexPaths) { + indexDocument(indexPath, path); + } + } + + /** + * Indexes a document by mapping the original path to the index path. + * + * @param originalPath the original relative path (excluding .json extension) of the document + * @param indexPath the index relative path (excluding .json extension) to map to the original document + */ + public void indexDocument(@NotNull String originalPath, @NotNull String indexPath) { + index.indexMap().put(originalPath, indexPath); + index.save(); + } + /** * Deletes the document(s) at the specified path. * * @param path the relative path (excluding .json extension) of the document(s) to be deleted */ public void delete(@NotNull String path) { + index.indexMap().remove(path); + File file = new File(baseDirectory, path); if (file.isDirectory()) { deleteDirectory(file); diff --git a/libraries/jdb/src/main/java/de/oliver/jdb/JIndex.java b/libraries/jdb/src/main/java/de/oliver/jdb/JIndex.java new file mode 100644 index 00000000..ebc05c28 --- /dev/null +++ b/libraries/jdb/src/main/java/de/oliver/jdb/JIndex.java @@ -0,0 +1,41 @@ +package de.oliver.jdb; + +import com.google.gson.annotations.SerializedName; + +import java.io.File; +import java.nio.file.Files; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public record JIndex( + String name, + @SerializedName("base_path") String basePath, + @SerializedName("index_map") Map indexMap // key -> original path +) { + + public static JIndex load(String name, String basePath) { + File indexFile = new File(basePath, name + ".json"); + if (!indexFile.exists()) { + return new JIndex(name, basePath, new ConcurrentHashMap<>()); + } + + try (var reader = Files.newBufferedReader(indexFile.toPath())) { + return JDB.GSON.fromJson(reader, JIndex.class); + } catch (Exception e) { + e.printStackTrace(); + return new JIndex(name, basePath, new ConcurrentHashMap<>()); + } + } + + public void save() { + File indexFile = new File(basePath, name + ".json"); + + String json = JDB.GSON.toJson(this); + try { + Files.write(indexFile.toPath(), json.getBytes()); + } catch (Exception e) { + e.printStackTrace(); + } + } + +}