add ConcatArray and ToJSONF functions

This commit is contained in:
Jonathan Shook 2024-02-26 09:30:19 -06:00
parent f1f6b3336c
commit 5be14f1c83
4 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,49 @@
/*
* 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.conversions.from_any;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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.api.bindings.VirtDataConversions;
import java.util.function.Function;
import java.util.function.LongFunction;
/**
* Convert the input object to a JSON string with Gson.
*/
@ThreadSafeMapper
@Categories({Category.conversion})
public class ToJSONF implements LongFunction<String> {
private final static Gson gson = new GsonBuilder().create();
private final LongFunction<?> objfunc;
public ToJSONF(LongFunction<Object> objFunc) {
this.objfunc = VirtDataConversions.adaptFunction(objFunc, LongFunction.class, String.class);
}
@Override
public String apply(long value) {
Object object = objfunc.apply(value);
return gson.toJson(object);
}
}

View File

@ -0,0 +1,48 @@
/*
* 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.conversions.from_any;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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.api.bindings.VirtDataConversions;
import java.util.function.LongFunction;
/**
* Convert the input object to a JSON string with Gson.
*/
@ThreadSafeMapper
@Categories({Category.conversion})
public class ToJSONFPretty implements LongFunction<String> {
private final static Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final LongFunction<?> objfunc;
public ToJSONFPretty(LongFunction<Object> objFunc) {
this.objfunc = VirtDataConversions.adaptFunction(objFunc, LongFunction.class, Object.class);
}
@Override
public String apply(long value) {
Object object = objfunc.apply(value);
return gson.toJson(object);
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 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_string;
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.util.function.LongFunction;
/**
* This variant of Concat allows you to apply a string concatenation to a series of
* string produced by the provided functions. Each position of a delimiter will simply contain
* all generated values, although usually, you won't need more than one.
*/
@ThreadSafeMapper
@Categories({Category.functional})
public class ConcatArray extends Concat {
private final int size;
private final String delimiter;
@Example({"ConcatArray(',',5,'{{}}', NumberNameToString())"})
public ConcatArray(String delimiter, int size, String template, Object... functions) {
super(template, functions);
this.delimiter = delimiter;
this.size = size;
}
@Override
public String apply(long cycle) {
StringBuilder buffer = new StringBuilder(1024);
for (int i = 0; i < literals.length - 1; i++) {
buffer.append(literals[i]);
for (int element = 0; element < this.size; element++) {
long value = cycleStepMapper.applyAsLong(cycle, element);
int funcIdx = Math.min(functions.length - 1, element);
LongFunction<String> selectedFunction = functions[funcIdx];
String string = selectedFunction.apply(value);
buffer.append(string).append(delimiter);
}
buffer.setLength(buffer.length()-delimiter.length());
}
buffer.append(literals[literals.length - 1]);
return buffer.toString();
}
}

View File

@ -0,0 +1 @@
["{ \" id \" : \" 0704. 0001 \", \" title \" : \" calculation of prompt diphoton production cross sections at tevatron and \\ n lhc energies \", \" abstract \" : \" a fully differential calculation in perturbative quantum chromodynamics is \\ npresented for the production of massive photon pairs at hadron colliders. all \\ nnext - to - leading order perturbative contributions from quark - antiquark, \\ ngluon - ( anti ) quark, and gluon - gluon subprocesses are included, as well as \\ nall - orders resummation of initial - state gluon radiation valid at \\ nnext - to - next - to - leading logarithmic accuracy. the region of phase space is \\ nspecified in which the calculation is most reliable. good agreement is \\ ndemonstrated with data from the fermilab tevatron, and predictions are made for \\ nmore detailed tests with cdf and do data. predictions are shown for \\ ndistributions of diphoton pairs produced at the energy of the large hadron \\ ncollider ( lhc ). distributions of the diphoton pairs from the decay of a higgs \\ nboson are contrasted with those produced from qcd processes at the lhc, showing \\ nthat enhanced sensitivity to the signal can be obtained with judicious \\ nselection of events. \\ n \", \" categories \" : \" hep - ph \" } { \" id \" : \" 0704. 0002 \", \" title \" : \" sparsity - certifying graph decompositions \", \" abstract \" : \" we describe a new algorithm, the $ ( k, \\ \\ ell ) $ - pebble game with colors, and use \\ nit obtain a characterization of the family of $ ( k, \\ \\ ell ) $ - sparse graphs and \\ nalgorithmic solutions to a family of problems concerning tree decompositions of \\ ngraphs. special instances of sparse graphs appear in rigidity theory and have \\ nreceived increased attention in recent years. in particular, our colored \\ npebbles generalize and strengthen the previous results of lee and streinu and \\ ngive a new proof of the tutte - nash - williams characterization of arboricity",". we \\ nalso present a new decomposition that certifies sparsity based on the \\ n $ ( k, \\ \\ ell ) $ - pebble game with colors. our work also exposes connections between \\ npebble game algorithms and previous sparse graph algorithms by gabow, gabow and \\ nwestermann and hendrickson. \\ n \", \" categories \" : \" math. co cs. cg \" } { \" id \" : \" 0704. 0003 \", \" title \" : \" the evolution of the earth - moon system based on the dark matter field \\ n fluid model \", \" abstract \" : \" the evolution of earth - moon system is described by the dark matter field \\ nfluid model proposed in the meeting of division of particle and field 2004, \\ namerican physical society. the current behavior of the earth - moon system agrees \\ nwith this model very well and the general pattern of the evolution of the \\ nmoon - earth system described by this model agrees with geological and fossil \\ nevidence. the closest distance of the moon to earth was about 259000 km at 4. 5 \\ nbillion years ago, which is far beyond the roche's limit. the result suggests \\ nthat the tidal friction may not be the primary cause for the evolution of the \\ nearth - moon system. the average dark matter field fluid constant derived from \\ nearth - moon system data is 4. 39 x 10 ^ ( - 22 ) s ^ ( - 1 ) m ^ ( - 1 ). this model predicts \\ nthat the mars's rotation is also slowing with the angular acceleration rate \\ nabout - 4. 38 x 10 ^ ( - 22 ) rad s ^ ( - 2 ). \\ n \", \" categories \" : \" physics. gen - ph \" } { \" id \" : \" 0704. 0004 \", \" title \" : \" a determinant of stirling cycle numbers counts unlabeled acyclic \\ n single - source automata \", \" abstract \" : \" we show that a determinant of stirling cycle numbers counts unlabeled acyclic \\ nsingle - source automata. the proof involves a bijection from these automata to \\ ncertain marked lattice paths and a sign - reversing involution to evaluate the \\ ndeterminant. \\ n"]