@Test
  public void vertexUniqueConstraint() {
    OrientGraph graph = newGraph();
    createVertexIndexLabel1(graph);
    String value = "value1";

    graph.addVertex(T.label, label1, key, value);
    graph.addVertex(T.label, label2, key, value);

    // no duplicates allowed for vertex with label1
    try {
      graph.addVertex(T.label, label1, key, value);
      Assert.fail("must throw duplicate key here!");
    } catch (ORecordDuplicatedException e) {
      // ok
    }

    // allow duplicate for vertex with label2
    graph.addVertex(T.label, label2, key, value);
  }
  // TODO: fix
  @Test
  public void indexCollation() {
    OrientGraph graph = newGraph();

    String label = "VC1";
    String key = "name";
    String value = "bob";

    Configuration config = new BaseConfiguration();
    config.setProperty("type", "UNIQUE");
    config.setProperty("keytype", OType.STRING);
    config.setProperty("collate", "ci");
    graph.createVertexIndex(key, label, config);

    graph.addVertex(T.label, label, key, value);
    // TODO: test with a "has" traversal, if/when that supports a case insensitive match predicate
    //        OrientIndexQuery indexRef = new OrientIndexQuery(true, Optional.of(label), key,
    // value.toUpperCase());
    //        Iterator<OrientVertex> result = graph.getIndexedVertices(indexRef).iterator();
    //        Assert.assertEquals(result.hasNext(), true);
  }
  @Test
  public void vertexIndexLookupWithValue() {
    OrientGraph graph = newGraph();
    createVertexIndexLabel1(graph);
    String value = "value1";

    // verify index created
    Assert.assertEquals(
        graph.getIndexedKeys(Vertex.class, label1), new HashSet<>(Collections.singletonList(key)));
    Assert.assertEquals(
        graph.getIndexedKeys(Vertex.class, label2), new HashSet<>(Collections.emptyList()));
    Assert.assertEquals(
        graph.getIndexedKeys(Edge.class, label1), new HashSet<>(Collections.emptyList()));

    Vertex v1 = graph.addVertex(T.label, label1, key, value);
    Vertex v2 = graph.addVertex(T.label, label2, key, value);

    // looking deep into the internals here - I can't find a nicer way to
    // auto verify that an index is actually used
    GraphTraversal<Vertex, Vertex> traversal =
        graph.traversal().V().has(T.label, P.eq(label1)).has(key, P.eq(value));
    OrientGraphStepStrategy.instance().apply(traversal.asAdmin());

    OrientGraphStep orientGraphStep = (OrientGraphStep) traversal.asAdmin().getStartStep();
    OrientIndexQuery orientIndexQuery = (OrientIndexQuery) orientGraphStep.findIndex().get();

    OIndex index = orientIndexQuery.index;
    Assert.assertEquals(1, index.getSize());
    Assert.assertEquals(v1.id(), index.get(value));
  }
 public Graph generateGraph(final String graphDirectoryName) {
   OrientGraph graph = (OrientGraph) super.generateGraph(graphDirectoryName);
   graph.setUseClassForEdgeLabel(false);
   graph.setUseClassForVertexLabel(false);
   return graph;
 }
 private void createVertexIndexLabel1(OrientGraph graph) {
   Configuration config = new BaseConfiguration();
   config.setProperty("type", "UNIQUE");
   config.setProperty("keytype", OType.STRING);
   graph.createVertexIndex(key, label1, config);
 }
    @Override
    public Void call() throws Exception {
      String name = Integer.toString(serverId);

      for (int i = 0; i < count; i += 2) {
        final OrientGraph graph = factory.getTx();

        final OrientVertex localVertex = graph.getVertex(v);

        try {
          if ((i + 1) % 100 == 0)
            System.out.println(
                "\nWriter "
                    + databaseUrl
                    + " id="
                    + Thread.currentThread().getId()
                    + " managed "
                    + (i + 1)
                    + "/"
                    + count
                    + " vertices so far");

          int retry = 0;
          boolean success = false;
          for (; retry < 200; ++retry) {
            try {
              updateVertex(localVertex);
              graph.commit();
              OLogManager.instance().debug(this, "Success count %d retry %d", i, retry);
              success = true;
              break;

            } catch (ODistributedRecordLockedException e) {
              lockExceptions.incrementAndGet();
              OLogManager.instance()
                  .info(this, "increment lockExceptions %d", lockExceptions.get());

            } catch (ONeedRetryException e) {
              OLogManager.instance().info(this, "Concurrent Exceptions " + e);

            } catch (Exception e) {
              graph.rollback();
              throw e;
            }

            Thread.sleep(10 + new Random().nextInt(500));

            localVertex.reload();

            OLogManager.instance()
                .info(
                    this,
                    "Retry %d with reloaded vertex v=%d",
                    retry,
                    localVertex.getRecord().getVersion());
          }

          Assert.assertTrue(
              "Unable to complete the transaction (last="
                  + i
                  + "/"
                  + count
                  + "), even after "
                  + retry
                  + " retries",
              success);

        } catch (InterruptedException e) {
          System.out.println("Writer received interrupt (db=" + databaseUrl);
          Thread.currentThread().interrupt();
          break;
        } catch (Exception e) {
          System.out.println("Writer received exception (db=" + databaseUrl);
          e.printStackTrace();
          break;
        } finally {
          graph.shutdown();
        }
      }

      System.out.println(
          "\nWriter " + name + " END. count = " + count + " lockExceptions: " + lockExceptions);
      return null;
    }