Skip to content

Commit 28f8b76

Browse files
committed
fixup! WIP Refactoring and cleanup of BTree storage classes
1 parent 42cb8a1 commit 28f8b76

10 files changed

Lines changed: 413 additions & 285 deletions

File tree

exist-core/src/main/java/org/exist/storage/NativeBroker.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ public NativeBroker(final BrokerPool pool, final Configuration config) throws EX
236236
if(configuredDomFile != null) {
237237
this.domDb = configuredDomFile;
238238
} else {
239-
this.domDb = new DOMFile(pool, DOM_DBX_ID, dataDir, config);
239+
this.domDb = DOMFile.open(pool, DOM_DBX_ID, dataDir);
240+
config.setProperty(DOMFile.getConfigKeyForFile(), domDb);
240241
}
241242
if(domDb.isReadOnly()) {
242243
LOG.warn("{} is read-only!", FileUtils.fileName(domDb.getFile()));
@@ -248,7 +249,8 @@ public NativeBroker(final BrokerPool pool, final Configuration config) throws EX
248249
if(configuredCollectionsDb != null) {
249250
this.collectionsDb = configuredCollectionsDb;
250251
} else {
251-
this.collectionsDb = new CollectionStore(pool, COLLECTIONS_DBX_ID, dataDir, config);
252+
this.collectionsDb = CollectionStore.open(pool, COLLECTIONS_DBX_ID, dataDir);
253+
config.setProperty(CollectionStore.getConfigKeyForFile(), collectionsDb);
252254
}
253255
if(collectionsDb.isReadOnly()) {
254256
LOG.warn("{} is read-only!", FileUtils.fileName(collectionsDb.getFile()));

exist-core/src/main/java/org/exist/storage/btree/AbstractBTree.java

Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,8 @@
8585
import org.apache.logging.log4j.LogManager;
8686
import org.apache.logging.log4j.Logger;
8787

88-
import org.exist.storage.BrokerPool;
89-
import org.exist.storage.BufferStats;
88+
import org.exist.storage.*;
9089

91-
import org.exist.storage.DefaultCacheManager;
92-
import org.exist.storage.NativeBroker;
9390
import org.exist.storage.cache.*;
9491
import org.exist.storage.journal.*;
9592
import org.exist.storage.txn.Txn;
@@ -112,7 +109,7 @@
112109
* {@link org.exist.storage.btree.Value}. The actual value data is not
113110
* stored in the B+tree itself. Instead, we use long pointers to record the
114111
* storage address of the value. This class has no methods to locate or
115-
* modify data records. Data handling is in the responsibilty of the
112+
* modify data records. Data handling is in the responsibility of the
116113
* proper subclasses: {@link org.exist.storage.index.BFile} and
117114
* {@link org.exist.storage.dom.DOMFile}.
118115
*
@@ -156,10 +153,10 @@ public abstract class AbstractBTree<HEADER extends BTreeFileHeader, PAGE_HEADER
156153

157154
private final BrokerPool pool;
158155

159-
protected final DefaultCacheManager cacheManager;
156+
protected final CacheManager cacheManager;
160157

161158
/** Cache of BTreeNode(s) */
162-
private Cache<BTreeNode> cache;
159+
private final Cache<BTreeNode> cache;
163160

164161
/** The LogManager for writing the transaction log */
165162
protected final @Nullable JournalManager logManager;
@@ -168,9 +165,9 @@ public abstract class AbstractBTree<HEADER extends BTreeFileHeader, PAGE_HEADER
168165

169166
private double splitFactor = -1;
170167

171-
protected AbstractBTree(final BrokerPool pool, final byte fileId, final short fileVersion,
172-
final boolean enableRecovery, final DefaultCacheManager cacheManager) {
173-
super(pool, fileVersion);
168+
protected AbstractBTree(final BrokerPool pool, final byte fileId, final BackingFile backingFile,
169+
final HEADER fileHeader, final boolean enableRecovery, final CacheManager cacheManager) {
170+
super(backingFile, fileHeader);
174171
this.pool = pool;
175172
this.cacheManager = cacheManager;
176173
this.fileId = fileId;
@@ -179,63 +176,19 @@ protected AbstractBTree(final BrokerPool pool, final byte fileId, final short fi
179176
} else {
180177
this.logManager = null;
181178
}
179+
this.cache = new BTreeCache<>(FileUtils.fileName(getFile()), cacheManager.getDefaultInitialSize(), 1.5, 0, Cache.CacheType.BTREE);
180+
this.cacheManager.registerCache(cache);
182181
}
183182

184183
protected boolean isRecoveryEnabled() {
185184
return logManager != null && pool.isRecoveryEnabled();
186185
}
187186

188-
public AbstractBTree(final BrokerPool pool, final byte fileId, final short fileVersion,
189-
final boolean enableRecovery,
190-
final DefaultCacheManager cacheManager, final Path file)
191-
throws DBException {
192-
this(pool, fileId, fileVersion, enableRecovery, cacheManager);
193-
setFile(file);
194-
}
195-
196-
public boolean create(final short fixedKeyLen) throws DBException {
197-
if (super.create()) {
198-
initCache();
199-
try {
200-
createRootNode(null);
201-
} catch (final IOException e) {
202-
LOG.warn("Can not create database file {}", getFile().toAbsolutePath().toString(), e);
203-
return false;
204-
}
205-
final ReentrantReadWriteLock.WriteLock fileHeaderWriteLock = fileHeader.writeLock();
206-
try {
207-
fileHeader.setFixedKeyLen(fixedKeyLen);
208-
fileHeader.write(raf);
209-
} catch (final IOException e) {
210-
throw new DBException("Error while writing file header: " + e.getMessage());
211-
} finally {
212-
fileHeaderWriteLock.unlock();
213-
}
214-
}
215-
return true;
216-
}
217-
218-
@Override
219-
public boolean open(final short expectedVersion) throws DBException {
220-
if (super.open(expectedVersion)) {
221-
initCache();
222-
return true;
223-
} else {
224-
return false;
225-
}
226-
}
227-
228187
@Override
229188
public String getLockName() {
230189
return null;
231190
}
232191

233-
protected void initCache() {
234-
this.cache = new BTreeCache<>(FileUtils.fileName(getFile()), cacheManager.getDefaultInitialSize(), 1.5,
235-
0, Cache.CacheType.BTREE);
236-
cacheManager.registerCache(cache);
237-
}
238-
239192
protected void setSplitFactor(final double factor) {
240193
if (factor > 1.0) {
241194
throw new IllegalArgumentException("splitFactor should be <= 1 > 0");
@@ -481,7 +434,7 @@ protected void setRootNode(final BTreeNode rootNode) throws IOException {
481434
final ReentrantReadWriteLock.WriteLock fileHeaderWriteLock = fileHeader.writeLock();
482435
try {
483436
fileHeader.setRootPage(rootNode.page.getPageNum());
484-
fileHeader.write(raf);
437+
fileHeader.write(backingFile.randomAccessFile);
485438
} finally {
486439
fileHeaderWriteLock.unlock();
487440
}
@@ -581,7 +534,7 @@ public void rawScan(final IndexQuery query, final BTreeCallback callback) throws
581534
final long pages = fileHeader.getTotalCount();
582535
for (int i = 1; i < pages; i++) {
583536
final Page<PAGE_HEADER> page = getPage(i);
584-
page.read(raf);
537+
page.read(backingFile.randomAccessFile);
585538
if (page.getPageHeader().getStatus() == PageStatus.LEAF) {
586539
final BTreeNode node = new BTreeNode(page, false);
587540
node.read();
@@ -632,7 +585,7 @@ private TreeInfo scanTree(final boolean removeBranches) throws IOException, Term
632585
page = node.page;
633586
} else {
634587
page = getPage(i);
635-
page.read(raf);
588+
page.read(backingFile.randomAccessFile);
636589
}
637590
if (page.getPageHeader().getStatus() == PageStatus.LEAF) {
638591
pageCount++;
@@ -660,7 +613,7 @@ private TreeInfo scanTree(final boolean removeBranches) throws IOException, Term
660613
if (removeBranches) {
661614
for (final long p : branchPages) {
662615
final Page<PAGE_HEADER> page = getPage(p);
663-
page.read(raf);
616+
page.read(backingFile.randomAccessFile);
664617
final BTreeNode node = new BTreeNode(page, false);
665618
node.read();
666619
cache.remove(node);
@@ -790,7 +743,7 @@ protected void redoCreateBTNode(final CreateBTNodeLoggable loggable) throws LogE
790743
// node is not yet loaded. Load it
791744
try {
792745
final Page<PAGE_HEADER> page = getPage(loggable.getPageNum());
793-
page.read(raf);
746+
page.read(backingFile.randomAccessFile);
794747
if ((page.getPageHeader().getStatus() == PageStatus.BRANCH ||
795748
page.getPageHeader().getStatus() == PageStatus.LEAF) &&
796749
(!page.getPageHeader().getLsn().equals(Lsn.LSN_INVALID)) &&
@@ -1266,7 +1219,7 @@ private boolean mustSplit() {
12661219
* @throws IOException if an I/O error occurs
12671220
*/
12681221
private void read() throws IOException {
1269-
final byte[] data = page.read(raf);
1222+
final byte[] data = page.read(backingFile.randomAccessFile);
12701223
final short keyLen;
12711224
final ReentrantReadWriteLock.ReadLock fileHeaderReadLock = fileHeader.readLock();
12721225
try {

0 commit comments

Comments
 (0)