Example usage for org.apache.zookeeper CreateMode fromFlag

List of usage examples for org.apache.zookeeper CreateMode fromFlag

Introduction

In this page you can find the example usage for org.apache.zookeeper CreateMode fromFlag.

Prototype

public static CreateMode fromFlag(int flag) throws KeeperException 

Source Link

Document

Map an integer value to a CreateMode value

Usage

From source file:com.haulmont.addon.zookeeper.jgroups.ConfigurableZooKeeperPing.java

License:Apache License

@Override
protected CreateMode getCreateMode() throws KeeperException {
    return CreateMode.fromFlag(mode);
}

From source file:com.zookeeper.zkclient.InMemoryConnection.java

License:Apache License

public List<OpResult> multi(Iterable<Op> ops) throws KeeperException, InterruptedException {
    List<OpResult> opResults = new ArrayList<OpResult>();
    for (Op op : ops) {
        if (Op.Check.class.isAssignableFrom(op.getClass())) {
            CheckVersionRequest check = (CheckVersionRequest) op.toRequestRecord();
            exists(check.getPath(), false);
            opResults.add(new OpResult.CheckResult());
        } else if (Op.Create.class.isAssignableFrom(op.getClass())) {
            CreateRequest create = (CreateRequest) op.toRequestRecord();
            String path = create(create.getPath(), create.getData(), CreateMode.fromFlag(create.getFlags()));
            opResults.add(new OpResult.CreateResult(path));
        } else if (Op.Delete.class.isAssignableFrom(op.getClass())) {
            DeleteRequest delete = (DeleteRequest) op.toRequestRecord();
            delete(delete.getPath());//from   www . j  av a2s.  c om
            opResults.add(new OpResult.DeleteResult());
        } else if (Op.SetData.class.isAssignableFrom(op.getClass())) {
            SetDataRequest setData = (SetDataRequest) op.toRequestRecord();
            writeData(setData.getPath(), setData.getData(), setData.getVersion());
            opResults.add(new OpResult.SetDataResult(null));
        }
    }
    return opResults;
}

From source file:org.apache.curator.framework.imps.CuratorMultiTransactionImpl.java

License:Apache License

@Override
public List<CuratorTransactionResult> forOperations(List<CuratorOp> operations) throws Exception {
    operations = Preconditions.checkNotNull(operations, "operations cannot be null");
    Preconditions.checkArgument(!operations.isEmpty(), "operations list cannot be empty");

    CuratorMultiTransactionRecord record = new CuratorMultiTransactionRecord();
    for (CuratorOp curatorOp : operations) {
        Schema schema = client.getSchemaSet().getSchema(curatorOp.getTypeAndPath().getForPath());
        record.add(curatorOp.get(), curatorOp.getTypeAndPath().getType(),
                curatorOp.getTypeAndPath().getForPath());
        if ((curatorOp.get().getType() == ZooDefs.OpCode.create)
                || (curatorOp.get().getType() == ZooDefs.OpCode.createContainer)) {
            CreateRequest createRequest = (CreateRequest) curatorOp.get().toRequestRecord();
            CreateMode createMode;/*from  w w w .  j ava  2 s. com*/
            if (client.isZk34CompatibilityMode()) {
                try {
                    createMode = CreateMode.fromFlag(createRequest.getFlags());
                } catch (KeeperException.BadArgumentsException dummy) {
                    createMode = CreateMode.PERSISTENT;
                }
            } else {
                createMode = CreateMode.fromFlag(createRequest.getFlags(), CreateMode.PERSISTENT);
            }
            schema.validateCreate(createMode, createRequest.getPath(), createRequest.getData(),
                    createRequest.getAcl());
        } else if ((curatorOp.get().getType() == ZooDefs.OpCode.delete)
                || (curatorOp.get().getType() == ZooDefs.OpCode.deleteContainer)) {
            DeleteRequest deleteRequest = (DeleteRequest) curatorOp.get().toRequestRecord();
            schema.validateDelete(deleteRequest.getPath());
        } else if (curatorOp.get().getType() == ZooDefs.OpCode.setData) {
            SetDataRequest setDataRequest = (SetDataRequest) curatorOp.get().toRequestRecord();
            schema.validateGeneral(setDataRequest.getPath(), setDataRequest.getData(), null);
        }
    }

    if (backgrounding.inBackground()) {
        client.processBackgroundOperation(new OperationAndData<>(this, record, backgrounding.getCallback(),
                null, backgrounding.getContext(), null), null);
        return null;
    } else {
        return forOperationsInForeground(record);
    }
}

