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);
}
}
}
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);
}
}
Aggregations