Class ParsedTemplateString

java.lang.Object
io.nosqlbench.virtdata.core.templates.ParsedTemplateString

public class ParsedTemplateString extends Object

A ParsedTemplate represents any string provided by a user which is meant to be a prototype for value used in an operation. A ParsedTemplate can represent string values with various levels of interpolation:

  • A single literal String value, such as
    select * from tablefoo;
    This is the simplest case, as there are no no bindings nor captures.
  • A value with named bindings interpolated into the string like
    select * from tablefoo where userid={userid};
    In this case, a single named binding point is specified. The userid value will be provided when the string needs to be fully composed before use.
  • A value with named captures like
    select [username] from tablefoo where userid=32;
    In this case, a single named capture is specified -- The field name username will be read from some native result form and saved into memory for later use.
  • A single binding reference:
    {username}
    This form is the simplest way to specify that a referenced binding should be used in a field. This form does not presume that you want the value to be read as a String. The type of value returned by bindings is used as-is, thus if you need to use a binding reference as a string, make sure your binding recipe returns a String directly. (All of the other forms presume to call .toString() automatically on any binding values.)

Details

Grammars used by native drivers can be decorated with named injection and extraction points for data, known respectively as BindPoints and CapturePoints. The syntax used for bind points and capture points is standard across all driver adapters. As such, this class captures the definition of decorative syntax and the rules for parsing them out. The key responsibilities of ParsedTemplate are:
  • recognize bind points within statement templates
  • recognize capture points within statement templates
  • render statement templates with bind and capture points elided using a native syntax for variables
  • provide metadata to drivers about defined bind and capture points
  • provide a text template for re-assembly with injected data
Once the parsed template is constructed, the method orError() should always called before it is used.

Validity Checks

A parsed template is considered to be valid if and only if the raw template contained only named anchors which were defined in the provided bindings. Extra bindings are not presumed to make a template invalid, but this interpretation is left to the caller for extra checks if needed.

Parsed Details

After parsing, the following details are available:

Parsed Spans

This is an alternating list of the literal sections of the raw template interspersed with the anchor names. This list always starts and ends with a literal section, so will always contain an odd number of elements. (some span sections may be empty if necessary, but not null)

Specific Bindings

These are the binding names and definitions which were used in a named anchor and also found in the provided bindings. If an anchor references a binding which is not provided, then it will not be in this map.

Missing Bindings

This is a list of binding names which were found in the raw template but which were not found in the provided bindings by name.

Extra Bindings

This is a list of binding names which were provided by the user, but which were not used in the raw template by name.

  • Constructor Details

    • ParsedTemplateString

      public ParsedTemplateString(String rawtemplate, Map<String,String> availableBindings)
      Parse the given raw template, check the bind points against the provided bindings, and provide detailed template checks for validity.
      Parameters:
      rawtemplate - A string template which contains optionally embedded named anchors
      availableBindings - The bindings which are provided by the user to fulfill the named anchors in this raw template
  • Method Details

    • of

      public static ParsedTemplateString of(String rawtemplate, Map<String,String> bindings)
    • getType

      public ParsedSpanType getType()
    • orError

      public ParsedTemplateString orError()
    • toString

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

      public boolean hasError()
      Returns:
      true if the parsed statement is not usable.
    • getMissing

      public Set<String> getMissing()
      Returns a list of binding names which were referenced in either
      {anchor}
      or
      ?anchor
      form, but which were not present in the provided bindings map. If any binding names are present in the returned set, then this binding will not be usable.
      Returns:
      A list of binding names which were referenced but not defined*
    • getAnchors

      public List<String> getAnchors()
      Returns:
      a list of anchors as found in the raw template.
    • getBindPoints

      public List<BindPoint> getBindPoints()
      Get the named anchors and their associated binding specifiers as found in the raw template.
      Returns:
      A list of bind points
      Throws:
      InvalidParameterException - if the template has an error, such as an anchor which has no provided binding.
    • getPositionalStatement

      public String getPositionalStatement(Function<String,String> tokenFormatter)
      Return the statement that can be used as-is by any driver specific version. This uses the anchor token as provided to yield a version of the statement which contains positional anchors, but no named bindings.
      Parameters:
      tokenFormatter - The mapping from a token name to a place holder
      Returns:
      A driver or usage-specific format of the statement, with anchors
    • getPositionalStatement

      public String getPositionalStatement()
    • getSpans

      public String[] getSpans()
      Return the parsed template in (literal, variable, ..., ..., literal) form.
      Returns:
      the sections of spans within the template
    • asBinding

      public Optional<BindPoint> asBinding()
      Returns the parsed template as a single binding spec if and only if the pattern matches a single binding anchor with no prefix or suffix.
      Returns:
      A single binding spec if that is all that was specified.
    • getCaptures

      public CapturePoints getCaptures()
    • getStmt

      public String getStmt()