Sqlite as storage backend (#1)
Some platforms has some problems with file locking, so I was forced to use an alternative. SQLite seems be the best option currently available * Migrated to an sqlite database * Removed unnecessary IOExceptions * Removed an util class not needed anymore * Updated README.md and docker-compose.yml to reflect new storage mechanism
This commit is contained in:
committed by
GitHub
parent
25adf04903
commit
7f275bf6af
@@ -1,36 +0,0 @@
|
||||
package tk.draganczuk.url;
|
||||
|
||||
/**
|
||||
* Pair
|
||||
*/
|
||||
public class Pair<T, U> {
|
||||
|
||||
private T left;
|
||||
private U right;
|
||||
|
||||
|
||||
public Pair(T left, U right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public static <T, U> Pair<T, U> of(T left, U right){
|
||||
return new Pair<T,U>(left, right);
|
||||
}
|
||||
|
||||
public T getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(T left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public U getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(U right) {
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
@@ -8,18 +8,14 @@ import java.io.IOException;
|
||||
|
||||
public class Routes {
|
||||
|
||||
private static UrlFile urlFile;
|
||||
private static UrlRepository urlRepository;
|
||||
|
||||
static {
|
||||
try {
|
||||
urlFile = new UrlFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
urlRepository = new UrlRepository();
|
||||
}
|
||||
|
||||
public static String getAll(Request req, Response res) throws IOException {
|
||||
return String.join("\n", urlFile.getAll());
|
||||
public static String getAll(Request req, Response res) {
|
||||
return String.join("\n", urlRepository.getAll());
|
||||
}
|
||||
|
||||
public static String addUrl(Request req, Response res) {
|
||||
@@ -31,7 +27,7 @@ public class Routes {
|
||||
}
|
||||
|
||||
if (Utils.validate(shortUrl)) {
|
||||
return urlFile.addUrl(longUrl, shortUrl);
|
||||
return urlRepository.addUrl(longUrl, shortUrl);
|
||||
} else {
|
||||
res.status(HttpStatus.BAD_REQUEST_400);
|
||||
return "shortUrl not valid ([a-z0-9]+)";
|
||||
@@ -41,7 +37,7 @@ public class Routes {
|
||||
|
||||
public static String goToLongUrl(Request req, Response res) {
|
||||
String shortUrl = req.params("shortUrl");
|
||||
var longUrlOpt = urlFile
|
||||
var longUrlOpt = urlRepository
|
||||
.findForShortUrl(shortUrl);
|
||||
|
||||
if (longUrlOpt.isEmpty()) {
|
||||
@@ -56,15 +52,8 @@ public class Routes {
|
||||
|
||||
public static String delete(Request req, Response res) {
|
||||
String shortUrl = req.params("shortUrl");
|
||||
var longUrlOpt = urlFile
|
||||
.findForShortUrl(shortUrl);
|
||||
|
||||
if (longUrlOpt.isEmpty()) {
|
||||
res.status(404);
|
||||
return "";
|
||||
}
|
||||
|
||||
urlFile.deleteEntry(String.format("%s,%s", shortUrl, longUrlOpt.get()));
|
||||
urlRepository.deleteEntry(shortUrl);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
package tk.draganczuk.url;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public class UrlFile {
|
||||
|
||||
private File file;
|
||||
|
||||
public UrlFile() throws IOException{
|
||||
String path = System.getenv().getOrDefault("file.location", "./urls.csv");
|
||||
this.file = new File(path);
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getAll() throws IOException{
|
||||
return Files.readAllLines(file.toPath());
|
||||
}
|
||||
|
||||
public String addUrl(String longURL, String shortUrl){
|
||||
String entry = String.format("%s,%s",shortUrl,longURL);
|
||||
try {
|
||||
var lineOpt = Files.lines(file.toPath())
|
||||
.filter(line -> line.equals(entry))
|
||||
.findAny();
|
||||
if(lineOpt.isEmpty()){
|
||||
Files.writeString(file.toPath(), entry + System.lineSeparator(), StandardOpenOption.APPEND);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
public Optional<String> findForShortUrl(String shortUrl){
|
||||
try {
|
||||
return Files.lines(file.toPath())
|
||||
.map(this::splitLine)
|
||||
.filter(pair -> pair.getLeft().equals(shortUrl))
|
||||
.map(Pair::getRight)
|
||||
.findAny();
|
||||
} catch (IOException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public Pair<String, String> splitLine(String line) {
|
||||
var split = line.split(",");
|
||||
return new Pair<>(split[0], split[1]);
|
||||
}
|
||||
|
||||
public void deleteEntry(String entry) {
|
||||
try {
|
||||
File tmp = File.createTempFile(file.getName(), ".tmp");
|
||||
if (!tmp.exists()) {
|
||||
tmp.createNewFile();
|
||||
}
|
||||
|
||||
Files.lines(file.toPath())
|
||||
.filter(line -> !line.equals(entry))
|
||||
.forEach(line -> {
|
||||
try {
|
||||
Files.writeString(tmp.toPath(), line + "\n", StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
Files.move(tmp.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
109
src/main/java/tk/draganczuk/url/UrlRepository.java
Normal file
109
src/main/java/tk/draganczuk/url/UrlRepository.java
Normal file
@@ -0,0 +1,109 @@
|
||||
package tk.draganczuk.url;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public class UrlRepository {
|
||||
private static final String INSERT_ROW_SQL = "INSERT INTO urls (long_url, short_url) VALUES (?, ?)";
|
||||
private static final String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS urls\n" +
|
||||
"(\n" +
|
||||
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
|
||||
" long_url TEXT NOT NULL,\n" +
|
||||
" short_url TEXT NOT NULL\n" +
|
||||
");";
|
||||
private static final String SELECT_FOR_SHORT_SQL = "SELECT long_url FROM urls WHERE short_url = ?";
|
||||
private static final String DELETE_ROW_SQL = "DELETE FROM urls WHERE short_url = ?";
|
||||
|
||||
private String databaseUrl;
|
||||
|
||||
|
||||
public UrlRepository() {
|
||||
String path = System.getenv().getOrDefault("db.url", "./urls.sqlite");
|
||||
|
||||
databaseUrl = "jdbc:sqlite:" + path;
|
||||
|
||||
try (Connection conn = DriverManager.getConnection(databaseUrl)) {
|
||||
if (conn != null) {
|
||||
DatabaseMetaData meta = conn.getMetaData();
|
||||
|
||||
conn.createStatement()
|
||||
.execute(CREATE_TABLE_SQL);
|
||||
|
||||
System.out.println("Database initialised");
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getAll() {
|
||||
try (final var con = DriverManager.getConnection(databaseUrl)) {
|
||||
var statement = con.createStatement();
|
||||
|
||||
statement.execute("SELECT * FROM urls");
|
||||
ResultSet rs = statement.getResultSet();
|
||||
|
||||
List<String> result = new ArrayList<>();
|
||||
|
||||
while (rs.next()) {
|
||||
result.add(String.format("%s,%s", rs.getString("short_url"), rs.getString("long_url")));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
public String addUrl(String longURL, String shortUrl) {
|
||||
try (final var con = DriverManager.getConnection(databaseUrl)) {
|
||||
final var stmt = con.prepareStatement(INSERT_ROW_SQL);
|
||||
stmt.setString(1, longURL);
|
||||
stmt.setString(2, shortUrl);
|
||||
if (stmt.execute()) {
|
||||
return String.format("%s,%s", shortUrl, longURL);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public Optional<String> findForShortUrl(String shortUrl) {
|
||||
try (final var con = DriverManager.getConnection(databaseUrl)) {
|
||||
final var stmt = con.prepareStatement(SELECT_FOR_SHORT_SQL);
|
||||
stmt.setString(1, shortUrl);
|
||||
if (stmt.execute()) {
|
||||
ResultSet rs = stmt.getResultSet();
|
||||
if (rs.next()) {
|
||||
return Optional.of(rs.getString("long_url"));
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public void deleteEntry(String shortUrl) {
|
||||
try (final var con = DriverManager.getConnection(databaseUrl)) {
|
||||
final var stmt = con.prepareStatement(DELETE_ROW_SQL);
|
||||
stmt.setString(1, shortUrl);
|
||||
stmt.execute();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user