Java Code Examples for com.orientechnologies.orient.core.index.OIndex#get()

The following examples show how to use com.orientechnologies.orient.core.index.OIndex#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OLuceneNearFunction.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
@Override
public Object execute(Object iThis, OIdentifiable iCurrentRecord, Object iCurrentResult, Object[] iParams,
    OCommandContext iContext) {

  String clazz = (String) iParams[0];
  String latField = (String) iParams[1];
  String lngField = (String) iParams[2];
  ODatabaseDocument databaseRecord = ODatabaseRecordThreadLocal.INSTANCE.get();
  Set<OIndex<?>> indexes = databaseRecord.getMetadata().getSchema().getClass(clazz).getInvolvedIndexes(latField, lngField);
  for (OIndex i : indexes) {
    if (OClass.INDEX_TYPE.SPATIAL.toString().equals(i.getInternal().getType())) {
      List<Object> params = new ArrayList<Object>();
      params.add(iParams[3]);
      params.add(iParams[4]);
      double distance = iParams.length > 5 ? ((Number) iParams[5]).doubleValue() : 0;
      Object ret = i.get(new OSpatialCompositeKey(params).setMaxDistance(distance));
      if (ret instanceof Collection) {
        if (context == null)
          context = new HashSet<Object>();
        context.addAll((Collection<?>) ret);
      }
      return ret;
    }
  }
  return null;
}
 
Example 2
Source File: LuceneInsertDeleteTest.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertUpdateWithIndex() throws Exception {

  databaseDocumentTx.getMetadata().reload();
  OSchema schema = databaseDocumentTx.getMetadata().getSchema();

  ODocument doc = new ODocument("City");
  doc.field("name", "Rome");
  databaseDocumentTx.save(doc);

  OIndex idx = schema.getClass("City").getClassIndex("City.name");
  Collection<?> coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 1);
  Assert.assertEquals(idx.getSize(), 1);
  doc = databaseDocumentTx.load((ORID) coll.iterator().next());

  databaseDocumentTx.delete(doc);

  coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 0);
  Assert.assertEquals(idx.getSize(), 0);

}
 
Example 3
Source File: LuceneInsertUpdateSingleDocumentNoTxTest.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertUpdateTransactionWithIndex() throws Exception {

  databaseDocumentTx.close();
  databaseDocumentTx.open("admin", "admin");
  OSchema schema = databaseDocumentTx.getMetadata().getSchema();
  schema.reload();
  ODocument doc = new ODocument("City");
  doc.field("name", "");
  ODocument doc1 = new ODocument("City");
  doc1.field("name", "");
  doc = databaseDocumentTx.save(doc);
  doc1 = databaseDocumentTx.save(doc1);

  doc = databaseDocumentTx.load(doc);
  doc1 = databaseDocumentTx.load(doc1);
  doc.field("name", "Rome");
  doc1.field("name", "Rome");
  databaseDocumentTx.save(doc);
  databaseDocumentTx.save(doc1);
  OIndex idx = schema.getClass("City").getClassIndex("City.name");
  Collection<?> coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 2);
  Assert.assertEquals(idx.getSize(), 2);
}
 
Example 4
Source File: AbstractTagRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected List<String> getTagEntityListDb(String host, String tagId) {
    List<String> entityList = null;
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        OIndex<?> tagHostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex("tagHostIdIdx");
        OCompositeKey key = new OCompositeKey(host, tagId);
        OIdentifiable oid = (OIdentifiable) tagHostIdIdx.get(key);
        if (oid != null) {
            ODocument doc = (ODocument)oid.getRecord();
            entityList = new ArrayList<String>();
            ORidBag entities = doc.field("in_HasTag");
            Iterator<OIdentifiable> iterator = entities.iterator();
            while (iterator.hasNext()) {
                OIdentifiable identifiable = iterator.next();
                entityList.add(identifiable.getIdentity().toString());
            }
        }
    } catch (Exception e) {
        logger.error("Exception:", e);
    } finally {
        graph.shutdown();
    }
    return entityList;
}
 
