mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Implement swap/restore in SqliteDBStore
This commit is contained in:
parent
a3d4976ce2
commit
961cec5bc8
11 changed files with 275 additions and 6 deletions
|
@ -104,6 +104,21 @@ public class SqliteDBStore implements DBStore {
|
|||
update(new SetProjectLastAccessedTime(projectName, lastAccessed));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swap(String projectName, String compressionMethod) {
|
||||
update(new UpdateSwap(projectName, compressionMethod));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(String projectName) {
|
||||
update(new UpdateRestore(projectName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwapCompression(String projectName) {
|
||||
return query(new GetSwapCompression(projectName));
|
||||
}
|
||||
|
||||
private Connection openConnectionTo(File dbFile) {
|
||||
File parentDir = dbFile.getParentFile();
|
||||
if (!parentDir.exists() && !parentDir.mkdirs()) {
|
||||
|
@ -127,20 +142,27 @@ public class SqliteDBStore implements DBStore {
|
|||
}
|
||||
|
||||
private void createTables() {
|
||||
try {
|
||||
doUpdate(new ProjectsAddLastAccessed());
|
||||
} catch (SQLException ignore) {
|
||||
/* We need to eat exceptions from here */
|
||||
}
|
||||
/* Migrations */
|
||||
/* We need to eat exceptions from here */
|
||||
try { doUpdate(new ProjectsAddLastAccessed()); } catch (SQLException ignore) {}
|
||||
try { doUpdate(new ProjectsAddSwapTime()); } catch (SQLException ignore) {}
|
||||
try { doUpdate(new ProjectsAddRestoreTime()); } catch (SQLException ignore) {}
|
||||
try { doUpdate(new ProjectsAddSwapCompression()); } catch (SQLException ignore) {}
|
||||
|
||||
/* Create tables (if they don't exist) */
|
||||
Stream.of(
|
||||
new CreateProjectsTableSQLUpdate(),
|
||||
new CreateProjectsIndexLastAccessed(),
|
||||
new CreateURLIndexStoreSQLUpdate(),
|
||||
new CreateIndexURLIndexStore()
|
||||
).forEach(this::update);
|
||||
|
||||
/* In the case of needing to change the schema, we need to check that
|
||||
ProjectsAddLastAccessed didn't just fail */
|
||||
migrations didn't just fail */
|
||||
Preconditions.checkState(query(new LastAccessedColumnExists()));
|
||||
Preconditions.checkState(query(new SwapTimeColumnExists()));
|
||||
Preconditions.checkState(query(new RestoreTimeColumnExists()));
|
||||
Preconditions.checkState(query(new SwapCompressionColumnExists()));
|
||||
}
|
||||
|
||||
private void update(SQLUpdate update) {
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.query;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLQuery;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class GetSwapCompression implements SQLQuery<String> {
|
||||
private static final String GET_SWAP_COMPRESSION =
|
||||
"SELECT `swap_compression` FROM `projects` WHERE `name` = ?";
|
||||
|
||||
private final String projectName;
|
||||
|
||||
public GetSwapCompression(String projectName) {
|
||||
this.projectName = projectName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processResultSet(ResultSet resultSet) throws SQLException {
|
||||
String compression = null;
|
||||
while (resultSet.next()) {
|
||||
compression = resultSet.getString("swap_compression");
|
||||
}
|
||||
return compression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQL() {
|
||||
return GET_SWAP_COMPRESSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParametersToStatement(
|
||||
PreparedStatement statement
|
||||
) throws SQLException {
|
||||
statement.setString(1, projectName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.query;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLQuery;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class RestoreTimeColumnExists implements SQLQuery<Boolean> {
|
||||
private static final String RESTORE_TIME_COLUMN_EXISTS =
|
||||
"PRAGMA table_info(`projects`)";
|
||||
|
||||
@Override
|
||||
public String getSQL() {
|
||||
return RESTORE_TIME_COLUMN_EXISTS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean processResultSet(ResultSet resultSet) throws SQLException {
|
||||
while (resultSet.next()) {
|
||||
if (resultSet.getString(2).equals("restore_time")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.query;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLQuery;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SwapCompressionColumnExists implements SQLQuery<Boolean> {
|
||||
private static final String SWAP_COMPRESSION_COLUMN_EXISTS =
|
||||
"PRAGMA table_info(`projects`)";
|
||||
|
||||
@Override
|
||||
public String getSQL() {
|
||||
return SWAP_COMPRESSION_COLUMN_EXISTS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean processResultSet(ResultSet resultSet) throws SQLException {
|
||||
while (resultSet.next()) {
|
||||
if (resultSet.getString(2).equals("swap_compression")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.query;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLQuery;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SwapTimeColumnExists implements SQLQuery<Boolean> {
|
||||
private static final String SWAP_TIME_COLUMN_EXISTS =
|
||||
"PRAGMA table_info(`projects`)";
|
||||
|
||||
@Override
|
||||
public String getSQL() {
|
||||
return SWAP_TIME_COLUMN_EXISTS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean processResultSet(ResultSet resultSet) throws SQLException {
|
||||
while (resultSet.next()) {
|
||||
if (resultSet.getString(2).equals("swap_time")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.update.alter;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
|
||||
|
||||
public class ProjectsAddRestoreTime implements SQLUpdate {
|
||||
private static final String PROJECTS_ADD_RESTORE_TIME =
|
||||
"ALTER TABLE `projects`\n" +
|
||||
"ADD COLUMN `restore_time` DATETIME NULL;\n";
|
||||
|
||||
@Override
|
||||
public String getSQL() {
|
||||
return PROJECTS_ADD_RESTORE_TIME;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.update.alter;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
|
||||
|
||||
public class ProjectsAddSwapCompression implements SQLUpdate {
|
||||
private static final String PROJECTS_ADD_SWAP_COMPRESSION =
|
||||
"ALTER TABLE `projects`\n" +
|
||||
"ADD COLUMN `swap_compression` VARCHAR NULL;\n";
|
||||
|
||||
@Override
|
||||
public String getSQL() {
|
||||
return PROJECTS_ADD_SWAP_COMPRESSION;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.update.alter;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
|
||||
|
||||
public class ProjectsAddSwapTime implements SQLUpdate {
|
||||
private static final String PROJECTS_ADD_SWAP_TIME =
|
||||
"ALTER TABLE `projects`\n" +
|
||||
"ADD COLUMN `swap_time` DATETIME NULL;\n";
|
||||
|
||||
@Override
|
||||
public String getSQL() {
|
||||
return PROJECTS_ADD_SWAP_TIME;
|
||||
}
|
||||
|
||||
}
|
|
@ -12,6 +12,9 @@ public class CreateProjectsTableSQLUpdate implements SQLUpdate {
|
|||
" `name` VARCHAR NOT NULL DEFAULT '',\n" +
|
||||
" `version_id` INT NOT NULL DEFAULT 0,\n" +
|
||||
" `last_accessed` DATETIME NULL DEFAULT 0,\n" +
|
||||
" `swap_time` DATETIME NULL,\n" +
|
||||
" `restore_time` DATETIME NULL,\n" +
|
||||
" `swap_compression` VARCHAR NULL,\n" +
|
||||
" PRIMARY KEY (`name`)\n" +
|
||||
")";
|
||||
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.update.insert;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class UpdateRestore implements SQLUpdate {
|
||||
private static final String UPDATE_RESTORE =
|
||||
"UPDATE `projects`\n" +
|
||||
"SET `last_accessed` = ?,\n" +
|
||||
" `swap_time` = NULL,\n" +
|
||||
" `restore_time` = ?,\n" +
|
||||
" `swap_compression` = NULL\n" +
|
||||
"WHERE `name` = ?;\n";
|
||||
|
||||
private final String projectName;
|
||||
private final Timestamp now;
|
||||
|
||||
public UpdateRestore(String projectName) {
|
||||
this.projectName = projectName;
|
||||
this.now = Timestamp.valueOf(LocalDateTime.now());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQL() {
|
||||
return UPDATE_RESTORE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParametersToStatement(
|
||||
PreparedStatement statement
|
||||
) throws SQLException {
|
||||
statement.setTimestamp(1, now);
|
||||
statement.setTimestamp(2, now);
|
||||
statement.setString(3, projectName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.update.insert;
|
||||
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class UpdateSwap implements SQLUpdate {
|
||||
private static final String UPDATE_SWAP =
|
||||
"UPDATE `projects`\n" +
|
||||
"SET `last_accessed` = NULL,\n" +
|
||||
" `swap_time` = ?,\n" +
|
||||
" `restore_time` = NULL,\n" +
|
||||
" `swap_compression` = ?\n" +
|
||||
"WHERE `name` = ?;\n";
|
||||
|
||||
private final String projectName;
|
||||
private final String compression;
|
||||
private final Timestamp now;
|
||||
|
||||
public UpdateSwap(String projectName, String compression) {
|
||||
this.projectName = projectName;
|
||||
this.compression = compression;
|
||||
this.now = Timestamp.valueOf(LocalDateTime.now());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQL() {
|
||||
return UPDATE_SWAP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParametersToStatement(
|
||||
PreparedStatement statement
|
||||
) throws SQLException {
|
||||
statement.setTimestamp(1, now);
|
||||
statement.setString(2, compression);
|
||||
statement.setString(3, projectName);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue