Change DBStore to target a file

This commit is contained in:
Winston Li 2016-08-23 18:43:26 +01:00 committed by Michael Mazour
parent 25fea8ef58
commit c3609803c0
3 changed files with 62 additions and 20 deletions

View file

@ -16,9 +16,9 @@ public class SqliteDBStore implements DBStore {
private final SQLiteWLDatabase database;
public SqliteDBStore(File rootDirectory) {
public SqliteDBStore(File dbFile) {
try {
database = new SQLiteWLDatabase(rootDirectory);
database = new SQLiteWLDatabase(dbFile);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
@ -87,12 +87,20 @@ public class SqliteDBStore implements DBStore {
@Override
public String getOldestUnswappedProject() {
throw new UnsupportedOperationException();
try {
return database.getOldestUnswappedProject();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void setLastAccessedTime(String projectName, Timestamp time) {
throw new UnsupportedOperationException();
try {
database.setLastAccessedTime(projectName, time);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql;
import com.google.api.client.repackaged.com.google.common.base.Preconditions;
import uk.ac.ic.wlgitbridge.data.model.db.sql.query.GetLatestVersionForProjectSQLQuery;
import uk.ac.ic.wlgitbridge.data.model.db.sql.query.GetPathForURLInProjectSQLQuery;
import uk.ac.ic.wlgitbridge.data.model.db.sql.query.GetProjectNamesSQLQuery;
@ -9,7 +10,6 @@ import uk.ac.ic.wlgitbridge.data.model.db.sql.update.create.CreateURLIndexStoreS
import uk.ac.ic.wlgitbridge.data.model.db.sql.update.delete.DeleteFilesForProjectSQLUpdate;
import uk.ac.ic.wlgitbridge.data.model.db.sql.update.insert.AddURLIndexSQLUpdate;
import uk.ac.ic.wlgitbridge.data.model.db.sql.update.insert.SetProjectSQLUpdate;
import uk.ac.ic.wlgitbridge.util.Log;
import java.io.File;
import java.sql.*;
@ -22,36 +22,54 @@ public class SQLiteWLDatabase {
private final Connection connection;
public SQLiteWLDatabase(File rootGitDirectory) throws SQLException, ClassNotFoundException {
File databaseFile = new File(rootGitDirectory, "/.wlgb/wlgb.db");
File dotWlgbDir = databaseFile.getParentFile();
if (!dotWlgbDir.exists()) {
if (!dotWlgbDir.mkdirs()) {
Log.error("{} directory didn't exist, and unable to create. Check your permissions", dotWlgbDir.getAbsolutePath());
}
}
public SQLiteWLDatabase(
File dbFile
) throws SQLException, ClassNotFoundException {
File parentDir = dbFile.getParentFile();
Preconditions.checkState(
parentDir.exists() || parentDir.mkdirs(),
parentDir.getAbsolutePath() + " directory didn't exist, " +
"and unable to create. Check your permissions."
);
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath());
connection = DriverManager.getConnection(
"jdbc:sqlite:" + dbFile.getAbsolutePath()
);
createTables();
}
public void setVersionIDForProject(String projectName, int versionID) throws SQLException {
public void setVersionIDForProject(
String projectName,
int versionID
) throws SQLException {
update(new SetProjectSQLUpdate(projectName, versionID));
}
public void addURLIndex(String projectName, String url, String path) throws SQLException {
public void addURLIndex(
String projectName,
String url,
String path
) throws SQLException {
update(new AddURLIndexSQLUpdate(projectName, url, path));
}
public void deleteFilesForProject(String projectName, String... paths) throws SQLException {
public void deleteFilesForProject(
String projectName,
String... paths
) throws SQLException {
update(new DeleteFilesForProjectSQLUpdate(projectName, paths));
}
public int getVersionIDForProjectName(String projectName) throws SQLException {
public int getVersionIDForProjectName(
String projectName
) throws SQLException {
return query(new GetLatestVersionForProjectSQLQuery(projectName));
}
public String getPathForURLInProject(String projectName, String url) throws SQLException {
public String getPathForURLInProject(
String projectName,
String url
) throws SQLException {
return query(new GetPathForURLInProjectSQLQuery(projectName, url));
}
@ -59,6 +77,17 @@ public class SQLiteWLDatabase {
return query(new GetProjectNamesSQLQuery());
}
public String getOldestUnswappedProject() throws SQLException {
throw new UnsupportedOperationException();
}
public void setLastAccessedTime(
String projectName,
Timestamp time
) throws SQLException {
throw new UnsupportedOperationException();
}
private void createTables() throws SQLException {
final SQLUpdate[] createTableUpdates = {
new CreateProjectsTableSQLUpdate(),

View file

@ -26,6 +26,7 @@ import javax.servlet.ServletException;
import java.io.File;
import java.io.InputStream;
import java.net.BindException;
import java.nio.file.Paths;
import java.util.EnumSet;
/**
@ -50,7 +51,11 @@ public class GitBridgeServer {
this.port = config.getPort();
this.rootGitDirectoryPath = config.getRootGitDirectory();
RepoStore repoStore = new FSRepoStore(rootGitDirectoryPath);
DBStore dbStore = new SqliteDBStore(repoStore.getRootDirectory());
DBStore dbStore = new SqliteDBStore(
Paths.get(
repoStore.getRootDirectory().getAbsolutePath()
).resolve(".wlgb").resolve("wlgb.db").toFile()
);
SwapStore swapStore = new SwapStore() {
@Override
public void upload(String projectName, InputStream uploadStream, long contentLength) {