Example 5
Source File: ComponentDatabaseUpgrade_1_10.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void apply() {
  if (hasSchemaClass(configDatabaseInstance, "repository")
      && hasSchemaClass(componentDatabaseInstance, "bucket")
      && hasSchemaClass(componentDatabaseInstance, "component")
      && hasSchemaClass(componentDatabaseInstance, "asset")) {

    List<String> repositories;
    try (ODatabaseDocumentTx configDb = configDatabaseInstance.get().connect()) {
      repositories = configDb.<List<ODocument>>query(
          new OSQLSynchQuery<ODocument>(SELECT_YUM_PROXY_REPOSITORIES))
          .stream()
          .map(d -> (String) d.field(P_REPOSITORY_NAME))
          .collect(toList());
    }

    log.info("Updating name and version for yum-proxy components, this could be a long-running operation");

    for (String repository : repositories) {
      try (ODatabaseDocumentTx componentDb = componentDatabaseInstance.get().connect()) {
        log.debug("Repairing component coordinates yum repository {}", repository);
        OIndex<?> bucketIdx = componentDb.getMetadata().getIndexManager().getIndex(I_REPOSITORY_NAME);
        OIdentifiable bucket = (OIdentifiable) bucketIdx.get(repository);
        if (bucket == null) {
          log.warn("Unable to find bucket for {}", repository);
        }
        else {
          fixComponentBatch(componentDb, bucket);
        }
      }
    }
  }
}
 
Example 6
Source File: OLuceneTextOperator.java    From orientdb-lucene with Apache License 2.0 5 votes vote down vote up
@Override
public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index, List<Object> keyParams, boolean ascSortOrder) {
  OIndexCursor cursor;
  Object indexResult = index.get(new OFullTextCompositeKey(keyParams).setContext(iContext));
  if (indexResult == null || indexResult instanceof OIdentifiable)
    cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, new OFullTextCompositeKey(keyParams));
  else
    cursor = new OIndexCursorCollectionValue(((Collection<OIdentifiable>) indexResult).iterator(), new OFullTextCompositeKey(
        keyParams));
  iContext.setVariable("$luceneIndex", true);
  return cursor;
}
 
Example 7
Source File: LuceneInsertUpdateTest.java    From orientdb-lucene with Apache License 2.0 5 votes vote down vote up
@Test
public void testInsertUpdateWithIndex() throws Exception {

  OSchema schema = databaseDocumentTx.getMetadata().getSchema();

  ODocument doc = new ODocument("City");
  doc.field("name", "Rome");

  databaseDocumentTx.save(doc);
  OIndex idx = schema.getClass("City").getClassIndex("City.name");
  Collection<?> coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 1);

  doc = databaseDocumentTx.load((ORID) coll.iterator().next());
  Assert.assertEquals(doc.field("name"), "Rome");

  doc.field("name", "London");
  databaseDocumentTx.save(doc);

  coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 0);
  coll = (Collection<?>) idx.get("London");
  Assert.assertEquals(coll.size(), 1);

  doc = databaseDocumentTx.load((ORID) coll.iterator().next());
  Assert.assertEquals(doc.field("name"), "London");

  doc.field("name", "Berlin");
  databaseDocumentTx.save(doc);

  coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 0);
  coll = (Collection<?>) idx.get("London");
  Assert.assertEquals(coll.size(), 0);
  coll = (Collection<?>) idx.get("Berlin");
  Assert.assertEquals(coll.size(), 1);

}
 
Example 8
Source File: LuceneInsertUpdateSingleDocumentTransactionTest.java    From orientdb-lucene with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: BranchRule.java    From light with Apache License 2.0 5 votes vote down vote up
public OrientVertex getBranchByHostId(OrientGraph graph, String branchType, String host, String categoryId) {
    OrientVertex branch = null;
    OIndex<?> hostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex(branchType + "HostIdIdx");
    OCompositeKey key = new OCompositeKey(host, categoryId);
    OIdentifiable oid = (OIdentifiable) hostIdIdx.get(key);
    if (oid != null) {
        branch = graph.getVertex(oid.getRecord());
    }
    return branch;
}
 
