Search in sources :

Example 91 with PropertyKey

use of com.baidu.hugegraph.schema.PropertyKey in project incubator-hugegraph by apache.

the class JsonUtilTest method testSerializeEdge.

@Test
public void testSerializeEdge() {
    FakeObjects fakeObject = new FakeObjects();
    PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = fakeObject.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
    PropertyKey city = fakeObject.newPropertyKey(IdGenerator.of(3), "city");
    PropertyKey date = fakeObject.newPropertyKey(IdGenerator.of(4), "date", DataType.DATE);
    PropertyKey weight = fakeObject.newPropertyKey(IdGenerator.of(5), "weight", DataType.DOUBLE);
    VertexLabel vl = fakeObject.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
    EdgeLabel el = fakeObject.newEdgeLabel(IdGenerator.of(1), "knows", Frequency.SINGLE, vl.id(), vl.id(), date.id(), weight.id());
    HugeVertex source = new HugeVertex(fakeObject.graph(), IdGenerator.of(123456), vl);
    HugeVertex target = new HugeVertex(fakeObject.graph(), IdGenerator.of(987654), vl);
    Id id = EdgeId.parse("L123456>1>>L987654");
    HugeEdge edge = new HugeEdge(fakeObject.graph(), id, el);
    Whitebox.setInternalState(edge, "sourceVertex", source);
    Whitebox.setInternalState(edge, "targetVertex", target);
    Date dateValue = Utils.date("2019-03-12");
    MutableIntObjectMap<HugeProperty<?>> properties = CollectionFactory.newIntObjectMap(date.id(), new HugeEdgeProperty<>(edge, date, dateValue), weight.id(), new HugeEdgeProperty<>(edge, weight, 0.8));
    Whitebox.setInternalState(edge, "properties", properties);
    String json = JsonUtil.toJson(edge);
    Assert.assertEquals("{\"id\":\"L123456>1>>L987654\"," + "\"label\":\"knows\",\"type\":\"edge\"," + "\"outV\":123456,\"outVLabel\":\"person\"," + "\"inV\":987654,\"inVLabel\":\"person\"," + "\"properties\":{\"date\":" + "\"2019-03-12 00:00:00.000\"," + "\"weight\":0.8}}", json);
}
Also used : HugeProperty(com.baidu.hugegraph.structure.HugeProperty) FakeObjects(com.baidu.hugegraph.unit.FakeObjects) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Date(java.util.Date) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 92 with PropertyKey

use of com.baidu.hugegraph.schema.PropertyKey in project incubator-hugegraph by apache.

the class JsonUtilTest method testSerializeVertexLabel.

@Test
public void testSerializeVertexLabel() {
    FakeObjects fakeObject = new FakeObjects();
    PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = fakeObject.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
    PropertyKey city = fakeObject.newPropertyKey(IdGenerator.of(3), "city");
    VertexLabel vl = fakeObject.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
    Mockito.when(fakeObject.graph().mapPkId2Name(vl.properties())).thenReturn(Arrays.asList(name.name(), age.name(), city.name()));
    String json = JsonUtil.toJson(vl);
    Assert.assertEquals("{\"id\":1,\"name\":\"person\"," + "\"id_strategy\":\"CUSTOMIZE_NUMBER\"," + "\"primary_keys\":[],\"nullable_keys\":[]," + "\"index_labels\":[]," + "\"properties\":[\"name\",\"age\",\"city\"]," + "\"status\":\"CREATED\"," + "\"ttl\":0,\"enable_label_index\":true," + "\"user_data\":{}}", json);
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 93 with PropertyKey

use of com.baidu.hugegraph.schema.PropertyKey in project incubator-hugegraph by apache.

the class BytesBufferTest method testPropertyWithList.

@Test
public void testPropertyWithList() {
    BytesBuffer buf = BytesBuffer.allocate(0);
    PropertyKey pkey = genListPkey(DataType.BOOLEAN);
    Object value = ImmutableList.of(true, false);
    byte[] bytes = genBytes("020100");
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.BYTE);
    value = ImmutableList.of();
    bytes = genBytes("00");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.BYTE);
    value = ImmutableList.of((byte) 127, (byte) 128);
    bytes = genBytes("027f8fffffff00");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.INT);
    value = ImmutableList.of(127, 128);
    bytes = genBytes("027f8100");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.FLOAT);
    value = ImmutableList.of(1.0f, 3.14f);
    bytes = genBytes("023f8000004048f5c3");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.LONG);
    value = ImmutableList.of(127L, 128L);
    bytes = genBytes("027f8100");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.DOUBLE);
    value = ImmutableList.of(1.0d, 3.14d);
    bytes = genBytes("023ff000000000000040091eb851eb851f");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.DATE);
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Beijing"));
    c.setTimeInMillis(1565851529514L);
    value = ImmutableList.of(c.getTime(), c.getTime());
    bytes = genBytes("02adc9a098e22aadc9a098e22a");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.TEXT);
    value = ImmutableList.of("abc", "123");
    bytes = genBytes("020361626303313233");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.BLOB);
    value = ImmutableList.of(genBytes("001199aabbcc"), genBytes("5566"));
    bytes = genBytes("0206001199aabbcc025566");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    List<?> list = (List<?>) BytesBuffer.wrap(bytes).readProperty(pkey);
    Assert.assertEquals(Blob.wrap(genBytes("001199aabbcc")), list.get(0));
    Assert.assertEquals(Blob.wrap(genBytes("5566")), list.get(1));
    pkey = genListPkey(DataType.UUID);
    UUID uuid = UUID.fromString("3cfcafc8-7906-4ab7-a207-4ded056f58de");
    value = ImmutableList.of(uuid, uuid);
    bytes = genBytes("023cfcafc879064ab7a2074ded056f58de" + "3cfcafc879064ab7a2074ded056f58de");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.OBJECT);
    value = ImmutableList.of(new Point(3, 8), new Point(3, 9));
    bytes = genBytes("021301006a6176612e6177742e506f696ef4010610" + "1301006a6176612e6177742e506f696ef4010612");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genListPkey(DataType.OBJECT);
    value = ImmutableList.of(new int[] { 1, 3 }, new int[] { 2, 5 });
    bytes = genBytes("020801005bc9010302060801005bc90103040a");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    list = (List<?>) BytesBuffer.wrap(bytes).readProperty(pkey);
    Assert.assertArrayEquals(new int[] { 1, 3 }, (int[]) list.get(0));
    Assert.assertArrayEquals(new int[] { 2, 5 }, (int[]) list.get(1));
}
Also used : Calendar(java.util.Calendar) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Point(java.awt.Point) UUID(java.util.UUID) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 94 with PropertyKey

