Skip to content

Commit af1d91e

Browse files
committed
Clean up storage API
1 parent 19495ea commit af1d91e

File tree

10 files changed

+146
-47
lines changed

10 files changed

+146
-47
lines changed

src/main/java/com/cinemamod/bukkit/CinemaModPlugin.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import com.cinemamod.bukkit.player.PlayerDataManager;
1010
import com.cinemamod.bukkit.service.infofetcher.FileVideoInfoFetcher;
1111
import com.cinemamod.bukkit.storage.VideoStorage;
12-
import com.cinemamod.bukkit.storage.sql.MySQLVideoStorage;
13-
import com.cinemamod.bukkit.storage.sql.SQLiteVideoStorage;
12+
import com.cinemamod.bukkit.storage.sql.MySQLDriver;
13+
import com.cinemamod.bukkit.storage.sql.SQLDriver;
14+
import com.cinemamod.bukkit.storage.sql.SQLiteDriver;
15+
import com.cinemamod.bukkit.storage.sql.video.SQLVideoStorage;
1416
import com.cinemamod.bukkit.task.PlayerListUpdateTask;
1517
import com.cinemamod.bukkit.theater.TheaterManager;
1618
import com.cinemamod.bukkit.util.NetworkUtil;
@@ -69,29 +71,31 @@ public void onEnable() {
6971
theaterManager = new TheaterManager(this);
7072
theaterManager.loadFromConfig(getConfig().getConfigurationSection("theaters"));
7173

74+
SQLDriver sqlDriver = null;
75+
7276
if (cinemaModConfig.useMysql) {
73-
try {
74-
videoStorage = new MySQLVideoStorage(cinemaModConfig);
75-
} catch (SQLException e) {
76-
e.printStackTrace();
77-
}
77+
sqlDriver = new MySQLDriver(cinemaModConfig);
7878
} else if (cinemaModConfig.useSqlite) {
79+
File dbFile = new File(getDataFolder(), "video_storage.db");
7980
try {
80-
File dbFile = new File(getDataFolder(), "video_storage.db");
81-
videoStorage = new SQLiteVideoStorage(dbFile);
82-
} catch (IOException e) {
83-
e.printStackTrace();
84-
} catch (SQLException e) {
85-
e.printStackTrace();
81+
sqlDriver = new SQLiteDriver(dbFile);
82+
} catch (IOException ignored) {
83+
getLogger().warning("Unable to create or load database file");
8684
}
8785
}
8886

89-
if (videoStorage == null) {
90-
getLogger().warning("No video storage type found.");
87+
if (sqlDriver == null) {
88+
getLogger().warning("Could not initialize video storage");
9189
getServer().getPluginManager().disablePlugin(this);
9290
return;
9391
}
9492

93+
try {
94+
videoStorage = new SQLVideoStorage(sqlDriver);
95+
} catch (SQLException e) {
96+
throw new RuntimeException(e);
97+
}
98+
9599
playerDataManager = new PlayerDataManager(this);
96100

97101
getServer().getPluginManager().registerEvents(new PlayerJoinQuitListener(this), this);

src/main/java/com/cinemamod/bukkit/storage/TheaterStorage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import java.util.Set;
66
import java.util.concurrent.CompletableFuture;
77

8-
public abstract class TheaterStorage {
8+
public interface TheaterStorage {
99

10-
public abstract CompletableFuture<Void> saveTheater(Theater theater);
10+
CompletableFuture<Void> saveTheater(Theater theater);
1111

12-
public abstract CompletableFuture<Set<Theater>> loadTheaters();
12+
CompletableFuture<Set<Theater>> loadTheaters();
1313

1414
}

src/main/java/com/cinemamod/bukkit/storage/VideoStorage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import java.util.UUID;
99
import java.util.concurrent.CompletableFuture;
1010

11-
public abstract class VideoStorage {
11+
public interface VideoStorage {
1212

13-
public abstract CompletableFuture<Void> saveVideoInfo(VideoInfo videoInfo);
13+
CompletableFuture<Void> saveVideoInfo(VideoInfo videoInfo);
1414

15-
public abstract CompletableFuture<VideoInfo> searchVideoInfo(VideoServiceType videoService, String id);
15+
CompletableFuture<VideoInfo> searchVideoInfo(VideoServiceType videoService, String id);
1616

17-
public abstract CompletableFuture<Void> saveVideoRequest(VideoRequest request);
17+
CompletableFuture<Void> saveVideoRequest(VideoRequest request);
1818

19-
public abstract CompletableFuture<Set<VideoRequest>> loadVideoRequests(UUID requester);
19+
CompletableFuture<Set<VideoRequest>> loadVideoRequests(UUID requester);
2020

2121
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.cinemamod.bukkit.storage.sql;
2+
3+
import com.cinemamod.bukkit.CinemaModConfig;
4+
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.SQLException;
8+
9+
public class MySQLDriver implements SQLDriver {
10+
11+
private final String host;
12+
private final int port;
13+
private final String database;
14+
private final String username;
15+
private final String password;
16+
17+
public MySQLDriver(CinemaModConfig config) {
18+
this.host = config.mysqlHost;
19+
this.port = config.mysqlPort;
20+
this.database = config.mysqlDatabase;
21+
this.username = config.mysqlUsername;
22+
this.password = config.mysqlPassword;
23+
}
24+
25+
@Override
26+
public Connection createConnection() throws SQLException {
27+
String jdbcUrl = "jdbc:mysql://" + host + ":" + port + "/" + database;
28+
return DriverManager.getConnection(jdbcUrl, username, password);
29+
}
30+
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.cinemamod.bukkit.storage.sql;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
6+
public interface SQLDriver {
7+
8+
Connection createConnection() throws SQLException;
9+
10+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.cinemamod.bukkit.storage.sql;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.SQLException;
8+
9+
public class SQLiteDriver implements SQLDriver {
10+
11+
private final File dbFile;
12+
13+
public SQLiteDriver(File dbFile) throws IOException {
14+
this.dbFile = dbFile;
15+
16+
if (!dbFile.exists()) {
17+
dbFile.createNewFile();
18+
}
19+
}
20+
21+
@Override
22+
public Connection createConnection() throws SQLException {
23+
String jdbcUrl = "jdbc:sqlite:" + dbFile.getName();
24+
return DriverManager.getConnection(jdbcUrl);
25+
}
26+
27+
}

src/main/java/com/cinemamod/bukkit/storage/sql/video/AbstractSQLVideoStorage.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@
1313
import java.util.UUID;
1414
import java.util.concurrent.CompletableFuture;
1515

16-
public abstract class SQLVideoStorage implements VideoStorage {
16+
public abstract class AbstractSQLVideoStorage implements VideoStorage {
1717

1818
private final SQLDriver driver;
1919

20-
public SQLVideoStorage(SQLDriver driver) throws SQLException {
20+
public AbstractSQLVideoStorage(SQLDriver driver) throws SQLException {
2121
this.driver = driver;
2222

2323
try (Connection connection = driver.createConnection()) {
2424
createTables(connection);
2525
}
2626
}
2727

28+
public SQLDriver getDriver() {
29+
return driver;
30+
}
31+
2832
public abstract void createTables(Connection connection) throws SQLException;
2933

3034
@Override

src/main/java/com/cinemamod/bukkit/storage/sql/video/RelationalVideoInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cinemamod.bukkit.storage.sql;
1+
package com.cinemamod.bukkit.storage.sql.video;
22

33
import com.cinemamod.bukkit.video.VideoInfo;
44

src/main/java/com/cinemamod/bukkit/storage/sql/video/RelationalVideoRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cinemamod.bukkit.storage.sql;
1+
package com.cinemamod.bukkit.storage.sql.video;
22

33
import com.cinemamod.bukkit.video.VideoRequest;
44

src/main/java/com/cinemamod/bukkit/storage/sql/video/SQLVideoStorage.java

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.cinemamod.bukkit.storage.sql.video;
22

33
import com.cinemamod.bukkit.service.VideoServiceType;
4+
import com.cinemamod.bukkit.storage.sql.MySQLDriver;
45
import com.cinemamod.bukkit.storage.sql.SQLDriver;
6+
import com.cinemamod.bukkit.storage.sql.SQLiteDriver;
57
import com.cinemamod.bukkit.video.VideoInfo;
68
import com.cinemamod.bukkit.video.VideoRequest;
79

@@ -10,33 +12,54 @@
1012
import java.util.Set;
1113
import java.util.UUID;
1214

13-
public class GenericSQLVideoStorage extends SQLVideoStorage {
15+
public class SQLVideoStorage extends AbstractSQLVideoStorage {
1416

15-
public GenericSQLVideoStorage(SQLDriver driver) {
17+
public SQLVideoStorage(SQLDriver driver) throws SQLException {
1618
super(driver);
1719
}
1820

1921
@Override
2022
public void createTables(Connection connection) throws SQLException {
2123
try (Statement statement = connection.createStatement()) {
22-
statement.execute("CREATE TABLE IF NOT EXISTS video_info (" +
23-
"id INT NOT NULL AUTO_INCREMENT, " +
24-
"service_type VARCHAR(16) NOT NULL, " +
25-
"service_id VARCHAR(255) NOT NULL, " +
26-
"title TEXT NOT NULL, " +
27-
"poster TEXT NOT NULL, " +
28-
"thumbnail_url TEXT NOT NULL, " +
29-
"duration_seconds BIGINT NOT NULL, " +
30-
"PRIMARY KEY (id), " +
31-
"UNIQUE (service_type, service_id));");
32-
statement.execute("CREATE TABLE IF NOT EXISTS video_requests (" +
33-
"requester VARCHAR(36) NOT NULL, " +
34-
"video_info_id INT NOT NULL, " +
35-
"last_requested BIGINT NOT NULL, " +
36-
"times_requested INT NOT NULL DEFAULT 1, " +
37-
"hidden BOOL NOT NULL DEFAULT 0, " +
38-
"FOREIGN KEY (video_info_id) REFERENCES video_info(id), " +
39-
"UNIQUE (requester, video_info_id));");
24+
if (getDriver() instanceof SQLiteDriver) {
25+
statement.execute("CREATE TABLE IF NOT EXISTS video_info (" +
26+
"id INT, " +
27+
"service_type VARCHAR(16), " +
28+
"service_id VARCHAR(255), " +
29+
"title TEXT, " +
30+
"poster TEXT, " +
31+
"thumbnail_url TEXT, " +
32+
"duration_seconds BIGINT, " +
33+
"PRIMARY KEY (id), " +
34+
"UNIQUE (service_type, service_id));");
35+
statement.execute("CREATE TABLE IF NOT EXISTS video_requests (" +
36+
"requester VARCHAR(36), " +
37+
"video_info_id INT, " +
38+
"last_requested BIGINT, " +
39+
"times_requested INT DEFAULT 1, " +
40+
"hidden BOOL DEFAULT 0, " +
41+
"FOREIGN KEY (video_info_id) REFERENCES video_info(id), " +
42+
"UNIQUE (requester, video_info_id));");
43+
} else if (getDriver() instanceof MySQLDriver) {
44+
statement.execute("CREATE TABLE IF NOT EXISTS video_info (" +
45+
"id INT NOT NULL AUTO_INCREMENT, " +
46+
"service_type VARCHAR(16) NOT NULL, " +
47+
"service_id VARCHAR(255) NOT NULL, " +
48+
"title TEXT NOT NULL, " +
49+
"poster TEXT NOT NULL, " +
50+
"thumbnail_url TEXT NOT NULL, " +
51+
"duration_seconds BIGINT NOT NULL, " +
52+
"PRIMARY KEY (id), " +
53+
"UNIQUE (service_type, service_id));");
54+
statement.execute("CREATE TABLE IF NOT EXISTS video_requests (" +
55+
"requester VARCHAR(36) NOT NULL, " +
56+
"video_info_id INT NOT NULL, " +
57+
"last_requested BIGINT NOT NULL, " +
58+
"times_requested INT NOT NULL DEFAULT 1, " +
59+
"hidden BOOL NOT NULL DEFAULT 0, " +
60+
"FOREIGN KEY (video_info_id) REFERENCES video_info(id), " +
61+
"UNIQUE (requester, video_info_id));");
62+
}
4063
}
4164
}
4265

0 commit comments

Comments
 (0)