Example 10
Source File: AbstractConfigRule.java    From light with Apache License 2.0 5 votes vote down vote up
public OrientVertex getConfigByHostId(OrientGraph graph, String host, String configId) {
    OrientVertex config = null;
    OIndex<?> hostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex("configHostIdIdx");
    OCompositeKey key = new OCompositeKey(host, configId);
    OIdentifiable oid = (OIdentifiable) hostIdIdx.get(key);
    if (oid != null) {
        config = graph.getVertex(oid.getRecord());
    }
    return config;
}
 
Example 11
Source File: LuceneInsertIntegrityRemoteTest.java    From orientdb-lucene with Apache License 2.0 4 votes vote down vote up
@Test
public void testInsertUpdateWithIndex() throws Exception {

  databaseDocumentTx.getMetadata().reload();
  OSchema schema = databaseDocumentTx.getMetadata().getSchema();

  ODocument doc = new ODocument("City");
  doc.field("name", "Rome");

  databaseDocumentTx.begin();
  databaseDocumentTx.save(doc);
  databaseDocumentTx.commit();
  OIndex idx = schema.getClass("City").getClassIndex("City.name");

  Collection<?> coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 1);

  doc = databaseDocumentTx.load((ORID) coll.iterator().next());
  Assert.assertEquals(doc.field("name"), "Rome");

  databaseDocumentTx.begin();
  doc.field("name", "London");
  databaseDocumentTx.save(doc);
  databaseDocumentTx.commit();

  coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 0);
  coll = (Collection<?>) idx.get("London");
  Assert.assertEquals(coll.size(), 1);

  doc = databaseDocumentTx.load((ORID) coll.iterator().next());
  Assert.assertEquals(doc.field("name"), "London");

  databaseDocumentTx.begin();
  doc.field("name", "Berlin");
  databaseDocumentTx.save(doc);
  databaseDocumentTx.commit();

  doc = databaseDocumentTx.load(doc.getIdentity(), null, true);
  Assert.assertEquals(doc.field("name"), "Berlin");

  coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 0);
  coll = (Collection<?>) idx.get("London");
  Assert.assertEquals(coll.size(), 0);
  coll = (Collection<?>) idx.get("Berlin");
  Assert.assertEquals(idx.getSize(), 1);
  Assert.assertEquals(coll.size(), 1);

  Thread.sleep(1000);
  kill(false);

  initDB(false);

  doc = databaseDocumentTx.load(doc.getIdentity(), null, true);

  Assert.assertEquals(doc.field("name"), "Berlin");

  schema = databaseDocumentTx.getMetadata().getSchema();
  idx = schema.getClass("City").getClassIndex("City.name");

  Assert.assertEquals(idx.getSize(), 1);
  coll = (Collection<?>) idx.get("Rome");
  Assert.assertEquals(coll.size(), 0);
  coll = (Collection<?>) idx.get("London");
  Assert.assertEquals(coll.size(), 0);
  coll = (Collection<?>) idx.get("Berlin");
  Assert.assertEquals(coll.size(), 1);
}
 
