1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.kahadb.index; 18 19 import java.io.DataInput; 20 import java.io.DataOutput; 21 import java.io.IOException; 22 import java.util.Map; 23 import java.util.TreeMap; 24 25 import org.apache.kahadb.page.Page; 26 import org.apache.kahadb.page.Transaction; 27 import org.apache.kahadb.util.VariableMarshaller; 28 29 /** 30 * Bin in a HashIndex 31 * 32 * @version $Revision: 777209 $ 33 */ 34 class HashBin<Key, Value> { 35 36 37 static public class Marshaller<Key, Value> extends VariableMarshaller<HashBin<Key, Value>> { 38 private final HashIndex<Key, Value> hashIndex; 39 40 public Marshaller(HashIndex<Key, Value> index) { 41 this.hashIndex = index; 42 } 43 44 public HashBin<Key, Value> readPayload(DataInput is) throws IOException { 45 HashBin<Key, Value> bin = new HashBin<Key, Value>(); 46 int size = is.readInt(); 47 for(int i=0; i < size; i++) { 48 Key key = hashIndex.getKeyMarshaller().readPayload(is); 49 Value value = hashIndex.getValueMarshaller().readPayload(is); 50 bin.data.put(key, value); 51 } 52 return bin; 53 } 54 55 public void writePayload(HashBin<Key, Value> bin, DataOutput os) throws IOException { 56 os.writeInt(bin.data.size()); 57 for (Map.Entry<Key, Value> entry : bin.data.entrySet()) { 58 hashIndex.getKeyMarshaller().writePayload(entry.getKey(), os); 59 hashIndex.getValueMarshaller().writePayload(entry.getValue(), os); 60 } 61 } 62 63 } 64 65 private Page<HashBin<Key, Value>> page; 66 private TreeMap<Key, Value> data = new TreeMap<Key, Value>(); 67 68 public int size() { 69 return data.size(); 70 } 71 72 public Value put(Key key, Value value) throws IOException { 73 return data.put(key, value); 74 } 75 76 public Value get(Key key) throws IOException { 77 return data.get(key); 78 } 79 80 public boolean containsKey(Key key) throws IOException { 81 return data.containsKey(key); 82 } 83 84 public Map<Key, Value> getAll(Transaction tx) throws IOException { 85 return data; 86 } 87 88 public Value remove(Key key) throws IOException { 89 return data.remove(key); 90 } 91 92 public Page<HashBin<Key, Value>> getPage() { 93 return page; 94 } 95 96 public void setPage(Page<HashBin<Key, Value>> page) { 97 this.page = page; 98 this.page.set(this); 99 } 100 101 102 }