forked from finos/architecture-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNitriteDBConfig.java
More file actions
116 lines (95 loc) · 3.49 KB
/
NitriteDBConfig.java
File metadata and controls
116 lines (95 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.finos.calm.config;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import org.bson.json.JsonParseException;
import org.dizitart.no2.Nitrite;
import org.dizitart.no2.mvstore.MVStoreModule;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Configuration for standalone mode using NitriteDB.
* This class manages the lifecycle of the NitriteDB instance.
*/
@ApplicationScoped
public class NitriteDBConfig {
private static final Logger LOG = LoggerFactory.getLogger(NitriteDBConfig.class);
@ConfigProperty(name = "calm.database.mode", defaultValue = "mongo")
String databaseMode;
@ConfigProperty(name = "calm.standalone.data-directory")
String dataDirectory;
@ConfigProperty(name = "calm.standalone.database-name", defaultValue = "calmSchemas")
String databaseName;
@ConfigProperty(name = "calm.standalone.username", defaultValue = "admin")
String username;
@ConfigProperty(name = "calm.standalone.password", defaultValue = "admin")
String password;
private Nitrite db;
@PostConstruct
void initialize() {
if ("standalone".equals(databaseMode)) {
LOG.info("Starting NitriteDB in standalone mode");
try {
// Ensure data directory exists
Path dataPath = Paths.get(dataDirectory);
if (!Files.exists(dataPath)) {
Files.createDirectories(dataPath);
}
// Configure and open NitriteDB
Path dbFilePath = dataPath.resolve(databaseName + ".db");
// Create the database
// Create the MVStoreModule with the file path
MVStoreModule storeModule = MVStoreModule.withConfig()
.filePath(dbFilePath.toString())
.autoCommit(true)
.build();
db = Nitrite.builder()
.loadModule(storeModule)
.openOrCreate(username, password);
initializeDatabase();
LOG.info("NitriteDB started successfully at {}", dbFilePath);
} catch (IOException e) {
LOG.error("Failed to start NitriteDB", e);
throw new RuntimeException("Failed to start NitriteDB", e);
}
}
}
/**
* Initilise the DB.
* @throws IOException
* @throws JsonParseException
*/
private void initializeDatabase() throws IOException, JsonParseException {
// Create collections - just getting the collection creates it if it doesn't exist
db.getCollection("architectures");
db.getCollection("patterns");
db.getCollection("namespaces");
db.getCollection("domains");
db.getCollection("flows");
db.getCollection("schemas");
db.getCollection("counters");
}
/**
* Get the underlying NitriteDB instance.
* Used in tests.
*/
@Produces
@ApplicationScoped
@StandaloneQualifier
public Nitrite getNitriteDb() {
return db;
}
@PreDestroy
void shutdown() {
if (db != null && !db.isClosed()) {
db.close();
LOG.info("NitriteDB stopped");
}
}
}