Example 12
Source File: AbstractCatalogRule.java    From light with Apache License 2.0 4 votes vote down vote up
protected void addProductDb(Map<String, Object> data) throws Exception {
    String host = (String)data.get("host");
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try{
        graph.begin();
        OrientVertex createUser = (OrientVertex)graph.getVertexByKey("User.userId", data.remove("createUserId"));
        String parentId = (String)data.remove("parentId");
        List<String> tags = (List<String>)data.remove("tags");
        OrientVertex product = graph.addVertex("class:Product", data);
        createUser.addEdge("Create", product);
        // parent
        OrientVertex parent = getBranchByHostId(graph, categoryType, host, parentId);
        if(parent != null) {
            parent.addEdge("HasProduct", product);
        }
        // tag
        if(tags != null && tags.size() > 0) {
            for(String tagId: tags) {
                Vertex tag = null;
                // get the tag is it exists
                OIndex<?> tagHostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex("tagHostIdIdx");
                OCompositeKey tagKey = new OCompositeKey(host, tagId);
                OIdentifiable tagOid = (OIdentifiable) tagHostIdIdx.get(tagKey);
                if (tagOid != null) {
                    tag = graph.getVertex(tagOid.getRecord());
                    product.addEdge("HasTag", tag);
                } else {
                    tag = graph.addVertex("class:Tag", "host", host, "tagId", tagId, "createDate", data.get("createDate"));
                    createUser.addEdge("Create", tag);
                    product.addEdge("HasTag", tag);
                }
            }
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 13
Source File: AbstractBfnRule.java    From light with Apache License 2.0 4 votes vote down vote up
protected void addPostDb(String bfnType, Map<String, Object> data) throws Exception {
    String className = bfnType.substring(0, 1).toUpperCase() + bfnType.substring(1);
    String host = (String)data.get("host");
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try{
        graph.begin();
        Vertex createUser = graph.getVertexByKey("User.userId", data.remove("createUserId"));
        List<String> tags = (List<String>)data.remove("tags");
        
        OrientVertex post = graph.addVertex("class:Post", data);
        createUser.addEdge("Create", post);
        // parent
        OrientVertex parent = getBranchByHostId(graph, bfnType, host, (String) data.get("parentId"));
        if(parent != null) {
            parent.addEdge("HasPost", post);
        }
        // tag
        if(tags != null && tags.size() > 0) {
            for(String tagId: tags) {
                Vertex tag = null;
                // get the tag is it exists
                OIndex<?> tagHostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex("tagHostIdIdx");
                OCompositeKey tagKey = new OCompositeKey(host, tagId);
                OIdentifiable tagOid = (OIdentifiable) tagHostIdIdx.get(tagKey);
                if (tagOid != null) {
                    tag = graph.getVertex(tagOid.getRecord());
                    post.addEdge("HasTag", tag);
                } else {
                    tag = graph.addVertex("class:Tag", "host", host, "tagId", tagId, "createDate", data.get("createDate"));
                    createUser.addEdge("Create", tag);
                    post.addEdge("HasTag", tag);
                }
            }
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
    } finally {
        graph.shutdown();
    }
}
 
Example 14
Source File: TestCreateNested1.java    From orientdb-lucene with Apache License 2.0 3 votes vote down vote up
@Test
public void testIndex() {

  ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:tmp");

  db.create();

  OClass test = db.getMetadata().getSchema().createClass("Test");

  test.createProperty("name", OType.STRING);
  test.createProperty("age", OType.INTEGER);

  test.createIndex("Test.name_age", OClass.INDEX_TYPE.NOTUNIQUE, "name", "age");

  ODocument doc = new ODocument("Test");

  doc.field("name", "Enrico");
  doc.field("age", 32);
  db.save(doc);

  OIndex<?> index = db.getMetadata().getIndexManager().getIndex("Test.name_age");

  Collection<OIdentifiable> results = (Collection<OIdentifiable>) index.get(new OCompositeKey(Arrays.asList("Enrico", 32)));

  Assert.assertEquals(results.size(), 1);

  results = (Collection<OIdentifiable>) index.get(new OCompositeKey(Arrays.asList("Enrico", 31)));
  Assert.assertEquals(results.size(), 0);
}
 
Example 15
Source File: LuceneInsertMultithreadTest.java    From orientdb-lucene with Apache License 2.0 3 votes vote down vote up
@Override
public void run() {

  databaseDocumentTx = new ODatabaseDocumentTx(url);
  databaseDocumentTx.open("admin", "admin");
  OSchema schema = databaseDocumentTx.getMetadata().getSchema();
  OIndex idx = schema.getClass("City").getClassIndex("City.name");

  for (int i = 0; i < cycle; i++) {

    Collection<?> coll = (Collection<?>) idx.get("Rome");

  }

}