Search in sources :

Example 1 with Metadatable

use of com.baidu.hugegraph.iterator.Metadatable in project incubator-hugegraph by apache.

the class JsonSerializer method writeIterator.

private String writeIterator(String label, Iterator<?> iter, boolean paging) {
    // Early throw if needed
    iter.hasNext();
    // Serialize Iterator
    try (ByteArrayOutputStream out = new ByteArrayOutputStream(LBUF_SIZE)) {
        out.write("{".getBytes(API.CHARSET));
        out.write(String.format("\"%s\":[", label).getBytes(API.CHARSET));
        // Write data
        boolean first = true;
        while (iter.hasNext()) {
            if (!first) {
                out.write(",".getBytes(API.CHARSET));
            } else {
                first = false;
            }
            out.write(JsonUtil.toJson(iter.next()).getBytes(API.CHARSET));
        }
        out.write("]".getBytes(API.CHARSET));
        // Write page
        if (paging) {
            String page;
            if (iter instanceof GraphTraversal<?, ?>) {
                page = TraversalUtil.page((GraphTraversal<?, ?>) iter);
            } else if (iter instanceof Metadatable) {
                page = PageInfo.pageInfo(iter);
            } else {
                throw new HugeException("Invalid paging iterator: %s", iter.getClass());
            }
            if (page != null) {
                page = String.format(",\"page\": \"%s\"", page);
            } else {
                page = ",\"page\": null";
            }
            out.write(page.getBytes(API.CHARSET));
        }
        out.write("}".getBytes(API.CHARSET));
        return out.toString(API.CHARSET);
    } catch (HugeException e) {
        throw e;
    } catch (Exception e) {
        throw new HugeException("Failed to serialize %s", e, label);
    } finally {
        try {
            CloseableIterator.closeIterator(iter);
        } catch (Exception e) {
            throw new HugeException("Failed to close for %s", e, label);
        }
    }
}
Also used : Metadatable(com.baidu.hugegraph.iterator.Metadatable) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HugeException(com.baidu.hugegraph.HugeException) HugeException(com.baidu.hugegraph.HugeException)

Example 2 with Metadatable

use of com.baidu.hugegraph.iterator.Metadatable in project incubator-hugegraph by apache.

the class GraphIndexTransaction method doIndexQueryOnce.

@Watched(prefix = "index")
private PageIds doIndexQueryOnce(IndexLabel indexLabel, ConditionQuery query) {
    // Query all or one page
    Iterator<BackendEntry> entries = null;
    LockUtil.Locks locks = new LockUtil.Locks(this.graphName());
    try {
        locks.lockReads(LockUtil.INDEX_LABEL_DELETE, indexLabel.id());
        locks.lockReads(LockUtil.INDEX_LABEL_REBUILD, indexLabel.id());
        Set<Id> ids = InsertionOrderUtil.newSet();
        entries = super.query(query).iterator();
        while (entries.hasNext()) {
            HugeIndex index = this.serializer.readIndex(graph(), query, entries.next());
            this.removeExpiredIndexIfNeeded(index, query.showExpired());
            ids.addAll(index.elementIds());
            if (query.reachLimit(ids.size())) {
                break;
            }
            Query.checkForceCapacity(ids.size());
            this.recordIndexValue(query, index);
        }
        // If there is no data, the entries is not a Metadatable object
        if (ids.isEmpty()) {
            return PageIds.EMPTY;
        }
        // NOTE: Memory backend's iterator is not Metadatable
        if (!query.paging()) {
            return new PageIds(ids, PageState.EMPTY);
        }
        E.checkState(entries instanceof Metadatable, "The entries must be Metadatable when query " + "in paging, but got '%s'", entries.getClass().getName());
        return new PageIds(ids, PageInfo.pageState(entries));
    } finally {
        locks.unlock();
        CloseableIterator.closeIterator(entries);
    }
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) Metadatable(com.baidu.hugegraph.iterator.Metadatable) LockUtil(com.baidu.hugegraph.util.LockUtil) PageIds(com.baidu.hugegraph.backend.page.PageIds) Id(com.baidu.hugegraph.backend.id.Id) HugeIndex(com.baidu.hugegraph.structure.HugeIndex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

Metadatable (com.baidu.hugegraph.iterator.Metadatable)2 HugeException (com.baidu.hugegraph.HugeException)1 Id (com.baidu.hugegraph.backend.id.Id)1 PageIds (com.baidu.hugegraph.backend.page.PageIds)1 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)1 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)1 HugeIndex (com.baidu.hugegraph.structure.HugeIndex)1 LockUtil (com.baidu.hugegraph.util.LockUtil)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)1