public VanillaSharedHashMap( SharedHashMapBuilder builder, File file, Class<K> kClass, Class<V> vClass) throws IOException { this.kClass = kClass; this.vClass = vClass; lockTimeOutNS = builder.lockTimeOutMS() * 1000000; this.replicas = builder.replicas(); this.entrySize = builder.entrySize(); this.errorListener = builder.errorListener(); this.generatedKeyType = builder.generatedKeyType(); this.generatedValueType = builder.generatedValueType(); this.putReturnsNull = builder.putReturnsNull(); this.removeReturnsNull = builder.removeReturnsNull(); int segments = builder.actualSegments(); this.segmentBits = Maths.intLog2(segments); int entriesPerSegment = builder.actualEntriesPerSegment(); this.entriesPerSegment = entriesPerSegment; @SuppressWarnings("unchecked") Segment[] ss = (VanillaSharedHashMap.Segment[]) new VanillaSharedHashMap.Segment[segments]; this.segments = ss; this.ms = new MappedStore(file, FileChannel.MapMode.READ_WRITE, sizeInBytes()); long offset = SharedHashMapBuilder.HEADER_SIZE; long segmentSize = segmentSize(); for (int i = 0; i < this.segments.length; i++) { this.segments[i] = new Segment(ms.createSlice(offset, segmentSize)); offset += segmentSize; } }
private V put0(K key, V value, boolean replaceIfPresent) { if (!kClass.isInstance(key)) return null; DirectBytes bytes = getKeyAsBytes(key); long hash = longHashCode(bytes); int segmentNum = (int) (hash & (builder.segments() - 1)); int hash2 = (int) (hash / builder.segments()); // System.out.println("[" + key + "] s: " + segmentNum + " h2: " + hash2); return segments[segmentNum].put(bytes, value, hash2, replaceIfPresent); }
private V removeUsing(Object key, V value) { if (!kClass.isInstance(key)) return null; DirectBytes bytes = getKeyAsBytes((K) key); long hash = longHashCode(bytes); int segmentNum = (int) (hash & (builder.segments() - 1)); int hash2 = (int) (hash / builder.segments()); // System.out.println("[" + key + "] s: " + segmentNum + " h2: " + hash2); return segments[segmentNum].remove(bytes, value, hash2); }
private DirectBytes getKeyAsBytes(K key) { DirectBytes bytes = acquireBytes(); if (builder.generatedKeyType()) ((BytesMarshallable) key).writeMarshallable(bytes); else bytes.writeInstance(kClass, key); bytes.flip(); return bytes; }
public VanillaSharedHashMap( SharedHashMapBuilder builder, MappedStore ms, Class<K> kClass, Class<V> vClass) { this.builder = builder; lockTimeOutNS = builder.lockTimeOutMS() * 1000000; this.ms = ms; this.kClass = kClass; this.vClass = vClass; @SuppressWarnings("unchecked") Segment[] segments = (VanillaSharedHashMap<K, V>.Segment[]) new VanillaSharedHashMap.Segment[builder.segments()]; this.segments = segments; long offset = SharedHashMapBuilder.HEADER_SIZE; long segmentSize = builder.segmentSize(); for (int i = 0; i < this.segments.length; i++) { this.segments[i] = new Segment(ms.createSlice(offset, segmentSize)); offset += segmentSize; } }
protected DirectBytes acquireBytes() { DirectBytes bytes = localBytes.get(); if (bytes == null) { localBytes.set( bytes = new DirectStore(ms.bytesMarshallerFactory(), builder.entrySize() * 2, false) .createSlice()); } else { bytes.clear(); } return bytes; }