From source file:org.apache.hadoop.hbase.zookeeper.ZKUtil.java

License:Apache License

private static void createAndFailSilent(ZooKeeperWatcher zkw, CreateAndFailSilent cafs) throws KeeperException {
    CreateRequest create = (CreateRequest) toZooKeeperOp(zkw, cafs).toRequestRecord();
    String znode = create.getPath();
    try {/*from w w w  .j  av  a  2  s .co  m*/
        RecoverableZooKeeper zk = zkw.getRecoverableZooKeeper();
        if (zk.exists(znode, false) == null) {
            zk.create(znode, create.getData(), create.getAcl(), CreateMode.fromFlag(create.getFlags()));
        }
    } catch (KeeperException.NodeExistsException nee) {
    } catch (KeeperException.NoAuthException nee) {
        try {
            if (null == zkw.getRecoverableZooKeeper().exists(znode, false)) {
                // If we failed to create the file and it does not already exist.
                throw (nee);
            }
        } catch (InterruptedException ie) {
            zkw.interruptedException(ie);
        }
    } catch (InterruptedException ie) {
        zkw.interruptedException(ie);
    }
}

From source file:org.apache.solr.cloud.autoscaling.sim.SimDistribStateManager.java

License:Apache License

@Override
public List<OpResult> multi(Iterable<Op> ops) throws BadVersionException, NoSuchElementException,
        AlreadyExistsException, IOException, KeeperException, InterruptedException {
    multiLock.lock();//from  w  w  w .java 2  s .c o  m
    List<OpResult> res = new ArrayList<>();
    try {
        for (Op op : ops) {
            Record r = op.toRequestRecord();
            try {
                if (op instanceof Op.Check) {
                    CheckVersionRequest rr = (CheckVersionRequest) r;
                    Node n = traverse(rr.getPath(), false, CreateMode.PERSISTENT);
                    if (n == null) {
                        throw new NoSuchElementException(rr.getPath());
                    }
                    if (rr.getVersion() != -1 && n.version != rr.getVersion()) {
                        throw new Exception("version mismatch");
                    }
                    // everything ok
                    res.add(new OpResult.CheckResult());
                } else if (op instanceof Op.Create) {
                    CreateRequest rr = (CreateRequest) r;
                    createData(rr.getPath(), rr.getData(), CreateMode.fromFlag(rr.getFlags()));
                    res.add(new OpResult.CreateResult(rr.getPath()));
                } else if (op instanceof Op.Delete) {
                    DeleteRequest rr = (DeleteRequest) r;
                    removeData(rr.getPath(), rr.getVersion());
                    res.add(new OpResult.DeleteResult());
                } else if (op instanceof Op.SetData) {
                    SetDataRequest rr = (SetDataRequest) r;
                    setData(rr.getPath(), rr.getData(), rr.getVersion());
                    VersionedData vd = getData(rr.getPath());
                    Stat s = new Stat();
                    s.setVersion(vd.getVersion());
                    res.add(new OpResult.SetDataResult(s));
                } else {
                    throw new Exception("Unknown Op: " + op);
                }
            } catch (Exception e) {
                res.add(new OpResult.ErrorResult(KeeperException.Code.APIERROR.intValue()));
            }
        }
    } finally {
        multiLock.unlock();
    }
    return res;
}

From source file:org.midonet.cluster.backend.MockDirectory.java

License:Apache License

