Repository navigation
Expand file tree
/
Copy pathDatabase.java
More file actions
178 lines (169 loc) · 8.16 KB
/
Copy pathDatabase.java
File metadata and controls
178 lines (169 loc) · 8.16 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.ladybugdb;
/**
* The Database class is the main class of LbugDB. It manages all database
* components.
*/
public class Database implements AutoCloseable {
long db_ref;
String db_path;
long buffer_size;
long max_db_size;
boolean enableCompression = true;
boolean destroyed = false;
boolean readOnly = false;
boolean autoCheckpoint = true;
boolean throwOnWalReplayFailure = true;
boolean enableChecksums = true;
boolean enableMultiWrites = true;
boolean enableDefaultHashIndex = true;
long max_num_threads;
long checkpointThreshold;
/**
* Creates a database object. The database will be created in memory with
* default settings.
*/
public Database() {
this("");
}
/**
* Creates a database object.
*
* @param databasePath: Database path. If the database does not already exist,
* it will be created.
*/
public Database(String databasePath) {
this.db_path = databasePath;
this.buffer_size = 0;
this.max_num_threads = 0;
this.max_db_size = 0;
this.checkpointThreshold = -1;
db_ref = Native.lbugDatabaseInit(databasePath, 0, true, false, max_db_size,
autoCheckpoint, checkpointThreshold, throwOnWalReplayFailure, enableChecksums);
}
/**
* Creates a database object.
*
* @param databasePath: Database path. If the path is empty, or equal to
* `:memory:`, the database will be created in memory.
* @param systemConfig: Runtime database configuration.
*/
public Database(String databasePath, SystemConfig systemConfig) {
this(databasePath, systemConfig.bufferPoolSize, systemConfig.maxNumThreads,
systemConfig.enableCompression, systemConfig.readOnly, systemConfig.maxDBSize,
systemConfig.autoCheckpoint, systemConfig.checkpointThreshold,
systemConfig.throwOnWalReplayFailure, systemConfig.enableChecksums,
systemConfig.enableMultiWrites, systemConfig.enableDefaultHashIndex);
}
/**
* Creates a database object.
*
* @param databasePath: Database path. If the path is empty, or equal to
* `:memory:`, the database will be created in
* memory.
* @param bufferPoolSize: Max size of the buffer pool in bytes.
* @param enableCompression: Enable compression in storage.
* @param readOnly: Open the database in READ_ONLY mode.
* @param maxDBSize: The maximum size of the database in bytes. Note
* that this is introduced
* temporarily for now to get around with the default
* 8TB mmap address space limit some
* environment.
* @param autoCheckpoint If true, the database will automatically
* checkpoint when the size of
* the WAL file exceeds the checkpoint threshold.
* @param checkpointThreshold The threshold of the WAL file size in bytes. When
* the size of the
* WAL file exceeds this threshold, the database will
* checkpoint if autoCheckpoint is true.
* @param throwOnWalReplayFailure If true, any WAL replaying failure when loading the database
* will throw an error. Otherwise, Lbug will silently ignore
* the failure and replay up to where the error occured.
* @param enableChecksums If true, the database will use checksums to detect
* corruption in the WAL file.
*/
public Database(String databasePath, long bufferPoolSize, boolean enableCompression, boolean readOnly,
long maxDBSize, boolean autoCheckpoint, long checkpointThreshold, boolean throwOnWalReplayFailure, boolean enableChecksums) {
this.db_path = databasePath;
this.buffer_size = bufferPoolSize;
this.max_num_threads = 0;
this.enableCompression = enableCompression;
this.readOnly = readOnly;
this.max_db_size = maxDBSize;
this.autoCheckpoint = autoCheckpoint;
this.checkpointThreshold = checkpointThreshold;
this.throwOnWalReplayFailure = throwOnWalReplayFailure;
this.enableChecksums = enableChecksums;
db_ref = Native.lbugDatabaseInit(databasePath, bufferPoolSize, enableCompression, readOnly,
maxDBSize, autoCheckpoint, checkpointThreshold, throwOnWalReplayFailure,
enableChecksums);
}
/**
* Creates a database object.
*
* @param databasePath Database path. If the path is empty, or equal to
* `:memory:`, the database will be created in memory.
* @param bufferPoolSize Max size of the buffer pool in bytes.
* @param maxNumThreads The maximum number of threads available to the database.
* @param enableCompression Enable compression in storage.
* @param readOnly Open the database in READ_ONLY mode.
* @param maxDBSize The maximum size of the database in bytes.
* @param autoCheckpoint If true, the database will automatically checkpoint.
* @param checkpointThreshold The threshold of the WAL file size in bytes.
* @param throwOnWalReplayFailure If true, WAL replay failures raise an error.
* @param enableChecksums If true, WAL and storage page checksums are enabled.
* @param enableMultiWrites If true, multiple concurrent write transactions are allowed.
* @param enableDefaultHashIndex If true, node tables create the default primary-key hash index.
*/
public Database(String databasePath, long bufferPoolSize, long maxNumThreads,
boolean enableCompression, boolean readOnly, long maxDBSize, boolean autoCheckpoint,
long checkpointThreshold, boolean throwOnWalReplayFailure, boolean enableChecksums,
boolean enableMultiWrites, boolean enableDefaultHashIndex) {
this.db_path = databasePath;
this.buffer_size = bufferPoolSize;
this.max_num_threads = maxNumThreads;
this.enableCompression = enableCompression;
this.readOnly = readOnly;
this.max_db_size = maxDBSize;
this.autoCheckpoint = autoCheckpoint;
this.checkpointThreshold = checkpointThreshold;
this.throwOnWalReplayFailure = throwOnWalReplayFailure;
this.enableChecksums = enableChecksums;
this.enableMultiWrites = enableMultiWrites;
this.enableDefaultHashIndex = enableDefaultHashIndex;
db_ref = Native.lbugDatabaseInitExtended(databasePath, bufferPoolSize, maxNumThreads,
enableCompression, readOnly, maxDBSize, autoCheckpoint, checkpointThreshold,
throwOnWalReplayFailure, enableChecksums, enableMultiWrites,
enableDefaultHashIndex);
}
/**
* Checks if the database instance has been destroyed.
*
* @throws RuntimeException If the database instance is destroyed.
*/
private void checkNotDestroyed() {
if (destroyed)
throw new RuntimeException("Database has been destroyed.");
}
/**
* Close the database and release the underlying resources. This method is
* invoked automatically on objects managed by the try-with-resources statement.
*
* @throws RuntimeException If the database instance has been
* destroyed.
*/
@Override
public void close() {
destroy();
}
/**
* Destroy the database instance.
*
* @throws RuntimeException If the database instance has been
* destroyed.
*/
private void destroy() {
checkNotDestroyed();
Native.lbugDatabaseDestroy(this);
destroyed = true;
}
}