Search in sources :

Example 46 with PropertyKey

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

the class JsonUtilTest method testSerializeIndexLabel.

@Test
public void testSerializeIndexLabel() {
    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());
    IndexLabel il = fakeObject.newIndexLabel(IdGenerator.of(1), "personByAgeAndCity", HugeType.VERTEX_LABEL, vl.id(), IndexType.SECONDARY, age.id(), city.id());
    Mockito.when(fakeObject.graph().vertexLabel(vl.id())).thenReturn(vl);
    Mockito.when(fakeObject.graph().mapPkId2Name(il.indexFields())).thenReturn(Arrays.asList(age.name(), city.name()));
    String json = JsonUtil.toJson(il);
    Assert.assertEquals("{\"id\":1," + "\"name\":\"personByAgeAndCity\"," + "\"base_type\":\"VERTEX_LABEL\"," + "\"base_value\":\"person\"," + "\"index_type\":\"SECONDARY\"," + "\"fields\":[\"age\",\"city\"]," + "\"status\":\"CREATED\"," + "\"user_data\":{}}", json);
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 47 with PropertyKey

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

the class JsonUtilTest method testSerializeVertexWithNumberId.

@Test
public void testSerializeVertexWithNumberId() {
    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());
    Id id = IdGenerator.of(123456L);
    HugeVertex vertex = new HugeVertex(fakeObject.graph(), id, vl);
    MutableIntObjectMap<HugeProperty<?>> properties = CollectionFactory.newIntObjectMap(name.id(), new HugeVertexProperty<>(vertex, name, "marko"), age.id(), new HugeVertexProperty<>(vertex, age, 29), city.id(), new HugeVertexProperty<>(vertex, city, "Beijing"));
    Whitebox.setInternalState(vertex, "properties", properties);
    String json = JsonUtil.toJson(vertex);
    Assert.assertEquals("{\"id\":123456,\"label\":\"person\"," + "\"type\":\"vertex\",\"properties\":{\"" + "name\":\"marko\",\"age\":29," + "\"city\":\"Beijing\"}}", json);
}
Also used : HugeProperty(com.baidu.hugegraph.structure.HugeProperty) FakeObjects(com.baidu.hugegraph.unit.FakeObjects) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) 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) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 48 with PropertyKey

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

the class JsonUtilTest method testSerializeEdgeLabel.

@Test
public void testSerializeEdgeLabel() {
    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());
    Mockito.when(fakeObject.graph().vertexLabel(vl.id())).thenReturn(vl);
    Mockito.when(fakeObject.graph().mapPkId2Name(el.properties())).thenReturn(Arrays.asList(date.name(), weight.name()));
    String json = JsonUtil.toJson(el);
    Assert.assertEquals("{\"id\":1,\"name\":\"knows\"," + "\"source_label\":\"person\"," + "\"target_label\":\"person\"," + "\"frequency\":\"SINGLE\",\"sort_keys\":[]," + "\"nullable_keys\":[],\"index_labels\":[]," + "\"properties\":[\"date\",\"weight\"]," + "\"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) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 49 with PropertyKey

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

the class BytesBufferTest method testProperty.

@Test
public void testProperty() {
    BytesBuffer buf = BytesBuffer.allocate(0);
    PropertyKey pkey = genPkey(DataType.BOOLEAN);
    Object value = true;
    byte[] bytes = genBytes("01");
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    value = false;
    bytes = genBytes("00");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.BYTE);
    value = (byte) 127;
    bytes = genBytes("7f");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.INT);
    value = 127;
    bytes = genBytes("7f");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.INT);
    value = 128;
    bytes = genBytes("8100");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.FLOAT);
    value = 1.0f;
    bytes = genBytes("3f800000");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.FLOAT);
    value = 3.14f;
    bytes = genBytes("4048f5c3");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.FLOAT);
    value = -1.0f;
    bytes = genBytes("bf800000");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.FLOAT);
    value = Float.MAX_VALUE;
    bytes = genBytes("7f7fffff");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.LONG);
    value = 127L;
    bytes = genBytes("7f");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.DOUBLE);
    value = 3.14d;
    bytes = genBytes("40091eb851eb851f");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.DATE);
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Beijing"));
    c.setTimeInMillis(1565851529514L);
    value = c.getTime();
    bytes = genBytes("adc9a098e22a");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.TEXT);
    value = "abc";
    bytes = genBytes("03616263");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.BLOB);
    value = genBytes("001199aabbcc");
    bytes = genBytes("06001199aabbcc");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(Blob.wrap((byte[]) value), BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.UUID);
    value = UUID.fromString("3cfcafc8-7906-4ab7-a207-4ded056f58de");
    bytes = genBytes("3cfcafc879064ab7a2074ded056f58de");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.OBJECT);
    value = new Point(3, 8);
    bytes = genBytes("1301006a6176612e6177742e506f696ef4010610");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.OBJECT);
    value = UUID.fromString("3cfcafc8-7906-4ab7-a207-4ded056f58de");
    bytes = genBytes("2101006a6176612e7574696c2e555549c401" + "3cfcafc879064ab7a2074ded056f58de");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertEquals(value, BytesBuffer.wrap(bytes).readProperty(pkey));
    pkey = genPkey(DataType.OBJECT);
    value = new int[] { 1, 3, 8 };
    bytes = genBytes("0901005bc90104020610");
    buf.forReadWritten();
    Assert.assertArrayEquals(bytes, buf.writeProperty(pkey, value).bytes());
    Assert.assertArrayEquals((int[]) value, (int[]) BytesBuffer.wrap(bytes).readProperty(pkey));
}
Also used : Calendar(java.util.Calendar) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) Point(java.awt.Point) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 50 with PropertyKey

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

the class SplicingIdGeneratorTest method testGenerate.

@Test
public void testGenerate() {
    FakeObjects fakeObjects = new FakeObjects();
    PropertyKey name = fakeObjects.newPropertyKey(IdGenerator.of(1), "name");
    VertexLabel vertexLabel = fakeObjects.newVertexLabel(IdGenerator.of(1L), "fake", IdStrategy.PRIMARY_KEY, name.id());
    HugeVertex vertex = Mockito.mock(HugeVertex.class);
    Mockito.when(vertex.schemaLabel()).thenReturn(vertexLabel);
    Mockito.when(vertex.name()).thenReturn("marko");
    Id vid = SplicingIdGenerator.instance().generate(vertex);
    Assert.assertEquals(IdGenerator.of("1:marko"), vid);
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) 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