@Override public int compareTo(Id other) { return this.id.compareTo(other.asString()); }
public static String desc(String prefix, Collection<Id> elems) { StringBuilder sb = new StringBuilder(); for (Id elem : elems) { sb.append("\"").append(elem.asString()).append("\","); } int endIdx = sb.lastIndexOf(",") > 0 ? sb.length() - 1 : sb.length(); return String.format(".%s(%s)", prefix, sb.substring(0, endIdx)); }
/** * Parse a single id into multiple parts with ID_SPLITOR * @param id the id object to be parsed * @return parsed string id parts */ public static String[] parse(Id id) { return StringUtil.unescape(id.asString(), ID_SPLITOR_STR, ESCAPE_STR); } }
private Lock lockWrite(String group, Id lock) { return LockUtil.lockWrite(join(this.graph, group), lock.asString(), WRITE_WAIT_TIMEOUT); }
@Override public int compareTo(Id other) { return this.asString().compareTo(other.asString()); }
private Lock lockRead(String group, Id lock) { return LockUtil.lockRead(join(this.graph, group), lock.asString()); }
private static Id generateId(HugeType type, Id id) { // NOTE: it's slower performance to use: // String.format("%x-%s", type.code(), name) return IdGenerator.of(type.string() + "-" + id.asString()); }
public static Id getIdValue(Object idValue) { Id id = HugeElement.getIdValue(idValue); if (id == null || id instanceof EdgeId) { return id; } return EdgeId.parse(id.asString()); } }
private static String writeIds(Collection<Id> ids) { Object[] array = new Object[ids.size()]; int i = 0; for (Id id : ids) { if (id.number()) { array[i++] = id.asLong(); } else { array[i++] = id.asString(); } } return JsonUtil.toJson(array); }
@Override public Object id() { return SplicingIdGenerator.concat(this.owner.id().asString(), this.key()); }
public static String formatTaskName(HugeType type, Id id, String name) { E.checkNotNull(type, "schema type"); E.checkNotNull(id, "schema id"); E.checkNotNull(name, "schema name"); return String.join(SPLITOR, type.toString(), id.asString(), name); }
public static Id getIdValue(HugeType type, Object idValue) { assert type.isGraph(); Id id = getIdValue(idValue); if (type.isVertex()) { return id; } else { return EdgeId.parse(id.asString()); } }
public Id generate() { if (this.idWorker == null) { throw new HugeException("Please initialize before using"); } Id id = this.generate(this.idWorker.nextId()); if (!this.forceString) { return id; } else { return IdGenerator.of(id.asString()); } }
@Override public void serialize(T value, JsonGenerator jsonGenerator, SerializerProvider serializer) throws IOException { if (value.number()) { jsonGenerator.writeNumber(value.asLong()); } else { jsonGenerator.writeString(value.asString()); } }
private static String writeId(Id id) { if (id.number()) { return JsonUtil.toJson(id.asLong()); } else { return JsonUtil.toJson(id.asString()); } }
/** * Generate a string id of HugeVertex from Vertex name */ @Override public Id generate(HugeVertex vertex) { /* * Hash for row-key which will be evenly distributed. * We can also use LongEncoding.encode() to encode the int/long hash * if needed. * id = String.format("%s%s%s", HashUtil.hash(id), ID_SPLITOR, id); */ // TODO: use binary Id with binary fields instead of string id return splicing(vertex.schemaLabel().id().asString(), vertex.name()); }
@Override protected Id writeQueryId(HugeType type, Id id) { if (type.isEdge()) { if (!(id instanceof EdgeId)) { id = EdgeId.parse(id.asString()); } } else if (type.isGraph()) { id = IdGenerator.of(IdUtil.writeString(id)); } return id; }
@Test public void testQueryById() { HugeGraph graph = graph(); init10Vertices(); String authorId = graph.vertexLabel("author").id().asString(); // Query vertex by id Id id = SplicingIdGenerator.splicing(authorId, "1"); List<Vertex> vertexes = graph.traversal().V(id).toList(); Assert.assertEquals(1, vertexes.size()); assertContains(vertexes, T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian"); }
protected Id asyncRemoveIndexLeft(ConditionQuery query, HugeElement element) { RemoveLeftIndexJob callable = new RemoveLeftIndexJob(query, element); HugeTask<?> task = EphemeralJobBuilder.of(this.graph()) .name(element.id().asString()) .job(callable) .schedule(); return task.id(); }
@Test public void testSplicingId() { HugeGraph graph = graph(); init10Vertices(); List<Vertex> vertexes = graph.traversal().V().toList(); String bookId = graph.vertexLabel("book").id().asString(); Assert.assertTrue(Utils.containsId(vertexes, SplicingIdGenerator.splicing(bookId, "java-1"))); Assert.assertTrue(Utils.containsId(vertexes, SplicingIdGenerator.splicing(bookId, "java-3"))); Assert.assertTrue(Utils.containsId(vertexes, SplicingIdGenerator.splicing(bookId, "java-5"))); }