Package org.haystack

Examples of org.haystack.HWatch


            watchId = req.meta().getStr("watchId");
        else
            watchDis = req.meta().getStr("watchDis");

        // open or lookup watch
        HWatch watch = watchId == null ? db.watchOpen(watchDis) : db.watch(watchId);

        // map grid to ids
        HRef[] ids = gridToIds(db, req);

        // subscribe and return resulting grid
        writeGrid(writer, watch.sub(ids));
    }
View Full Code Here


    @Override
    public void onService(HServer db, HGrid req, HGridWriter writer) throws Exception {
        // lookup watch, silently ignore failure
        String watchId = req.meta().getStr("watchId");
        HWatch watch = db.watch(watchId, false);

        // check for close or unsub
        if (watch != null) {
            if (req.meta().has("close"))
                watch.close();
            else
                watch.unsub(gridToIds(db, req));
        }

        // nothing to return
        writeGrid(writer, HGrid.EMPTY);
    }
View Full Code Here

    @Override
    public void onService(HServer db, HGrid req, HGridWriter writer) throws Exception {
        // lookup watch
        String watchId = req.meta().getStr("watchId");
        HWatch watch = db.watch(watchId);

        // poll cov or refresh
        if (req.meta().has("refresh"))
            writeGrid(writer, watch.pollRefresh());
        else
            writeGrid(writer, watch.pollChanges());
    }
View Full Code Here

     * If not found return null or raise UnknownWatchException based on
     * checked flag.
     */
    @Override
    public HWatch watch(String id, boolean checked) {
        HWatch w = watches.get(id);
        if (w != null)
            return w;
        if (checked)
            throw new UnknownWatchException(id);
        return null;
View Full Code Here

    // Watches
    //////////////////////////////////////////////////////////////////////////

    void verifyWatches() throws Exception {
        // create new watch
        HWatch w = client.watchOpen("Java Haystack Test");
        verifyEq(w.id(), null);
        verifyEq(w.dis(), "Java Haystack Test");

        // do query to get some recs
        HGrid recs = client.readAll("ahu");
        verify(recs.numRows() >= 4);
        HDict a = recs.row(0);
        HDict b = recs.row(1);
        HDict c = recs.row(2);
        HDict d = recs.row(3);

        // do first sub
        HGrid sub = w.sub(new HRef[] { a.id(), b.id() });
        verifyEq(sub.numRows(), 2);
        verifyEq(sub.row(0).dis(), a.dis());
        verifyEq(sub.row(1).dis(), b.dis());

        // now add c, bad, d
        HRef badId = HRef.make("badBadBad");
        try {
            w.sub(new HRef[] { badId }).dump();
            fail();
        }
        catch (UnknownRecException e) {
            verifyException(e);
        }
        sub = w.sub(new HRef[] { c.id(), badId, d.id() }, false);
        verifyEq(sub.numRows(), 3);
        verifyEq(sub.row(0).dis(), c.dis());
        verifyEq(sub.row(1).missing("id"), true);
        verifyEq(sub.row(2).dis(), d.dis());

        // verify state of watch now
        verify(client.watch(w.id()) == w);
        verifyEq(client.watches().length, 1);
        verify(client.watches()[0] == w);
        verifyEq(w.lease().millis(), 60000L);

        // poll for changes (should be none yet)
        HGrid poll = w.pollChanges();
        verifyEq(poll.numRows(), 0);

        // make change to b and d
        verifyEq(b.has("javaTest"), false);
        verifyEq(d.has("javaTest"), false);
        client.eval("commit(diff(readById(@" + b.id().val + "), {javaTest:123}))");
        client.eval("commit(diff(readById(@" + d.id().val + "), {javaTest:456}))");
        poll = w.pollChanges();
        verifyEq(poll.numRows(), 2);
        HDict newb, newd;
        if (poll.row(0).id().equals(b.id())) {
            newb = poll.row(0);
            newd = poll.row(1);
        }
        else {
            newb = poll.row(1);
            newd = poll.row(0);
        }
        verifyEq(newb.dis(), b.dis());
        verifyEq(newd.dis(), d.dis());
        verifyEq(newb.get("javaTest"), HNum.make(123));
        verifyEq(newd.get("javaTest"), HNum.make(456));

        // poll refresh
        poll = w.pollRefresh();
        verifyEq(poll.numRows(), 4);
        verifyGridContains(poll, "id", a.id());
        verifyGridContains(poll, "id", b.id());
        verifyGridContains(poll, "id", c.id());
        verifyGridContains(poll, "id", d.id());

        // remove d, and then poll changes
        w.unsub(new HRef[] { d.id() });
        client.eval("commit(diff(readById(@" + b.id().val + "), {-javaTest}))");
        client.eval("commit(diff(readById(@" + d.id().val + "), {-javaTest}))");
        poll = w.pollChanges();
        verifyEq(poll.numRows(), 1);
        verifyEq(poll.row(0).dis(), b.dis());
        verifyEq(poll.row(0).has("javaTest"), false);

        // remove a and c and poll refresh
        w.unsub(new HRef[] { a.id(), c.id() });
        poll = w.pollRefresh();
        verifyEq(poll.numRows(), 1);
        verifyEq(poll.row(0).dis(), b.dis());

        // close
        String expr = "folioDebugWatches().findAll(x=>x->dis.contains(\"Java Haystack Test\")).size";
        verifyEq(client.eval(expr).row(0).getInt("val"), 1);
        w.close();
        try {
            poll = w.pollRefresh();
            fail();
        }
        catch (Exception e) {
            verifyException(e);
        }
        verifyEq(client.eval(expr).row(0).getInt("val"), 0);
        verifyEq(client.watch(w.id(), false), null);
        verifyEq(client.watches().length, 0);
    }
View Full Code Here

     * Lookup a watch by its unique identifier. If not found then
     * raise UnknownWatchErr or return null based on checked flag.
     */
    @Override
    public HWatch watch(String id, boolean checked) {
        HWatch w = onWatch(id);
        if (w != null)
            return w;
        if (checked)
            throw new UnknownWatchException(id);
        return null;
View Full Code Here

TOP

Related Classes of org.haystack.HWatch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.