HBASE-26850 Optimize the implementation of LRUCache in LRUDictionary

This commit is contained in:
tianhang 2023-02-14 11:30:59 +08:00
parent 8ba56cccd3
commit eb2537e7a8
2 changed files with 42 additions and 20 deletions

View File

@ -105,23 +105,32 @@ public class LRUDictionary implements Dictionary {
}
private short putInternal(byte[] stored) {
if (currSize < initSize) {
// There is space to add without evicting.
if (indexToNode[currSize] == null) {
indexToNode[currSize] = new ByteArrayBackedNode();
}
indexToNode[currSize].setContents(stored, 0, stored.length);
setHead(indexToNode[currSize]);
short ret = (short) currSize++;
nodeToIndex.put(indexToNode[ret], ret);
return ret;
Node node = new ByteArrayBackedNode();
node.setContents(stored, 0, stored.length);
if (nodeToIndex.containsKey(node)) {
short index = nodeToIndex.get(node);
node = indexToNode[index];
moveToHead(node);
return index;
} else {
short s = nodeToIndex.remove(tail);
tail.setContents(stored, 0, stored.length);
// we need to rehash this.
nodeToIndex.put(tail, s);
moveToHead(tail);
return s;
if (currSize < initSize) {
// There is space to add without evicting.
if (indexToNode[currSize] == null) {
indexToNode[currSize] = new ByteArrayBackedNode();
}
indexToNode[currSize].setContents(stored, 0, stored.length);
setHead(indexToNode[currSize]);
short ret = (short) currSize++;
nodeToIndex.put(indexToNode[ret], ret);
return ret;
} else {
short s = nodeToIndex.remove(tail);
tail.setContents(stored, 0, stored.length);
// we need to rehash this.
nodeToIndex.put(tail, s);
moveToHead(tail);
return s;
}
}
}

View File

@ -68,12 +68,25 @@ public class TestLRUDictionary {
@Test
public void testPassingSameArrayToAddEntry() {
// Add random predefined byte array, in this case a random byte array from
// HConstants. Assert that when we add, we get new index. Thats how it
// works.
// HConstants.
// Assert that when we add, we get old index.
// Because we DO NOT need to write a new one.
int len = HConstants.CATALOG_FAMILY.length;
int index = testee.addEntry(HConstants.CATALOG_FAMILY, 0, len);
assertFalse(index == testee.addEntry(HConstants.CATALOG_FAMILY, 0, len));
assertFalse(index == testee.addEntry(HConstants.CATALOG_FAMILY, 0, len));
assertTrue(index == testee.addEntry(HConstants.CATALOG_FAMILY, 0, len));
assertTrue(index == testee.addEntry(HConstants.CATALOG_FAMILY, 0, len));
}
@Test
public void testPassingSameArrayToAddEntryThenEvict() {
testee.init(3);
byte[] byte0 = Bytes.toBytes(0);
byte[] byte1 = Bytes.toBytes(1);
assertEquals(0, testee.addEntry(byte0, 0, byte0.length));
assertEquals(0, testee.addEntry(byte0, 0, byte0.length));
assertEquals(0, testee.addEntry(byte0, 0, byte0.length));
assertEquals(1, testee.addEntry(byte1, 0, byte1.length));
assertEquals(0, testee.addEntry(byte0, 0, byte0.length));
}
@Test