public static GremlinRequest fromJson(String json) { return JsonUtil.fromJson(json, GremlinRequest.class); } }
@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 static <T> T deepCopy(T object) { @SuppressWarnings("unchecked") Class<T> cls = (Class<T>) object.getClass(); return JsonUtil.fromJson(JsonUtil.toJson(object), cls); } }
public static <T> T assertJsonContains(String response, String key) { Map<?, ?> json = JsonUtil.fromJson(response, Map.class); return assertMapContains(json, key); }
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)); } }
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()); } } }
@Override protected void readUserdata(SchemaElement schema, TableBackendEntry entry) { assert entry instanceof MysqlBackendEntry; // Parse all user data of a schema element String json = entry.column(HugeKeys.USER_DATA); @SuppressWarnings("unchecked") Map<String, Object> userdata = JsonUtil.fromJson(json, Map.class); for (Map.Entry<String, Object> e : userdata.entrySet()) { schema.userdata(e.getKey(), e.getValue()); } } }
@Override protected void readUserdata(SchemaElement schema, TableBackendEntry entry) { assert entry instanceof CassandraBackendEntry; // Parse all user data of a schema element Map<String, String> userdata = entry.column(HugeKeys.USER_DATA); for (Map.Entry<String, String> e : userdata.entrySet()) { String key = e.getKey(); Object value = JsonUtil.fromJson(e.getValue(), Object.class); schema.userdata(key, value); } } }
private static Id readId(String id) { Object value = JsonUtil.fromJson(id, Object.class); if (value instanceof Number) { return IdGenerator.of(((Number) value).longValue()); } else { assert value instanceof String; return IdGenerator.of(value.toString()); } }
private static Id[] readIds(String str) { Object[] values = JsonUtil.fromJson(str, Object[].class); Id[] ids = new Id[values.length]; for (int i = 0; i < values.length; i++) { Object value = values[i]; if (value instanceof Number) { ids[i] = IdGenerator.of(((Number) value).longValue()); } else { assert value instanceof String; ids[i] = IdGenerator.of(value.toString()); } } return ids; }
private void readUserdata(SchemaElement schema) { // Parse all user data of a schema element byte[] userdataBytes = column(HugeKeys.USER_DATA); String userdataStr = StringEncoding.decode(userdataBytes); @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()); } }
private static PageState fromBytes(byte[] bytes) { String json; try { json = new String(bytes, CHARSET); } catch (UnsupportedEncodingException e) { throw new BackendException(e); } @SuppressWarnings("unchecked") Map<String, Object> columns = JsonUtil.fromJson(json, Map.class); Map<HugeKeys, Object> keyColumns = new LinkedHashMap<>(); for (Map.Entry<String, Object> entry : columns.entrySet()) { HugeKeys key = MysqlTable.parseKey(entry.getKey()); keyColumns.put(key, entry.getValue()); } return new PageState(keyColumns); } }
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)); } }
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)); } }
@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); } }
@Override public PropertyKey readPropertyKey(HugeGraph graph, BackendEntry backendEntry) { if (backendEntry == null) { return null; } TextBackendEntry entry = this.convertEntry(backendEntry); Id id = readId(entry.id()); String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME), String.class); String dataType = entry.column(HugeKeys.DATA_TYPE); String cardinality = entry.column(HugeKeys.CARDINALITY); String properties = entry.column(HugeKeys.PROPERTIES); String status = entry.column(HugeKeys.STATUS); PropertyKey propertyKey = new PropertyKey(graph, id, name); propertyKey.dataType(JsonUtil.fromJson(dataType, DataType.class)); propertyKey.cardinality(JsonUtil.fromJson(cardinality, Cardinality.class)); propertyKey.properties(readIds(properties)); readUserdata(propertyKey, entry); propertyKey.status(JsonUtil.fromJson(status, SchemaStatus.class)); return propertyKey; }
@Override public IndexLabel readIndexLabel(HugeGraph graph, BackendEntry backendEntry) { if (backendEntry == null) { return null; } TextBackendEntry entry = this.convertEntry(backendEntry); Id id = readId(entry.id()); String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME), String.class); String baseType = entry.column(HugeKeys.BASE_TYPE); String baseValue = entry.column(HugeKeys.BASE_VALUE); String indexType = entry.column(HugeKeys.INDEX_TYPE); String indexFields = entry.column(HugeKeys.FIELDS); String status = entry.column(HugeKeys.STATUS); IndexLabel indexLabel = new IndexLabel(graph, id, name); indexLabel.baseType(JsonUtil.fromJson(baseType, HugeType.class)); indexLabel.baseValue(readId(baseValue)); indexLabel.indexType(JsonUtil.fromJson(indexType, IndexType.class)); indexLabel.indexFields(readIds(indexFields)); indexLabel.status(JsonUtil.fromJson(status, SchemaStatus.class)); return indexLabel; }
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); } } }
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 public HugeIndex readIndex(HugeGraph graph, ConditionQuery query, BackendEntry backendEntry) { E.checkNotNull(graph, "serializer graph"); if (backendEntry == null) { return null; } TextBackendEntry entry = this.convertEntry(backendEntry); String indexValues = entry.column( formatSyspropName(HugeKeys.FIELD_VALUES)); String indexLabelId = entry.column( formatSyspropName(HugeKeys.INDEX_LABEL_ID)); String elementIds = entry.column( formatSyspropName(HugeKeys.ELEMENT_IDS)); IndexLabel indexLabel = IndexLabel.label(graph, readId(indexLabelId)); HugeIndex index = new HugeIndex(indexLabel); index.fieldValues(JsonUtil.fromJson(indexValues, Object.class)); index.elementIds(readIds(elementIds)); return index; }