1 5 package org.h2.index; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Session; 10 import org.h2.store.DataPage; 11 import org.h2.store.Record; 12 import org.h2.util.ObjectArray; 13 import org.h2.value.Value; 14 15 18 public class LinearHashBucket extends Record { 19 private LinearHashIndex index; 20 private int nextBucket; 21 private ObjectArray records; 22 private boolean writePos; 23 24 public LinearHashBucket(LinearHashIndex index, DataPage s) throws SQLException { 25 this.index = index; 26 writePos = s.readByte() == 'P'; 27 nextBucket = s.readInt(); 28 int len = s.readInt(); 29 records = new ObjectArray(); 30 for(int i=0; i<len; i++) { 31 LinearHashEntry entry = new LinearHashEntry(); 32 if(!writePos) { 33 Value key = s.readValue(); 34 entry.key = key; 35 } 36 entry.hash = s.readInt(); 37 entry.value = s.readInt(); 38 entry.home = index.getPos(entry.hash); 39 records.add(entry); 40 } 41 } 42 43 public LinearHashBucket(LinearHashIndex index) { 44 this.index = index; 45 this.records = new ObjectArray(); 46 this.nextBucket = -1; 47 } 48 49 private void update(Session session) throws SQLException { 50 index.updateBucket(session, this); 51 } 52 53 void setNext(Session session, int nextBucket) throws SQLException { 54 this.nextBucket = nextBucket; 55 update(session); 56 } 57 58 int getNextBucket() { 59 return nextBucket; 60 } 61 62 LinearHashEntry getRecord(int i) { 63 return (LinearHashEntry)records.get(i); 64 } 65 66 void addRecord(Session session, LinearHashEntry r) throws SQLException { 67 records.add(r); 68 update(session); 69 } 70 71 void removeRecord(Session session, int i) throws SQLException { 72 records.remove(i); 73 update(session); 74 } 75 76 int getRecordSize() { 77 return records.size(); 78 } 79 80 public void write(DataPage buff) throws SQLException { 81 getRealByteCount(buff); 82 buff.writeByte((byte)'B'); 83 if(writePos) { 84 buff.writeByte((byte)'P'); 85 } else { 86 buff.writeByte((byte)'D'); 87 } 88 buff.writeInt(nextBucket); 89 buff.writeInt(records.size()); 90 for(int i=0; i<records.size(); i++) { 91 LinearHashEntry record = (LinearHashEntry) records.get(i); 92 if(!writePos) { 94 buff.writeValue(record.key); 95 } 96 buff.writeInt(record.hash); 97 buff.writeInt(record.value); 98 } 99 } 100 101 public int getByteCount(DataPage dummy) throws SQLException { 102 return index.getBucketSize(); 103 } 104 105 public int getRealByteCount(DataPage dummy) throws SQLException { 106 int size = 2 + dummy.getIntLen() + dummy.getIntLen(); 107 int datasize = 0; 108 for(int i=0; i<records.size(); i++) { 109 LinearHashEntry record = (LinearHashEntry) records.get(i); 110 datasize += dummy.getValueLen(record.key); 112 size += 2 * dummy.getIntLen(); 113 } 114 if(size + datasize >= index.getBucketSize()) { 115 writePos = true; 116 return size; 117 } else { 118 writePos = false; 119 return size + datasize; 120 } 121 } 122 123 public boolean isEmpty() { 124 return false; 125 } 126 127 } 128 | Popular Tags |