@Override
 protected void defineType(final OClass type) {
   type.createProperty(P_APIKEY, OType.STRING).setMandatory(true).setNotNull(true);
   type.createProperty(P_PRIMARY_PRINCIPAL, OType.STRING).setMandatory(true).setNotNull(true);
   type.createProperty(P_PRINCIPALS, OType.BINARY).setMandatory(true).setNotNull(true);
   type.createIndex(I_APIKEY, INDEX_TYPE.UNIQUE, P_APIKEY);
   type.createIndex(I_PRIMARY_PRINCIPAL, INDEX_TYPE.UNIQUE, P_PRIMARY_PRINCIPAL);
 }
  private void initializeExceptionRecordTable(String databaseUrl) {
    ODatabaseDocumentTx database = new ODatabaseDocumentTx(databaseUrl).create();
    OClass exceptionRecordClass = database.getMetadata().getSchema().createClass("ExceptionRecord");
    OClass exceptionVersionClass =
        database.getMetadata().getSchema().createClass("ExceptionRecordVersion");
    try {

      exceptionRecordClass.createProperty("id", OType.STRING);
      exceptionRecordClass.createProperty("status", OType.STRING);
      exceptionRecordClass.createProperty("approved", OType.BOOLEAN);
      exceptionRecordClass.createProperty("recordType", OType.STRING);
      exceptionRecordClass.createProperty("groupNonException", OType.BOOLEAN);
      exceptionRecordClass.createProperty("comment", OType.STRING);
      exceptionRecordClass.createProperty("jobId", OType.STRING);
      exceptionRecordClass.createProperty("dataflowName", OType.STRING);
      exceptionRecordClass.createProperty("username", OType.STRING);
      exceptionRecordClass.createProperty("timestamp", OType.LONG);
      exceptionRecordClass.createProperty("stageLabel", OType.STRING);
      exceptionRecordClass.createProperty("groupColumn", OType.STRING);
      exceptionRecordClass.createProperty("lastModifiedBy", OType.STRING);
      exceptionRecordClass.createProperty("lastModified", OType.LONG);
      exceptionRecordClass.createProperty("data", OType.STRING);

      exceptionVersionClass.createProperty("key-exceptionId", OType.STRING);
      exceptionVersionClass.createProperty("key-version", OType.STRING);
      exceptionVersionClass.createProperty("jobId", OType.STRING);
      exceptionVersionClass.createProperty("dataflowName", OType.STRING);

      exceptionRecordClass.createIndex("ExceptionRecord_pk", INDEX_TYPE.UNIQUE, "id");
      exceptionRecordClass.createIndex(
          "ExceptionRecord_idx",
          INDEX_TYPE.NOTUNIQUE,
          "status",
          "approved",
          "recordType",
          "groupNonException",
          "comment",
          "jobId",
          "dataflowName",
          "username",
          "timestamp",
          "stageLabel",
          "groupColumn",
          "lastModifiedBy",
          "lastModified");

      exceptionVersionClass.createIndex(
          "ExceptionVersion_Index",
          INDEX_TYPE.NOTUNIQUE,
          "key-exceptionId",
          "key-version",
          "jobId",
          "dataflowname");
    } finally {
      database.close();
    }
  }
  @BeforeClass
  public void setupSchema() {
    final OClass linkMapIndexTestClass =
        database.getMetadata().getSchema().createClass("LinkMapIndexTestClass");
    linkMapIndexTestClass.createProperty("linkMap", OType.LINKMAP);

    linkMapIndexTestClass.createIndex("mapIndexTestKey", OClass.INDEX_TYPE.NOTUNIQUE, "linkMap");
    linkMapIndexTestClass.createIndex(
        "mapIndexTestValue", OClass.INDEX_TYPE.NOTUNIQUE, "linkMap by value");

    database.getMetadata().getSchema().save();
  }
  public static OClass create(OSchema schema) {
    // Create Book table
    OClass oClass = schema.createClass("Comment");

    // comment STRING
    OProperty prop = oClass.createProperty("comment", OType.STRING);
    prop.setNotNull(true);
    // submittedDate DATETIME
    prop = oClass.createProperty("submittedDate", OType.DATETIME);
    prop.setNotNull(true);
    // eMailAddress STRING 0 60
    prop = oClass.createProperty("eMailAddress", OType.STRING);
    prop.setMin("0");
    prop.setMax("60");
    // userId STRING 6 20
    prop = oClass.createProperty("userId", OType.STRING);
    prop.setMin("6");
    prop.setMax("20");

    // Idexes
    // submittedDate_idx NOTUNIQUE submittedDate
    oClass.createIndex("submittedDate_idx", INDEX_TYPE.NOTUNIQUE, "submittedDate");

    return oClass;
  }
  protected void open() {
    log.debug("try to open database");

    this.graphFactory =
        new OrientGraphFactory(this.databaseUrl, this.username, this.password).setupPool(10, 10);

    OSchema schema = this.graphFactory.getDatabase().getMetadata().getSchema();

    OClass oClass = schema.getClass("V");

    if (!oClass.areIndexed("mailadress")) {
      oClass.createProperty("mailadress", OType.STRING).setNotNull(true);
      oClass.createIndex("mailadressIndex", OClass.INDEX_TYPE.UNIQUE, "mailadress");
    }

    log.debug("Succesfully opened database");
  }
  @Test
  public void queryCountWithConditions() {
    OClass indexed = database.getMetadata().getSchema().getOrCreateClass("Indexed");
    indexed.createProperty("key", OType.STRING);
    indexed.createIndex("keyed", OClass.INDEX_TYPE.NOTUNIQUE, "key");
    database.<ODocument>newInstance("Indexed").field("key", "one").save();
    database.<ODocument>newInstance("Indexed").field("key", "two").save();

    List<ODocument> result =
        database
            .command(
                new OSQLSynchQuery<ODocument>(
                    "select count(*) as total from Indexed where key > 'one'"))
            .execute();

    Assert.assertTrue(result.size() == 1);
    for (ODocument d : result) {
      Assert.assertNotNull(d.field("total"));
      Assert.assertTrue(((Number) d.field("total")).longValue() > 0);
    }
  }
  @BeforeMethod
  public void beforeMethod() throws Exception {
    super.beforeMethod();

    hashFunction.setValueSerializer(new OIntegerSerializer());

    if (database.getMetadata().getSchema().existsClass("AutoShardingTest"))
      database.getMetadata().getSchema().dropClass("AutoShardingTest");

    cls = database.getMetadata().getSchema().createClass("AutoShardingTest");
    cls.createProperty("id", OType.INTEGER);

    idx =
        cls.createIndex(
            "testAutoSharding",
            OClass.INDEX_TYPE.NOTUNIQUE.toString(),
            (OProgressListener) null,
            (ODocument) null,
            "AUTOSHARDING",
            new String[] {"id"});

    clusterIds = cls.getClusterIds();
  }