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

org.apache.solr.common.cloud.ZkNodeProps Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.solr.common.cloud;

import static org.apache.solr.common.util.Utils.toJSONString;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.apache.solr.common.MapWriter;
import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.common.util.Utils;

/** ZkNodeProps contains generic immutable properties. */
public class ZkNodeProps implements MapWriter {

  protected final Map propMap;

  /** Construct ZKNodeProps from map. */
  public ZkNodeProps(Map propMap) {
    this.propMap = new HashMap<>(propMap); // We need propMap to be mutable

    // TODO: store an unmodifiable map, but in a way that guarantees not to wrap more than once.
    // Always wrapping introduces a memory leak.
  }

  public ZkNodeProps(MapWriter mw) {
    propMap = mw.toMap(new HashMap<>());
  }

  public ZkNodeProps plus(String key, Object val) {
    return plus(Collections.singletonMap(key, val));
  }

  public ZkNodeProps plus(Map newVals) {
    LinkedHashMap copy = new LinkedHashMap<>(propMap);
    if (newVals == null || newVals.isEmpty()) return new ZkNodeProps(copy);
    copy.putAll(newVals);
    return new ZkNodeProps(copy);
  }

  /**
   * Constructor that populates the from array of Strings in form key1, value1, key2, value2, ...,
   * keyN, valueN
   */
  public ZkNodeProps(String... keyVals) {
    this(Utils.makeMap((Object[]) keyVals));
  }

  /**
   * @deprecated use {@link ZkNodeProps#ZkNodeProps(String...)}
   */
  @Deprecated(since = "9.0.0")
  public static ZkNodeProps fromKeyVals(Object... keyVals) {
    return new ZkNodeProps(Utils.makeMap(keyVals));
  }

  /** Get property keys. */
  public Set keySet() {
    return propMap.keySet();
  }

  /** Get all properties as map. */
  public Map getProperties() {
    return propMap;
  }

  /** Returns a shallow writable copy of the properties */
  public Map shallowCopy() {
    return new LinkedHashMap<>(propMap);
  }

  /** Create Replica from json string that is typically stored in zookeeper. */
  @SuppressWarnings({"unchecked"})
  public static ZkNodeProps load(byte[] bytes) {
    Map props;
    if (bytes[0] == 2) {
      try (JavaBinCodec jbc = new JavaBinCodec()) {
        props = (Map) jbc.unmarshal(bytes);
      } catch (IOException e) {
        throw new RuntimeException("Unable to parse javabin content");
      }
    } else {
      props = (Map) Utils.fromJSON(bytes);
    }
    return new ZkNodeProps(props);
  }

  /** Get a string property value. */
  public String getStr(String key) {
    return getStr(key, null);
  }

  /** Get a string property value. */
  public Integer getInt(String key, Integer def) {
    Object o = propMap.get(key);
    return o == null ? def : Integer.valueOf(o.toString());
  }

  /** Get a string property value. */
  public String getStr(String key, String def) {
    Object o = propMap.get(key);
    return o == null ? def : o.toString();
  }

  public Object get(String key) {
    return propMap.get(key);
  }

  @Override
  public String toString() {
    return toJSONString(this);
  }

  /** Check if property key exists. */
  public boolean containsKey(String key) {
    return propMap.containsKey(key);
  }

  public boolean getBool(String key, boolean b) {
    Object o = propMap.get(key);
    if (o == null) return b;
    if (o instanceof Boolean) return (boolean) o;
    return Boolean.parseBoolean(o.toString());
  }

  @Override
  public void writeMap(EntryWriter ew) throws IOException {
    propMap.forEach(ew.getBiConsumer());
  }

  @Override
  public boolean equals(Object that) {
    return that instanceof ZkNodeProps && ((ZkNodeProps) that).propMap.equals(this.propMap);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(propMap);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy