Skip to content

Commit dafda74

Browse files
committed
[refactor] Allow .dbx files to be versioned individually
1 parent 9a877a3 commit dafda74

File tree

12 files changed

+51
-69
lines changed

12 files changed

+51
-69
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
@@ -35,6 +35,7 @@ public class SortIndex extends AbstractIndex implements RawBackupSupport {
3535

3636
public static final String ID = SortIndex.class.getName();
3737
public static final String FILE_NAME = "sort.dbx";
38+
public final static short FILE_FORMAT_VERSION_ID = 2;
3839
public static final byte SORT_INDEX_ID = 0x10;
3940
protected static final Logger LOG = LogManager.getLogger(SortIndex.class);
4041
protected BTreeStore btree;
@@ -44,7 +45,7 @@ public void open() throws DatabaseConfigurationException {
4445
final Path file = getDataDir().resolve(FILE_NAME);
4546
LOG.debug("Creating '" + FileUtils.fileName(file) + "'...");
4647
try {
47-
btree = new BTreeStore(pool, SORT_INDEX_ID, false,
48+
btree = new BTreeStore(pool, SORT_INDEX_ID, FILE_FORMAT_VERSION_ID, false,
4849
file, pool.getCacheManager());
4950
} catch (final DBException e) {
5051
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
@@ -119,6 +119,7 @@ public class NativeValueIndex implements ContentLoadingObserver {
119119
private final static Logger LOG = LogManager.getLogger(NativeValueIndex.class);
120120

121121
public static final String FILE_NAME = "values.dbx";
122+
public static final short FILE_FORMAT_VERSION_ID = 13;
122123
public static final String FILE_KEY_IN_CONFIG = "db-connection.values";
123124

124125
private static final double DEFAULT_VALUE_CACHE_GROWTH = 1.25;
@@ -180,7 +181,7 @@ public NativeValueIndex(final DBBroker broker, final byte id, final Path dataDir
180181
//use inheritance
181182
final Path file = dataDir.resolve(getFileName());
182183
LOG.debug("Creating '" + FileUtils.fileName(file) + "'...");
183-
nativeFile = new BFile(broker.getBrokerPool(), id, false, file,
184+
nativeFile = new BFile(broker.getBrokerPool(), id, FILE_FORMAT_VERSION_ID, false, file,
184185
broker.getBrokerPool().getCacheManager(), cacheGrowth,
185186
cacheValueThresHold);
186187
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
@@ -160,9 +160,9 @@ public class BTree extends Paged implements Lockable {
160160

161161
private double splitFactor = -1;
162162

163-
protected BTree(final BrokerPool pool, final byte fileId, final boolean recoveryEnabled,
163+
protected BTree(final BrokerPool pool, final byte fileId, final short fileVersion, final boolean recoveryEnabled,
164164
final DefaultCacheManager cacheManager) throws DBException {
165-
super(pool);
165+
super(pool, fileVersion);
166166
this.pool = pool;
167167
this.cacheManager = cacheManager;
168168
this.fileId = fileId;
@@ -180,19 +180,14 @@ protected boolean isRecoveryEnabled() {
180180
return logManager.isPresent() && pool.isRecoveryEnabled();
181181
}
182182

183-
public BTree(final BrokerPool pool, final byte fileId,
183+
public BTree(final BrokerPool pool, final byte fileId, final short fileVersion,
184184
final boolean recoveryEnabled,
185185
final DefaultCacheManager cacheManager, final Path file)
186186
throws DBException {
187-
this(pool, fileId, recoveryEnabled, cacheManager);
187+
this(pool, fileId, fileVersion, recoveryEnabled, cacheManager);
188188
setFile(file);
189189
}
190190

191-
@Override
192-
public short getFileVersion() {
193-
return -1;
194-
}
195-
196191
public boolean create(final short fixedKeyLen) throws DBException {
197192
if (super.create()) {
198193
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
@@ -187,7 +187,7 @@ public class DOMFile extends BTree implements Lockable {
187187
private final AddValueLoggable addValueLog = new AddValueLoggable();
188188

189189
public DOMFile(final BrokerPool pool, final byte id, final Path dataDir, final Configuration config) throws DBException {
190-
super(pool, id, true, pool.getCacheManager());
190+
super(pool, id, FILE_FORMAT_VERSION_ID, true, pool.getCacheManager());
191191
this.lockManager = pool.getLockManager();
192192
fileHeader = (BTreeFileHeader)getFileHeader();
193193
fileHeader.setPageCount(0);
@@ -289,10 +289,6 @@ protected final Cache getPageBuffer() {
289289
return dataCache;
290290
}
291291

292-
@Override
293-
public short getFileVersion() {
294-
return FILE_FORMAT_VERSION_ID;
295-
}
296292

297293
@Override
298294
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
@@ -83,8 +83,6 @@
8383
public class BFile extends BTree {
8484

8585
protected final static Logger LOGSTATS = LogManager.getLogger( NativeBroker.EXIST_STATISTICS_LOGGER );
86-
87-
public final static short FILE_FORMAT_VERSION_ID = 13;
8886

8987
public final static long UNKNOWN_ADDRESS = -1;
9088

@@ -142,9 +140,9 @@ public class BFile extends BTree {
142140
protected final int maxValueSize;
143141

144142

145-
public BFile(final BrokerPool pool, final byte fileId, final boolean recoveryEnabled, final Path file, final DefaultCacheManager cacheManager,
143+
public BFile(final BrokerPool pool, final byte fileId, final short fileVersion, final boolean recoveryEnabled, final Path file, final DefaultCacheManager cacheManager,
146144
final double cacheGrowth, final double thresholdData) throws DBException {
147-
super(pool, fileId, recoveryEnabled, cacheManager, file);
145+
super(pool, fileId, fileVersion, recoveryEnabled, cacheManager, file);
148146
lockManager = pool.getLockManager();
149147
fileHeader = (BFileHeader) getFileHeader();
150148
dataCache = new LRUCache<>(FileUtils.fileName(file), 64, cacheGrowth, thresholdData, Cache.CacheType.DATA);
@@ -153,7 +151,7 @@ public BFile(final BrokerPool pool, final byte fileId, final boolean recoveryEna
153151
maxValueSize = fileHeader.getWorkSize() / 2;
154152

155153
if(exists()) {
156-
open();
154+
open(fileVersion);
157155
} else {
158156
if (LOG.isDebugEnabled()) {
159157
LOG.debug("Creating data file: " + FileUtils.fileName(getFile()));
@@ -162,14 +160,6 @@ public BFile(final BrokerPool pool, final byte fileId, final boolean recoveryEna
162160
}
163161
}
164162

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

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

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010

1111
public class BTreeStore extends BTree {
1212

13-
public final static short FILE_FORMAT_VERSION_ID = 2;
14-
15-
public BTreeStore(final BrokerPool pool, final byte fileId, final boolean recoverEnabled, final Path file, final DefaultCacheManager cacheManager) throws DBException {
16-
super(pool, fileId, recoverEnabled, cacheManager, file);
13+
public BTreeStore(final BrokerPool pool, final byte fileId, final short fileVersion, final boolean recoverEnabled, final Path file, final DefaultCacheManager cacheManager) throws DBException {
14+
super(pool, fileId, fileVersion, recoverEnabled, cacheManager, file);
1715

1816
if(exists()) {
19-
open(FILE_FORMAT_VERSION_ID);
17+
open(fileVersion);
2018
} else {
2119
if (LOG.isDebugEnabled()) {
2220
LOG.debug("Creating data file: " + FileUtils.fileName(getFile()));
@@ -30,8 +28,4 @@ public BTreeStore(final BrokerPool pool, final byte fileId, final boolean recove
3028
public String getLockName() {
3129
return FileUtils.fileName(getFile());
3230
}
33-
34-
public short getFileVersion() {
35-
return FILE_FORMAT_VERSION_ID;
36-
}
3731
}

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)