use of com.orientechnologies.common.util.OResettable in project orientdb by orientechnologies.
the class OLazyRecordMultiIterator method getCurrentIterator.
@SuppressWarnings("unchecked")
private Iterator<OIdentifiable> getCurrentIterator() {
if (iteratorIndex > underlyingIterators.length)
throw new NoSuchElementException();
Object next = underlyingIterators[iteratorIndex];
if (next == null) {
// GET THE ITERATOR
if (underlyingSources[iteratorIndex] instanceof OResettable) {
// REUSE IT
((OResettable) underlyingSources[iteratorIndex]).reset();
underlyingIterators[iteratorIndex] = underlyingSources[iteratorIndex];
} else if (underlyingSources[iteratorIndex] instanceof Iterable<?>) {
// CREATE A NEW ONE FROM THE COLLECTION
underlyingIterators[iteratorIndex] = ((Iterable<?>) underlyingSources[iteratorIndex]).iterator();
} else if (underlyingSources[iteratorIndex] instanceof Iterator<?>) {
// COPY IT
underlyingIterators[iteratorIndex] = underlyingSources[iteratorIndex];
} else
throw new IllegalStateException("Unsupported iteration source: " + underlyingSources[iteratorIndex]);
next = underlyingIterators[iteratorIndex];
}
if (next instanceof Iterator<?>)
return (Iterator<OIdentifiable>) next;
return ((Collection<OIdentifiable>) next).iterator();
}
use of com.orientechnologies.common.util.OResettable in project orientdb by orientechnologies.
the class ORuntimeResult method applyRecord.
@SuppressWarnings("unchecked")
public static ODocument applyRecord(final ODocument iValue, final Map<String, Object> iProjections, final OCommandContext iContext, final OIdentifiable iRecord) {
// APPLY PROJECTIONS
ORecord record = (iRecord != null ? iRecord.getRecord() : null);
//MANAGE SPECIFIC CASES FOR RECORD BYTES
if (ORecordBytes.RECORD_TYPE == ORecordInternal.getRecordType(record)) {
for (Entry<String, Object> projection : iProjections.entrySet()) {
if ("@rid".equalsIgnoreCase("" + projection.getValue())) {
iValue.field(projection.getKey(), record.getIdentity());
} else if ("@size".equalsIgnoreCase("" + projection.getValue())) {
iValue.field(projection.getKey(), record.getSize());
} else if ("@version".equalsIgnoreCase("" + projection.getValue())) {
iValue.field(projection.getKey(), record.getVersion());
} else {
Object val = projection.getValue();
if (val instanceof Number || val instanceof String || val instanceof Boolean) {
iValue.field(projection.getKey(), val);
} else {
iValue.field(projection.getKey(), (Object) null);
}
}
}
return iValue;
}
final ODocument inputDocument = (ODocument) record;
if (iProjections.isEmpty())
// SELECT * CASE
inputDocument.copyTo(iValue);
else {
for (Entry<String, Object> projection : iProjections.entrySet()) {
final String prjName = projection.getKey();
final Object v = projection.getValue();
if (v == null && prjName != null) {
iValue.field(prjName, (Object) null);
continue;
}
final Object projectionValue;
if (v != null && v.equals("*")) {
// COPY ALL
inputDocument.copyTo(iValue);
// 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();
if (last != null && last.getKey().getMethod() instanceof OSQLMethodField && last.getValue() != null && last.getValue().length == 1 && last.getValue()[0].equals("*")) {
final Object value = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
if (inputDocument != null && value != null && inputDocument instanceof ODocument && value instanceof ODocument) {
// COPY FIELDS WITH PROJECTION NAME AS PREFIX
for (String fieldName : ((ODocument) value).fieldNames()) {
iValue.field(prjName + fieldName, ((ODocument) value).field(fieldName));
}
}
projectionValue = null;
} else
// RETURN A VARIABLE FROM THE CONTEXT
projectionValue = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
} else if (v instanceof OSQLFunctionRuntime) {
final OSQLFunctionRuntime f = (OSQLFunctionRuntime) v;
projectionValue = f.execute(inputDocument, inputDocument, iValue, iContext);
} 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)
iValue.field(prjName, new ORidBag((ORidBag) projectionValue));
else if (projectionValue instanceof OIdentifiable && !(projectionValue instanceof ORID) && !(projectionValue instanceof ORecord))
iValue.field(prjName, ((OIdentifiable) projectionValue).getRecord());
else if (projectionValue instanceof Iterator) {
boolean link = true;
// make temporary value typical case graph database elemenet's iterator edges
if (projectionValue instanceof OResettable)
((OResettable) projectionValue).reset();
final List<Object> iteratorValues = new ArrayList<Object>();
final Iterator projectionValueIterator = (Iterator) projectionValue;
while (projectionValueIterator.hasNext()) {
Object value = projectionValueIterator.next();
if (value instanceof OIdentifiable) {
value = ((OIdentifiable) value).getRecord();
if (value != null && !((OIdentifiable) value).getIdentity().isPersistent())
link = false;
}
if (value != null)
iteratorValues.add(value);
}
iValue.field(prjName, iteratorValues, link ? OType.LINKLIST : OType.EMBEDDEDLIST);
} else if (projectionValue instanceof ODocument && !((ODocument) projectionValue).getIdentity().isPersistent()) {
iValue.field(prjName, projectionValue, OType.EMBEDDED);
} else if (projectionValue instanceof Set<?>) {
OType type = OType.getTypeByValue(projectionValue);
if (type == OType.LINKSET && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
type = OType.EMBEDDEDSET;
iValue.field(prjName, projectionValue, type);
} else if (projectionValue instanceof Map<?, ?>) {
OType type = OType.getTypeByValue(projectionValue);
if (type == OType.LINKMAP && !entriesPersistent(((Map<?, OIdentifiable>) projectionValue).values()))
type = OType.EMBEDDEDMAP;
iValue.field(prjName, projectionValue, type);
} else if (projectionValue instanceof List<?>) {
OType type = OType.getTypeByValue(projectionValue);
if (type == OType.LINKLIST && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
type = OType.EMBEDDEDLIST;
iValue.field(prjName, projectionValue, type);
} else
iValue.field(prjName, projectionValue);
}
}
return iValue;
}
Aggregations