-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JDB JSON storage and CI workflow
- Loading branch information
1 parent
79fe19a
commit 6df02ab
Showing
6 changed files
with
1,213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
|
||
- name: Cache Gradle packages | ||
uses: actions/cache@v3 | ||
with: | ||
path: ~/.gradle/caches | ||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | ||
restore-keys: | | ||
${{ runner.os }}-gradle- | ||
- name: Grant execute permission for gradlew | ||
run: chmod +x ./gradlew | ||
|
||
- name: Build with Gradle | ||
run: ./gradlew build | ||
|
||
- name: Run tests | ||
run: ./gradlew test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package de.oliver.fancylib.jdb; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 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() | ||
.serializeNulls() | ||
.setPrettyPrinting() | ||
.create(); | ||
|
||
private final @NotNull String basePath; | ||
private final @NotNull File baseFolder; | ||
|
||
/** | ||
* Constructs a new JDB instance with the specified base path. | ||
* | ||
* @param basePath the base directory path where documents will be stored | ||
*/ | ||
public JDB(@NotNull String basePath) { | ||
this.basePath = basePath; | ||
this.baseFolder = new File(basePath); | ||
} | ||
|
||
/** | ||
* Retrieves a document from the specified path, deserializing it into the given class type. | ||
* | ||
* @param <T> the type of the object to be returned | ||
* @param path the relative path (excluding .json extension) where the document is located | ||
* @param clazz the class type to which the document should be deserialized | ||
* @return a JDocument containing the deserialized object and its path, or null if the file does not exist | ||
* @throws IOException if an I/O error occurs during file reading | ||
*/ | ||
public <T> T get(@NotNull String path, @NotNull Class<T> clazz) throws IOException { | ||
File file = new File(baseFolder, basePath + path + ".json"); | ||
if (!file.exists()) { | ||
return null; | ||
} | ||
|
||
BufferedReader bufferedReader = Files.newBufferedReader(file.toPath()); | ||
|
||
return GSON.fromJson(bufferedReader, clazz); | ||
} | ||
|
||
/** | ||
* Retrieves all documents from the specified directory path, deserializing them into the given class type. | ||
* | ||
* @param <T> the type of objects to be returned | ||
* @param path the relative directory path containing the documents | ||
* @param clazz the class type to which the documents should be deserialized | ||
* @return a List of JDocument objects containing the deserialized objects and their paths, or null if the directory or files do not exist | ||
* @throws IOException if an I/O error occurs during file reading | ||
*/ | ||
public <T> List<T> getAll(@NotNull String path, @NotNull Class<T> clazz) throws IOException { | ||
File folder = new File(baseFolder, basePath + path); | ||
if (!folder.exists()) { | ||
return new ArrayList<>(); | ||
} | ||
|
||
File[] files = folder.listFiles(); | ||
if (files == null) { | ||
return new ArrayList<>(); | ||
} | ||
|
||
List<T> documents = new ArrayList<>(files.length); | ||
for (File file : files) { | ||
documents.add(get(path + "/" + file.getName().replace(".json", ""), clazz)); | ||
} | ||
|
||
return documents; | ||
} | ||
|
||
/** | ||
* Saves the given value as a document at the specified path. | ||
* | ||
* @param <T> the type of the object to be saved | ||
* @param path the relative path (excluding .json extension) where the document will be saved | ||
* @param value the object to be saved as a JSON document | ||
* @throws IOException if an I/O error occurs during file writing | ||
*/ | ||
public <T> void set(@NotNull String path, @NotNull T value) throws IOException { | ||
File file = new File(baseFolder, basePath + path + ".json"); | ||
|
||
if (!file.exists()) { | ||
file.getParentFile().mkdirs(); | ||
file.createNewFile(); | ||
} | ||
|
||
String json = GSON.toJson(value); | ||
|
||
Files.write(file.toPath(), json.getBytes()); | ||
} | ||
|
||
/** | ||
* Deletes the document(s) at the specified path. | ||
* | ||
* @param path the relative path (excluding .json extension) where the document(s) are located | ||
*/ | ||
public void delete(@NotNull String path) { | ||
File file = new File(baseFolder, basePath + path + ".json"); | ||
if (file.exists()) { | ||
file.delete(); | ||
} | ||
} | ||
} |
Oops, something went wrong.