diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/DBInitException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/DBInitException.java new file mode 100644 index 0000000000..0c8a6d7b04 --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/DBInitException.java @@ -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); + } + +} diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/SqliteDBStore.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/SqliteDBStore.java deleted file mode 100644 index d0476a7137..0000000000 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/SqliteDBStore.java +++ /dev/null @@ -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 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); - } - } - -} diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/SQLQuery.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SQLQuery.java similarity index 82% rename from services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/SQLQuery.java rename to services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SQLQuery.java index df0c2869d4..0873d133b8 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/SQLQuery.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SQLQuery.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/SQLUpdate.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SQLUpdate.java similarity index 84% rename from services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/SQLUpdate.java rename to services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SQLUpdate.java index 097e2b1188..b550c6f7ef 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/SQLUpdate.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SQLUpdate.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/SQLiteWLDatabase.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java similarity index 62% rename from services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/SQLiteWLDatabase.java rename to services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java index 70302c1ba5..08b1d5d646 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/SQLiteWLDatabase.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java @@ -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 getProjectNames() throws SQLException { + @Override + public List 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 query(SQLQuery 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 query(SQLQuery query) throws SQLException { + private T doQuery(SQLQuery query) throws SQLException { PreparedStatement statement = null; ResultSet results = null; try { diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetLatestVersionForProjectSQLQuery.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetLatestVersionForProjectSQLQuery.java index 49c1a7d447..5bac1e1032 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetLatestVersionForProjectSQLQuery.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetLatestVersionForProjectSQLQuery.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetPathForURLInProjectSQLQuery.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetPathForURLInProjectSQLQuery.java index a449ecc8a5..f243ccb11f 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetPathForURLInProjectSQLQuery.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetPathForURLInProjectSQLQuery.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetProjectNamesSQLQuery.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetProjectNamesSQLQuery.java index 0985c3402a..e0dce1a413 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetProjectNamesSQLQuery.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/query/GetProjectNamesSQLQuery.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateIndexURLIndexStore.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateIndexURLIndexStore.java index 6666722ff6..50a0c8ca64 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateIndexURLIndexStore.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateIndexURLIndexStore.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateProjectsTableSQLUpdate.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateProjectsTableSQLUpdate.java index 6ac597148a..d315048390 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateProjectsTableSQLUpdate.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateProjectsTableSQLUpdate.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateURLIndexStoreSQLUpdate.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateURLIndexStoreSQLUpdate.java index 0ecf77e6d9..327a145535 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateURLIndexStoreSQLUpdate.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/create/CreateURLIndexStoreSQLUpdate.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/delete/DeleteFilesForProjectSQLUpdate.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/delete/DeleteFilesForProjectSQLUpdate.java index db9ef39f95..de3c83c575 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/delete/DeleteFilesForProjectSQLUpdate.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/delete/DeleteFilesForProjectSQLUpdate.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/insert/AddURLIndexSQLUpdate.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/insert/AddURLIndexSQLUpdate.java index 48e6badb0b..cc12fe971c 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/insert/AddURLIndexSQLUpdate.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/insert/AddURLIndexSQLUpdate.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/insert/SetProjectSQLUpdate.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/insert/SetProjectSQLUpdate.java index 4db17c9d1b..5df6396f21 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/insert/SetProjectSQLUpdate.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/db/sql/update/insert/SetProjectSQLUpdate.java @@ -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; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java index 3bd2c12921..60ec0a80b7 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java @@ -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;