示例#1
0
  public void testUpdateEdgeContent() {
    final OSchema schema = database.getMetadata().getSchema();
    OClass vertex = schema.getClass("V");
    OClass edge = schema.getClass("E");

    schema.createClass("UpdateEdgeContentV", vertex);
    schema.createClass("UpdateEdgeContentE", edge);

    final ORID vOneId =
        ((ODocument)
                database.command(new OCommandSQL("create vertex UpdateEdgeContentV")).execute())
            .getIdentity();
    final ORID vTwoId =
        ((ODocument)
                database.command(new OCommandSQL("create vertex UpdateEdgeContentV")).execute())
            .getIdentity();

    database
        .command(new OCommandSQL("create edge UpdateEdgeContentE from " + vOneId + " to " + vTwoId))
        .execute();
    database
        .command(new OCommandSQL("create edge UpdateEdgeContentE from " + vOneId + " to " + vTwoId))
        .execute();
    database
        .command(new OCommandSQL("create edge UpdateEdgeContentE from " + vOneId + " to " + vTwoId))
        .execute();

    List<ODocument> result =
        database.query(
            new OSQLSynchQuery<ODocument>("select outV(), inV() from UpdateEdgeContentE"));

    Assert.assertEquals(result.size(), 3);

    for (ODocument doc : result) {
      Assert.assertEquals(doc.field("outV"), vOneId);
      Assert.assertEquals(doc.field("inV"), vTwoId);
    }

    database
        .command(new OCommandSQL("update UpdateEdgeContentE content {value : 'val'}"))
        .execute();

    result =
        database.query(
            new OSQLSynchQuery<ODocument>("select outV(), inV() from UpdateEdgeContentE"));

    Assert.assertEquals(result.size(), 3);

    for (ODocument doc : result) {
      Assert.assertEquals(doc.field("outV"), vOneId);
      Assert.assertEquals(doc.field("inV"), vTwoId);
    }

    result = database.query(new OSQLSynchQuery<ODocument>("select from UpdateEdgeContentE"));
    Assert.assertEquals(result.size(), 3);
    for (ODocument doc : result) {
      Assert.assertEquals(doc.field("value"), "val");
    }
  }
  @Test
  public void testInsertUpdateTransactionWithIndex() throws Exception {

    databaseDocumentTx.close();
    databaseDocumentTx.open("admin", "admin");
    OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    schema.reload();
    databaseDocumentTx.begin();
    ODocument doc = new ODocument("City");
    doc.field("name", "");
    ODocument doc1 = new ODocument("City");
    doc1.field("name", "");
    doc = databaseDocumentTx.save(doc);
    doc1 = databaseDocumentTx.save(doc1);
    databaseDocumentTx.commit();
    databaseDocumentTx.begin();
    doc = databaseDocumentTx.load(doc);
    doc1 = databaseDocumentTx.load(doc1);
    doc.field("name", "Rome");
    doc1.field("name", "Rome");
    databaseDocumentTx.save(doc);
    databaseDocumentTx.save(doc1);
    databaseDocumentTx.commit();
    OIndex idx = schema.getClass("City").getClassIndex("City.name");
    Collection<?> coll = (Collection<?>) idx.get("Rome");
    Assert.assertEquals(coll.size(), 2);
    Assert.assertEquals(idx.getSize(), 2);
  }
  @BeforeClass
  @Override
  public void init() {
    super.init();
    OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    OClass v = schema.getClass("V");
    OClass oClass = schema.createClass("Place");
    oClass.setSuperClass(v);
    oClass.createProperty("location", OType.EMBEDDED, schema.getClass("OMultiLineString"));
    oClass.createProperty("name", OType.STRING);

    databaseDocumentTx
        .command(
            new OCommandSQL("CREATE INDEX Place.location ON Place(location) SPATIAL ENGINE LUCENE"))
        .execute();
  }
