Remove DB fluff layer

This commit is contained in:
Winston Li 2016-08-23 19:04:28 +01:00 committed by Michael Mazour
parent c3609803c0
commit cfc02bbcc8
15 changed files with 122 additions and 177 deletions

View file

@ -0,0 +1,20 @@
package uk.ac.ic.wlgitbridge.bridge.db;
/**
* Created by winston on 23/08/2016.
*/
public class DBInitException extends RuntimeException {
public DBInitException(String message) {
super(message);
}
public DBInitException(String message, Throwable cause) {
super(message, cause);
}
public DBInitException(Throwable cause) {
super(cause);
}
}

View file

@ -1,106 +0,0 @@
package uk.ac.ic.wlgitbridge.bridge.db;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLiteWLDatabase;
import uk.ac.ic.wlgitbridge.util.Log;
import java.io.File;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
/**
* Created by winston on 20/08/2016.
*/
public class SqliteDBStore implements DBStore {
private final SQLiteWLDatabase database;
public SqliteDBStore(File dbFile) {
try {
database = new SQLiteWLDatabase(dbFile);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Override
public List<String> getProjectNames() {
try {
return database.getProjectNames();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void setLatestVersionForProject(String project, int versionID) {
try {
database.setVersionIDForProject(project, versionID);
Log.info("[{}] Wrote latest versionId: {}", project, versionID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public int getLatestVersionForProject(String project) {
try {
return database.getVersionIDForProjectName(project);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void addURLIndexForProject(String projectName, String url, String path) {
try {
database.addURLIndex(projectName, url, path);
Log.info("[{}] Wrote url index: {} -> {}", projectName, url, path);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteFilesForProject(String project, String... files) {
try {
database.deleteFilesForProject(project, files);
Log.info(
"[{}] Deleting from url index: {}",
project,
Arrays.toString(files)
);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public String getPathForURLInProject(String projectName, String url) {
try {
return database.getPathForURLInProject(projectName, url);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public String getOldestUnswappedProject() {
try {
return database.getOldestUnswappedProject();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void setLastAccessedTime(String projectName, Timestamp time) {
try {
database.setLastAccessedTime(projectName, time);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql;
package uk.ac.ic.wlgitbridge.bridge.db.sqlite;
import java.sql.ResultSet;
import java.sql.SQLException;

View file

@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql;
package uk.ac.ic.wlgitbridge.bridge.db.sqlite;
import java.sql.PreparedStatement;
import java.sql.SQLException;

View file

@ -1,6 +1,7 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql;
package uk.ac.ic.wlgitbridge.bridge.db.sqlite;
import com.google.api.client.repackaged.com.google.common.base.Preconditions;
import uk.ac.ic.wlgitbridge.bridge.db.DBInitException;
import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
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;
@ -14,93 +15,123 @@ import uk.ac.ic.wlgitbridge.data.model.db.sql.update.insert.SetProjectSQLUpdate;
import java.io.File;
import java.sql.*;
import java.util.List;
import java.util.stream.Stream;
/**
* Created by Winston on 17/11/14.
*/
public class SQLiteWLDatabase {
public class SqliteDBStore implements DBStore {
private final Connection connection;
public SQLiteWLDatabase(
public SqliteDBStore(
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:" + dbFile.getAbsolutePath()
);
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new DBInitException(
parentDir.getAbsolutePath() + " directory didn't exist, " +
"and unable to create. Check your permissions."
);
}
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
throw new DBInitException(e);
}
try {
connection = DriverManager.getConnection(
"jdbc:sqlite:" + dbFile.getAbsolutePath()
);
} catch (SQLException e) {
throw new DBInitException("Unable to connect to DB", e);
}
createTables();
}
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 {
update(new AddURLIndexSQLUpdate(projectName, url, path));
}
public void deleteFilesForProject(
String projectName,
String... paths
) throws SQLException {
update(new DeleteFilesForProjectSQLUpdate(projectName, paths));
}
public int getVersionIDForProjectName(
String projectName
) throws SQLException {
return query(new GetLatestVersionForProjectSQLQuery(projectName));
}
public String getPathForURLInProject(
String projectName,
String url
) throws SQLException {
return query(new GetPathForURLInProjectSQLQuery(projectName, url));
}
public List<String> getProjectNames() throws SQLException {
@Override
public List<String> getProjectNames() {
return query(new GetProjectNamesSQLQuery());
}
public String getOldestUnswappedProject() throws SQLException {
@Override
public void setLatestVersionForProject(
String projectName,
int versionID
) {
update(new SetProjectSQLUpdate(projectName, versionID));
}
@Override
public int getLatestVersionForProject(
String projectName
) {
return query(new GetLatestVersionForProjectSQLQuery(projectName));
}
@Override
public void addURLIndexForProject(
String projectName,
String url,
String path
) {
update(new AddURLIndexSQLUpdate(projectName, url, path));
}
@Override
public void deleteFilesForProject(
String projectName,
String... paths
) {
update(new DeleteFilesForProjectSQLUpdate(projectName, paths));
}
@Override
public String getPathForURLInProject(
String projectName,
String url
) {
return query(new GetPathForURLInProjectSQLQuery(projectName, url));
}
@Override
public String getOldestUnswappedProject() {
throw new UnsupportedOperationException();
}
@Override
public void setLastAccessedTime(
String projectName,
Timestamp time
) throws SQLException {
) {
throw new UnsupportedOperationException();
}
private void createTables() throws SQLException {
final SQLUpdate[] createTableUpdates = {
private void createTables() {
Stream.of(
new CreateProjectsTableSQLUpdate(),
new CreateURLIndexStoreSQLUpdate(),
new CreateIndexURLIndexStore()
};
).forEach(this::update);
}
for (SQLUpdate update : createTableUpdates) {
update(update);
private void update(SQLUpdate update) {
try {
doUpdate(update);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private void update(SQLUpdate update) throws SQLException {
private <T> T query(SQLQuery<T> query) {
try {
return doQuery(query);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private void doUpdate(SQLUpdate update) throws SQLException {
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(update.getSQL());
@ -113,7 +144,7 @@ public class SQLiteWLDatabase {
}
}
private <T> T query(SQLQuery<T> query) throws SQLException {
private <T> T doQuery(SQLQuery<T> query) throws SQLException {
PreparedStatement statement = null;
ResultSet results = null;
try {

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql.query;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLQuery;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLQuery;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql.query;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLQuery;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLQuery;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql.query;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLQuery;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLQuery;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql.update.create;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLUpdate;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
import java.sql.PreparedStatement;
import java.sql.SQLException;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql.update.create;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLUpdate;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
import java.sql.PreparedStatement;
import java.sql.SQLException;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql.update.create;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLUpdate;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
import java.sql.PreparedStatement;
import java.sql.SQLException;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql.update.delete;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLUpdate;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
import java.sql.PreparedStatement;
import java.sql.SQLException;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql.update.insert;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLUpdate;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
import java.sql.PreparedStatement;
import java.sql.SQLException;

View file

@ -1,6 +1,6 @@
package uk.ac.ic.wlgitbridge.data.model.db.sql.update.insert;
import uk.ac.ic.wlgitbridge.data.model.db.sql.SQLUpdate;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
import java.sql.PreparedStatement;
import java.sql.SQLException;

View file

@ -10,7 +10,7 @@ import uk.ac.ic.wlgitbridge.application.config.Config;
import uk.ac.ic.wlgitbridge.application.jetty.NullLogger;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
import uk.ac.ic.wlgitbridge.bridge.db.SqliteDBStore;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SqliteDBStore;
import uk.ac.ic.wlgitbridge.bridge.repo.FSRepoStore;
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
import uk.ac.ic.wlgitbridge.bridge.swap.SwapStore;