Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:orientechnologies/orientdb into …
Browse files Browse the repository at this point in the history
…develop
  • Loading branch information
laa committed Jan 16, 2015
2 parents 6e84112 + b692ad2 commit ddf02bd
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 189 deletions.
Expand Up @@ -19,13 +19,6 @@
*/
package com.orientechnologies.orient.core.sql;

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.orientechnologies.common.collection.OMultiCollectionIterator;
import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.common.concur.resource.OSharedResource;
Expand Down Expand Up @@ -87,6 +80,12 @@
import com.orientechnologies.orient.core.storage.OStorage;
import com.orientechnologies.orient.core.storage.OStorage.LOCKING_STRATEGY;

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* Executes the SQL SELECT statement. the parse() method compiles the query and builds the meta information needed by the execute().
* If the query contains the ORDER BY clause, the results are temporary collected internally, then ordered and finally returned all
Expand Down Expand Up @@ -764,24 +763,20 @@ protected void searchInClasses() {
}

protected int parseProjections() {
if (!parserOptionalKeyword(KEYWORD_SELECT)) {
if (!parserOptionalKeyword(KEYWORD_SELECT))
return -1;
}

int upperBound = OStringSerializerHelper.getLowerIndexOf(parserTextUpperCase, parserGetCurrentPosition(), KEYWORD_FROM_2FIND,
KEYWORD_LET_2FIND);
if (upperBound == -1)
// UP TO THE END
{
// UP TO THE END
upperBound = parserText.length();
}

int lastRealPositionProjection = -1;

int currPos = parserGetCurrentPosition();
if (currPos == -1) {
if (currPos == -1)
return -1;
}

final String projectionString = parserText.substring(currPos, upperBound);
if (projectionString.trim().length() > 0) {
Expand All @@ -795,33 +790,29 @@ protected int parseProjections() {
for (String projectionItem : items) {
String projection = OStringSerializerHelper.smartTrim(projectionItem.trim(), true, true);

if (projectionDefinition == null) {
if (projectionDefinition == null)
throw new OCommandSQLParsingException("Projection not allowed with FLATTEN() and EXPAND() operators");
}

final List<String> words = OStringSerializerHelper.smartSplit(projection, ' ');

String fieldName;
if (words.size() > 1 && words.get(1).trim().equalsIgnoreCase(KEYWORD_AS)) {
// FOUND AS, EXTRACT ALIAS
if (words.size() < 3) {
if (words.size() < 3)
throw new OCommandSQLParsingException("Found 'AS' without alias");
}

fieldName = words.get(2).trim();

if (projectionDefinition.containsKey(fieldName)) {
if (projectionDefinition.containsKey(fieldName))
throw new OCommandSQLParsingException("Field '" + fieldName
+ "' is duplicated in current SELECT, choose a different name");
}

projection = words.get(0).trim();

if (words.size() > 3) {
if (words.size() > 3)
lastRealPositionProjection = projectionString.indexOf(words.get(3));
} else {
else
lastRealPositionProjection += projectionItem.length() + 1;
}

} else {
// EXTRACT THE FIELD NAME WITHOUT FUNCTIONS AND/OR LINKS
Expand All @@ -830,42 +821,38 @@ protected int parseProjections() {

lastRealPositionProjection = projectionString.indexOf(fieldName) + fieldName.length() + 1;

if (fieldName.charAt(0) == '@') {
if (fieldName.charAt(0) == '@')
fieldName = fieldName.substring(1);
}

endPos = extractProjectionNameSubstringEndPosition(fieldName);

if (endPos > -1) {
if (endPos > -1)
fieldName = fieldName.substring(0, endPos);
}

// FIND A UNIQUE NAME BY ADDING A COUNTER
for (int fieldIndex = 2; projectionDefinition.containsKey(fieldName); ++fieldIndex) {
for (int fieldIndex = 2; projectionDefinition.containsKey(fieldName); ++fieldIndex)
fieldName += fieldIndex;
}
}

String p = upperCase(projection);
final String p = upperCase(projection);
if (p.startsWith("FLATTEN(") || p.startsWith("EXPAND(")) {
if (p.startsWith("FLATTEN(")) {
if (p.startsWith("FLATTEN("))
OLogManager.instance().debug(this, "FLATTEN() operator has been replaced by EXPAND()");
}

List<String> pars = OStringSerializerHelper.getParameters(projection);
if (pars.size() != 1) {
if (pars.size() != 1)
throw new OCommandSQLParsingException(
"EXPAND/FLATTEN operators expects the field name as parameter. Example EXPAND( out )");
}

expandTarget = OSQLHelper.parseValue(this, pars.get(0).trim(), context);

// BY PASS THIS AS PROJECTION BUT TREAT IT AS SPECIAL
projectionDefinition = null;
projections = null;

if (groupedResult == null && expandTarget instanceof OSQLFunctionRuntime
&& ((OSQLFunctionRuntime) expandTarget).aggregateResults()) {
&& ((OSQLFunctionRuntime) expandTarget).aggregateResults())
getProjectionGroup(null);
}

continue;
}
Expand Down Expand Up @@ -895,13 +882,12 @@ protected int parseProjections() {
}
}

if (upperBound < parserText.length() - 1) {
if (upperBound < parserText.length() - 1)
parserSetCurrentPosition(upperBound);
} else if (lastRealPositionProjection > -1) {
else if (lastRealPositionProjection > -1)
parserMoveCurrentPosition(lastRealPositionProjection);
} else {
else
parserSetEndOfText();
}

return parserGetCurrentPosition();
}
Expand Down Expand Up @@ -1324,7 +1310,9 @@ private void fetchFromTarget(final Iterator<? extends OIdentifiable> iTarget) {

if (queryScanThresholdWarning > 0 && browsed > queryScanThresholdWarning && compiledFilter != null) {
reportTip(String
.format("Query '%s' fetched more than %d records: to speed up the execution, create an index or change the query to use an existent index", parserText, queryScanThresholdWarning));
.format(
"Query '%s' fetched more than %d records: to speed up the execution, create an index or change the query to use an existent index",
parserText, queryScanThresholdWarning));
queryScanThresholdWarning = 0;
}
}
Expand Down
Expand Up @@ -94,7 +94,9 @@ public static ODocument applyRecord(final ODocument iValue, final Map<String, Ob
if (v.equals("*")) {
// COPY ALL
inputDocument.copyTo(iValue);
projectionValue = null;
// CONTINUE WITH NEXT ITEM
continue;

} else if (v instanceof OSQLFilterItemVariable || v instanceof OSQLFilterItemField) {
final OSQLFilterItemAbstract var = (OSQLFilterItemAbstract) v;
final OPair<OSQLMethodRuntime, Object[]> last = var.getLastChainOperator();
Expand All @@ -115,8 +117,14 @@ public static ODocument applyRecord(final ODocument iValue, final Map<String, Ob
} else if (v instanceof OSQLFunctionRuntime) {
final OSQLFunctionRuntime f = (OSQLFunctionRuntime) v;
projectionValue = f.execute(inputDocument, inputDocument, iValue, iContext);
} else
} else {
if (v == null) {
// SIMPLE NULL VALUE: SET IT IN DOCUMENT
iValue.field(prjName, v);
continue;
}
projectionValue = v;
}

if (projectionValue != null)
if (projectionValue instanceof ORidBag)
Expand Down
Expand Up @@ -483,15 +483,15 @@ public void testJSONLinkd() {
+ jaimeDoc.toJSON() + "}}");
tyrionDoc.save();

System.out.println("The saved documents are:");
// System.out.println("The saved documents are:");
for (ODocument o : database.browseClass("PersonTest")) {
System.out.println("my id is " + o.getIdentity().toString());
System.out.println("my name is: " + o.field("name"));
System.out.println("my ODocument representation is " + o);
System.out.println("my JSON representation is " + o.toJSON());
System.out.println("my traversable links are: ");
// System.out.println("my id is " + o.getIdentity().toString());
// System.out.println("my name is: " + o.field("name"));
// System.out.println("my ODocument representation is " + o);
// System.out.println("my JSON representation is " + o.toJSON());
// System.out.println("my traversable links are: ");
for (OIdentifiable id : new OSQLSynchQuery<ODocument>("traverse * from " + o.getIdentity().toString())) {
System.out.println(database.load(id.getIdentity()).toJSON());
database.load(id.getIdentity()).toJSON();
}
}
}
Expand Down Expand Up @@ -688,8 +688,8 @@ public void call(ORecordId iRID, Long iParameter) {
final ODatabaseDocumentTx db = pool.acquire();
long tot;
while ((tot = db.countClusterElements("Account")) < startRecordNumber + TOT_RECORDS) {
System.out.println("Asynchronous insertion: found " + tot + " records but waiting till " + (startRecordNumber + TOT_RECORDS)
+ " is reached");
// System.out.println("Asynchronous insertion: found " + tot + " records but waiting till " + (startRecordNumber + TOT_RECORDS)
// + " is reached");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Expand Down
Expand Up @@ -15,17 +15,15 @@
*/
package com.orientechnologies.orient.test.database.auto;

import com.orientechnologies.orient.core.command.script.OCommandScript;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.orientechnologies.orient.core.command.script.OCommandScript;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.sql.OCommandSQL;

@Test
public class ConcurrentCommandAndOpenTest extends DocumentDBBaseTest {
protected final static int MAX_CONNS = 10;
Expand All @@ -45,9 +43,9 @@ public void concurrentCommands() throws Exception {
public void run() {
ODatabaseDocumentTx db = new ODatabaseDocumentTx(url).open("admin", "admin");
try {
System.out.println("Start sleeping...");
//System.out.println("Start sleeping...");
db.command(new OCommandScript("SQL", "sleep 5000")).execute();
System.out.println("Sleeping done!");
//System.out.println("Sleeping done!");

} finally {
db.close();
Expand All @@ -62,20 +60,20 @@ public void run() {
int commandExecuted = 0;
int iterations = MAX_CONNS * 5;
for (int i = 0; i < iterations; ++i) {
System.out.println("Open database " + i);
//System.out.println("Open database " + i);
ODatabaseDocumentTx db = new ODatabaseDocumentTx(url).open("admin", "admin");
try {
db.command(new OCommandSQL("select from OUser")).execute();
System.out.println("Command executed " + i);
//System.out.println("Command executed " + i);
commandExecuted++;
} finally {
db.close();
}
}

System.out.println("Waiting for the sleeping thread...");
//System.out.println("Waiting for the sleeping thread...");
thread.join();
System.out.println("Done!");
//System.out.println("Done!");

Assert.assertEquals(commandExecuted, iterations);
}
Expand Down
Expand Up @@ -15,20 +15,18 @@
*/
package com.orientechnologies.orient.test.database.auto;

import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;

import com.orientechnologies.common.concur.ONeedRetryException;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.test.ConcurrentTestHelper;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.orientechnologies.common.concur.ONeedRetryException;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.test.ConcurrentTestHelper;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;

@Test
public class ConcurrentQueriesTest extends DocumentDBBaseTest {
Expand Down Expand Up @@ -94,7 +92,7 @@ public void init() {

@Test
public void concurrentCommands() throws Exception {
System.out.println("Spanning " + THREADS + " threads...");
// System.out.println("Spanning " + THREADS + " threads...");

ConcurrentTestHelper.test(THREADS, new ConcurrentTestHelper.TestFactory<Void>() {
@Override
Expand All @@ -103,8 +101,8 @@ public Callable<Void> createWorker() {
}
});

System.out.println("Done! Total queries executed in parallel: " + counter.get() + " average retries: "
+ ((float) totalRetries.get() / (float) counter.get()));
// System.out.println("Done! Total queries executed in parallel: " + counter.get() + " average retries: "
// + ((float) totalRetries.get() / (float) counter.get()));

Assert.assertEquals(counter.get(), CYCLES * THREADS);
}
Expand Down

0 comments on commit ddf02bd

Please sign in to comment.