com.orientechnologies.orient.core.command.script.OCommandScript Java Examples

The following examples show how to use com.orientechnologies.orient.core.command.script.OCommandScript. 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: PurgeUnusedSnapshotsFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void mockPagesOfComponents() {
  OCommandRequest oCommandRequest1 = getCommandRequest(10,
      testData(taskOlderThan.minusDays(1), "my.company", "foo", "1.0-SNAPSHOT"),
      testData(taskOlderThan.minusDays(2), "my.company", "bar", "2.0-SNAPSHOT"));
  OCommandRequest oCommandRequest2 = getCommandRequest(10,
      // non-match, not a snapshot
      testData(taskOlderThan.minusDays(1), "your.company", "biz", "1.0")
  );
  OCommandRequest oCommandRequest3 = getCommandRequest(10,
      testData(taskOlderThan.minusDays(2), "this.company", "baz", "3.0-SNAPSHOT")
  );
  OCommandRequest oCommandRequest4 = getCommandRequest(5,
      testData(taskOlderThan.minusDays(3), "my.company", "foo", "0.1-SNAPSHOT"),
      testData(taskOlderThan.minusDays(6), "your.company", "biz", "1.0-SNAPSHOT"),
      // non-match, this is same day
      testData(taskOlderThan, "that.company", "fizz", "1.2.3-SNAPSHOT")
  );

  when(oDatabaseDocumentTx.command(any(OCommandScript.class)))
      .thenReturn(oCommandRequest1, oCommandRequest2, oCommandRequest3, oCommandRequest4);
}
 
Example #2
Source File: OScriptImporterListener.java    From orientdb-etl with Apache License 2.0 6 votes vote down vote up
private Object executeEvent(final ODatabaseDocumentTx db, final String iEventName, final OCommandContext iContext) {
  if (events == null)
    return null;

  OCommandScript script = scripts.get(iEventName);

  if (script == null) {
    final String code = events.get(iEventName);
    if (code != null) {
      // CACHE IT
      script = new OCommandScript(code).setLanguage("Javascript");
      scripts.put(iEventName, script);
    }
  }

  if (script != null) {
    final Map<String, Object> pars = new HashMap<String, Object>();
    pars.put("task", iContext);
    pars.put("importer", this);

    return db.command(script).execute(pars);
  }
  return null;
}
 
Example #3
Source File: LuceneMixIndexTest.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void init() {

  initDB();
  OSchema schema = databaseDocumentTx.getMetadata().getSchema();
  OClass v = schema.getClass("V");
  OClass song = schema.createClass("Song");
  song.setSuperClass(v);
  song.createProperty("title", OType.STRING);
  song.createProperty("author", OType.STRING);
  song.createProperty("lyrics", OType.STRING);

  databaseDocumentTx.command(new OCommandSQL("create index Song.author on Song (author) NOTUNIQUE")).execute();

  databaseDocumentTx.command(new OCommandSQL("create index Song.composite on Song (title,lyrics) FULLTEXT ENGINE LUCENE"))
      .execute();

  InputStream stream = ClassLoader.getSystemResourceAsStream("testLuceneIndex.sql");

  databaseDocumentTx.command(new OCommandScript("sql", getScriptFromStream(stream))).execute();

}
 
Example #4
Source File: AbstractDbRule.java    From light with Apache License 2.0 6 votes vote down vote up
protected String execUpdateCmd(Map<String, Object> data, boolean commit) {
    String result = "";
    String script = (String) data.get("script");
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try{
        graph.command(new OCommandScript("sql", script)).execute();
        if(commit) {
            graph.commit();
        } else {
            graph.rollback();
        }
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
        result = e.getMessage();
    } finally {
        graph.shutdown();
    }
    return result;
}
 
Example #5
Source File: PurgeUnusedSnapshotsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the next page of snapshot components that were last accessed before the specified date. The date when a
 * component was last downloaded is the last time an asset of that snapshot was downloaded.
 *
 * Uses an iterative approach in order to handle large repositories with many hundreds of thousands or millions of
 * assets. Note the current implementation is a bit hacky due to an Orient bug which prevents us from using a GROUP
 * BY with LIMIT. Furthermore, because of the required use of the Orient 'script sql' approach, we must loop through
 * the entire component set. Forward looking to Orient 3, it fixes the GROUP BY approach and this can be much
 * simplified. See NEXUS-13130 for further details.
 */
