Search in sources :

Example 1 with OResultSet

use of com.orientechnologies.orient.core.sql.query.OResultSet in project orientdb by orientechnologies.

the class OCommandExecutorSQLLiveSelect method execute.

public Object execute(final Map<Object, Object> iArgs) {
    try {
        final ODatabaseDocumentInternal db = getDatabase();
        execInSeparateDatabase(new OCallable() {

            @Override
            public Object call(Object iArgument) {
                return execDb = ((ODatabaseDocumentTx) db).copy();
            }
        });
        synchronized (random) {
            // TODO do something better ;-)!
            token = random.nextInt();
        }
        subscribeToLiveQuery(token, db);
        bindDefaultContextVariables();
        if (iArgs != null) // BIND ARGUMENTS INTO CONTEXT TO ACCESS FROM ANY POINT (EVEN FUNCTIONS)
        {
            for (Map.Entry<Object, Object> arg : iArgs.entrySet()) {
                context.setVariable(arg.getKey().toString(), arg.getValue());
            }
        }
        if (timeoutMs > 0) {
            getContext().beginExecution(timeoutMs, timeoutStrategy);
        }
        ODocument result = new ODocument();
        // TODO change this name...?
        result.field("token", token);
        ((OResultSet) getResult()).add(result);
        return getResult();
    } finally {
        if (request != null && request.getResultListener() != null) {
            request.getResultListener().end();
        }
    }
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.query.OResultSet) OCallable(com.orientechnologies.common.util.OCallable) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Map(java.util.Map) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with OResultSet

use of com.orientechnologies.orient.core.sql.query.OResultSet in project orientdb by orientechnologies.

the class OCommandExecutorSQLSelect method parallelExec.

private boolean parallelExec(final Iterator<? extends OIdentifiable> iTarget) {
    final OResultSet result = (OResultSet) getResultInstance();
    // BROWSE ALL THE RECORDS ON CURRENT THREAD BUT DELEGATE UNMARSHALLING AND FILTER TO A THREAD POOL
    final ODatabaseDocumentInternal db = getDatabase();
    if (limit > -1) {
        if (result != null) {
            result.setLimit(limit);
        }
    }
    final boolean res = execParallelWithPool((ORecordIteratorClusters) iTarget, (ODatabaseDocumentTx) db);
    if (OLogManager.instance().isDebugEnabled())
        OLogManager.instance().debug(this, "Parallel query '%s' completed", parserText);
    return res;
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.query.OResultSet) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 3 with OResultSet

use of com.orientechnologies.orient.core.sql.query.OResultSet in project orientdb by orientechnologies.

the class OQueryOperatorIn method executeIndexQuery.

