Skip to content

Commit c9730dd

Browse files
committed
[refactor] Allow .dbx files to be versioned individually
1 parent 683829a commit c9730dd

File tree

12 files changed

+53
-71
lines changed

12 files changed

+53
-71
lines changed

extensions/indexes/ngram/src/org/exist/indexing/ngram/NGramIndex.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
*/
4343
public class NGramIndex extends AbstractIndex implements RawBackupSupport {
4444

45+
public static final short FILE_FORMAT_VERSION_ID = 13;
46+
4547
public final static String ID = NGramIndex.class.getName();
4648

4749
private final static Logger LOG = LogManager.getLogger(NGramIndex.class);
@@ -68,7 +70,7 @@ public void configure(BrokerPool pool, Path dataDir, Element config) throws Data
6870
@Override
6971
public void open() throws DatabaseConfigurationException {
7072
try {
71-
db = new BFile(pool, (byte) 0, false, dataFile, pool.getCacheManager(), 1.4, 0.07);
73+
db = new BFile(pool, (byte) 0, FILE_FORMAT_VERSION_ID, false, dataFile, pool.getCacheManager(), 1.4, 0.07);
7274
} catch (DBException e) {
7375
throw new DatabaseConfigurationException("Failed to create index file: " + dataFile.toAbsolutePath().toString() + ": " +
7476
e.getMessage());

extensions/indexes/sort/src/org/exist/indexing/sort/SortIndex.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class SortIndex extends AbstractIndex implements RawBackupSupport {
3434

3535
public static final String ID = SortIndex.class.getName();
3636
public static final String FILE_NAME = "sort.dbx";
37+
public final static short FILE_FORMAT_VERSION_ID = 2;
3738
public static final byte SORT_INDEX_ID = 0x10;
3839
protected static final Logger LOG = LogManager.getLogger(SortIndex.class);
3940
protected BTreeStore btree;
@@ -43,7 +44,7 @@ public void open() throws DatabaseConfigurationException {
4344
final Path file = getDataDir().resolve(FILE_NAME);
4445
LOG.debug("Creating '" + FileUtils.fileName(file) + "'...");
4546
try {
46-
btree = new BTreeStore(pool, SORT_INDEX_ID, false,
47+
btree = new BTreeStore(pool, SORT_INDEX_ID, FILE_FORMAT_VERSION_ID, false,
4748
file, pool.getCacheManager());
4849
} catch (final DBException e) {
4950
LOG.error("Failed to initialize structural index: " + e.getMessage(), e);

src/org/exist/BTreeTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
public class BTreeTest {
1616

17+
private final static byte BTREE_TEST_FILE_ID = 0x7F;
18+
private final static short BTREE_TEST_FILE_VERSION = Short.MIN_VALUE;
19+
1720
private Path file;
1821

1922
private BrokerPool pool = null;
@@ -40,7 +43,7 @@ public void create(int count) throws DBException, IOException {
4043
FileUtils.deleteQuietly(file);
4144
BTree btree = null;
4245
try {
43-
btree = new BTree(pool, (byte) 0, false, pool.getCacheManager(), file);
46+
btree = new BTree(pool, BTREE_TEST_FILE_ID, BTREE_TEST_FILE_VERSION, false, pool.getCacheManager(), file);
4447
btree.create((short) -1);
4548

4649
String prefixStr = "KEY";
@@ -65,7 +68,7 @@ public void rebuild() throws DBException, IOException, TerminatedException {
6568
BTree btree = null;
6669
try {
6770
System.out.println("Loading btree ...");
68-
btree = new BTree(pool, (byte) 0, false, pool.getCacheManager(), file);
71+
btree = new BTree(pool, BTREE_TEST_FILE_ID, BTREE_TEST_FILE_VERSION, false, pool.getCacheManager(), file);
6972
btree.open((short)-1);
7073

7174
System.out.println("Rebuilding ...");
@@ -86,7 +89,7 @@ public void read(int count) throws DBException, IOException, TerminatedException
8689
BTree btree = null;
8790
try {
8891
System.out.println("Loading btree ...");
89-
btree = new BTree(pool, (byte) 0, false, pool.getCacheManager(), file);
92+
btree = new BTree(pool, BTREE_TEST_FILE_ID, BTREE_TEST_FILE_VERSION, false, pool.getCacheManager(), file);
9093
btree.open((short)-1);
9194

9295
String prefixStr = "KEY";

src/org/exist/storage/NativeValueIndex.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public class NativeValueIndex implements ContentLoadingObserver {
118118
private final static Logger LOG = LogManager.getLogger(NativeValueIndex.class);
119119

120120
public static final String FILE_NAME = "values.dbx";
121+
public static final short FILE_FORMAT_VERSION_ID = 13;
121122
public static final String FILE_KEY_IN_CONFIG = "db-connection.values";
122123

123124
private static final double DEFAULT_VALUE_CACHE_GROWTH = 1.25;
@@ -176,7 +177,7 @@ public NativeValueIndex(final DBBroker broker, final byte id, final Path dataDir
176177
//use inheritance
177178
final Path file = dataDir.resolve(getFileName());
178179
LOG.debug("Creating '" + FileUtils.fileName(file) + "'...");
179-
nativeFile = new BFile(broker.getBrokerPool(), id, false, file,
180+
nativeFile = new BFile(broker.getBrokerPool(), id, FILE_FORMAT_VERSION_ID, false, file,
180181
broker.getBrokerPool().getCacheManager(), cacheGrowth,
181182
cacheValueThresHold);
182183
config.setProperty(getConfigKeyForFile(), nativeFile);

src/org/exist/storage/btree/BTree.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ public class BTree extends Paged implements Lockable {
161161

162162
private double splitFactor = -1;
163163

164-
protected BTree(final BrokerPool pool, final byte fileId, final boolean recoveryEnabled,
164+
protected BTree(final BrokerPool pool, final byte fileId, final short fileVersion, final boolean recoveryEnabled,
165165
final DefaultCacheManager cacheManager) throws DBException {
166-
super(pool);
166+
super(pool, fileVersion);
167167
this.pool = pool;
168168
this.cacheManager = cacheManager;
169169
this.fileId = fileId;
@@ -181,19 +181,14 @@ protected boolean isRecoveryEnabled() {
181181
return logManager.isPresent() && pool.isRecoveryEnabled();
182182
}
183183

184-
public BTree(final BrokerPool pool, final byte fileId,
184+
public BTree(final BrokerPool pool, final byte fileId, final short fileVersion,
185185
final boolean recoveryEnabled,
186186
final DefaultCacheManager cacheManager, final Path file)
187187
throws DBException {
188-
this(pool, fileId, recoveryEnabled, cacheManager);
188+
this(pool, fileId, fileVersion, recoveryEnabled, cacheManager);
189189
setFile(file);
190190
}
191191

192-
@Override
193-
public short getFileVersion() {
194-
return -1;
195-
}
196-
197192
public boolean create(final short fixedKeyLen) throws DBException {
198193
if (super.create()) {
199194
initCache();

src/org/exist/storage/btree/Paged.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,23 @@ public abstract class Paged implements AutoCloseable {
128128

129129
protected static int PAGE_SIZE = 4096;
130130

131+
protected final short fileVersion;
132+
private final FileHeader fileHeader;
133+
private final byte[] tempPageData;
134+
private final byte[] tempHeaderData;
135+
131136
private RandomAccessFile raf;
132137
private Path file;
133-
private final FileHeader fileHeader;
134138
private boolean readOnly = false;
135139
private boolean fileIsNew = false;
136-
137-
private final byte[] tempPageData;
138-
private final byte[] tempHeaderData;
139140

140-
public Paged(final BrokerPool pool) {
141+
public Paged(final BrokerPool pool, final short fileVersion) {
142+
this.fileVersion = fileVersion;
141143
this.fileHeader = createFileHeader(pool.getPageSize());
142144
this.tempPageData = new byte[fileHeader.pageSize];
143145
this.tempHeaderData = new byte[fileHeader.pageHeaderSize];
144146
}
145147

146-
public abstract short getFileVersion();
147-
148148
public final static void setPageSize(final int pageSize) {
149149
PAGE_SIZE = pageSize;
150150
}
@@ -551,7 +551,7 @@ protected final void writeValue(final long page, final Value value) throws IOExc
551551
* @author Wolfgang Meier <[email protected]>
552552
*/
553553
public abstract class FileHeader {
554-
private short versionId;
554+
private short version;
555555

556556
private boolean dirty = false;
557557
private long firstFreePage = Page.NO_PAGE;
@@ -573,7 +573,7 @@ public FileHeader(final long pageCount, final int pageSize) {
573573
this.pageCount = pageCount;
574574
this.totalCount = pageCount;
575575
this.headerSize = (short) pageSize;
576-
this.versionId = getFileVersion();
576+
this.version = fileVersion;
577577
this.buf = new byte[headerSize];
578578
calculateWorkSize();
579579
}
@@ -681,7 +681,7 @@ public final int getWorkSize() {
681681
}
682682

683683
public final short getVersion() {
684-
return versionId;
684+
return version;
685685
}
686686

687687
/**
@@ -710,7 +710,7 @@ public final synchronized void read() throws IOException {
710710
}
711711

712712
public int read(final byte[] buf) throws IOException {
713-
versionId = ByteConversion.byteToShort(buf, OFFSET_VERSION_ID);
713+
version = ByteConversion.byteToShort(buf, OFFSET_VERSION_ID);
714714
headerSize = ByteConversion.byteToShort(buf, OFFSET_HEADER_SIZE);
715715
pageSize = ByteConversion.byteToInt(buf, OFFSET_PAGE_SIZE);
716716
pageCount = ByteConversion.byteToLong(buf, OFFSET_PAGE_COUNT);
@@ -724,7 +724,7 @@ public int read(final byte[] buf) throws IOException {
724724
}
725725

726726
public int write(final byte[] buf) throws IOException {
727-
ByteConversion.shortToByte(versionId, buf, OFFSET_VERSION_ID);
727+
ByteConversion.shortToByte(version, buf, OFFSET_VERSION_ID);
728728
ByteConversion.shortToByte(headerSize, buf, OFFSET_HEADER_SIZE);
729729
ByteConversion.intToByte(pageSize, buf, OFFSET_PAGE_SIZE);
730730
ByteConversion.longToByte(pageCount, buf, OFFSET_PAGE_COUNT);

src/org/exist/storage/dom/DOMFile.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public class DOMFile extends BTree implements Lockable {
189189
private final AddValueLoggable addValueLog = new AddValueLoggable();
190190

191191
public DOMFile(final BrokerPool pool, final byte id, final Path dataDir, final Configuration config) throws DBException {
192-
super(pool, id, true, pool.getCacheManager());
192+
super(pool, id, FILE_FORMAT_VERSION_ID, true, pool.getCacheManager());
193193
lock = new ReentrantReadWriteLock(getFileName());
194194
fileHeader = (BTreeFileHeader)getFileHeader();
195195
fileHeader.setPageCount(0);
@@ -291,10 +291,6 @@ protected final Cache getPageBuffer() {
291291
return dataCache;
292292
}
293293

294-
@Override
295-
public short getFileVersion() {
296-
return FILE_FORMAT_VERSION_ID;
297-
}
298294

299295
@Override
300296
public boolean create() throws DBException {

src/org/exist/storage/index/BFile.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@
8484
public class BFile extends BTree {
8585

8686
protected final static Logger LOGSTATS = LogManager.getLogger( NativeBroker.EXIST_STATISTICS_LOGGER );
87-
88-
public final static short FILE_FORMAT_VERSION_ID = 13;
8987

9088
public final static long UNKNOWN_ADDRESS = -1;
9189

@@ -143,9 +141,9 @@ public class BFile extends BTree {
143141
protected final int maxValueSize;
144142

145143

146-
public BFile(final BrokerPool pool, final byte fileId, final boolean recoveryEnabled, final Path file, final DefaultCacheManager cacheManager,
144+
public BFile(final BrokerPool pool, final byte fileId, final short fileVersion, final boolean recoveryEnabled, final Path file, final DefaultCacheManager cacheManager,
147145
final double cacheGrowth, final double thresholdData) throws DBException {
148-
super(pool, fileId, recoveryEnabled, cacheManager, file);
146+
super(pool, fileId, fileVersion, recoveryEnabled, cacheManager, file);
149147
fileHeader = (BFileHeader) getFileHeader();
150148
dataCache = new LRUCache<>(FileUtils.fileName(file), 64, cacheGrowth, thresholdData, CacheManager.DATA_CACHE);
151149
cacheManager.registerCache(dataCache);
@@ -154,7 +152,7 @@ public BFile(final BrokerPool pool, final byte fileId, final boolean recoveryEna
154152
maxValueSize = fileHeader.getWorkSize() / 2;
155153

156154
if(exists()) {
157-
open();
155+
open(fileVersion);
158156
} else {
159157
if (LOG.isDebugEnabled()) {
160158
LOG.debug("Creating data file: " + FileUtils.fileName(getFile()));
@@ -163,14 +161,6 @@ public BFile(final BrokerPool pool, final byte fileId, final boolean recoveryEna
163161
}
164162
}
165163

166-
/**
167-
* @return file version
168-
*/
169-
@Override
170-
public short getFileVersion() {
171-
return FILE_FORMAT_VERSION_ID;
172-
}
173-
174164
/**
175165
* Returns the Lock object responsible for this BFile.
176166
*
@@ -602,10 +592,6 @@ public ArrayList<Value> getValues() throws IOException, BTreeException, Terminat
602592
return cb.getValues();
603593
}
604594

605-
public boolean open() throws DBException {
606-
return super.open(FILE_FORMAT_VERSION_ID);
607-
}
608-
609595
/**
610596
* Put data under given key.
611597
*

src/org/exist/storage/index/BTreeStore.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212

1313
public class BTreeStore extends BTree {
1414

15-
public final static short FILE_FORMAT_VERSION_ID = 2;
15+
private final Lock lock;
1616

17-
protected Lock lock = null;
18-
19-
public BTreeStore(final BrokerPool pool, final byte fileId, final boolean recoverEnabled, final Path file, final DefaultCacheManager cacheManager) throws DBException {
20-
super(pool, fileId, recoverEnabled, cacheManager, file);
21-
lock = new ReentrantReadWriteLock(FileUtils.fileName(file));
17+
public BTreeStore(final BrokerPool pool, final byte fileId, final short fileVersion, final boolean recoverEnabled, final Path file, final DefaultCacheManager cacheManager) throws DBException {
18+
super(pool, fileId, fileVersion, recoverEnabled, cacheManager, file);
19+
this.lock = new ReentrantReadWriteLock(FileUtils.fileName(file));
2220

2321
if(exists()) {
24-
open(FILE_FORMAT_VERSION_ID);
22+
open(fileVersion);
2523
} else {
2624
if (LOG.isDebugEnabled()) {
2725
LOG.debug("Creating data file: " + FileUtils.fileName(getFile()));
@@ -35,8 +33,4 @@ public BTreeStore(final BrokerPool pool, final byte fileId, final boolean recove
3533
public Lock getLock() {
3634
return lock;
3735
}
38-
39-
public short getFileVersion() {
40-
return FILE_FORMAT_VERSION_ID;
41-
}
4236
}

src/org/exist/storage/index/CollectionStore.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
public class CollectionStore extends BFile {
2929

30+
public static final short FILE_FORMAT_VERSION_ID = 13;
31+
3032
public static final String FILE_NAME = "collections.dbx";
3133
public static final String FILE_KEY_IN_CONFIG = "db-connection.collections";
3234

@@ -49,7 +51,7 @@ public class CollectionStore extends BFile {
4951
* @throws DBException
5052
*/
5153
public CollectionStore(BrokerPool pool, byte id, Path dataDir, Configuration config) throws DBException {
52-
super(pool, id, true, dataDir.resolve(getFileName()),
54+
super(pool, id, FILE_FORMAT_VERSION_ID, true, dataDir.resolve(getFileName()),
5355
pool.getCacheManager(), 1.25, 0.03);
5456
config.setProperty(getConfigKeyForFile(), this);
5557
}

0 commit comments

Comments
 (0)