mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-05 23:33:36 +00:00
jdb: Add basic document indexing
This commit is contained in:
@@ -1 +1 @@
|
|||||||
1.0.2
|
1.0.3
|
||||||
@@ -16,7 +16,7 @@ import java.util.Map;
|
|||||||
* The JDB class provides a simple JSON document-based storage system in a specified directory.
|
* The JDB class provides a simple JSON document-based storage system in a specified directory.
|
||||||
*/
|
*/
|
||||||
public class JDB {
|
public class JDB {
|
||||||
private final static Gson GSON = new GsonBuilder()
|
public final static Gson GSON = new GsonBuilder()
|
||||||
.setPrettyPrinting()
|
.setPrettyPrinting()
|
||||||
.disableHtmlEscaping()
|
.disableHtmlEscaping()
|
||||||
.create();
|
.create();
|
||||||
@@ -24,6 +24,7 @@ public class JDB {
|
|||||||
private static final String FILE_EXTENSION = ".json";
|
private static final String FILE_EXTENSION = ".json";
|
||||||
private final @NotNull String basePath;
|
private final @NotNull String basePath;
|
||||||
private final @NotNull File baseDirectory;
|
private final @NotNull File baseDirectory;
|
||||||
|
private final JIndex index;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new JDB instance with the specified base path.
|
* Constructs a new JDB instance with the specified base path.
|
||||||
@@ -33,6 +34,8 @@ public class JDB {
|
|||||||
public JDB(@NotNull String basePath) {
|
public JDB(@NotNull String basePath) {
|
||||||
this.basePath = basePath;
|
this.basePath = basePath;
|
||||||
this.baseDirectory = new File(basePath);
|
this.baseDirectory = new File(basePath);
|
||||||
|
|
||||||
|
this.index = JIndex.load("jdb_index", basePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,6 +50,13 @@ public class JDB {
|
|||||||
public <T> T get(@NotNull String path, @NotNull Class<T> clazz) throws IOException {
|
public <T> T get(@NotNull String path, @NotNull Class<T> clazz) throws IOException {
|
||||||
File documentFile = new File(baseDirectory, createFilePath(path));
|
File documentFile = new File(baseDirectory, createFilePath(path));
|
||||||
if (!documentFile.exists()) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
BufferedReader bufferedReader = Files.newBufferedReader(documentFile.toPath());
|
BufferedReader bufferedReader = Files.newBufferedReader(documentFile.toPath());
|
||||||
@@ -133,12 +143,35 @@ public class JDB {
|
|||||||
Files.write(documentFile.toPath(), json.getBytes());
|
Files.write(documentFile.toPath(), json.getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the given value as a document at the specified path and indexes it under additional paths.
|
||||||
|
*/
|
||||||
|
public <T> 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.
|
* Deletes the document(s) at the specified path.
|
||||||
*
|
*
|
||||||
* @param path the relative path (excluding .json extension) of the document(s) to be deleted
|
* @param path the relative path (excluding .json extension) of the document(s) to be deleted
|
||||||
*/
|
*/
|
||||||
public void delete(@NotNull String path) {
|
public void delete(@NotNull String path) {
|
||||||
|
index.indexMap().remove(path);
|
||||||
|
|
||||||
File file = new File(baseDirectory, path);
|
File file = new File(baseDirectory, path);
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
deleteDirectory(file);
|
deleteDirectory(file);
|
||||||
|
|||||||
41
libraries/jdb/src/main/java/de/oliver/jdb/JIndex.java
Normal file
41
libraries/jdb/src/main/java/de/oliver/jdb/JIndex.java
Normal file
@@ -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<String, String> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user