HCOLL-194 Add support for new MultiMaps in the future.

This commit is contained in:
Peter Lawrey
2014-11-10 14:12:01 +00:00
parent c143726275
commit 6e2881a014
10 changed files with 130 additions and 37 deletions
@@ -26,6 +26,7 @@ import net.openhft.chronicle.hash.serialization.SizeMarshaller;
import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop;
import net.openhft.chronicle.hash.serialization.internal.MetaBytesWriter;
import net.openhft.chronicle.hash.serialization.internal.MetaProvider;
import net.openhft.chronicle.map.utils.MultiMapFactory;
import net.openhft.chronicle.set.ChronicleSetBuilder;
import net.openhft.lang.Maths;
import net.openhft.lang.io.ByteBufferBytes;
@@ -344,13 +345,13 @@ public abstract class AbstractChronicleMapBuilder<K, V,
actualEntriesPerSegment + " given");
if (tooManyEntriesPerSegment(actualEntriesPerSegment))
throw new IllegalArgumentException("max entries per segment is " +
VanillaIntIntMultiMap.MAX_CAPACITY + ", " + actualEntriesPerSegment + " given");
MultiMapFactory.MAX_CAPACITY + ", " + actualEntriesPerSegment + " given");
this.actualEntriesPerSegment = actualEntriesPerSegment;
return self();
}
private boolean tooManyEntriesPerSegment(long entriesPerSegment) {
return entriesPerSegment > VanillaIntIntMultiMap.MAX_CAPACITY;
return entriesPerSegment > MultiMapFactory.MAX_CAPACITY;
}
long actualEntriesPerSegment() {
@@ -361,7 +362,7 @@ public abstract class AbstractChronicleMapBuilder<K, V,
long actualEntriesPerSegment = divideUpper(actualEntries, actualSegments);
if (tooManyEntriesPerSegment(actualEntriesPerSegment))
throw new IllegalStateException("max entries per segment is " +
VanillaIntIntMultiMap.MAX_CAPACITY + " configured entries() and " +
MultiMapFactory.MAX_CAPACITY + " configured entries() and " +
"actualSegments() so that there should be " + actualEntriesPerSegment +
" entries per segment");
return actualEntriesPerSegment;
@@ -419,11 +420,11 @@ public abstract class AbstractChronicleMapBuilder<K, V,
int actualSegments() {
if (actualSegments > 0)
return actualSegments;
long shortMMapSegments = trySegments(VanillaShortShortMultiMap.MAX_CAPACITY,
long shortMMapSegments = trySegments(MultiMapFactory.I16_MAX_CAPACITY,
MAX_SEGMENTS_TO_CHAISE_COMPACT_MULTI_MAPS);
if (shortMMapSegments > 0L)
return (int) shortMMapSegments;
long intMMapSegments = trySegments(VanillaIntIntMultiMap.MAX_CAPACITY, MAX_SEGMENTS);
long intMMapSegments = trySegments(MultiMapFactory.MAX_CAPACITY, MAX_SEGMENTS);
if (intMMapSegments > 0L)
return (int) intMMapSegments;
throw new IllegalStateException("Max segments is " + MAX_SEGMENTS + ", configured so much" +
@@ -938,5 +939,9 @@ public abstract class AbstractChronicleMapBuilder<K, V,
// reduce false sharing unless we have a lot of segments.
return segments <= 8192 ? 64 : 16;
}
public MultiMapFactory multiMapFactory() {
return MultiMapFactory.forCapacity(actualEntriesPerSegment());
}
}
@@ -23,6 +23,7 @@ import net.openhft.chronicle.hash.replication.TimeProvider;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop;
import net.openhft.chronicle.hash.serialization.internal.MetaBytesWriter;
import net.openhft.chronicle.map.utils.MultiMap;
import net.openhft.lang.Maths;
import net.openhft.lang.collection.ATSDirectBitSet;
import net.openhft.lang.io.Bytes;
@@ -25,16 +25,17 @@ import net.openhft.chronicle.hash.serialization.SizeMarshaller;
import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop;
import net.openhft.chronicle.hash.serialization.internal.MetaBytesWriter;
import net.openhft.chronicle.hash.serialization.internal.MetaProvider;
import net.openhft.chronicle.map.utils.MultiMap;
import net.openhft.chronicle.map.utils.MultiMapFactory;
import net.openhft.lang.collection.DirectBitSet;
import net.openhft.lang.collection.SingleThreadedDirectBitSet;
import net.openhft.lang.io.*;
import net.openhft.lang.io.serialization.JDKObjectSerializer;
import net.openhft.lang.io.serialization.ObjectFactory;
import net.openhft.lang.io.serialization.impl.VanillaBytesMarshallerFactory;
import net.openhft.lang.model.constraints.Nullable;
import net.openhft.lang.threadlocal.Provider;
import net.openhft.lang.threadlocal.ThreadLocalCopies;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -96,6 +97,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, KI>,
private final int segmentHeaderSize;
private final HashSplitting hashSplitting;
final boolean isNativeValueClass;
final MultiMapFactory multiMapFactory;
transient Provider<BytesReader<K>> keyReaderProvider;
transient Provider<KI> keyInteropProvider;
transient Provider<BytesReader<V>> valueReaderProvider;
@@ -142,6 +144,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, KI>,
this.actualSegments = builder.actualSegments();
this.entriesPerSegment = builder.actualEntriesPerSegment();
this.multiMapFactory = builder.multiMapFactory();
this.metaDataBytes = builder.metaDataBytes();
this.eventListener = builder.eventListener();
this.segmentHeaderSize = builder.segmentHeaderSize();
@@ -247,19 +250,11 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, KI>,
}
long sizeOfMultiMap() {
return useSmallMultiMaps() ?
VanillaShortShortMultiMap.sizeInBytes(entriesPerSegment) :
VanillaIntIntMultiMap.sizeInBytes(entriesPerSegment);
return multiMapFactory.sizeInBytes(entriesPerSegment);
}
long sizeOfMultiMapBitSet() {
return useSmallMultiMaps() ?
VanillaShortShortMultiMap.sizeOfBitSetInBytes(entriesPerSegment) :
VanillaIntIntMultiMap.sizeOfBitSetInBytes(entriesPerSegment);
}
boolean useSmallMultiMaps() {
return entriesPerSegment <= VanillaShortShortMultiMap.MAX_CAPACITY;
return multiMapFactory.sizeOfBitSetInBytes(entriesPerSegment);
}
long sizeOfBitSets() {
@@ -725,9 +720,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, KI>,
new NativeBytes(new VanillaBytesMarshallerFactory(), start,
start + sizeOfMultiMapBitSet(), null);
multiMapBytes.load();
return useSmallMultiMaps() ?
new VanillaShortShortMultiMap(multiMapBytes, sizeOfMultiMapBitSetBytes) :
new VanillaIntIntMultiMap(multiMapBytes, sizeOfMultiMapBitSetBytes);
return multiMapFactory.create(multiMapBytes, sizeOfMultiMapBitSetBytes);
}
public int getIndex() {
@@ -16,7 +16,7 @@
* limitations under the License.
*/
package net.openhft.chronicle.map;
package net.openhft.chronicle.map.utils;
import net.openhft.lang.collection.DirectBitSet;
@@ -14,14 +14,14 @@
* limitations under the License.
*/
package net.openhft.chronicle.map;
package net.openhft.chronicle.map.utils;
import net.openhft.lang.collection.DirectBitSet;
/**
* This is only used to store keys and positions, but it could store int/int key/values for another purpose.
*/
interface MultiMap {
public interface MultiMap {
/**
* @return gets all the active positions as a bitset
@@ -0,0 +1,96 @@
/*
* Copyright 2014 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.map.utils;
import net.openhft.lang.io.NativeBytes;
/**
* Created by peter on 09/11/14.
*
* In future we might support I32 -> I16 or similar.
*/
public enum MultiMapFactory {
I16 {
@Override
public long sizeInBytes(long minCapacity) {
return VanillaShortShortMultiMap.sizeInBytes(minCapacity);
}
@Override
public long sizeOfBitSetInBytes(long minCapacity) {
return VanillaShortShortMultiMap.sizeOfBitSetInBytes(minCapacity);
}
@Override
public MultiMap create(NativeBytes bytes, NativeBytes bitSetBytes) {
return new VanillaShortShortMultiMap(bytes, bitSetBytes);
}
},
I24 {
@Override
public long sizeInBytes(long minCapacity) {
return VanillaI24I24MultiMap.sizeInBytes(minCapacity);
}
@Override
public long sizeOfBitSetInBytes(long minCapacity) {
return VanillaI24I24MultiMap.sizeOfBitSetInBytes(minCapacity);
}
@Override
public MultiMap create(NativeBytes bytes, NativeBytes bitSetBytes) {
return new VanillaI24I24MultiMap(bytes, bitSetBytes);
}
},
I32 {
@Override
public long sizeInBytes(long minCapacity) {
return VanillaIntIntMultiMap.sizeInBytes(minCapacity);
}
@Override
public long sizeOfBitSetInBytes(long minCapacity) {
return VanillaIntIntMultiMap.sizeOfBitSetInBytes(minCapacity);
}
@Override
public MultiMap create(NativeBytes bytes, NativeBytes bitSetBytes) {
return new VanillaIntIntMultiMap(bytes, bitSetBytes);
}
};
public static final long I16_MAX_CAPACITY = VanillaShortShortMultiMap.MAX_CAPACITY;
public static final long MAX_CAPACITY = VanillaIntIntMultiMap.MAX_CAPACITY;
public static MultiMapFactory forCapacity(long capacity) {
if (capacity <= VanillaShortShortMultiMap.MAX_CAPACITY)
return I16;
// if (capacity <= VanillaI24I24MultiMap.MAX_CAPACITY)
// return I24;
if (capacity <= VanillaIntIntMultiMap.MAX_CAPACITY)
return I32;
throw new IllegalArgumentException("Capacity " + capacity + " not supported");
}
public abstract long sizeInBytes(long minCapacity);
public abstract long sizeOfBitSetInBytes(long minCapacity);
public abstract MultiMap create(NativeBytes bytes, NativeBytes bitSetBytes);
}
@@ -16,7 +16,7 @@
* limitations under the License.
*/
package net.openhft.chronicle.map;
package net.openhft.chronicle.map.utils;
import net.openhft.lang.collection.ATSDirectBitSet;
import net.openhft.lang.collection.DirectBitSet;
@@ -16,7 +16,7 @@
* limitations under the License.
*/
package net.openhft.chronicle.map;
package net.openhft.chronicle.map.utils;
import net.openhft.lang.Maths;
import net.openhft.lang.collection.ATSDirectBitSet;
@@ -16,7 +16,7 @@
* limitations under the License.
*/
package net.openhft.chronicle.map;
package net.openhft.chronicle.map.utils;
import net.openhft.lang.collection.ATSDirectBitSet;
@@ -24,7 +24,7 @@ import net.openhft.lang.collection.DirectBitSet;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.DirectStore;
import static net.openhft.chronicle.map.VanillaIntIntMultiMap.multiMapCapacity;
import static net.openhft.chronicle.map.utils.VanillaIntIntMultiMap.multiMapCapacity;
import static net.openhft.lang.Maths.isPowerOf2;
/**