@Override public void process(TransactionBase tx, Bytes row, Column col) throws Exception { TypedTransactionBase ttx = FluoConstants.TYPEL.wrap(tx); String nextJson = ttx.get().row(row).col(FluoConstants.PAGE_NEW_COL).toString(""); if (nextJson.isEmpty()) { log.error("An empty page was set at row {} col {}", row.toString(), col.toString()); return; } Gson gson = new Gson(); Page nextPage; if (nextJson.equals("delete")) { ttx.mutate().row(row).col(FluoConstants.PAGE_CUR_COL).delete(); nextPage = Page.EMPTY; } else { ttx.mutate().row(row).col(FluoConstants.PAGE_CUR_COL).set(nextJson); nextPage = gson.fromJson(nextJson, Page.class); } String curJson = ttx.get().row(row).col(FluoConstants.PAGE_CUR_COL).toString(""); Set<Page.Link> curLinks = Collections.emptySet(); if (!curJson.isEmpty()) { Page curPage = gson.fromJson(curJson, Page.class); curLinks = curPage.getOutboundLinks(); } else { Long score = ttx.get().row(row).col(FluoConstants.PAGE_SCORE_COL).toLong(0); ttx.mutate().row(row).col(FluoConstants.PAGE_SCORE_COL).set(score + 1); } Set<Page.Link> nextLinks = nextPage.getOutboundLinks(); String pageUri = row.toString().substring(2); Sets.SetView<Page.Link> addLinks = Sets.difference(nextLinks, curLinks); for (Page.Link link : addLinks) { String r = "p:" + link.getUri(); ttx.mutate() .row(r) .fam(FluoConstants.INLINKS_UPDATE) .qual(pageUri) .set("add," + link.getAnchorText()); ttx.mutate().row(r).col(FluoConstants.INLINKS_CHG_NTFY).weaklyNotify(); } Sets.SetView<Page.Link> delLinks = Sets.difference(curLinks, nextLinks); for (Page.Link link : delLinks) { String r = "p:" + link.getUri(); ttx.mutate().row(r).fam(FluoConstants.INLINKS_UPDATE).qual(pageUri).set("del"); ttx.mutate().row(r).col(FluoConstants.INLINKS_CHG_NTFY).weaklyNotify(); } // clean up ttx.mutate().row(row).col(FluoConstants.PAGE_NEW_COL).delete(); }
@Test public void testBasic() { Page page = new Page("http://example.com"); Assert.assertEquals(new Long(0), page.getNumOutbound()); page.addOutboundLink("http://test1.com", "test1"); Assert.assertEquals(new Long(1), page.getNumOutbound()); page.addOutboundLink("http://test2.com", "test2"); Assert.assertEquals(new Long(2), page.getNumOutbound()); page.addOutboundLink("http://test2.com", "test1234"); Assert.assertEquals(new Long(2), page.getNumOutbound()); Gson gson = new Gson(); String json = gson.toJson(page); Assert.assertNotNull(json); Assert.assertFalse(json.isEmpty()); Page after = gson.fromJson(json, Page.class); Assert.assertEquals(page.getUrl(), after.getUrl()); Assert.assertEquals(page.getOutboundLinks().size(), after.getOutboundLinks().size()); }