Example usage for org.apache.zookeeper Op toRequestRecord

List of usage examples for org.apache.zookeeper Op toRequestRecord

Introduction

In this page you can find the example usage for org.apache.zookeeper Op toRequestRecord.

Prototype

public abstract Record toRequestRecord();

Source Link

Document

Encodes an op for wire transmission.

Usage

From source file:com.ery.estorm.zk.RecoverableZooKeeper.java

License:Apache License

/**
 * Convert Iterable of {@link ZKOp} we got into the ZooKeeper.Op instances
 * to actually pass to multi (need to do this in order to appendMetaData).
 *///from  w  ww.  jav a2 s . c o  m
private Iterable<Op> prepareZKMulti(Iterable<Op> ops) throws UnsupportedOperationException {
    if (ops == null)
        return null;

    List<Op> preparedOps = new LinkedList<Op>();
    for (Op op : ops) {
        if (op.getType() == ZooDefs.OpCode.create) {
            CreateRequest create = (CreateRequest) op.toRequestRecord();
            preparedOps.add(Op.create(create.getPath(), appendMetaData(create.getData()), create.getAcl(),
                    create.getFlags()));
        } else if (op.getType() == ZooDefs.OpCode.delete) {
            // no need to appendMetaData for delete
            preparedOps.add(op);
        } else if (op.getType() == ZooDefs.OpCode.setData) {
            SetDataRequest setData = (SetDataRequest) op.toRequestRecord();
            preparedOps.add(
                    Op.setData(setData.getPath(), appendMetaData(setData.getData()), setData.getVersion()));
        } else {
            throw new UnsupportedOperationException("Unexpected ZKOp type: " + op.getClass().getName());
        }
    }
    return preparedOps;
}

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  w w  w. java  2 s  . co m
            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.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 www  . j a  va2  s  .com*/
    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 {/*from w w w .j a v  a2s  .  co  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 {/*  ww w .ja  v a2s .co 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;
}