summaryrefslogtreecommitdiff
path: root/platform/util/src/com/intellij/util/io/IntToIntBtree.java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/util/src/com/intellij/util/io/IntToIntBtree.java')
-rw-r--r--platform/util/src/com/intellij/util/io/IntToIntBtree.java85
1 files changed, 48 insertions, 37 deletions
diff --git a/platform/util/src/com/intellij/util/io/IntToIntBtree.java b/platform/util/src/com/intellij/util/io/IntToIntBtree.java
index b17fc6cc5c6c..475f3cb67a94 100644
--- a/platform/util/src/com/intellij/util/io/IntToIntBtree.java
+++ b/platform/util/src/com/intellij/util/io/IntToIntBtree.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ import java.util.Arrays;
* Date: 7/12/11
* Time: 1:34 PM
*/
-class IntToIntBtree {
+public class IntToIntBtree {
public static int version() {
return 4 + (IOUtil.ourByteBuffersUseNativeByteOrder ? 0xFF : 0);
}
@@ -55,10 +55,10 @@ class IntToIntBtree {
private boolean hasZeroKey;
private int zeroKeyValue;
- private boolean isLarge = true;
+ private final boolean isLarge = true;
private final ResizeableMappedFile storage;
private final boolean offloadToSiblingsBeforeSplit = false;
- private boolean indexNodeIsHashTable = true;
+ private final boolean indexNodeIsHashTable = true;
final int metaDataLeafPageLength;
final int hashPageCapacity;
@@ -66,8 +66,8 @@ class IntToIntBtree {
private TIntIntHashMap myCachedMappings;
private final int myCachedMappingsSize;
- public IntToIntBtree(int _pageSize, File file, PagedFileStorage.StorageLockContext storageLockContext, boolean initial) throws IOException {
- pageSize = _pageSize;
+ public IntToIntBtree(int pageSize, @NotNull File file, @NotNull PagedFileStorage.StorageLockContext storageLockContext, boolean initial) throws IOException {
+ this.pageSize = pageSize;
if (initial) {
FileUtil.delete(file);
@@ -82,13 +82,12 @@ class IntToIntBtree {
root.setIndexLeaf(true);
}
- int i = (pageSize - BtreePage.RESERVED_META_PAGE_LEN) / BtreeIndexNodeView.INTERIOR_SIZE - 1;
+ int i = (this.pageSize - BtreePage.RESERVED_META_PAGE_LEN) / BtreeIndexNodeView.INTERIOR_SIZE - 1;
assert i < Short.MAX_VALUE && i % 2 == 0;
maxInteriorNodes = (short)i;
maxLeafNodes = (short)i;
- int metaPageLen = BtreePage.RESERVED_META_PAGE_LEN;
-
+ int metaPageLen;
if (indexNodeIsHashTable) {
++i;
while(!isPrime(i)) i -= 2;
@@ -97,8 +96,10 @@ class IntToIntBtree {
metaPageLen = BtreePage.RESERVED_META_PAGE_LEN;
i = (int)(hashPageCapacity * 0.9);
if ((i & 1) == 1) ++i;
- } else {
+ }
+ else {
hashPageCapacity = -1;
+ metaPageLen = BtreePage.RESERVED_META_PAGE_LEN;
}
metaDataLeafPageLength = metaPageLen;
@@ -108,20 +109,18 @@ class IntToIntBtree {
if (hasCachedMappings) {
myCachedMappings = new TIntIntHashMap(myCachedMappingsSize = 4 * maxLeafNodes);
- } else {
+ }
+ else {
myCachedMappings = null;
myCachedMappingsSize = -1;
}
}
- public void persistVars(BtreeDataStorage storage, boolean toDisk) {
- if (toDisk) {
- storage.persistInt(0, height | (hasZeroKey ? HAS_ZERO_KEY_MASK :0), true);
- } else {
- int i = storage.persistInt(0, 0, false);
- hasZeroKey = (i & HAS_ZERO_KEY_MASK) != 0;
- height = i & ~HAS_ZERO_KEY_MASK;
- }
+ // return total number of bytes needed for storing information
+ public int persistVars(@NotNull BtreeDataStorage storage, boolean toDisk) {
+ int i = storage.persistInt(0, height | (hasZeroKey ? HAS_ZERO_KEY_MASK :0), toDisk);
+ hasZeroKey = (i & HAS_ZERO_KEY_MASK) != 0;
+ height = i & ~HAS_ZERO_KEY_MASK;
pagesCount = storage.persistInt(4, pagesCount, toDisk);
movedMembersCount = storage.persistInt(8, movedMembersCount, toDisk);
@@ -132,9 +131,10 @@ class IntToIntBtree {
hashedPagesCount = storage.persistInt(28, hashedPagesCount, toDisk);
root.setAddress(storage.persistInt(32, root.address, toDisk));
zeroKeyValue = storage.persistInt(36, zeroKeyValue, toDisk);
+ return 40;
}
- interface BtreeDataStorage {
+ public interface BtreeDataStorage {
int persistInt(int offset, int value, boolean toDisk);
}
@@ -155,10 +155,11 @@ class IntToIntBtree {
}
private BtreeIndexNodeView myAccessNodeView;
- private int myLastGetKey, myOptimizedInserts;
+ private int myLastGetKey;
+ private int myOptimizedInserts;
private boolean myCanUseLastKey;
- public boolean get(int key, int[] result) {
+ public boolean get(int key, @NotNull int[] result) {
if (key == 0) {
if (hasZeroKey) {
result[0] = zeroKeyValue;
@@ -199,7 +200,8 @@ class IntToIntBtree {
if (hasCachedMappings) {
myCachedMappings.put(key, value);
if (myCachedMappings.size() == myCachedMappingsSize) flushCachedMappings();
- } else {
+ }
+ else {
boolean canUseLastKey = myCanUseLastKey;
if (canUseLastKey) {
myCanUseLastKey = false;
@@ -256,12 +258,12 @@ class IntToIntBtree {
}
}
- void doClose() throws IOException {
+ public void doClose() throws IOException {
myCachedMappings = null;
storage.close();
}
- void doFlush() {
+ public void doFlush() {
flushCachedMappings();
storage.force();
}
@@ -445,11 +447,11 @@ class IntToIntBtree {
putInt(offset, value);
}
- private final int indexToOffset(int i) {
+ private int indexToOffset(int i) {
return i * INTERIOR_SIZE + (isHashedLeaf() ? btree.metaDataLeafPageLength:RESERVED_META_PAGE_LEN);
}
- private final int keyAt(int i) {
+ private int keyAt(int i) {
if (doSanityCheck) {
if (isHashedLeaf()) myAssert(i < btree.hashPageCapacity);
else myAssert(i < getChildrenCount());
@@ -495,7 +497,7 @@ class IntToIntBtree {
setFlag(INDEX_LEAF_MASK, value);
}
- private final boolean isHashedLeaf() {
+ private boolean isHashedLeaf() {
return isHashedLeaf;
}
@@ -608,7 +610,8 @@ class IntToIntBtree {
if (hashedLeaf) {
hashLeafData = new HashLeafData(this, recordCount);
if (doOffloadToSiblingsWhenHashed(parent, hashLeafData)) return parentAddress;
- } else {
+ }
+ else {
if (doOffloadToSiblingsSorted(parent)) return parentAddress;
}
}
@@ -1074,16 +1077,14 @@ class IntToIntBtree {
private static final boolean useDoubleHash = true;
private int hashIndex(int value) {
- int hash, index;
-
final int length = btree.hashPageCapacity;
- hash = value & 0x7fffffff;
- index = hash % length;
+ int hash = value & 0x7fffffff;
+ int index = hash % length;
int keyAtIndex = keyAt(index);
- int total = 0;
btree.hashSearchRequests++;
+ int total = 0;
if (useDoubleHash) {
if (keyAtIndex != value && keyAtIndex != HASH_FREE) {
// see Knuth, p. 529
@@ -1148,11 +1149,21 @@ class IntToIntBtree {
if (childrenAddresses.length > 0) {
BtreeIndexNodeView child = new BtreeIndexNodeView(this);
- for(int i = 0; i < childrenAddresses.length; ++i) {
- child.setAddress(childrenAddresses[i]);
- if (!processLeafPages(child, processor)) return false;
+ for (int childrenAddress : childrenAddresses) {
+ child.setAddress(childrenAddress);
+ if (!processLeafPages(child, processor)) return false;
}
}
return true;
}
+
+ public void withStorageLock(@NotNull Runnable runnable) {
+ storage.getPagedFileStorage().lock();
+ try {
+ runnable.run();
+ }
+ finally {
+ storage.getPagedFileStorage().unlock();
+ }
+ }
}