aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/jshipit/OCIDataStore.java
diff options
context:
space:
mode:
authoraxtloss <axtlos@getcryst.al>2023-05-26 00:03:58 +0200
committeraxtloss <axtlos@getcryst.al>2023-05-26 00:03:58 +0200
commit87cc052844e2cacca591edd68884f1e7f6c49a3a (patch)
tree2e39577998921746253a7ed1ea08ee99e6d4ecc9 /src/main/java/io/github/jshipit/OCIDataStore.java
parent3fdb1012609b71a41e551ea0aad3b8a27b134e21 (diff)
downloadjshipit-87cc052844e2cacca591edd68884f1e7f6c49a3a.tar.gz
jshipit-87cc052844e2cacca591edd68884f1e7f6c49a3a.tar.bz2
Add comments to functions
Diffstat (limited to 'src/main/java/io/github/jshipit/OCIDataStore.java')
-rwxr-xr-xsrc/main/java/io/github/jshipit/OCIDataStore.java122
1 files changed, 107 insertions, 15 deletions
diff --git a/src/main/java/io/github/jshipit/OCIDataStore.java b/src/main/java/io/github/jshipit/OCIDataStore.java
index e399e75..c77f671 100755
--- a/src/main/java/io/github/jshipit/OCIDataStore.java
+++ b/src/main/java/io/github/jshipit/OCIDataStore.java
@@ -10,6 +10,10 @@ import java.sql.*;
import java.util.ArrayList;
import java.util.List;
+/*
+ * OCI Data Store
+ * This class is responsible for managing the OCI data store
+ */
public class OCIDataStore {
private String path;
@@ -18,9 +22,11 @@ public class OCIDataStore {
public OCIDataStore(String path) {
this.path = path;
this.databasePath = path + "/datastore.db";
+ // Create OCI Data Store if it does not exist
if (!Files.isDirectory(Path.of(path))) {
createStore();
}
+ // Create OCI Data Store database if it does not exist
if (!Files.exists(Path.of(this.databasePath))) {
try {
createStoreDatabase();
@@ -30,6 +36,9 @@ public class OCIDataStore {
}
}
+ /*
+ * Creates the OCI Data Store
+ */
private void createStore() {
System.out.println("Creating OCI Data Store");
Path path = Path.of(this.path);
@@ -43,6 +52,9 @@ public class OCIDataStore {
}
}
+ /*
+ * Creates the OCI Data Store database
+ */
private void createStoreDatabase() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -52,6 +64,7 @@ public class OCIDataStore {
Statement statement = conn.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
+ // We use two tables so that we can deduplicate blobs and keep track of containers that the user created
statement.executeUpdate("CREATE TABLE IF NOT EXISTS blobs (id INTEGER PRIMARY KEY AUTOINCREMENT, digest TEXT, path TEXT)");
statement.executeUpdate("CREATE TABLE IF NOT EXISTS containers (containerID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, path TEXT, image TEXT, tag TEXT, apiRepo TEXT, repo TEXT)");
System.out.println("A new database has been created.");
@@ -62,6 +75,11 @@ public class OCIDataStore {
}
}
+ /*
+ * Adds a downloaded blob to the database
+ *
+ * @param blob The blob to add
+ */
public void addBlobToDatabase(String blob) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -78,6 +96,13 @@ public class OCIDataStore {
}
}
+ /*
+ * Checks if a blob is in the database
+ *
+ * @param blob The blob to check
+ *
+ * @return True if the blob is in the database, false otherwise
+ */
public boolean isBlobInDatabase(String blob) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -96,6 +121,16 @@ public class OCIDataStore {
return false;
}
+ /*
+ * Adds a container to the database
+ *
+ * @param path The path to the container
+ * @param name The name of the container
+ * @param image The image that the container uses
+ * @param tag The image tag that the container uses
+ * @param apiRepo The API repository of the container
+ * @param repo The repository of the container
+ */
public void addContainerToDatabase(String path, String name, String image, String tag, String apiRepo, String repo) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -112,6 +147,11 @@ public class OCIDataStore {
}
}
+ /*
+ * Deletes a container from the database
+ *
+ * @param name The name of the container to delete
+ */
public void deleteContainerFromDatabase(String name) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -128,6 +168,13 @@ public class OCIDataStore {
}
}
+ /*
+ * Checks if a container exists in the database
+ *
+ * @param name The name of the container to check
+ *
+ * @return True if the container exists, false otherwise
+ */
public boolean containerExists(String name) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -146,6 +193,13 @@ public class OCIDataStore {
return false;
}
+ /*
+ * Gets the path to a container
+ *
+ * @param name The name of the container
+ *
+ * @return The path to the container
+ */
public String getContainerPath(String name) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -164,6 +218,13 @@ public class OCIDataStore {
return null;
}
+ /*
+ * Gets the image of a container
+ *
+ * @param name The name of the container
+ *
+ * @return The image of the container
+ */
public String getContainerImage(String name) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -182,6 +243,14 @@ public class OCIDataStore {
return null;
}
+
+ /*
+ * Gets the tag of a container
+ *
+ * @param name The name of the container
+ *
+ * @return The tag of the container
+ */
public String getContainerTag(String name) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -200,6 +269,14 @@ public class OCIDataStore {
return null;
}
+
+ /*
+ * Gets the API repository of a container
+ *
+ * @param name The name of the container
+ *
+ * @return The API repository of the container
+ */
public String getContainerApiRepo(String name) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -218,6 +295,13 @@ public class OCIDataStore {
return null;
}
+ /*
+ * Gets the repository of a container
+ *
+ * @param name The name of the container
+ *
+ * @return The repository of the container
+ */
public String getContainerRepo(String name) {
String url = "jdbc:sqlite:" + this.databasePath;
@@ -236,6 +320,17 @@ public class OCIDataStore {
return null;
}
+ /*
+ * Creates the directory for a container and adds it to the database
+ *
+ * @param image The image of the container
+ * @param tag The tag of the container
+ * @param name The name of the container
+ * @param apiRepo The API repository of the container
+ * @param repo The repository of the container
+ *
+ * @return The path to the container
+ */
public String createContainerDirectory(String image, String tag, String name, String apiRepo, String repo) {
Path containerPath = Path.of(this.path+"/"+image+"/"+tag+"/"+name);
try {
@@ -249,8 +344,15 @@ public class OCIDataStore {
return containerPath.toString();
}
+ /*
+ * Downlaods an oci-image, extracts it and adds it to the blob database
+ *
+ * @param apiRepo The API repository of the container
+ * @param repo The repository of the container
+ * @param image The image of the container
+ * @param tag The tag of the container
+ */
public void createImage(String apiRepo, String repo, String image, String tag) {
-
Path imgPath = Path.of(this.path+"/"+image);
try {
Files.createDirectory(imgPath);
@@ -298,10 +400,11 @@ public class OCIDataStore {
return;
}
}
- List <String> layerDigests = new ArrayList<>();
+
+ List <String> layerDigests = new ArrayList<>(); // We store the blobs an image uses in a file, so that we know which blobs are used by which images
for (JsonNode layer : layers) {
try {
- if (!isBlobInDatabase(layer.get("digest").asText())) {
+ if (!isBlobInDatabase(layer.get("digest").asText())) { // We only download blobs that are not already in the database
api.fetchBlob(layer.get("digest").asText(), layerpath, true, null);
addBlobToDatabase(layer.get("digest").asText());
layerDigests.add(layer.get("digest").asText());
@@ -319,6 +422,7 @@ public class OCIDataStore {
e.printStackTrace();
}
+ // We download the config file of the image (I was too lazy to make a separate download function for that (having this part async is not good))
try {
api.fetchBlob(manifest.get("config").get("digest").asText(), this.path+"/"+image+"/"+tag, false, this.path+"/"+image+"/"+tag+"/config");
} catch (IOException e) {
@@ -330,16 +434,4 @@ public class OCIDataStore {
public String getPath() {
return path;
}
-
- public void setPath(String path) {
- this.path = path;
- }
-
- public String getDatabasePath() {
- return databasePath;
- }
-
- public void setDatabasePath(String databasePath) {
- this.databasePath = databasePath;
- }
}