示例#1
0
  /**
   * Change the shared value value irrespective of its previous state
   *
   * @param newValue new value
   * @throws Exception ZK errors, interruptions, etc.
   */
  public void setValue(byte[] newValue) throws Exception {
    Preconditions.checkState(state.get() == State.STARTED, "not started");

    client.setData().forPath(path, newValue);
    stat.setVersion(stat.getVersion() + 1);
    value = Arrays.copyOf(newValue, newValue.length);
  }
示例#2
0
 public static void copyStat(Stat from, Stat to) {
   to.setAversion(from.getAversion());
   to.setCtime(from.getCtime());
   to.setCversion(from.getCversion());
   to.setCzxid(from.getCzxid());
   to.setMtime(from.getMtime());
   to.setMzxid(from.getMzxid());
   to.setPzxid(from.getPzxid());
   to.setVersion(from.getVersion());
   to.setEphemeralOwner(from.getEphemeralOwner());
   to.setDataLength(from.getDataLength());
   to.setNumChildren(from.getNumChildren());
 }
示例#3
0
  /**
   * Changes the shared value only if its value has not changed since this client last read it. If
   * the value has changed, the value is not set and this client's view of the value is updated.
   * i.e. if the value is not successful you can get the updated value by calling {@link
   * #getValue()}.
   *
   * @param newValue the new value to attempt
   * @return true if the change attempt was successful, false if not. If the change was not
   *     successful, {@link #getValue()} will return the updated value
   * @throws Exception ZK errors, interruptions, etc.
   */
  public boolean trySetValue(byte[] newValue) throws Exception {
    Preconditions.checkState(state.get() == State.STARTED, "not started");

    try {
      client.setData().withVersion(stat.getVersion()).forPath(path, newValue);
      stat.setVersion(stat.getVersion() + 1);
      value = Arrays.copyOf(newValue, newValue.length);
      return true;
    } catch (KeeperException.BadVersionException ignore) {
      // ignore
    }

    readValue();
    return false;
  }