private List<Component> findNextPageOfUnusedSnapshots(final StorageTx tx,
                                                      final LocalDate olderThan,
                                                      final ORID bucketId,
                                                      final String unusedWhereTemplate)
{
  String query = format(UNUSED_QUERY, bucketId, lastComponent, findUnusedLimit, unusedWhereTemplate, olderThan,
      findUnusedLimit - 1);

  List<ODocument> result = tx.getDb().command(new OCommandScript("sql", query)).execute();

  if (isEmpty(result)) {
    return emptyList();
  }

  // get the last component to use in the WHERE for the next page
  ODocument lastDoc = Iterables.getLast(result).field(P_COMPONENT);
  lastComponent = lastDoc.getIdentity();

  Date olderThanDate = java.sql.Date.valueOf(olderThan);

  // filters in this stream are to check the last record as all others are filtered out in the 3rd part of the command query
  return result.stream() // remove entries that don't match on last download date
      .filter(entries -> {
        Date lastDownloaded = entries.field("lastdownloaded");
        return lastDownloaded.compareTo(olderThanDate) < 0;
      })
      .map(doc -> componentEntityAdapter.readEntity(doc.field(P_COMPONENT)))
      // remove entries that are not snapshots
      .filter(
          component -> {
            String baseVersion = (String) component.attributes().child(Maven2Format.NAME).get(P_BASE_VERSION);
            return baseVersion != null && baseVersion.endsWith("SNAPSHOT");
          })
      .collect(Collectors.toList());
}
 
Example #6
Source File: RemoveSnapshotsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Find all components (snapshot *OR* release) for a given GAV
 */
@VisibleForTesting
Iterable<Component> findComponentsForGav(final StorageTx tx, final Repository repository, final GAV gav)
{
  final Bucket bucket = tx.findBucket(repository);
  final ORID bucketId = AttachedEntityHelper.id(bucket);

  // the version to use for a release version search. E.g. gav.baseVersion is 1.1-SNAPSHOT, we need to search for 1.1
  String releaseVersion = gav.baseVersion.replace("-SNAPSHOT", "");

  final OResultSet<ODocument> result = tx.getDb().command(new OCommandScript("sql", COMPONENTS_FOR_GABV))
      .execute(gav.group, gav.name, bucketId, gav.baseVersion, releaseVersion);
  return result.stream().map(componentEntityAdapter::readEntity).collect(Collectors.toList());
}
 
Example #7
Source File: PurgeUnusedSnapshotsFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void assertQueries() {
  // assertions for the four queries run for the four 'pages'
  ArgumentCaptor<OCommandScript> argumentCaptor = ArgumentCaptor.forClass(OCommandScript.class);
  verify(oDatabaseDocumentTx, times(4)).command(argumentCaptor.capture());
  List<OCommandScript> args = argumentCaptor.getAllValues();
  assertThat(args.size(), equalTo(4));

  // finally, assert the actual queries
  String query = "sql.LET $a = (SELECT FROM component WHERE bucket = %s AND @rid > %s ORDER BY @rid LIMIT %d); " +
      "LET $b = (SELECT component, max(ifnull(last_downloaded, blob_created)) as lastdownloaded " +
      "FROM asset WHERE ((bucket = #1:1 AND component = $a[0]) OR " +
      "(bucket = #1:1 AND component = $a[1]) OR " +
      "(bucket = #1:1 AND component = $a[2]) OR " +
      "(bucket = #1:1 AND component = $a[3]) OR " +
      "(bucket = #1:1 AND component = $a[4]) OR " +
      "(bucket = #1:1 AND component = $a[5]) OR " +
      "(bucket = #1:1 AND component = $a[6]) OR " +
      "(bucket = #1:1 AND component = $a[7]) OR " +
      "(bucket = #1:1 AND component = $a[8]) OR " +
      "(bucket = #1:1 AND component = $a[9])) " +
      "GROUP BY component ORDER BY component); " +
      "SELECT FROM $b WHERE (component.attributes.maven2.baseVersion LIKE '%%SNAPSHOT' " +
      "AND lastdownloaded < '%s') OR component = $a[9];";
  String date = makeIso8601(taskOlderThan);
  assertThat(args.get(0).toString(), equalTo(format(query, bucketId, "#-1:-1", 10, date)));
  assertThat(args.get(1).toString(), equalTo(format(query, bucketId, "#1:10", 10, date)));
  assertThat(args.get(2).toString(), equalTo(format(query, bucketId, "#1:20", 10, date)));
  assertThat(args.get(3).toString(), equalTo(format(query, bucketId, "#1:30", 10, date)));
}
 
