Search in sources :

Example 1 with Blob

use of com.baidu.hugegraph.util.Blob in project incubator-hugegraph by apache.

the class PropertyCoreTest method testTypeBlob.

@Test
public void testTypeBlob() {
    byte[] img = new byte[] { 1, 2, 8, 50, 80, 96, 110, 125, -1, -10, -100 };
    Assert.assertEquals(Blob.wrap(img), property("blob", img));
    Object buf = Blob.wrap(img);
    Assert.assertEquals(Blob.wrap(img), property("blob", buf));
    buf = BytesBuffer.wrap(img).forReadAll();
    Assert.assertEquals(Blob.wrap(img), property("blob", buf));
    Blob bytes = Blob.wrap(new byte[] { 97, 49, 50, 51, 52 });
    buf = ImmutableList.of((byte) 97, (byte) 49, 50, 51, 52);
    Assert.assertEquals(bytes, property("blob", buf));
    Object base64 = "YTEyMzQ=";
    Assert.assertEquals(bytes, property("blob", base64));
    Object hex = "0x6131323334";
    Assert.assertEquals(bytes, property("blob", hex));
    hex = "0x";
    Assert.assertEquals(Blob.wrap(new byte[] {}), property("blob", hex));
    Blob img1 = Blob.wrap(new byte[] { 1, 2, 8, 50 });
    Blob img2 = Blob.wrap(new byte[] { -1, -2, -8, -50 });
    Blob img3 = Blob.wrap(new byte[] { 1, 127, -128, 0 });
    List<Blob> list = ImmutableList.of(img1, img2, img2, img3);
    Assert.assertEquals(list, propertyList("blob", img1, img2, img2, img3));
    Set<Blob> set = ImmutableSet.of(img1, img2, img2, img3);
    Assert.assertEquals(3, set.size());
    Assert.assertEquals(set, propertySet("blob", img1, img2, img2, img3));
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        // Invalid base64
        property("blob", "#");
    }, e -> {
        Assert.assertContains("Invalid property value '#' for key 'blob'", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        // Invalid hex
        property("blob", "0xh");
    }, e -> {
        Assert.assertContains("Invalid property value '0xh' for key 'blob'", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        property("blob", 123f);
    }, e -> {
        Assert.assertContains("Invalid property value '123.0' " + "for key 'blob'", e.getMessage());
        Assert.assertContains("expect a value of type Blob, " + "actual type Float", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        property("blob", ImmutableList.of((byte) 97, 49, 50f));
    }, e -> {
        Assert.assertContains("Invalid property value '[97, 49, 50.0]' " + "for key 'blob'", e.getMessage());
        Assert.assertContains("expect byte or int value, but got '50.0'", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        propertyList("blob", img1, true);
    }, e -> {
        Assert.assertContains("Invalid property value '[Blob{01020832}, " + "true]' for key 'list_blob'", e.getMessage());
        Assert.assertContains("expect a value of type List<Blob>", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        propertySet("blob", img1, true);
    }, e -> {
        Assert.assertContains("Invalid property value '[Blob{01020832}, " + "true]' for key 'set_blob'", e.getMessage());
        Assert.assertContains("expect a value of type Set<Blob>", e.getMessage());
    });
}
Also used : Blob(com.baidu.hugegraph.util.Blob) Test(org.junit.Test)

Example 2 with Blob

use of com.baidu.hugegraph.util.Blob in project incubator-hugegraph by apache.

the class VertexCoreTest method testQueryByBlobProperty.

@Test
public void testQueryByBlobProperty() {
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.propertyKey("blob").asBlob().create();
    schema.vertexLabel("user").primaryKeys("id").properties("id", "blob").create();
    schema.indexLabel("userByBlob").secondary().onV("user").by("blob").create();
    Blob blob1 = Blob.wrap(new byte[] { 97, 49, 50, 51, 52 });
    Blob blob2 = Blob.wrap(new byte[] { 97, 98, 50, 51, 52 });
    graph().addVertex(T.label, "user", "id", 1, "blob", blob1);
    graph().addVertex(T.label, "user", "id", 2, "blob", blob2);
    this.mayCommitTx();
    List<Vertex> vertices = graph.traversal().V().hasLabel("user").has("blob", blob1).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "user", "id", 1, "blob", blob1);
    vertices = graph.traversal().V().hasLabel("user").has("blob", blob2).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "user", "id", 2, "blob", blob2);
}
Also used : FakeVertex(com.baidu.hugegraph.testutil.FakeObjects.FakeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) Blob(com.baidu.hugegraph.util.Blob) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Test(org.junit.Test)

Aggregations

Blob (com.baidu.hugegraph.util.Blob)2 Test (org.junit.Test)2 HugeGraph (com.baidu.hugegraph.HugeGraph)1 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)1 FakeVertex (com.baidu.hugegraph.testutil.FakeObjects.FakeVertex)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1