Class ParsedTemplateMap

java.lang.Object
io.nosqlbench.engine.api.templating.ParsedTemplateMap
All Implemented Interfaces:
DynamicFieldReader, StaticFieldReader, LongFunction<Map<String,?>>

public class ParsedTemplateMap extends Object implements LongFunction<Map<String,?>>, StaticFieldReader, DynamicFieldReader
A parsed map template, which allows construction of extracted or projected functions related to dynamic value templates. This is the backing implementation for ParsedOp which is map-based at the root level, but which can have recursive structure and thus needed an inner value type. This implementation supports parsing/restructuring around string, map, and list templates. The provided map is interpreted as a map of string to object templates using these rules:
  1. If the value is a String and contains no binding points, it is interpreted as a literal
  2. If the value is a String and contains only a binding point with no leading nor trailing text, it is interpreted as an object binding
  3. If the value is a String and contains a binding point with any leading or trailing text, it is interpreted as a String template binding
  4. If the value is a map, list, or set, then each element is interpreted as above
TODO: Proactively check casting on functional methods, fallback to NBTypeConverter only if needed and can
  • Constructor Details

  • Method Details

    • getCaptures

      public CapturePoints getCaptures()
    • isDynamic

      public boolean isDynamic()
      Returns:
      true if any field of this template map is dynamic
    • isStatic

      public boolean isStatic()
    • isConfig

      public boolean isConfig(String fieldname)
    • getStaticPrototype

      public Map<String,Object> getStaticPrototype()
    • getDynamicPrototype

      public Map<String,LongFunction<?>> getDynamicPrototype()
    • getConfigPrototype

      public Map<String,Object> getConfigPrototype()
    • apply

      public Map<String,Object> apply(long value)
      create a map of op field names and values, containing all statically and dynamically defined fields, but not including auxilliary config like params or activity params.
      Specified by:
      apply in interface LongFunction<Map<String,?>>
      Parameters:
      value - The input value to the binding functions
      Returns:
      A Map of String to Object
    • applyFull

      public Map<String,Object> applyFull(long value)
    • isDynamic

      public boolean isDynamic(String field)
      Specified by:
      isDynamic in interface DynamicFieldReader
      Returns:
      true if the specified field is found in the dynamic op fields
    • isStatic

      public boolean isStatic(String field)
      Returns:
      true if and only if the named field is present in the static field map.
    • isStatic

      public boolean isStatic(String field, Class<?> type)
      Returns:
      true if and only if the named field is present in the static field map and the type is assignable to the specified class
    • isDefined

      public boolean isDefined(String... fields)
      Specified by:
      isDefined in interface StaticFieldReader
      Parameters:
      fields - Names of fields to look for in the static, dynamic, or config field maps.
      Returns:
      true if and only if all provided field names are present in the static or dynamic or config field maps.
    • getStaticValue

      public <T> T getStaticValue(String field, Class<T> classOfT)
      Get the static value for the provided name, cast to the required type.
      Specified by:
      getStaticValue in interface StaticFieldReader
      Type Parameters:
      T - The parameter type of the return type, used at compile time only to qualify asserted return type
      Parameters:
      field - Name of the field to get
      classOfT - The type of the field to return. If actual type is not compatible to a cast to this type, then a casting error will be thrown.
      Returns:
      A value of type T, or null
    • takeStaticValue

      public <T> T takeStaticValue(String field, Class<T> classOfT)
    • getStaticValue

      public <T> T getStaticValue(String field)
      Get the static value for the provided name, cast to the required type, where the type is inferred from the calling context.
      Specified by:
      getStaticValue in interface StaticFieldReader
      Type Parameters:
      T - The parameter type of the return type. used at compile time only to quality return type.
      Parameters:
      field - Name of the field to get
      Returns:
      A value of type T, or null
    • getStaticValueOr

      public <T> T getStaticValueOr(String name, T defaultValue)
      Get the named static field value, or return the provided default, but throw an exception if the named field is dynamic.
      Specified by:
      getStaticValueOr in interface StaticFieldReader
      Type Parameters:
      T - The type of the field to return.
      Parameters:
      name - The name of the field value to return.
      defaultValue - A value to return if the named value is not present in static nor dynamic fields.
      Returns:
      The value
      Throws:
      RuntimeException - if the field name is only present in the dynamic fields.
    • getStaticConfigOr

      public <T> T getStaticConfigOr(String name, T defaultValue)
      Get the specified parameter by the user using the defined field which is closest to the op template. This is the standard way of getting parameter values which can be specified at the op template, op param, or activity level.
      Type Parameters:
      T - The type of the value to return
      Parameters:
      name - The name of the configuration param
      defaultValue - the default value to return if the value is not defined anywhere in (op fields, op params, activity params)
      Returns:
      A configuration value
      Throws:
      NBConfigError - if the named field is defined dynamically, as in this case, it is presumed that the parameter is not supported unless it is defined statically.
    • getStaticConfig

      public <T> T getStaticConfig(String name, Class<T> clazz)
    • takeStaticConfigOr

      public <T> T takeStaticConfigOr(String name, T defaultValue)
    • getOptionalStaticConfig

      public <T> Optional<T> getOptionalStaticConfig(String name, Class<T> type)
    • getConfigOr

      public <T> T getConfigOr(String name, T defaultValue, long input)
      Works exactly like getStaticConfigOr(String, Object), except that dynamic values at the op field level will be generated on a per-input basis. This is a shortcut method for allowing configuration values to be accessed dynamically where it makes sense.
    • getOptionalStaticValue

      public <T> Optional<T> getOptionalStaticValue(String field, Class<T> classOfT)
      Return an optional value for the named field. This is an Optional form of getStaticValue(java.lang.String, java.lang.Class<T>).
      Specified by:
      getOptionalStaticValue in interface StaticFieldReader
      Type Parameters:
      T - The parameter type of the return
      Parameters:
      field - Name of the field to get
      classOfT - The type of field to return. If the actual type is not compatible to a cast to this type, then a casting error will be thrown.
      Returns:
      An optional value, empty unless the named value is defined in the static field map.
    • get

      public <T> T get(String field, long input)
      Get the named field value for a given long input. This uses parameter type inference -- The casting to the return type will be based on the type of any assignment or casting on the caller's side. Thus, if the actual type is not compatable to a cast to the needed return type, a casting error will be thrown.
      Specified by:
      get in interface DynamicFieldReader
      Type Parameters:
      T - The parameter type of the returned value. Inferred from usage context.
      Parameters:
      field - The name of the field to get.
      input - The seed value, or cycle value for which to generate the value.
      Returns:
      The value.
    • getOpFieldNames

      public Set<String> getOpFieldNames()
      Returns:
      a set of names which are defined, whether in static fields or dynamic fields, but NOT including params nor other config
    • getAsRequiredFunction

      public LongFunction<? extends String> getAsRequiredFunction(String name)
      Get the op field as a LongFunction of String. This is a convenience form for getAsRequiredFunction(String, Class)
      Parameters:
      name - The field name which must be defined as static or dynamic
      Returns:
      A function which can provide the named field value
    • getAsOptionalEnumFunction

      public <V extends Enum<V>> Optional<LongFunction<V>> getAsOptionalEnumFunction(String name, Class<? extends V> type)
    • getAsOptionalFunction

      public <V> Optional<LongFunction<V>> getAsOptionalFunction(String name, Class<? extends V> type)
      Get the op field as a LongFunction, using statics, then dynamics, then config sources.
      Parameters:
      name - The field name which must be defined as static or dynamic
      type - The value type which the field must be assignable to
      Returns:
      A function which can provide a value for the given name and type
    • getAsRequiredFunction

      public <V> LongFunction<V> getAsRequiredFunction(String name, Class<? extends V> type)
    • getAsFunctionOr

      public <V> LongFunction<V> getAsFunctionOr(String name, V defaultValue)
      Get a LongFunction which returns either the static value, the dynamic value, or the default value, in that order, depending on where it is found first.
      Specified by:
      getAsFunctionOr in interface DynamicFieldReader
      Type Parameters:
      V - The type of value to return
      Parameters:
      name - The param name for the value
      defaultValue - The default value to provide the value is not defined for static nor dynamic
      Returns:
      A LongFunction of type V
    • getAsCachedFunctionOr

      public <V> LongFunction<V> getAsCachedFunctionOr(String fieldname, String defaultValue, Function<String,V> init)
      Get a LongFunction that first creates a LongFunction of String as in getAsFunctionOr(String, Object) )}, but then applies the result and cached it for subsequent access. This relies on ObjectCache internally. The purpose of this is to avoid costly re-computation for mapped values over pure functions where the computation cost is significantly high. For trivial functions, the cost is generally lower than the hash lookup within the object cache..
      Type Parameters:
      V - The type of object to return
      Parameters:
      fieldname - The name of the field which could contain a static or dynamic value
      defaultValue - The default value to use in the init function if the fieldname is not defined as static nor dynamic
      init - A function to apply to the value to produce the product type
      Returns:
      A caching function which chains to the init function, with caching
    • isDefined

      public boolean isDefined(String field)
      Specified by:
      isDefined in interface StaticFieldReader
      Parameters:
      field - The requested field name
      Returns:
      true if the named field is defined as static or dynamic or config (params and activity params)
    • isUndefined

      public boolean isUndefined(String field)
      Inverse of isDefined(String), provided for clarify in some situations
      Parameters:
      field - The field name
      Returns:
      true if the named field is defined neither as static nor as dynamic
    • isDefined

      public boolean isDefined(String field, Class<?> type)
      Specified by:
      isDefined in interface StaticFieldReader
      Parameters:
      field - The requested field name
      type - The required type of the field value
      Returns:
      true if the named field is defined as static or dynamic and the value produced can be assigned to the specified type
    • takeAsOptionalStringTemplate

      public Optional<ParsedTemplateString> takeAsOptionalStringTemplate(String field)
    • takeAsOptionalRawSpecifier

      public <V> Optional<V> takeAsOptionalRawSpecifier(String field)
    • takeAsNamedTemplates

      public Map<String,ParsedTemplateString> takeAsNamedTemplates(String fieldname)
      Take the value of the specified field from the dynamic or static layers, or reference it from the config layer without removal. Then, flatten any string, list, or map structures into a map of strings with names injected as needed. Then, convert the values to string templates and return that.
      Parameters:
      fieldname - the field to take the templates from
      Returns:
      A map of templates, or an empty map if the field is not defined or is empty.
    • getAsStringTemplate

      public Optional<ParsedTemplateString> getAsStringTemplate(String fieldname)
    • isDefinedAll

      public boolean isDefinedAll(String... fields)
      convenience method for conjugating isDefined(String) with AND
      Returns:
      true if all specified fields are defined as static or dynamic or config
    • newListBinder

      public LongFunction<List<Object>> newListBinder(String... fields)
      Parameters:
      fields - The ordered field names for which the ListBinder will be created
      Returns:
      a new ListBinder which can produce a List of Objects from a long input.
    • newListBinder

      public LongFunction<List<Object>> newListBinder(List<String> fields)
      Parameters:
      fields - The ordered field names for which the ListBinder will be created
      Returns:
      a new ListBinder which can produce a List of Objects from a long input.
    • newOrderedMapBinder

      public LongFunction<Map<String,Object>> newOrderedMapBinder(String... fields)
      Parameters:
      fields - The ordered field names for which the OrderedMapBinder will be created
      Returns:
      a new OrderedMapBinder which can produce a Map of String to Objects from a long input.
    • newArrayBinder

      public LongFunction<Object[]> newArrayBinder(String... fields)
      Parameters:
      fields - The ordered field names for which the ArrayBinder will be created
      Returns:
      a new ArrayBinder which can produce a Object array from a long input.
    • newArrayBinder

      public LongFunction<Object[]> newArrayBinder(List<String> fields)
      Parameters:
      fields - The ordered field names for which the ArrayBinder will be created
      Returns:
      a new ArrayBinder which can produce a Object array from a long input.
    • newArrayBinderFromBindPoints

      public LongFunction<Object[]> newArrayBinderFromBindPoints(List<BindPoint> bindPoints)
      Parameters:
      bindPoints - The BindPoints for which the ArrayBinder will be created
      Returns:
      a new ArrayBinder which can produce a Object array from a long input.
    • getMapper

      public LongFunction<?> getMapper(String field)
      Get the LongFunction which is used to resolve a dynamic field value.
      Parameters:
      field - The field name for a dynamic parameter
      Returns:
      The mapping function
    • getSize

      public int getSize()
      Returns:
      the logical map size, including all static and dynamic fields
    • getValueType

      public Class<?> getValueType(String fieldname)
    • getOptionalTargetEnum

      public <E extends Enum<E>, V> Optional<TypeAndTarget<E,V>> getOptionalTargetEnum(Class<E> enumclass, String typeFieldName, String valueFieldName, Class<V> valueClass)
    • getOptionalTargetEnum

      public <E extends Enum<E>, V> Optional<TypeAndTarget<E,V>> getOptionalTargetEnum(Class<E> enumclass, Class<V> valueClass, String alternateTypeField, String alternateValueField)
    • getOptionalTargetEnum

      public <E extends Enum<E>, V> Optional<TypeAndTarget<E,V>> getOptionalTargetEnum(Class<E> enumclass, Class<? extends V> valueClass)
      Given an enum of any type, return the enum value which is found in any of the field names of the op template, ignoring case and any non-word characters. This is useful for matching op templates to op types where the presence of a field determines the type. Further, if there are multiple matching names, an OpConfigError is thrown to avoid possible ambiguity.
      Type Parameters:
      E - Generic type for the enum class
      Parameters:
      enumclass - The enum class for matching values
      Returns:
      Optionally, an enum value which matches, or Optional.empty()
      Throws:
      OpConfigError - if more than one field matches
    • getTargetEnum

      public <E extends Enum<E>, V> TypeAndTarget<E,V> getTargetEnum(Class<E> enumclass, Class<V> valueClass, String tname, String vname)
    • getTargetEnum

      public <E extends Enum<E>, V> TypeAndTarget<E,V> getTargetEnum(Class<E> enumclass, Class<V> valueClass)
    • getOptionalEnumFromField

      public <E extends Enum<E>> Optional<E> getOptionalEnumFromField(Class<E> enumclass, String fieldname)
      Map a named op field to an enum
      Type Parameters:
      E - The generic type of the enum
      Parameters:
      enumclass - The type of enum to look within
      fieldname - The field name to look for
      Returns:
      An optional enum value
    • takeOptionalEnumFromField

      public <E extends Enum<E>> Optional<E> takeOptionalEnumFromField(Class<E> enumClass, String fieldName)
    • parseStaticCmdMap

      public Map<String,Object> parseStaticCmdMap(String taskname, String mainField)
    • parseStaticCmdMaps

      public List<Map<String,Object>> parseStaticCmdMaps(String key, String mainField)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isDefinedExactly

      public boolean isDefinedExactly(String[] fields)
    • getCombinedPrototype

      public Map<String,Object> getCombinedPrototype()