add binding functions ToJavaInstant, ToJodaInstant, Expr to Object

This commit is contained in:
Jonathan Shook 2022-05-17 17:20:32 -05:00
parent 53acf75438
commit 1b3cead29a
3 changed files with 187 additions and 0 deletions

View File

@ -0,0 +1,59 @@
/*
* 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.
*/
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_object;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.library.basics.core.MVELExpr;
import io.nosqlbench.virtdata.library.basics.core.threadstate.SharedState;
import org.mvel2.MVEL;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.LongFunction;
@ThreadSafeMapper
@Categories({Category.functional})
public class Expr implements LongFunction<Object> {
private final String expr;
private final Serializable compiledExpr;
public Expr(String expr) {
this.expr = expr;
this.compiledExpr = MVELExpr.compile(long.class, "cycle", expr);
}
@Override
public Object apply(long operand) {
ConcurrentHashMap<String, Object> gl_map = SharedState.gl_ObjectMap;
HashMap<String, Object> map = SharedState.tl_ObjectMap.get();
// merge gl into tl, for duplicates use the value from tl
for (Map.Entry<String, Object> stringObjectEntry : gl_map.entrySet()) {
map.merge(stringObjectEntry.getKey(), stringObjectEntry.getValue(), (entry1, entry2) -> entry1);
}
map.put("cycle",operand);
UUID result = MVEL.executeExpression(compiledExpr, map, UUID.class);
return result;
}
}

View File

@ -0,0 +1,64 @@
/*
* 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.
*/
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_time_types;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.time.Instant;
import java.util.function.LongFunction;
/**
* Convert the input epoch millisecond to a {@code Java Instant}, by multiplying and then dividing
* by the provided parameters. This is in contrast to the ToJodaInstant function which does the same
* thing using the Joda API Type.
*/
@ThreadSafeMapper
@Categories({Category.datetime})
public class ToJavaInstant implements LongFunction<Instant> {
private final long spacing;
private final long repeat_count;
@Example({"ToDate(86400000,2)","produce two Date values per day"})
public ToJavaInstant(int millis_multiplier, int millis_divisor){
this.spacing = millis_multiplier;
this.repeat_count = millis_divisor;
}
@Example({"ToDate(86400000)","produce a single Date() per day"})
public ToJavaInstant(int spacing){
this(spacing, 1);
}
public ToJavaInstant() {
this.spacing=1;
this.repeat_count=1;
}
@Override
public Instant apply(long input) {
input = (input*spacing)/repeat_count;
return Instant.ofEpochMilli(input);
}
public String toString() {
return getClass().getSimpleName() + ":" + spacing+ ":" + repeat_count;
}
}

View File

@ -0,0 +1,64 @@
/*
* 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.
*/
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_time_types;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import org.joda.time.Instant;
import java.util.function.LongFunction;
/**
* Convert the input epoch millisecond to a {@code JodaTime Instant}, by multiplying and then dividing
* by the provided parameters. This is in contrast to the ToJavaInstant function which does the same
* thing, only using the Java API type.
*/
@ThreadSafeMapper
@Categories({Category.datetime})
public class ToJodaInstant implements LongFunction<Instant> {
private final long spacing;
private final long repeat_count;
@Example({"ToDate(86400000,2)","produce two Date values per day"})
public ToJodaInstant(int millis_multiplier, int millis_divisor){
this.spacing = millis_multiplier;
this.repeat_count = millis_divisor;
}
@Example({"ToDate(86400000)","produce a single Date() per day"})
public ToJodaInstant(int spacing){
this(spacing, 1);
}
public ToJodaInstant() {
this.spacing=1;
this.repeat_count=1;
}
@Override
public Instant apply(long input) {
input = (input*spacing)/repeat_count;
return Instant.ofEpochMilli(input);
}
public String toString() {
return getClass().getSimpleName() + ":" + spacing+ ":" + repeat_count;
}
}