Skip to content

Commit 982676d

Browse files
committed
fixup! WIP Refactoring and cleanup of BTree storage classes
1 parent 372e813 commit 982676d

27 files changed

+736
-682
lines changed

exist-core/src/main/java/org/exist/management/Cache.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
/*
2+
* Copyright (C) 2014 Evolved Binary Ltd
3+
*
4+
* Changes made by Evolved Binary are proprietary and are not Open Source.
5+
*
6+
* NOTE: Parts of this file contain code from The eXist-db Authors.
7+
* The original license header is included below.
8+
*
9+
* ----------------------------------------------------------------------------
10+
*
211
* eXist-db Open Source Native XML Database
312
* Copyright (C) 2001 The eXist-db Authors
413
*

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,11 @@ void release(final DBBroker broker) {
14011401
//TODO : use a "clean" dedicated method (we have some below) ?
14021402
if(syncRequired) {
14031403
//Note that the broker is not yet really inactive ;-)
1404-
sync(broker, syncEvent);
1404+
try {
1405+
sync(broker, syncEvent);
1406+
} catch (final EXistException e) {
1407+
LOG.error(e.getMessage(), e);
1408+
}
14051409
this.syncRequired = false;
14061410
this.checkpoint = false;
14071411
}
@@ -1414,7 +1418,7 @@ void release(final DBBroker broker) {
14141418
}
14151419
}
14161420

1417-
public DBBroker enterServiceMode(final Subject user) throws PermissionDeniedException {
1421+
public DBBroker enterServiceMode(final Subject user) throws EXistException, PermissionDeniedException {
14181422
if(!user.hasDbaRole()) {
14191423
throw new PermissionDeniedException("Only users of group dba can switch the db to service mode");
14201424
}
@@ -1474,8 +1478,10 @@ public long getLastMajorSync() {
14741478
*
14751479
* @param broker A broker responsible for executing the job
14761480
* @param syncEvent One of {@link org.exist.storage.sync.Sync}
1481+
*
1482+
* @throws EXistException if an error occurs during synchronization.
14771483
*/
1478-
public void sync(final DBBroker broker, final Sync syncEvent) {
1484+
public void sync(final DBBroker broker, final Sync syncEvent) throws EXistException {
14791485

14801486
/**
14811487
* Database Systems - The Complete Book (Second edition)
@@ -1519,6 +1525,8 @@ public void sync(final DBBroker broker, final Sync syncEvent) {
15191525
// LOG.debug("Minor sync");
15201526
}
15211527
//TODO : touch this.syncEvent and syncRequired ?
1528+
} catch (final IOException e) {
1529+
throw new EXistException(e.getMessage(), e);
15221530
} finally {
15231531
broker.popSubject();
15241532
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
/*
2+
* Copyright (C) 2014 Evolved Binary Ltd
3+
*
4+
* Changes made by Evolved Binary are proprietary and are not Open Source.
5+
*
6+
* NOTE: Parts of this file contain code from The eXist-db Authors.
7+
* The original license header is included below.
8+
*
9+
* ----------------------------------------------------------------------------
10+
*
211
* eXist-db Open Source Native XML Database
312
* Copyright (C) 2001 The eXist-db Authors
413
*
@@ -23,6 +32,8 @@
2332

2433
import org.exist.storage.cache.Cache;
2534

35+
import java.io.IOException;
36+
2637
public interface CacheManager {
2738

2839
/**
@@ -46,23 +57,23 @@ public interface CacheManager {
4657
* @param cache cache to grow
4758
* @return new cache size, or -1 if no free pages available.
4859
*/
49-
int requestMem(Cache cache);
60+
int requestMem(Cache cache) throws IOException;
5061

5162
/**
5263
* Called from the global major sync event to check if caches can
5364
* be shrinked.
5465
*
5566
* If shrinked, the cache will be reset to the default initial cache size.
5667
*/
57-
void checkCaches();
68+
void checkCaches() throws IOException;
5869

5970
/**
6071
* Called from the global minor sync event to check if a smaller
6172
* cache wants to be resized. If a huge cache is available, the method
6273
* might decide to shrink this cache by a certain amount to make
6374
* room for the smaller cache to grow.
6475
*/
65-
void checkDistribution();
76+
void checkDistribution() throws IOException;
6677

6778
/**
6879
* @return Maximum size of all Caches (unit of measurement is implementation defined)

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
/*
2+
* Copyright (C) 2014 Evolved Binary Ltd
3+
*
4+
* Changes made by Evolved Binary are proprietary and are not Open Source.
5+
*
6+
* NOTE: Parts of this file contain code from The eXist-db Authors.
7+
* The original license header is included below.
8+
*
9+
* ----------------------------------------------------------------------------
10+
*
211
* eXist-db Open Source Native XML Database
312
* Copyright (C) 2001 The eXist-db Authors
413
*
@@ -30,6 +39,7 @@
3039
import org.exist.util.Configuration;
3140
import org.exist.util.DatabaseConfigurationException;
3241

42+
import java.io.IOException;
3343
import java.text.NumberFormat;
3444

3545
import java.util.ArrayList;
@@ -179,8 +189,7 @@ public void deregisterCache( Cache cache )
179189

180190

181191
@Override
182-
public int requestMem( Cache cache )
183-
{
192+
public int requestMem( Cache cache ) throws IOException {
184193
if( currentPageCount >= totalPageCount ) {
185194

186195
if( cache.getBuffers() < maxCacheSize ) {
@@ -241,8 +250,7 @@ public int requestMem( Cache cache )
241250
* If shrinked, the cache will be reset to the default initial cache size.
242251
*/
243252
@Override
244-
public void checkCaches()
245-
{
253+
public void checkCaches() throws IOException {
246254
final int minSize = (int)( totalPageCount * MIN_SHRINK_FACTOR );
247255
Cache cache;
248256
int load;
@@ -272,8 +280,7 @@ public void checkCaches()
272280

273281

274282
@Override
275-
public void checkDistribution()
276-
{
283+
public void checkDistribution() throws IOException {
277284
if( lastRequest == null ) {
278285
return;
279286
}

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,6 @@ protected AbstractBTree(final BrokerPool pool, final byte fileId, final short fi
173173
this.pool = pool;
174174
this.cacheManager = cacheManager;
175175
this.fileId = fileId;
176-
this.fileHeader.setPageCount(0);
177-
this.fileHeader.setTotalCount(0);
178176
if (recoveryEnabled && pool.isRecoveryEnabled()) {
179177
this.logManager = pool.getJournalManager();
180178
} else {
@@ -575,7 +573,7 @@ public void rawScan(final IndexQuery query, final BTreeCallback callback) throws
575573
TerminatedException {
576574
final ReentrantReadWriteLock.ReadLock fileHeaderReadLock = fileHeader.readLock();
577575
try {
578-
final long pages = fileHeader.totalCount;
576+
final long pages = fileHeader.getTotalCount();
579577
for (int i = 1; i < pages; i++) {
580578
final Page<PAGE_HEADER> page = getPage(i);
581579
page.read(raf);
@@ -619,7 +617,7 @@ private TreeInfo scanTree(final boolean removeBranches) throws IOException, Term
619617
int pageCount = 0;
620618
final ReentrantReadWriteLock.ReadLock fileHeaderReadLock = fileHeader.readLock();
621619
try {
622-
final long pages = fileHeader.totalCount;
620+
final long pages = fileHeader.getTotalCount();
623621
for (long i = 0; i < pages; i++) {
624622
// first check if page is in cache. if yes, use it.
625623
BTreeNode node = cache.get(i);
@@ -978,7 +976,7 @@ public void setParent(final BTreeNode parent) {
978976
if (parent != null) {
979977
pageHeader.setParentPage(parent.page.getPageNum());
980978
} else {
981-
pageHeader.setPareantPage(Page.NO_PAGE);
979+
pageHeader.setParentPage(Page.NO_PAGE);
982980
}
983981
saved = false;
984982
}
@@ -1035,17 +1033,13 @@ public boolean isInnerPage() {
10351033
}
10361034

10371035
@Override
1038-
public boolean sync(final boolean syncJournal) {
1039-
if(isDirty()) {
1040-
try {
1041-
write();
1042-
if (isRecoveryEnabled() && syncJournal) {
1043-
logManager.ifPresent(l -> l.flush(true, false));
1044-
}
1045-
return true;
1046-
} catch (final IOException e) {
1047-
LOG.error("IO error while writing page: {}", page.getPageNum(), e);
1036+
public boolean sync(final boolean syncJournal) throws IOException {
1037+
if (isDirty()) {
1038+
write();
1039+
if (isRecoveryEnabled() && syncJournal) {
1040+
logManager.ifPresent(l -> l.flush(true, false));
10481041
}
1042+
return true;
10491043
}
10501044
return false;
10511045
}
@@ -1344,7 +1338,7 @@ private void write() throws IOException {
13441338
final int keyLen;
13451339
final ReentrantReadWriteLock.ReadLock fileHeaderReadLock = fileHeader.readLock();
13461340
try {
1347-
workSize = fileHeader.workSize;
1341+
workSize = fileHeader.getWorkSize();
13481342
keyLen = fileHeader.getFixedKeyLen();
13491343
} finally {
13501344
fileHeaderReadLock.unlock();
@@ -1510,7 +1504,7 @@ private long addValue(final Txn transaction, final Value value, final long point
15101504
final int workSize;
15111505
final ReentrantReadWriteLock.ReadLock fileHeaderReadLock = fileHeader.readLock();
15121506
try {
1513-
workSize = fileHeader.workSize;
1507+
workSize = fileHeader.getWorkSize();
15141508
} finally {
15151509
fileHeaderReadLock.unlock();
15161510
}
@@ -1554,7 +1548,7 @@ private void promoteValue(final Txn transaction, final Value value, final BTreeN
15541548
final int workSize;
15551549
final ReentrantReadWriteLock.ReadLock fileHeaderReadLock = fileHeader.readLock();
15561550
try {
1557-
workSize = fileHeader.workSize;
1551+
workSize = fileHeader.getWorkSize();
15581552
} finally {
15591553
fileHeaderReadLock.unlock();
15601554
}

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

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,6 @@
106106
*/
107107
public abstract class AbstractPagedFile<HEADER extends AbstractPagedFileHeader, PAGE_HEADER extends AbstractPageHeader> implements PagedFile<HEADER, PAGE_HEADER>, AutoCloseable {
108108

109-
public static final int LENGTH_VERSION_ID = 2; //sizeof short
110-
public static final int LENGTH_HEADER_SIZE = 2; //sizeof short
111-
public static final int LENGTH_PAGE_COUNT = 8; //sizeof long
112-
public static final int LENGTH_PAGE_SIZE = 4; //sizeof int
113-
public static final int LENGTH_TOTAL_COUNT = 8; //sizeof long
114-
public static final int LENGTH_FIRST_FREE_PAGE = 8; //sizeof long
115-
public static final int LENGTH_LAST_FREE_PAGE = 8; //sizeof long
116-
public static final int LENGTH_PAGE_HEADER_SIZE = 1; //sizeof byte
117-
public static final int LENGTH_MAX_KEY_SIZE = 2; //sizeof short
118-
public static final int LENGTH_RECORD_COUNT = 8; //sizeof long
119-
120-
public static final int OFFSET_VERSION_ID = 0;
121-
public static final int OFFSET_HEADER_SIZE = OFFSET_VERSION_ID + LENGTH_VERSION_ID; //2
122-
public static final int OFFSET_PAGE_SIZE = OFFSET_HEADER_SIZE + LENGTH_HEADER_SIZE; //4
123-
public static final int OFFSET_PAGE_COUNT = OFFSET_PAGE_SIZE + LENGTH_PAGE_SIZE; //8
124-
public static final int OFFSET_TOTAL_COUNT = OFFSET_PAGE_COUNT + LENGTH_PAGE_COUNT; //16
125-
public static final int OFFSET_FIRST_FREE_PAGE = OFFSET_TOTAL_COUNT + LENGTH_TOTAL_COUNT; //24
126-
public static final int OFFSET_LAST_FREE_PAGE = OFFSET_FIRST_FREE_PAGE + LENGTH_FIRST_FREE_PAGE; //32
127-
public static final int OFFSET_PAGE_HEADER_SIZE = OFFSET_LAST_FREE_PAGE + LENGTH_LAST_FREE_PAGE; //40
128-
public static final int OFFSET_MAX_KEY_SIZE = OFFSET_PAGE_HEADER_SIZE + LENGTH_PAGE_HEADER_SIZE; //41
129-
public static final int OFFSET_RECORD_COUNT = OFFSET_MAX_KEY_SIZE + LENGTH_MAX_KEY_SIZE; //43
130-
public static final int OFFSET_REMAINDER = OFFSET_RECORD_COUNT + LENGTH_RECORD_COUNT; //51
131-
132109
protected static final Logger LOG = LogManager.getLogger(AbstractPagedFile.class);
133110

134111
protected static int PAGE_SIZE = 4096;
@@ -147,8 +124,8 @@ public abstract class AbstractPagedFile<HEADER extends AbstractPagedFileHeader,
147124
public AbstractPagedFile(final BrokerPool pool, final short fileVersion) {
148125
this.fileVersion = fileVersion;
149126
this.fileHeader = createFileHeader(pool.getPageSize());
150-
this.tempPageData = new byte[fileHeader.pageSize];
151-
this.tempHeaderData = new byte[fileHeader.pageHeaderSize];
127+
this.tempPageData = new byte[fileHeader.getPageSize()];
128+
this.tempHeaderData = new byte[fileHeader.getPageHeaderSize()];
152129
}
153130

154131
public final static void setPageSize(final int pageSize) {
@@ -263,7 +240,7 @@ public final Path getFile() {
263240
// }
264241

265242
protected final Page<PAGE_HEADER> getFreePage() throws IOException {
266-
return getFreePage(fileHeader,true);
243+
return getFreePage(true);
267244
}
268245

269246
/**
@@ -278,24 +255,24 @@ protected final Page<PAGE_HEADER> getFreePage() throws IOException {
278255
*
279256
* @throws IOException if an I/O error occurs
280257
*/
281-
private static Page<PAGE_HEADER> getFreePage(final AbstractPagedFileHeader fileHeader, final boolean reuseDeleted) throws IOException {
258+
protected Page<PAGE_HEADER> getFreePage(final boolean reuseDeleted) throws IOException {
282259
final Page<PAGE_HEADER> page;
283260
final ReentrantReadWriteLock.WriteLock fileHeaderWriteLock = fileHeader.writeLock();
284261
try {
285-
long pageNum = fileHeader.firstFreePage;
262+
long pageNum = fileHeader.getFirstFreePage();
286263
if (reuseDeleted && pageNum != Page.NO_PAGE) {
287264

288265
// Steal a deleted page
289266
page = new Page<>(tempPageData, tempHeaderData, pageNum);
290267
page.read(raf);
291268

292-
fileHeader.firstFreePage = page.header.nextPage;
293-
if (fileHeader.firstFreePage == Page.NO_PAGE) {
269+
fileHeader.setFirstFreePage(page.header.getNextPage());
270+
if (fileHeader.getFirstFreePage() == Page.NO_PAGE) {
294271
fileHeader.setLastFreePage(Page.NO_PAGE);
295272
}
296273
} else {
297274
// Grow the file
298-
pageNum = fileHeader.totalCount;
275+
pageNum = fileHeader.getTotalCount();
299276
if(pageNum == Integer.MAX_VALUE) {
300277
throw new IOException("page limit reached: " + pageNum);
301278
}
@@ -385,7 +362,7 @@ public boolean open(final short requiredVersion) throws DBException {
385362
public void printFreeSpaceList(final PrintStream out) throws IOException {
386363
final ReentrantReadWriteLock.ReadLock fileHeaderReadLock = fileHeader.readLock();
387364
try {
388-
long pageNum = fileHeader.firstFreePage;
365+
long pageNum = fileHeader.getFirstFreePage();
389366
out.println("first free page: " + pageNum);
390367
Page<PAGE_HEADER> next;
391368
out.println("free pages for " + FileUtils.fileName(getFile()));
@@ -450,11 +427,11 @@ protected void unlinkPages(final Page<PAGE_HEADER> page) throws IOException {
450427
page.header.setLsn(Lsn.LSN_INVALID);
451428
final ReentrantReadWriteLock.WriteLock fileHeaderWriteLock = fileHeader.writeLock();
452429
try {
453-
if (fileHeader.firstFreePage == Page.NO_PAGE) {
430+
if (fileHeader.getFirstFreePage() == Page.NO_PAGE) {
454431
fileHeader.setFirstFreePage(page.pageNum);
455432
page.header.updateNextPage(Page.NO_PAGE);
456433
} else {
457-
final long firstFreePage = fileHeader.firstFreePage;
434+
final long firstFreePage = fileHeader.getFirstFreePage();
458435
fileHeader.setFirstFreePage(page.pageNum);
459436
page.header.updateNextPage(firstFreePage);
460437
}
@@ -478,8 +455,8 @@ protected final void unlinkPages(final long pageNum) throws IOException {
478455
}
479456

480457
/**
481-
* Clears the {@link AbstractPagedFileHeader#firstFreePage} and
482-
* {@link AbstractPagedFileHeader#lastFreePage}.
458+
* Clears the {@link AbstractPagedFileHeader#getFirstFreePage()} and
459+
* {@link AbstractPagedFileHeader#getLastFreePage()}.
483460
*
484461
* This is needed in recovery, as the free page list
485462
* may have become corrupted.
@@ -561,7 +538,7 @@ protected final void writeValue(final Page<PAGE_HEADER> page, final byte[] data)
561538
final ReentrantReadWriteLock.ReadLock fileHeaderReadLock = fileHeader.readLock();
562539
final int workSize;
563540
try {
564-
workSize = fileHeader.workSize;
541+
workSize = fileHeader.getWorkSize();
565542
} finally {
566543
fileHeaderReadLock.unlock();
567544
}

0 commit comments

Comments
 (0)