Exemplo n.º 1
0
  @Override
  public String getSchemaJSON() {
    if (!changed && schemaJSON != null) {
      return schemaJSON;
    }

    if (schemaKeys == null) {
      schema.remove(Schema.FIELD_SCHEMA_KEYS);
    } else {
      try {
        schema.put(Schema.FIELD_SCHEMA_KEYS, SchemaUtils.createJSONObject(schemaKeys));
      } catch (JSONException ex) {
        throw new RuntimeException(ex);
      }
    }

    schemaJSON = schema.toString();
    return schemaJSON;
  }
Exemplo n.º 2
0
  /**
   * This is a helper method to initialize the schema.
   *
   * @throws JSONException This exception is thrown if there is an error parsing the JSON which
   *     specified this schema.
   */
  private void initialize() throws JSONException {
    schema = new JSONObject(schemaJSON);

    Preconditions.checkState(
        schema.length() == NUM_KEYS_FIRST_LEVEL,
        "Expected "
            + NUM_KEYS_FIRST_LEVEL
            + " keys in the first level but found "
            + schema.length());

    if (schemaKeys != null) {
      schema.put(Schema.FIELD_SCHEMA_KEYS, SchemaUtils.createJSONObject(schemaKeys));
    }

    valueToType = Maps.newHashMap();

    JSONArray values = schema.getJSONArray(FIELD_VALUES);

    Preconditions.checkState(values.length() > 0, "The schema does not specify any values.");

    for (int index = 0; index < values.length(); index++) {
      JSONObject value = values.getJSONObject(index);
      String name = value.getString(FIELD_VALUES_NAME);
      String typeName = value.getString(FIELD_VALUES_TYPE);

      Type type = Type.NAME_TO_TYPE.get(typeName);
      valueToType.put(name, type);

      Preconditions.checkArgument(type != null, typeName + " is not a valid type.");
    }

    valueToType = Collections.unmodifiableMap(valueToType);
    valuesDescriptor = new FieldsDescriptor(valueToType);

    try {
      schema.put(FIELD_SCHEMA_TYPE, SCHEMA_TYPE);
      schema.put(FIELD_SCHEMA_VERSION, SCHEMA_VERSION);
    } catch (JSONException e) {
      throw new RuntimeException(e);
    }

    schemaJSON = this.schema.toString();
  }
  @Override
  public String getSchemaJSON() {
    if (!changed && schemaJSON != null) {
      // If there are no changes, return the pre computed JSON
      return schemaJSON;
    }

    if (changedSchemaKeys) {
      // If the schema keys change, recompute the schema keys portion of the JSON
      changedSchemaKeys = false;

      if (schemaKeys == null) {
        schema.remove(Schema.FIELD_SCHEMA_KEYS);
      } else {
        try {
          schema.put(Schema.FIELD_SCHEMA_KEYS, SchemaUtils.createJSONObject(schemaKeys));
        } catch (JSONException ex) {
          throw new RuntimeException(ex);
        }
      }
    }

    if (changedFromTo) {
      // If the from to times have changed then recompute the time portion of the schema.
      changedFromTo = false;
      Preconditions.checkState(
          !(from == null ^ to == null),
          "Either both from and to should be set or both should be not set.");

      if (from != null) {
        Preconditions.checkState(
            to >= from, "to {} must be greater than or equal to from {}.", to, from);
      }

      if (from == null) {
        time.remove(FIELD_TIME_FROM);
        time.remove(FIELD_TIME_TO);
      } else {
        try {
          time.put(FIELD_TIME_FROM, from);
          time.put(FIELD_TIME_TO, to);
        } catch (JSONException ex) {
          throw new RuntimeException(ex);
        }
      }
    }

    if (this.areEnumsUpdated) {
      // If the enums have been updated, recompute the key portion of the schema.
      for (int keyIndex = 0; keyIndex < keys.length(); keyIndex++) {
        JSONObject keyData;
        String name;

        try {
          keyData = keys.getJSONObject(keyIndex);
          name = keyData.getString(DimensionalConfigurationSchema.FIELD_KEYS_NAME);
        } catch (JSONException ex) {
          throw new RuntimeException(ex);
        }

        List<Object> enumVals = currentEnumVals.get(name);

        if (enumVals == null || enumVals.isEmpty()) {
          keyData.remove(DimensionalConfigurationSchema.FIELD_KEYS_ENUMVALUES);
          continue;
        }

        JSONArray newEnumValues = new JSONArray();

        for (Object enumVal : enumVals) {
          newEnumValues.put(enumVal);
        }

        try {
          keyData.put(DimensionalConfigurationSchema.FIELD_KEYS_ENUMVALUES, newEnumValues);
        } catch (JSONException ex) {
          throw new RuntimeException(ex);
        }
      }

      this.areEnumsUpdated = false;
    }

    // Rebuild the schema JSON string.
    schemaJSON = schema.toString();
    return schemaJSON;
  }
  /**
   * Initializes the schema JSON and schema metadata.
   *
   * @throws JSONException This exception is thrown when there is an exception building the schema
   *     for the AppData dimensions schema.
   */
  private void initialize() throws JSONException {
    schema = new JSONObject();

    if (schemaKeys != null) {
      schema.put(Schema.FIELD_SCHEMA_KEYS, SchemaUtils.createJSONObject(schemaKeys));
    }

    schema.put(SnapshotSchema.FIELD_SCHEMA_TYPE, DimensionalSchema.SCHEMA_TYPE);
    schema.put(SnapshotSchema.FIELD_SCHEMA_VERSION, DimensionalSchema.SCHEMA_VERSION);

    if (!configurationSchema.getTags().isEmpty()) {
      schema.put(FIELD_TAGS, new JSONArray(configurationSchema.getTags()));
    }

    // time
    time = new JSONObject();
    schema.put(FIELD_TIME, time);
    JSONArray bucketsArray = new JSONArray(configurationSchema.getBucketsString());
    time.put(FIELD_TIME_BUCKETS, bucketsArray);
    time.put(FIELD_SLIDING_AGGREGATE_SUPPORTED, true);

    // keys
    keys = new JSONArray(configurationSchema.getKeysString());

    for (int keyIndex = 0; keyIndex < keys.length(); keyIndex++) {
      JSONObject keyJo = keys.getJSONObject(keyIndex);
      String keyName = keyJo.getString(DimensionalConfigurationSchema.FIELD_KEYS_NAME);
      List<String> tags = configurationSchema.getKeyToTags().get(keyName);

      if (!tags.isEmpty()) {
        keyJo.put(FIELD_TAGS, new JSONArray(tags));
      }
    }

    schema.put(DimensionalConfigurationSchema.FIELD_KEYS, keys);

    // values
    JSONArray values = new JSONArray();
    schema.put(SnapshotSchema.FIELD_VALUES, values);

    FieldsDescriptor inputValuesDescriptor = configurationSchema.getInputValuesDescriptor();
    Map<String, Map<String, Type>> allValueToAggregator =
        configurationSchema.getSchemaAllValueToAggregatorToType();

    for (Map.Entry<String, Map<String, Type>> entry : allValueToAggregator.entrySet()) {
      String valueName = entry.getKey();

      for (Map.Entry<String, Type> entryAggType : entry.getValue().entrySet()) {
        String aggregatorName = entryAggType.getKey();
        Type outputValueType = entryAggType.getValue();

        JSONObject value = new JSONObject();
        String combinedName =
            valueName + DimensionalConfigurationSchema.ADDITIONAL_VALUE_SEPERATOR + aggregatorName;
        value.put(SnapshotSchema.FIELD_VALUES_NAME, combinedName);
        value.put(SnapshotSchema.FIELD_VALUES_TYPE, outputValueType.getName());

        List<String> tags = configurationSchema.getValueToTags().get(valueName);

        if (!tags.isEmpty()) {
          value.put(FIELD_TAGS, new JSONArray(tags));
        }

        values.put(value);
      }
    }

    JSONArray dimensions = new JSONArray();

    for (int combinationID = 0;
        combinationID < configurationSchema.getDimensionsDescriptorIDToKeys().size();
        combinationID++) {

      Fields fields = configurationSchema.getDimensionsDescriptorIDToKeys().get(combinationID);
      Map<String, Set<String>> fieldToAggregatorAdditionalValues =
          configurationSchema
              .getDimensionsDescriptorIDToFieldToAggregatorAdditionalValues()
              .get(combinationID);

      JSONObject combination = new JSONObject();
      JSONArray combinationArray = new JSONArray();

      for (String field : fields.getFields()) {
        combinationArray.put(field);
      }

      combination.put(
          DimensionalConfigurationSchema.FIELD_DIMENSIONS_COMBINATIONS, combinationArray);

      if (!fieldToAggregatorAdditionalValues.isEmpty()) {
        JSONArray additionalValueArray = new JSONArray();

        for (Map.Entry<String, Set<String>> entry : fieldToAggregatorAdditionalValues.entrySet()) {
          String valueName = entry.getKey();

          for (String aggregatorName : entry.getValue()) {
            JSONObject additionalValueObject = new JSONObject();
            String combinedName =
                valueName
                    + DimensionalConfigurationSchema.ADDITIONAL_VALUE_SEPERATOR
                    + aggregatorName;
            Type inputValueType = inputValuesDescriptor.getType(valueName);

            if (!configurationSchema.getAggregatorRegistry().isAggregator(aggregatorName)) {
              if (aggregatorName == null) {
                LOG.error("{} is not a valid aggregator.", aggregatorName);
              }
            }

            Type outputValueType;

            if (configurationSchema
                .getAggregatorRegistry()
                .isIncrementalAggregator(aggregatorName)) {
              IncrementalAggregator aggregator =
                  configurationSchema
                      .getAggregatorRegistry()
                      .getNameToIncrementalAggregator()
                      .get(aggregatorName);

              outputValueType = aggregator.getOutputType(inputValueType);
            } else {
              outputValueType =
                  configurationSchema
                      .getAggregatorRegistry()
                      .getNameToOTFAggregators()
                      .get(aggregatorName)
                      .getOutputType();
            }

            additionalValueObject.put(
                DimensionalConfigurationSchema.FIELD_VALUES_NAME, combinedName);
            additionalValueObject.put(
                DimensionalConfigurationSchema.FIELD_VALUES_TYPE, outputValueType.getName());
            additionalValueArray.put(additionalValueObject);
          }
        }

        combination.put(
            DimensionalConfigurationSchema.FIELD_DIMENSIONS_ADDITIONAL_VALUES,
            additionalValueArray);
      }

      dimensions.put(combination);
    }

    schema.put(DimensionalConfigurationSchema.FIELD_DIMENSIONS, dimensions);

    this.schemaJSON = this.schema.toString();
  }