@SuppressWarnings("unchecked")
@Override
public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index, List<Object> keyParams, boolean ascSortOrder) {
    final OIndexDefinition indexDefinition = index.getDefinition();
    final OIndexInternal<?> internalIndex = index.getInternal();
    OIndexCursor cursor;
    if (!internalIndex.canBeUsedInEqualityOperators())
        return null;
    if (indexDefinition.getParamCount() == 1) {
        final Object inKeyValue = keyParams.get(0);
        Collection<Object> inParams;
        if (inKeyValue instanceof List<?>)
            inParams = (Collection<Object>) inKeyValue;
        else if (inKeyValue instanceof OSQLFilterItem)
            inParams = (Collection<Object>) ((OSQLFilterItem) inKeyValue).getValue(null, null, iContext);
        else
            inParams = Collections.singleton(inKeyValue);
        if (inParams instanceof OResultSet) {
            //manage IN (subquery)
            Set newInParams = new HashSet();
            for (Object o : ((OResultSet) inParams)) {
                if (o instanceof ODocument && ((ODocument) o).getIdentity().getClusterId() < -1) {
                    ODocument doc = (ODocument) o;
                    String[] fieldNames = doc.fieldNames();
                    if (fieldNames.length == 1) {
                        newInParams.add(doc.field(fieldNames[0]));
                    } else {
                        newInParams.add(o);
                    }
                } else {
                    newInParams.add(o);
                }
            }
            inParams = newInParams;
        }
        final List<Object> inKeys = new ArrayList<Object>();
        boolean containsNotCompatibleKey = false;
        for (final Object keyValue : inParams) {
            final Object key;
            if (indexDefinition instanceof OIndexDefinitionMultiValue)
                key = ((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(OSQLHelper.getValue(keyValue));
            else
                key = indexDefinition.createValue(OSQLHelper.getValue(keyValue));
            if (key == null) {
                containsNotCompatibleKey = true;
                break;
            }
            inKeys.add(key);
        }
        if (containsNotCompatibleKey)
            return null;
        cursor = index.iterateEntries(inKeys, ascSortOrder);
    } else {
        final List<Object> partialKey = new ArrayList<Object>();
        partialKey.addAll(keyParams);
        partialKey.remove(keyParams.size() - 1);
        final Object inKeyValue = keyParams.get(keyParams.size() - 1);
        final Collection<Object> inParams;
        if (inKeyValue instanceof List<?>)
            inParams = (Collection<Object>) inKeyValue;
        else if (inKeyValue instanceof OSQLFilterItem)
            inParams = (Collection<Object>) ((OSQLFilterItem) inKeyValue).getValue(null, null, iContext);
        else
            throw new IllegalArgumentException("Key '" + inKeyValue + "' is not valid");
        final List<Object> inKeys = new ArrayList<Object>();
        final OCompositeIndexDefinition compositeIndexDefinition = (OCompositeIndexDefinition) indexDefinition;
        boolean containsNotCompatibleKey = false;
        for (final Object keyValue : inParams) {
            List<Object> fullKey = new ArrayList<Object>();
            fullKey.addAll(partialKey);
            fullKey.add(keyValue);
            final Object key = compositeIndexDefinition.createSingleValue(fullKey);
            if (key == null) {
                containsNotCompatibleKey = true;
                break;
            }
            inKeys.add(key);
        }
        if (containsNotCompatibleKey) {
            return null;
        }
        if (inKeys == null)
            return null;
        if (indexDefinition.getParamCount() == keyParams.size()) {
            final Object indexResult;
            indexResult = index.iterateEntries(inKeys, ascSortOrder);
            if (indexResult == null || indexResult instanceof OIdentifiable) {
                cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, inKeys);
            } else if (indexResult instanceof OIndexCursor) {
                cursor = (OIndexCursor) indexResult;
            } else {
                cursor = new OIndexCursorCollectionValue((Collection<OIdentifiable>) indexResult, inKeys);
            }
        } else
            return null;
    }
    updateProfiler(iContext, internalIndex, keyParams, indexDefinition);
    return cursor;
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.query.OResultSet) OSQLFilterItem(com.orientechnologies.orient.core.sql.filter.OSQLFilterItem) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OResultSet(com.orientechnologies.orient.core.sql.query.OResultSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with OResultSet

use of com.orientechnologies.orient.core.sql.query.OResultSet in project orientdb by orientechnologies.

the class FunctionsTest method testMultiThreadsFunctionCallMoreThanPool.

@Test
public void testMultiThreadsFunctionCallMoreThanPool() {
    final OIdentifiable f = database.command(new OCommandSQL("create function testMTCall \"return 3;\" LANGUAGE Javascript")).execute();
    Assert.assertNotNull(f);
    final int TOT = 1000;
    final int threadNum = OGlobalConfiguration.SCRIPT_POOL.getValueAsInteger() * 3;
    // System.out.println("Starting " + threadNum + " concurrent threads with scriptPool="
    // + OGlobalConfiguration.SCRIPT_POOL.getValueAsInteger() + " executing function for " + TOT + " times");
    final long startTime = System.currentTimeMillis();
    final AtomicLong counter = new AtomicLong();
    final Thread[] threads = new Thread[threadNum];
    for (int i = 0; i < threadNum; ++i) {
        threads[i] = new Thread() {

            public void run() {
                for (int cycle = 0; cycle < TOT; ++cycle) {
                    OResultSet<OIdentifiable> res1 = database.command(new OCommandSQL("select testMTCall()")).execute();
                    Assert.assertNotNull(res1);
                    Assert.assertNotNull(res1.get(0));
                    Assert.assertEquals(((ODocument) res1.get(0)).field("testMTCall"), 3);
                    counter.incrementAndGet();
                }
            }
        };
        threads[i].start();
    }
    for (int i = 0; i < threadNum; ++i) try {
        threads[i].join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Assert.assertEquals(counter.get(), (long) threadNum * TOT);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OResultSet(com.orientechnologies.orient.core.sql.query.OResultSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

OResultSet (com.orientechnologies.orient.core.sql.query.OResultSet)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OCallable (com.orientechnologies.common.util.OCallable)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)1 OSQLFilterItem (com.orientechnologies.orient.core.sql.filter.OSQLFilterItem)1 Map (java.util.Map)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.testng.annotations.Test)1