1 5 package org.h2.util; 6 7 import java.sql.SQLException ; 8 9 public abstract class HashBase { 10 protected int mask, len, size, deletedCount, level; 11 protected boolean zeroKey; 12 private int maxSize, minSize, maxDeleted; 13 private static final int MAX_LOAD = 90; 14 15 public HashBase() { 16 reset(2); 17 } 18 19 public int size() { 20 return size + (zeroKey ? 1 : 0); 21 } 22 23 protected void checkSizePut() throws SQLException { 24 if(deletedCount > size) { 25 rehash(level); 26 } 27 if(size + deletedCount >= maxSize) { 28 rehash(level+1); 29 } 30 } 31 32 protected void checkSizeRemove() throws SQLException { 33 if(size < minSize && level>0) { 34 rehash(level-1); 35 } else if(deletedCount > maxDeleted) { 36 rehash(level); 37 } 38 } 39 40 protected abstract void rehash(int newLevel) throws SQLException ; 41 42 protected void reset(int newLevel) { 43 minSize = size * 3 / 4; 44 size = 0; 45 level = newLevel; 46 len = 2 << level; 47 mask = len - 1; 48 maxSize = (int)(len * MAX_LOAD / 100L); 49 deletedCount = 0; 50 maxDeleted = 20 + len / 2; 51 } 52 53 protected int getIndex(int hash) { 54 return hash & mask; 55 } 56 57 } 58 | Popular Tags |