@Override
public List<OpResult> multi(List<Op> ops) throws InterruptedException, KeeperException {
    List<OpResult> results = new ArrayList<>();
    // Fire watchers after finishing multi operation.
    // Copy to the local Set to avoid concurrent access.
    Map<Watcher, WatchedEvent> watchers = new HashMap<>();
    try {/*  w w  w  .j a  v  a  2s .c  o  m*/
        for (Op op : ops) {
            Record record = op.toRequestRecord();
            if (record instanceof CreateRequest) {
                // TODO(pino, ryu): should we use the try/catch and create
                // new ErrorResult? Don't for now, but this means that the
                // unit tests can't purposely make a bad Op.
                // try {
                CreateRequest req = CreateRequest.class.cast(record);
                String path = this.add(req.getPath(), req.getData(), CreateMode.fromFlag(req.getFlags()), true);
                results.add(new OpResult.CreateResult(path));
                // } catch (KeeperException e) {
                // e.printStackTrace();
                // results.add(
                // new OpResult.ErrorResult(e.code().intValue()));
                // }
            } else if (record instanceof SetDataRequest) {
                SetDataRequest req = SetDataRequest.class.cast(record);
                this.update(req.getPath(), req.getData(), true);
                // We create the SetDataResult with Stat=null. The
                // Directory interface doesn't provide the Stat object.
                results.add(new OpResult.SetDataResult(null));
            } else if (record instanceof DeleteRequest) {
                DeleteRequest req = DeleteRequest.class.cast(record);
                this.delete(req.getPath(), true);
                results.add(new OpResult.DeleteResult());
            } else {
                // might be CheckVersionRequest or some new type we miss.
                throw new IllegalArgumentException(
                        "This mock implementation only supports Create, " + "SetData and Delete operations");
            }
        }
    } finally {
        synchronized (multiDataWatchers) {
            watchers.putAll(multiDataWatchers);
            multiDataWatchers.clear();
        }
    }

    for (Watcher watcher : watchers.keySet()) {
        watcher.process(watchers.get(watcher));
    }

    return results;
}

From source file:org.midonet.midolman.state.MockDirectory.java

License:Apache License

@Override
public List<OpResult> multi(List<Op> ops) throws InterruptedException, KeeperException {
    List<OpResult> results = new ArrayList<OpResult>();
    // Fire watchers after finishing multi operation.
    // Copy to the local Set to avoid concurrent access.
    Map<Watcher, WatchedEvent> watchers = new HashMap<Watcher, WatchedEvent>();
    try {//from w ww  .java 2s . c o m
        for (Op op : ops) {
            Record record = op.toRequestRecord();
            if (record instanceof CreateRequest) {
                // TODO(pino, ryu): should we use the try/catch and create
                // new ErrorResult? Don't for now, but this means that the
                // unit tests can't purposely make a bad Op.
                // try {
                CreateRequest req = CreateRequest.class.cast(record);
                String path = this.add(req.getPath(), req.getData(), CreateMode.fromFlag(req.getFlags()), true);
                results.add(new OpResult.CreateResult(path));
                // } catch (KeeperException e) {
                // e.printStackTrace();
                // results.add(
                // new OpResult.ErrorResult(e.code().intValue()));
                // }
            } else if (record instanceof SetDataRequest) {
                SetDataRequest req = SetDataRequest.class.cast(record);
                this.update(req.getPath(), req.getData(), true);
                // We create the SetDataResult with Stat=null. The
                // Directory interface doesn't provide the Stat object.
                results.add(new OpResult.SetDataResult(null));
            } else if (record instanceof DeleteRequest) {
                DeleteRequest req = DeleteRequest.class.cast(record);
                this.delete(req.getPath(), true);
                results.add(new OpResult.DeleteResult());
            } else {
                // might be CheckVersionRequest or some new type we miss.
                throw new IllegalArgumentException(
                        "This mock implementation only supports Create, " + "SetData and Delete operations");
            }
        }
    } finally {
        synchronized (multiDataWatchers) {
            watchers.putAll(multiDataWatchers);
            multiDataWatchers.clear();
        }
    }

    for (Watcher watcher : watchers.keySet()) {
        watcher.process(watchers.get(watcher));
    }

    return results;
}