Example #8
Source File: OCodeBlock.java    From orientdb-etl with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(OETLProcessor iProcessor, final ODocument iConfiguration, OCommandContext iContext) {
  super.configure(iProcessor, iConfiguration, iContext);
  if (iConfiguration.containsField("language"))
    language = iConfiguration.field("language");

  if (iConfiguration.containsField("code"))
    code = iConfiguration.field("code");
  else
    throw new IllegalArgumentException("'code' parameter is mandatory in Code Transformer");

  cmd = new OCommandExecutorScript().parse(new OCommandScript(language, code));
}
 
Example #9
Source File: OCodeTransformer.java    From orientdb-etl with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(OETLProcessor iProcessor, final ODocument iConfiguration, OCommandContext iContext) {
  super.configure(iProcessor, iConfiguration, iContext);
  if (iConfiguration.containsField("language"))
    language = iConfiguration.field("language");

  String code;
  if (iConfiguration.containsField("code"))
    code = iConfiguration.field("code");
  else
    throw new IllegalArgumentException("'code' parameter is mandatory in Code Transformer");

  cmd = new OCommandExecutorScript().parse(new OCommandScript(language, code));
}
 
Example #10
Source File: LuceneVsLuceneTest.java    From orientdb-lucene with Apache License 2.0 5 votes vote down vote up
@Test
public void testLuceneVsLucene() throws IOException, ParseException {
  InputStream stream = ClassLoader.getSystemResourceAsStream("testLuceneIndex.sql");

  databaseDocumentTx.command(new OCommandScript("sql", getScriptFromStream(stream))).execute();

  for (ODocument oDocument : databaseDocumentTx.browseClass("Song")) {

    String title = oDocument.field("title");
    if (title != null) {
      Document d = new Document();
      d.add(new Field("title", title, Field.Store.NO, Field.Index.ANALYZED));

      indexWriter.addDocument(d);

    }
  }

  indexWriter.close();
  IndexReader reader = DirectoryReader.open(getDirectory());
  IndexSearcher searcher = new IndexSearcher(reader);
  Query query = new MultiFieldQueryParser(OLuceneIndexManagerAbstract.LUCENE_VERSION, new String[] { "title" },
      new StandardAnalyzer(OLuceneIndexManagerAbstract.LUCENE_VERSION)).parse("down the");
  final TopDocs docs = searcher.search(query, Integer.MAX_VALUE);
  ScoreDoc[] hits = docs.scoreDocs;
  List<ODocument> oDocs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(
      "select *,$score from Song where title LUCENE \"down the\""));
  Assert.assertEquals(oDocs.size(), hits.length);

  int i = 0;
  for (ScoreDoc hit : hits) {
    Assert.assertEquals(oDocs.get(i).field("$score"), hit.score);
    i++;
  }
  reader.close();

}
 
Example #11
Source File: AbstractDbRule.java    From light with Apache License 2.0 5 votes vote down vote up
protected String execSchemaCmd(Map<String, Object> data) {
    String result = "";
    String script = (String) data.get("script");
    OrientGraphNoTx graph = ServiceLocator.getInstance().getGraphNoTx();
    try{
        graph.command(new OCommandScript("sql", script)).execute();
    } catch (Exception e) {
        logger.error("Exception:", e);
        result = e.getMessage();
    } finally {
        graph.shutdown();
    }
    return result;
}
 
