mirror of
https://github.com/gmbrax/MediaManager-core.git
synced 2026-11-09 14:00:17 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
353fee5985 | ||
|
|
e56d86c0e5 |
@@ -84,10 +84,12 @@
|
||||
<version>4.32.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.44.1.0</version>
|
||||
<groupId>io.zonky.test</groupId>
|
||||
<artifactId>embedded-postgres</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.mediamanager;
|
||||
|
||||
import com.mediamanager.service.database.DatabaseManager;
|
||||
import com.mediamanager.service.delegate.DelegateActionManager;
|
||||
import com.mediamanager.service.ipc.IPCManager;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -10,12 +9,12 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.mediamanager.service.database.SqliteDatabaseManager;
|
||||
import com.mediamanager.service.database.LocalDatabaseManager;
|
||||
|
||||
public class MediaManagerApplication {
|
||||
private static final Logger logger = LogManager.getLogger(MediaManagerApplication.class);
|
||||
private static Properties config;
|
||||
private static DatabaseManager databaseManager;
|
||||
private static LocalDatabaseManager databaseManager;
|
||||
private static DelegateActionManager actionManager;
|
||||
private static IPCManager ipcManager;
|
||||
|
||||
@@ -53,7 +52,7 @@ public class MediaManagerApplication {
|
||||
switch (mode) {
|
||||
case LOCAL:
|
||||
logger.info("Starting local database...");
|
||||
databaseManager = new SqliteDatabaseManager(config);
|
||||
databaseManager = new LocalDatabaseManager(config);
|
||||
break;
|
||||
case SERVER:
|
||||
throw new Exception("Server mode not yet implemented");
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.mediamanager.service.database;
|
||||
|
||||
import io.zonky.test.db.postgres.embedded.EmbeddedPostgres;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
|
||||
public class LocalDatabaseManager extends DatabaseManager {
|
||||
private static final Logger logger = LogManager.getLogger(LocalDatabaseManager.class);
|
||||
private EmbeddedPostgres embeddedPostgres;
|
||||
|
||||
public LocalDatabaseManager(Properties config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
logger.info("Initializing embedded PostgreSQL database...");
|
||||
|
||||
// Configurações específicas do modo local
|
||||
String dataDir = configuration.getProperty("local.database.dir",
|
||||
System.getProperty("user.home") + "/.mediamanager/db");
|
||||
int port = Integer.parseInt(
|
||||
configuration.getProperty("local.database.port", "54321")
|
||||
);
|
||||
|
||||
// Cria diretório se não existir
|
||||
Path dataPath = Paths.get(dataDir);
|
||||
Files.createDirectories(dataPath);
|
||||
logger.debug("Using data directory: {}", dataDir);
|
||||
|
||||
// Inicia o PostgreSQL embedded
|
||||
logger.info("Starting embedded PostgreSQL on port {}", port);
|
||||
embeddedPostgres = EmbeddedPostgres.builder()
|
||||
.setDataDirectory(dataPath)
|
||||
.setCleanDataDirectory(false) // Mantém dados
|
||||
.setPort(port)
|
||||
.start();
|
||||
|
||||
// Cria a conexão
|
||||
this.connection = createConnection();
|
||||
logger.info("Embedded database connection established successfully");
|
||||
|
||||
// Usa o método da classe pai para sanity checks
|
||||
performSanityChecks();
|
||||
logger.info("Database sanity checks passed successfully");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connection createConnection() throws Exception {
|
||||
if (embeddedPostgres == null) {
|
||||
throw new IllegalStateException("Embedded PostgreSQL not started");
|
||||
}
|
||||
|
||||
try {
|
||||
// Pega conexão do embedded
|
||||
Connection conn = embeddedPostgres.getPostgresDatabase().getConnection();
|
||||
logger.debug("Got connection from embedded PostgreSQL");
|
||||
return conn;
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to get embedded connection", e);
|
||||
throw new Exception("Embedded connection failed: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// Primeiro fecha a conexão (método da classe pai)
|
||||
super.close();
|
||||
|
||||
// Depois para o embedded
|
||||
if (embeddedPostgres != null) {
|
||||
try {
|
||||
logger.info("Stopping embedded PostgreSQL...");
|
||||
embeddedPostgres.close();
|
||||
logger.info("Embedded PostgreSQL stopped successfully");
|
||||
} catch (Exception e) {
|
||||
logger.error("Error stopping embedded PostgreSQL", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
package com.mediamanager.service.database;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
public class SqliteDatabaseManager extends DatabaseManager {
|
||||
private static final Logger logger = LogManager.getLogger(SqliteDatabaseManager.class);
|
||||
|
||||
|
||||
private String connectionUrl;
|
||||
|
||||
public SqliteDatabaseManager(Properties config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
|
||||
logger.info("Initializing SQLite database...");
|
||||
String dataDir = configuration.getProperty("database.dir",
|
||||
System.getProperty("user.home") + "/.mediamanager/db");
|
||||
String dbFilename = configuration.getProperty("database.filename", "mediamanager.db");
|
||||
String driverClassName = configuration.getProperty("database.driver", "org.sqlite.JDBC");
|
||||
try {
|
||||
Class.forName(driverClassName);
|
||||
}catch(ClassNotFoundException e) {
|
||||
logger.error("Failed to load SQLite driver", e);
|
||||
throw e;
|
||||
}
|
||||
Path dataPath = Paths.get(dataDir);
|
||||
if (!Files.exists(dataPath)) {
|
||||
Files.createDirectories(dataPath);
|
||||
logger.debug("Created database directory: {}", dataDir);
|
||||
}
|
||||
Path dbFile = dataPath.resolve(dbFilename);
|
||||
this.connectionUrl = "jdbc:sqlite:" + dbFile.toAbsolutePath().toString();
|
||||
logger.info("Database file path: {}", dbFile);
|
||||
logger.info("Connection URL: {}", this.connectionUrl);
|
||||
this.connection = createConnection();
|
||||
configurePerformancePragmas();
|
||||
performSanityChecks();
|
||||
ensureSchemaExists();
|
||||
logger.info("SQLite database initialized successfully");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connection createConnection() throws Exception {
|
||||
try {
|
||||
// O driver org.xerial.sqlite-jdbc é carregado automaticamente aqui
|
||||
Connection conn = DriverManager.getConnection(this.connectionUrl);
|
||||
logger.debug("Got connection to SQLite file");
|
||||
return conn;
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed to create SQLite connection", e);
|
||||
throw new Exception("SQLite connection failed: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void configurePerformancePragmas() throws SQLException {
|
||||
try (Statement stmt = connection.createStatement()) {
|
||||
|
||||
stmt.execute("PRAGMA journal_mode=WAL;");
|
||||
|
||||
|
||||
stmt.execute("PRAGMA foreign_keys=ON;");
|
||||
|
||||
|
||||
stmt.execute("PRAGMA synchronous=NORMAL;");
|
||||
|
||||
stmt.execute("PRAGMA busy_timeout=5000;");
|
||||
|
||||
logger.debug("SQLite performance PRAGMAs applied (WAL, Synchronous, ForeignKeys).");
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureSchemaExists() throws SQLException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
super.close();
|
||||
logger.info("SQLite resources released.");
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,7 @@ hibernate.format_sql=true
|
||||
ipc.pipe.name=mediamanager-pipe
|
||||
ipc.pipe.path=/tmp/mediamanager
|
||||
ipc.buffer.size=8192
|
||||
runtype=local
|
||||
|
||||
ipc.socket.path=/tmp/mediamanager
|
||||
|
||||
|
||||
runtype=local
|
||||
|
||||
|
||||
@@ -28,6 +28,29 @@
|
||||
<AppenderRef ref="FileAppender"/>
|
||||
</Logger>
|
||||
|
||||
<Logger name="io.zonky.test" level="INFO" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="FileAppender"/>
|
||||
</Logger>
|
||||
|
||||
<!-- PostgreSQL JDBC Driver -->
|
||||
<Logger name="org.postgresql" level="WARN" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="FileAppender"/>
|
||||
</Logger>
|
||||
|
||||
<!-- Processo do PostgreSQL embedded -->
|
||||
<Logger name="org.postgresql.embedded" level="INFO" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="FileAppender"/>
|
||||
</Logger>
|
||||
|
||||
<!-- HikariCP Connection Pool -->
|
||||
<Logger name="com.zaxxer.hikari" level="INFO" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="FileAppender"/>
|
||||
</Logger>
|
||||
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="FileAppender"/>
|
||||
|
||||
Reference in New Issue
Block a user