public static GremlinRequest fromJson(String json) { return JsonUtil.fromJson(json, GremlinRequest.class); } }
public String toJson() { return JsonUtil.toJson(this); }
protected void parseProperty(Id key, String colValue, HugeElement owner) { // Get PropertyKey by PropertyKey name PropertyKey pkey = owner.graph().propertyKey(key); // Parse value Object value = JsonUtil.fromJson(colValue, pkey.clazz()); // Set properties of vertex/edge if (pkey.cardinality() == Cardinality.SINGLE) { owner.addProperty(pkey, value); } else { if (!(value instanceof Collection)) { throw new BackendException( "Invalid value of non-single property: %s", value); } for (Object v : (Collection<?>) value) { v = JsonUtil.castNumber(v, pkey.dataType().clazz()); owner.addProperty(pkey, v); } } }
@Override protected void parseProperties(HugeElement element, TableBackendEntry.Row row) { String properties = row.column(HugeKeys.PROPERTIES); // Query edge will wraped by a vertex, whose properties is empty if (properties.isEmpty()) { return; } @SuppressWarnings("unchecked") Map<String, Object> props = JsonUtil.fromJson(properties, Map.class); for (Map.Entry<String, Object> prop : props.entrySet()) { /* * The key is string instead of int, because the key in json * must be string */ Id pkeyId = this.toId(Long.valueOf(prop.getKey())); String colJson = JsonUtil.toJson(prop.getValue()); this.parseProperty(pkeyId, colJson, element); } }
private void parseProperty(String colName, String colValue, HugeElement owner) { String[] colParts = SplicingIdGenerator.split(colName); assert colParts.length == 2 : colName; // Get PropertyKey by PropertyKey id PropertyKey pkey = owner.graph().propertyKey(readId(colParts[1])); // Parse value Object value = JsonUtil.fromJson(colValue, pkey.clazz()); // Set properties of vertex/edge if (pkey.cardinality() == Cardinality.SINGLE) { owner.addProperty(pkey, value); } else { if (!(value instanceof Collection)) { throw new BackendException( "Invalid value of non-sigle property: %s", colValue); } for (Object v : (Collection<?>) value) { v = JsonUtil.castNumber(v, pkey.dataType().clazz()); owner.addProperty(pkey, v); } } }
@Override protected Object toLongList(Collection<Id> ids) { long[] values = new long[ids.size()]; int i = 0; for (Id id : ids) { values[i++] = id.asLong(); } return JsonUtil.toJson(values); }
@Override protected Id[] toIdArray(Object object) { assert object instanceof String; String value = (String) object; Number[] values = JsonUtil.fromJson(value, Number[].class); Id[] ids = new Id[values.length]; int i = 0; for (Number number : values) { ids[i++] = IdGenerator.of(number.longValue()); } return ids; }
public void append(TextBackendEntry entry) { for (Entry<String, String> col : entry.columns.entrySet()) { String newValue = col.getValue(); String oldValue = this.column(col.getKey()); // TODO: use more general method if (col.getKey().startsWith(HugeType.PROPERTY.string())) { this.columns.put(col.getKey(), col.getValue()); continue; } // TODO: use more general method if (!col.getKey().endsWith(HugeKeys.ELEMENT_IDS.string())) { continue; } // TODO: ensure the old value is a list and json format (for index) List<Object> values = new ArrayList<>(); Object[] oldValues = JsonUtil.fromJson(oldValue, Object[].class); Object[] newValues = JsonUtil.fromJson(newValue, Object[].class); values.addAll(Arrays.asList(oldValues)); values.addAll(Arrays.asList(newValues)); // Update the old value this.column(col.getKey(), JsonUtil.toJson(values)); } }
protected void parseProperty(Id key, String colValue, HugeElement owner) { // Get PropertyKey by PropertyKey name PropertyKey pkey = owner.graph().propertyKey(key); // Parse value Object value = JsonUtil.fromJson(colValue, pkey.clazz()); // Set properties of vertex/edge if (pkey.cardinality() == Cardinality.SINGLE) { owner.addProperty(pkey, value); } else { if (!(value instanceof Collection)) { throw new BackendException( "Invalid value of non-single property: %s", value); } for (Object v : (Collection<?>) value) { v = JsonUtil.castNumber(v, pkey.dataType().clazz()); owner.addProperty(pkey, v); } } }
public byte[] toBytes() { String json = JsonUtil.toJson(this.columns); try { return json.getBytes(CHARSET); } catch (UnsupportedEncodingException e) { throw new BackendException(e); } }
public static Id checkAndParseVertexId(String idValue) { if (idValue == null) { return null; } try { Object id = JsonUtil.fromJson(idValue, Object.class); return HugeVertex.getIdValue(id); } catch (Exception e) { throw new IllegalArgumentException(String.format( "The vertex id must be formatted as String or " + "Number, but got '%s'", idValue)); } }
public void eliminate(TextBackendEntry entry) { for (Entry<String, String> col : entry.columns.entrySet()) { String newValue = col.getValue(); String oldValue = this.column(col.getKey()); // TODO: use more general method if (col.getKey().startsWith(HugeType.PROPERTY.string()) || col.getKey().startsWith(HugeType.EDGE_OUT.string()) || col.getKey().startsWith(HugeType.EDGE_IN.string())) { this.columns.remove(col.getKey()); continue; } // TODO: use more general method if (!col.getKey().endsWith(HugeKeys.ELEMENT_IDS.string())) { continue; } // TODO: ensure the old value is a list and json format (for index) List<Object> values = new ArrayList<>(); Object[] oldValues = JsonUtil.fromJson(oldValue, Object[].class); Object[] newValues = JsonUtil.fromJson(newValue, Object[].class); values.addAll(Arrays.asList(oldValues)); values.removeAll(Arrays.asList(newValues)); // Update the old value this.column(col.getKey(), JsonUtil.toJson(values)); } }
private void parseProperty(String colName, String colValue, HugeElement owner) { String[] colParts = SplicingIdGenerator.split(colName); assert colParts.length == 2 : colName; // Get PropertyKey by PropertyKey id PropertyKey pkey = owner.graph().propertyKey(readId(colParts[1])); // Parse value Object value = JsonUtil.fromJson(colValue, pkey.clazz()); // Set properties of vertex/edge if (pkey.cardinality() == Cardinality.SINGLE) { owner.addProperty(pkey, value); } else { if (!(value instanceof Collection)) { throw new BackendException( "Invalid value of non-sigle property: %s", colValue); } for (Object v : (Collection<?>) value) { v = JsonUtil.castNumber(v, pkey.dataType().clazz()); owner.addProperty(pkey, v); } } }
private String formatPropertyValue(HugeProperty<?> prop) { // May be a single value or a list of values return JsonUtil.toJson(prop.value()); }
public static <T> T assertJsonContains(String response, String key) { Map<?, ?> json = JsonUtil.fromJson(response, Map.class); return assertMapContains(json, key); }
@Override protected void writeUserdata(SchemaElement schema, TableBackendEntry entry) { assert entry instanceof CassandraBackendEntry; for (Map.Entry<String, Object> e : schema.userdata().entrySet()) { entry.column(HugeKeys.USER_DATA, e.getKey(), JsonUtil.toJson(e.getValue())); } }
private static void readUserdata(SchemaElement schema, TextBackendEntry entry) { // Parse all user data of a schema element String userdataStr = entry.column(HugeKeys.USER_DATA); @SuppressWarnings("unchecked") Map<String, Object> userdata = JsonUtil.fromJson(userdataStr, Map.class); for (Map.Entry<String, Object> e : userdata.entrySet()) { schema.userdata(e.getKey(), e.getValue()); } } }
public void append(TextBackendEntry entry) { for (Entry<String, String> col : entry.columns.entrySet()) { String newValue = col.getValue(); String oldValue = this.column(col.getKey()); // TODO: use more general method if (col.getKey().startsWith(HugeType.PROPERTY.string())) { this.columns.put(col.getKey(), col.getValue()); continue; } // TODO: use more general method if (!col.getKey().endsWith(HugeKeys.ELEMENT_IDS.string())) { continue; } // TODO: ensure the old value is a list and json format (for index) List<Object> values = new ArrayList<>(); Object[] oldValues = JsonUtil.fromJson(oldValue, Object[].class); Object[] newValues = JsonUtil.fromJson(newValue, Object[].class); values.addAll(Arrays.asList(oldValues)); values.addAll(Arrays.asList(newValues)); // Update the old value this.column(col.getKey(), JsonUtil.toJson(values)); } }