1 5 package org.h2.util; 6 7 import java.sql.SQLException ; 8 9 import org.h2.message.Message; 10 import org.h2.store.DataHandler; 11 import org.h2.value.Value; 12 import org.h2.value.ValueNull; 13 14 public class ValueHashMap extends HashBase { 15 16 private Value[] keys; 17 private Object [] values; 18 private DataHandler database; 19 20 public ValueHashMap(DataHandler database) { 21 this.database = database; 22 } 23 24 protected void reset(int newLevel) { 25 super.reset(newLevel); 26 keys = new Value[len]; 27 values = new Object [len]; 28 } 29 30 protected void rehash(int newLevel) throws SQLException { 31 Value[] oldKeys = keys; 32 Object [] oldValues = values; 33 reset(newLevel); 34 for(int i=0; i<oldKeys.length; i++) { 35 Value k = oldKeys[i]; 36 if(k != null && k != ValueNull.DELETED) { 37 put(k, oldValues[i]); 38 } 39 } 40 } 41 42 private int getIndex(Value key) { 43 return key.hashCode() & mask; 44 } 45 46 public void put(Value key, Object value) throws SQLException { 47 checkSizePut(); 48 int index = getIndex(key); 49 int plus = 1; 50 int deleted = -1; 51 do { 52 Value k = keys[index]; 53 if(k==null) { 54 if(deleted>=0) { 56 index = deleted; 57 deletedCount--; 58 } 59 size++; 60 keys[index] = key; 61 values[index] = value; 62 return; 63 } else if(k==ValueNull.DELETED) { 64 if(deleted<0) { 66 deleted = index; 67 } 68 } else if(database.compareTypeSave(k, key)==0) { 69 values[index] = value; 71 return; 72 } 73 index = (index + plus++) & mask; 74 } while(plus <= len); 75 throw Message.getInternalError("hashmap is full"); 77 } 78 79 public void remove(Value key) throws SQLException { 80 checkSizeRemove(); 81 int index = getIndex(key); 82 int plus = 1; 83 do { 84 Value k = keys[index]; 85 if(k==null) { 86 return; 88 } else if(k==ValueNull.DELETED) { 89 } else if(database.compareTypeSave(k, key)==0) { 91 keys[index] = ValueNull.DELETED; 93 values[index] = null; 94 deletedCount++; 95 size--; 96 return; 97 } 98 index = (index + plus++) & mask; 99 k = keys[index]; 100 } while(plus <= len); 101 } 103 104 public Object get(Value key) throws SQLException { 105 int index = getIndex(key); 106 int plus = 1; 107 do { 108 Value k = keys[index]; 109 if(k==null) { 110 return null; 112 } else if(k == ValueNull.DELETED) { 113 } else if(database.compareTypeSave(k, key)==0) { 115 return values[index]; 117 } 118 index = (index + plus++) & mask; 119 } while(plus <= len); 120 return null; 121 } 122 123 public ObjectArray keys() { 124 ObjectArray list = new ObjectArray(size); 125 for(int i=0; i<keys.length; i++) { 126 Value k = keys[i]; 127 if(k != null && k != ValueNull.DELETED) { 128 list.add(k); 129 } 130 } 131 return list; 132 } 133 134 public ObjectArray values() { 135 ObjectArray list = new ObjectArray(size); 136 for(int i=0; i<keys.length; i++) { 137 Value k = keys[i]; 138 if(k != null && k != ValueNull.DELETED) { 139 list.add(values[i]); 140 } 141 } 142 return list; 143 } 144 145 } 146 | Popular Tags |