use of com.baidu.hugegraph.schema.PropertyKey in project incubator-hugegraph by apache.

the class BytesBufferTest method testPropertyWithSet.

@Test
public void testPropertyWithSet() {
    BytesBuffer buf = BytesBuffer.allocate(0);
    PropertyKey pkey = genSetPkey(DataType.BOOLEAN);
    Object value = ImmutableSet.of(true, false);
    byte[] bytes = genBytes("020100");
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.BYTE);
    value = ImmutableSet.of();
    bytes = genBytes("00");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.BYTE);
    value = ImmutableSet.of((byte) 127, (byte) 128);
    bytes = genBytes("027f8fffffff00");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.INT);
    value = ImmutableSet.of(127, 128);
    bytes = genBytes("027f8100");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.FLOAT);
    value = ImmutableSet.of(1.0f, 3.14f);
    bytes = genBytes("023f8000004048f5c3");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.LONG);
    value = ImmutableSet.of(127L, 128L);
    bytes = genBytes("027f8100");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.DOUBLE);
    value = ImmutableSet.of(1.0d, 3.14d);
    bytes = genBytes("023ff000000000000040091eb851eb851f");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.DATE);
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Beijing"));
    c.setTimeInMillis(1565851529514L);
    value = ImmutableSet.of(c.getTime(), c.getTime());
    bytes = genBytes("01adc9a098e22a");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.TEXT);
    value = ImmutableSet.of("abc", "123");
    bytes = genBytes("020361626303313233");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.BLOB);
    value = ImmutableSet.of(genBytes("001199aabbcc"), genBytes("5566"));
    bytes = genBytes("0206001199aabbcc025566");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Set<?> set = (Set<?>) BytesBuffer.wrap(bytes).readProperty(pkey);
    Iterator<?> iterator = set.iterator();
    Assert.assertEquals(Blob.wrap(genBytes("001199aabbcc")), iterator.next());
    Assert.assertEquals(Blob.wrap(genBytes("5566")), iterator.next());
    pkey = genSetPkey(DataType.UUID);
    UUID uuid = UUID.fromString("3cfcafc8-7906-4ab7-a207-4ded056f58de");
    value = ImmutableSet.of(uuid, uuid);
    bytes = genBytes("013cfcafc879064ab7a2074ded056f58de");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.OBJECT);
    value = ImmutableSet.of(new Point(3, 8), new Point(3, 9));
    bytes = genBytes("021301006a6176612e6177742e506f696ef4010610" + "1301006a6176612e6177742e506f696ef4010612");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genSetPkey(DataType.OBJECT);
    value = ImmutableSet.of(new int[] { 1, 3 }, new int[] { 2, 5 });
    bytes = genBytes("020801005bc9010302060801005bc90103040a");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    set = (Set<?>) BytesBuffer.wrap(bytes).readProperty(pkey);
    iterator = set.iterator();
    Assert.assertArrayEquals(new int[] { 1, 3 }, (int[]) iterator.next());
    Assert.assertArrayEquals(new int[] { 2, 5 }, (int[]) iterator.next());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Calendar(java.util.Calendar) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) Point(java.awt.Point) UUID(java.util.UUID) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

PropertyKey (com.baidu.hugegraph.schema.PropertyKey)94 Id (com.baidu.hugegraph.backend.id.Id)31 Test (org.junit.Test)24 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)20 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)15 HugeGraph (com.baidu.hugegraph.HugeGraph)13 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)11 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)9 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)9 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)9 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)7 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)7 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)6 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)6 DataType (com.baidu.hugegraph.type.define.DataType)6 Map (java.util.Map)6 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)5 Timed (com.codahale.metrics.annotation.Timed)5 RolesAllowed (jakarta.annotation.security.RolesAllowed)5 Collection (java.util.Collection)5