示例#4
0
  public void testUpdateVertexContent() {
    final OSchema schema = database.getMetadata().getSchema();
    OClass vertex = schema.getClass("V");
    schema.createClass("UpdateVertexContent", vertex);

    final ORID vOneId =
        ((ODocument)
                database.command(new OCommandSQL("create vertex UpdateVertexContent")).execute())
            .getIdentity();
    final ORID vTwoId =
        ((ODocument)
                database.command(new OCommandSQL("create vertex UpdateVertexContent")).execute())
            .getIdentity();

    database.command(new OCommandSQL("create edge from " + vOneId + " to " + vTwoId)).execute();
    database.command(new OCommandSQL("create edge from " + vOneId + " to " + vTwoId)).execute();
    database.command(new OCommandSQL("create edge from " + vOneId + " to " + vTwoId)).execute();

    List<ODocument> result =
        database.query(
            new OSQLSynchQuery<ODocument>(
                "select sum(outE().size(), inE().size()) from UpdateVertexContent"));

    Assert.assertEquals(result.size(), 2);

    for (ODocument doc : result) {
      Assert.assertEquals(doc.field("sum"), 3);
    }

    database
        .command(
            new OCommandSQL(
                "update UpdateVertexContent content {value : 'val'} where @rid = " + vOneId))
        .execute();
    database
        .command(
            new OCommandSQL(
                "update UpdateVertexContent content {value : 'val'} where @rid =  " + vTwoId))
        .execute();

    result =
        database.query(
            new OSQLSynchQuery<ODocument>(
                "select sum(outE().size(), inE().size()) from UpdateVertexContent"));

    Assert.assertEquals(result.size(), 2);

    for (ODocument doc : result) {
      Assert.assertEquals(doc.field("sum"), 3);
    }

    result = database.query(new OSQLSynchQuery<ODocument>("select from UpdateVertexContent"));
    Assert.assertEquals(result.size(), 2);
    for (ODocument doc : result) {
      Assert.assertEquals(doc.field("value"), "val");
    }
  }
 @Override
 protected Object handleGet(String propName, Class<?> returnType) {
   if ("ownerClass".equals(propName)) {
     OSchema schema = OrientDbWebSession.get().getDatabase().getMetadata().getSchema();
     return schema.getClass(className);
   } else if ("fullName".equals(propName)) {
     return className + "." + values.get(NAME);
   } else return super.handleGet(propName, returnType);
 }
  @Before
  public void init() {
    initDB();
    OSchema schema = databaseDocumentTx.getMetadata().getSchema();

    if (schema.getClass("City") == null) {
      OClass oClass = schema.createClass("City");
      oClass.createProperty("name", OType.STRING);
    }
    databaseDocumentTx
        .command(new OCommandSQL("create index City.name on City (name) FULLTEXT ENGINE LUCENE"))
        .execute();
  }
  @Before
  public void init() {
    initDB();
    OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    OClass v = schema.getClass("V");
    OClass song = schema.createClass("City");
    song.setSuperClass(v);
    song.createProperty("name", OType.STRING);

    databaseDocumentTx
        .command(new OCommandSQL("create index City.name on City (name) FULLTEXT ENGINE LUCENE"))
        .execute();
  }
示例#8
0
  public static void drop(OSchema schema) {
    OClass oClass = schema.getClass("Comment");
    if (oClass != null) {
      // Indexes
      Set<OIndex<?>> idxs = oClass.getIndexes();
      if (idxs != null && idxs.size() > 0) {
        for (OIndex<?> idx : idxs) {
          idx.delete();
        }
      }

      // class
      schema.dropClass("Comment");
    }
  }
  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");
  }
示例#10
0
  @Test
  public void insertAvoidingSubQuery() {
    database.open("admin", "admin");

    final OSchema schema = database.getMetadata().getSchema();
    if (schema.getClass("test") == null) schema.createClass("test");

    ODocument doc =
        (ODocument)
            database
                .command(new OCommandSQL("INSERT INTO test(text) VALUES ('(Hello World)')"))
                .execute();

    Assert.assertTrue(doc != null);
    Assert.assertEquals(doc.field("text"), "(Hello World)");

    database.close();
  }
示例#11
0
  @Test
  public void insertSubQuery() {
    database.open("admin", "admin");

    final OSchema schema = database.getMetadata().getSchema();
    if (schema.getClass("test") == null) schema.createClass("test");

    ODocument doc =
        (ODocument)
            database
                .command(new OCommandSQL("INSERT INTO test SET names = (select name from OUser)"))
                .execute();

    Assert.assertTrue(doc != null);
    Assert.assertNotNull(doc.field("names"));
    Assert.assertTrue(doc.field("names") instanceof Collection);
    Assert.assertEquals(((Collection<?>) doc.field("names")).size(), 3);

    database.close();
  }
示例#12
0
  /**
   * Check if a class already exists, otherwise create it at the fly. If a transaction is running
   * commit changes, create the class and begin a new transaction.
   *
   * @param className Class's name
   */
  protected String checkForClassInSchema(final String className) {
    if (className == null) return null;

    OrientBaseGraph graph = getGraph();
    if (graph == null) return className;

    final OSchema schema = graph.getRawGraph().getMetadata().getSchema();

    if (!schema.existsClass(className)) {
      // CREATE A NEW CLASS AT THE FLY
      try {
        graph.executeOutsideTx(
            new OCallable<OClass, OrientBaseGraph>() {

              @Override
              public OClass call(final OrientBaseGraph g) {
                return schema.createClass(className, schema.getClass(getBaseClassName()));
              }
            },
            "Committing the active transaction to create the new type '",
            className,
            "' as subclass of '",
            getBaseClassName(),
            "'. The transaction will be reopen right after that. To avoid this behavior create the classes outside the transaction");

      } catch (OSchemaException e) {
        if (!schema.existsClass(className)) throw e;
      }
    } else {
      // CHECK THE CLASS INHERITANCE
      final OClass cls = schema.getClass(className);
      if (!cls.isSubClassOf(getBaseClassName()))
        throw new IllegalArgumentException(
            "Class '" + className + "' is not an instance of " + getBaseClassName());
    }

    return className;
  }
 @Override
 protected OProperty createInstance(OProperty proxy) {
   OSchema schema = OrientDbWebSession.get().getDatabase().getMetadata().getSchema();
   OClass oClass = schema.getClass(className);
   return oClass.createProperty(proxy.getName(), proxy.getType());
 }