All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nstream.adapter.common.ext.PulsarIngressJetTranslator Maven / Gradle / Ivy

There is a newer version: 4.14.22
Show newest version
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// 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 nstream.adapter.common.ext;

import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import swim.structure.Attr;
import swim.structure.Record;
import swim.structure.Value;
import swim.util.Log;

class PulsarIngressJetTranslator extends JetTranslator {

  private static final Map ENTRIES = Map.of(
      "clientProvisionName", Entry.STRING,
      "consumerPropertiesProvisionName", Entry.STRING,
      "schemaType", Entry.STRING,
      "contentEncodingOverride", Entry.STRING,
      "contentTypeOverride", Entry.STRING,
      "valueMolder", Entry.RECON,
      "relaySchema", Entry.RECON);

  PulsarIngressJetTranslator() {
    super(PulsarIngressSettings.form().tag());
  }

  @Override
  public PulsarIngressSettings translate(Log log, PulsarIngressSettings settings, Properties unified) {
    return translate(log, settings, PulsarIngressSettings.form(), unified);
  }

  @Override
  public Record moldFromProperties(Log log, Properties p) {
    return moldFromProperties(log, p, ENTRIES);
  }

  @Override
  protected Record negotiateSchemas(Log log, PulsarIngressSettings settings, Record mold, Properties unified) {
    return negotiateAvro(log, settings, mold, unified);
  }

  Record negotiateAvro(Log log, PulsarIngressSettings settings, Record mold, Properties unified) {
    final String avroSchema = unified.getProperty("valueAvroSchema", unified.getProperty("value.avro.schema", null));
    if (settings.schemaType().toLowerCase(Locale.ROOT).contains("auto")) {
      mold = setPulsarGenericRecordAssembler(log, mold, settings.valueMolder(), "schema " + settings.schemaType());
    } else if (settings.contentTypeOverride() != null && settings.contentTypeOverride().contains("avro")) {
      mold = setSwimAvroAssembler(log, mold, "valueMolder", settings.valueMolder(), avroSchema);
    }
    return mold;
  }

  Record setPulsarGenericRecordAssembler(Log log, Record settingsMold,
                                         Value assembler, String replacee) {
    final Attr graAttr = Attr.of("valueAssembler", "nstream.adapter.pulsar.GenericRecordAssembler");
    if (!(assembler instanceof Record)) {
      log.info("Inferred valueMolder as pulsar.GenericRecordAssembler");
      settingsMold = settingsMold.updatedSlot("valueMolder", Record.create(1).item(graAttr));
    } else if (!(assembler.head().equals(graAttr))) {
      log.warn("Replaced valueMolder as GenericRecordAssembler from " + assembler.head()
          + " due to incompatibility with " + replacee);
      settingsMold = settingsMold.updatedSlot("valueMolder", Record.create(1).item(graAttr));
    }
    return settingsMold;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy