1 24 25 package com.mckoi.database; 26 27 import java.util.ArrayList ; 28 import java.io.IOException ; 29 import com.mckoi.store.Store; 30 import com.mckoi.store.AreaWriter; 31 import com.mckoi.store.MutableArea; 32 import com.mckoi.store.Area; 33 34 70 71 public class FixedRecordList { 72 73 76 private final static int MAGIC = 0x087131AA; 77 78 81 private final Store store; 82 83 86 private final int element_size; 87 88 89 92 private long list_header_p; 93 94 97 private MutableArea list_header_area; 98 99 102 private int list_block_count; 103 104 107 private long[] list_block_element; 108 private MutableArea[] list_block_area; 109 110 111 114 public FixedRecordList(Store store, int element_size) { 115 this.store = store; 116 this.element_size = element_size; 117 list_block_element = new long[64]; 118 list_block_area = new MutableArea[64]; 119 } 120 121 124 public long create() throws IOException { 125 AreaWriter writer = store.createArea(528); 127 list_header_p = writer.getID(); 128 writer.putInt(MAGIC); 129 writer.finish(); 130 131 list_header_area = store.getMutableArea(list_header_p); 132 list_block_count = 0; 133 updateListHeaderArea(); 134 135 return list_header_p; 136 } 137 138 141 public void init(long list_pointer) throws IOException { 142 list_header_p = list_pointer; 143 list_header_area = store.getMutableArea(list_header_p); 144 145 int magic = list_header_area.getInt(); if (magic != MAGIC) { 147 throw new IOException ("Incorrect magic for list block. [magic=" + 148 magic + "]"); 149 } 150 this.list_block_count = list_header_area.getInt(); 151 list_header_area.getLong(); 152 for (int i = 0; i < list_block_count; ++i) { 153 long block_pointer = list_header_area.getLong(); 154 list_block_element[i] = block_pointer; 155 list_block_area[i] = store.getMutableArea(block_pointer); 156 } 157 } 158 159 163 public void addAllAreasUsed(ArrayList list) throws IOException { 164 list.add(new Long (list_header_p)); 165 for (int i = 0; i < list_block_count; ++i) { 166 list.add(new Long (list_block_element[i])); 167 } 168 } 169 170 174 public long getReservedLong() throws IOException { 175 list_header_area.position(8); 176 return list_header_area.getLong(); 177 } 178 179 183 public void setReservedLong(long v) throws IOException { 184 list_header_area.position(8); 185 list_header_area.putLong(v); 186 list_header_area.checkOut(); 187 } 188 189 194 private void updateListHeaderArea() throws IOException { 195 list_header_area.position(4); 196 list_header_area.putInt(list_block_count); 197 list_header_area.position(16); 198 for (int i = 0; i < list_block_count; ++i) { 199 list_header_area.putLong(list_block_element[i]); 200 } 201 list_header_area.checkOut(); 202 } 203 204 210 public MutableArea positionOnNode(final long record_number) 211 throws IOException { 212 int bit = 0; 214 long work = record_number + 32; 215 while (work != 0) { 216 work = work >> 1; 217 ++bit; 218 } 219 long start_val = (1 << (bit - 1)) - 32; 220 int block_offset = bit - 6; 221 long record_offset = record_number - start_val; 222 223 MutableArea block_area = list_block_area[block_offset]; 225 block_area.position((int) (record_offset * element_size)); 232 233 return block_area; 234 } 235 236 240 public int listBlockCount() { 241 return list_block_count; 242 } 243 244 251 public long addressableNodeCount() { 252 return listBlockFirstPosition(list_block_count); 253 } 254 255 259 public long listBlockNodeCount(int block_number) { 260 return 32L << block_number; 261 } 262 263 268 public long listBlockFirstPosition(int block_number) { 269 long start_index = 0; 270 int i = block_number; 271 long diff = 32; 272 while (i > 0) { 273 start_index = start_index + diff; 274 diff = diff << 1; 275 --i; 276 } 277 return start_index; 278 } 279 280 284 public void increaseSize() throws IOException { 285 long size_of_block = 32L << list_block_count; 287 AreaWriter writer = store.createArea(size_of_block * element_size); 289 long nblock_p = writer.getID(); 290 writer.finish(); 291 MutableArea nblock_area = store.getMutableArea(nblock_p); 292 list_block_element[list_block_count] = nblock_p; 294 list_block_area[list_block_count] = nblock_area; 295 ++list_block_count; 296 updateListHeaderArea(); 298 } 299 300 304 public void decreaseSize() throws IOException { 305 --list_block_count; 306 store.deleteArea(list_block_element[list_block_count]); 308 list_block_area[list_block_count] = null; 310 list_block_element[list_block_count] = 0; 311 updateListHeaderArea(); 313 } 314 315 } 316 317 | Popular Tags |