Example #12
Source File: ScriptMethodExtension.java    From guice-persist-orient with MIT License 4 votes vote down vote up
@Override
protected OCommandRequest createQueryCommand(final ScriptCommandMethodDescriptor descriptor,
                                             final SqlCommandDescriptor desc) {
    return new OCommandScript(descriptor.language, desc.command);
}
 
Example #13
Source File: LuceneSkipLimitTest.java    From orientdb-lucene with Apache License 2.0 4 votes vote down vote up
public void testContext() {
    InputStream stream = ClassLoader.getSystemResourceAsStream("testLuceneIndex.sql");

    databaseDocumentTx.command(new OCommandScript("sql", getScriptFromStream(stream))).execute();


    List<ODocument> docs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(
            "select * from Song where [title] LUCENE \"(title:man)\""));


    Assert.assertEquals(docs.size(), 14);

    ODocument doc = docs.get(9);
    docs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(
            "select * from Song where [title] LUCENE \"(title:man)\" skip 10 limit 10"));

    Assert.assertEquals(docs.size(), 4);

    Assert.assertEquals(docs.contains(doc), false);

    docs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(
            "select * from Song where [title] LUCENE \"(title:man)\" skip 14 limit 10"));

    Assert.assertEquals(docs.size(), 0);
}
 
Example #14
Source File: LuceneMultiFieldTest.java    From orientdb-lucene with Apache License 2.0 4 votes vote down vote up
@Test
public void loadAndTest() {

  InputStream stream = ClassLoader.getSystemResourceAsStream("testLuceneIndex.sql");

  databaseDocumentTx.command(new OCommandScript("sql", getScriptFromStream(stream))).execute();

  List<ODocument> docs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(
      "select * from Song where [title,author] LUCENE \"(title:mountain AND author:Fabbio)\""));

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

  docs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(
      "select * from Song where [title,author] LUCENE \"(title:mountain OR author:Fabbio)\""));

  Assert.assertEquals(docs.size(), 90);

  docs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select * from Song where [title,author] LUCENE \"mountain\""));

  Assert.assertEquals(docs.size(), 4);

  docs = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select * from Song where [title,author] LUCENE \"fabbio\""));

  Assert.assertEquals(docs.size(), 87);
}
 
Example #15
Source File: LuceneCreateIndexRemote.java    From orientdb-lucene with Apache License 2.0 3 votes vote down vote up
@Override
public void loadAndTest() {
  InputStream stream = ClassLoader.getSystemResourceAsStream("testLuceneIndex.sql");

  databaseDocumentTx.command(new OCommandScript("sql", getScriptFromStream(stream))).execute();

  databaseDocumentTx.command(new OCommandSQL("create index Song.title on Song (title) FULLTEXT ENGINE LUCENE")).execute();
  databaseDocumentTx.command(new OCommandSQL("create index Song.author on Song (author) FULLTEXT ENGINE LUCENE")).execute();

  assertQuery();

  restart();

  assertQuery();

  ODocument doc = new ODocument("Song");

  doc.field("title", "Test");
  doc.field("author", "Author");

  databaseDocumentTx.save(doc);

  assertNewQuery();

  restart();

  assertNewQuery();

}
 
Example #16
Source File: LuceneCreateIndexTest.java    From orientdb-lucene with Apache License 2.0 3 votes vote down vote up
@Override
public void loadAndTest() {
  InputStream stream = ClassLoader.getSystemResourceAsStream("testLuceneIndex.sql");

  databaseDocumentTx.command(new OCommandScript("sql", getScriptFromStream(stream))).execute();

  databaseDocumentTx.command(new OCommandSQL("create index Song.title on Song (title) FULLTEXT ENGINE LUCENE")).execute();
  databaseDocumentTx.command(new OCommandSQL("create index Song.author on Song (author) FULLTEXT ENGINE LUCENE")).execute();

  ODocument doc = new ODocument("Song");

  doc.field("title", "Local");
  doc.field("author", "Local");

  databaseDocumentTx.save(doc);
  assertQuery();

  assertNewQuery();

  databaseDocumentTx.close();

  databaseDocumentTx.open("admin", "admin");

  assertQuery();

  assertNewQuery();
}