mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
make space function adaptive to binding type
This commit is contained in:
parent
5512830f31
commit
37e570cc42
@ -33,6 +33,7 @@ import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.LongFunction;
|
||||
import java.util.function.LongToIntFunction;
|
||||
import java.util.function.LongUnaryOperator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class BaseDriverAdapter<R extends Op, S extends Space> extends NBBaseComponent implements DriverAdapter<R, S>, NBConfigurable, NBReconfigurable {
|
||||
@ -193,8 +194,26 @@ public abstract class BaseDriverAdapter<R extends Op, S extends Space> extends N
|
||||
|
||||
@Override
|
||||
public LongFunction<S> getSpaceFunc(ParsedOp pop) {
|
||||
LongToIntFunction spaceIdxF = pop.getAsFunctionOrInt("space", 0);
|
||||
ConcurrentSpaceCache<? extends S> cache = getSpaceCache();
|
||||
return l -> getSpaceCache().get(spaceIdxF.applyAsInt(l));
|
||||
|
||||
Optional<LongFunction<String>> spaceFuncTest = pop.getAsOptionalFunction("space");
|
||||
LongUnaryOperator cycleToSpaceF;
|
||||
if (spaceFuncTest.isEmpty()) {
|
||||
cycleToSpaceF = (long l) -> 0;
|
||||
} else {
|
||||
Object example = spaceFuncTest.get().apply(0L);
|
||||
if (example instanceof Number n) {
|
||||
logger.trace("mapping space indirectly with Number type");
|
||||
LongFunction<Number> numberF = pop.getAsRequiredFunction("space", Number.class);
|
||||
cycleToSpaceF= l -> numberF.apply(l).longValue();
|
||||
} else {
|
||||
logger.trace("mapping space indirectly through hash table to index pool");
|
||||
LongFunction<?> sourceF = pop.getAsRequiredFunction("space", String.class);
|
||||
LongFunction<String> namerF = l -> sourceF.apply(l).toString();
|
||||
ConcurrentIndexCacheWrapper wrapper = new ConcurrentIndexCacheWrapper();
|
||||
cycleToSpaceF = l -> wrapper.mapKeyToIndex(namerF.apply(l));
|
||||
}
|
||||
}
|
||||
ConcurrentSpaceCache<S> spaceCache1 = getSpaceCache();
|
||||
return l -> spaceCache1.get(cycleToSpaceF.applyAsLong(l));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
package io.nosqlbench.adapters.api.activityimpl.uniform;
|
||||
|
||||
/*
|
||||
* Copyright (c) 2022 nosqlbench
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
import io.nosqlbench.virtdata.library.basics.shared.functionadapters.ToLongFunction;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* This is only here for backwards compatibility. If you get a warning that this class is being used,
|
||||
* you can probably ignore it for low cardinality of spaces. However, if you want to optimize your
|
||||
* client to hold spaces more densely (avoiding the egregious heap overhead of hashing), then modify
|
||||
* your space binding to produce a long or int value. In that case, this wrapper type will
|
||||
* not be used, and the memory overhead will be minimal.
|
||||
*/
|
||||
public class ConcurrentIndexCacheWrapper {
|
||||
|
||||
private ConcurrentHashMap<Object,Long> forwardMap = new ConcurrentHashMap<>();
|
||||
|
||||
public long mapKeyToIndex(Object key) {
|
||||
return forwardMap.computeIfAbsent(key, this::nextIndex);
|
||||
}
|
||||
|
||||
private long idx=0;
|
||||
private synchronized long nextIndex(Object any) {
|
||||
return idx++;
|
||||
}
|
||||
|
||||
}
|
@ -138,7 +138,7 @@ public interface DriverAdapter<OPTYPE extends Op, SPACETYPE extends Space> exten
|
||||
* things needed by operations, or things needed during the
|
||||
* construction of operations.
|
||||
*
|
||||
* See {@link StringDriverSpaceCache} for details on when and how to use this function.
|
||||
* See {@link ConcurrentIndexCache} for details on when and how to use this function.
|
||||
*
|
||||
* <p>During Adapter Initialization, Op Mapping, Op Synthesis, or Op Execution,
|
||||
* you may need access to the objects in (the or a) space cache. You can build the
|
||||
|
Loading…
Reference in New Issue
Block a user