use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.
the class OTransactionRealAbstract method getIndexFieldRidDependencies.
private static Dependency[] getIndexFieldRidDependencies(OIndex<?> index) {
final OIndexDefinition definition = index.getDefinition();
if (// type for untyped index it still no resolved
definition == null)
return null;
final OType[] types = definition.getTypes();
final Dependency[] dependencies = new Dependency[types.length];
for (int i = 0; i < types.length; ++i) dependencies[i] = getTypeRidDependency(types[i]);
return dependencies;
}
use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.
the class OJDBCExtractor method begin.
@Override
public void begin() {
try {
stm = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
stm.setFetchSize(fetchSize);
if (queryCount != null) {
// GET THE TOTAL COUNTER
final ResultSet countRs = stm.executeQuery(query);
try {
if (countRs != null && countRs.next())
total = countRs.getInt(1);
} finally {
if (countRs != null)
try {
countRs.close();
} catch (SQLException e) {
}
}
}
rs = stm.executeQuery(query);
rsColumns = rs.getMetaData().getColumnCount();
columnNames = new ArrayList<String>(rsColumns);
columnTypes = new ArrayList<OType>(rsColumns);
for (int i = 1; i <= rsColumns; ++i) {
final String colName = rs.getMetaData().getColumnName(i);
columnNames.add(colName);
OType type = OType.ANY;
final int sqlType = rs.getMetaData().getColumnType(i);
switch(sqlType) {
case Types.BIT:
case Types.BOOLEAN:
type = OType.BOOLEAN;
break;
case Types.SMALLINT:
type = OType.SHORT;
break;
case Types.INTEGER:
type = OType.INTEGER;
break;
case Types.FLOAT:
type = OType.FLOAT;
break;
case Types.DOUBLE:
type = OType.DOUBLE;
break;
case Types.BIGINT:
type = OType.LONG;
break;
case Types.DECIMAL:
type = OType.DECIMAL;
break;
case Types.DATE:
type = OType.DATE;
break;
case Types.TIMESTAMP:
type = OType.DATETIME;
break;
case Types.VARCHAR:
case Types.LONGNVARCHAR:
case Types.LONGVARCHAR:
type = OType.STRING;
break;
case Types.BINARY:
case Types.BLOB:
type = OType.BINARY;
break;
case Types.CHAR:
case Types.TINYINT:
type = OType.BYTE;
break;
}
columnTypes.add(type);
}
} catch (SQLException e) {
throw new OExtractorException("[JDBC extractor] error on executing query '" + query + "'", e);
}
}
use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.
the class OObjectSerializerHelper method multiValueToStream.
private static Object multiValueToStream(final Object iMultiValue, OType iType, final OEntityManager iEntityManager, final OUserObject2RecordHandler iObj2RecHandler, final ODatabaseObject db, final ODocument iRecord, final boolean iSaveOnlyDirty) {
if (iMultiValue == null)
return null;
final Collection<Object> sourceValues;
if (iMultiValue instanceof Collection<?>) {
sourceValues = (Collection<Object>) iMultiValue;
} else {
sourceValues = (Collection<Object>) ((Map<?, ?>) iMultiValue).values();
}
if (iType == null) {
if (sourceValues.size() == 0)
return iMultiValue;
// TRY TO UNDERSTAND THE COLLECTION TYPE BY ITS CONTENT
final Object firstValue = sourceValues.iterator().next();
if (firstValue == null)
return iMultiValue;
// DETERMINE THE RIGHT TYPE BASED ON SOURCE MULTI VALUE OBJECT
if (OType.isSimpleType(firstValue)) {
if (iMultiValue instanceof List)
iType = OType.EMBEDDEDLIST;
else if (iMultiValue instanceof Set)
iType = OType.EMBEDDEDSET;
else
iType = OType.EMBEDDEDMAP;
} else {
if (iMultiValue instanceof List)
iType = OType.LINKLIST;
else if (iMultiValue instanceof Set)
iType = OType.LINKSET;
else
iType = OType.LINKMAP;
}
}
Object result = iMultiValue;
final OType linkedType;
// CREATE THE RETURN MULTI VALUE OBJECT BASED ON DISCOVERED TYPE
if (iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.LINKSET)) {
if (iRecord != null && iType.equals(OType.EMBEDDEDSET))
result = new OTrackedSet<Object>(iRecord);
else
result = new ORecordLazySet(iRecord);
} else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.LINKLIST)) {
if (iRecord != null && iType.equals(OType.EMBEDDEDLIST))
result = new OTrackedList<Object>(iRecord);
else
result = new ArrayList<Object>();
}
if (iType.equals(OType.LINKLIST) || iType.equals(OType.LINKSET) || iType.equals(OType.LINKMAP))
linkedType = OType.LINK;
else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.EMBEDDEDMAP))
linkedType = OType.EMBEDDED;
else
throw new IllegalArgumentException("Type " + iType + " must be a multi value type (collection or map)");
if (iMultiValue instanceof Set<?>) {
for (Object o : sourceValues) {
((Collection<Object>) result).add(typeToStream(o, linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
}
} else if (iMultiValue instanceof List<?>) {
for (int i = 0; i < sourceValues.size(); i++) {
((List<Object>) result).add(typeToStream(((List<?>) sourceValues).get(i), linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
}
} else {
if (iMultiValue instanceof OObjectLazyMap<?>) {
result = ((OObjectLazyMap<?>) iMultiValue).getUnderlying();
} else {
if (iRecord != null && iType.equals(OType.EMBEDDEDMAP))
result = new OTrackedMap<Object>(iRecord);
else
result = new HashMap<Object, Object>();
for (Entry<Object, Object> entry : ((Map<Object, Object>) iMultiValue).entrySet()) {
((Map<Object, Object>) result).put(entry.getKey(), typeToStream(entry.getValue(), linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
}
}
}
return result;
}
use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.
the class OObjectProxyMethodHandler method manageCollectionSave.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object manageCollectionSave(final Object self, final Field f, Collection<?> value, final boolean customSerialization, final boolean isFieldUpdate) {
final Class genericType = OReflectionHelper.getGenericMultivalueType(f);
if (customSerialization) {
if (value instanceof List<?>) {
final List<Object> list = new ArrayList<Object>();
setDocFieldValue(f.getName(), list, OType.EMBEDDEDLIST);
value = new OObjectCustomSerializerList(OObjectEntitySerializer.getSerializedType(f), doc, new ArrayList<Object>(), (List<Object>) value);
} else {
final Set<Object> set = new HashSet<Object>();
setDocFieldValue(f.getName(), set, OType.EMBEDDEDSET);
value = new OObjectCustomSerializerSet(OObjectEntitySerializer.getSerializedType(f), doc, set, (Set<Object>) value);
}
} else if (genericType != null && genericType.isEnum()) {
if (value instanceof List<?>) {
final List<Object> list = new ArrayList<Object>();
setDocFieldValue(f.getName(), list, OType.EMBEDDEDLIST);
value = new OObjectEnumLazyList(genericType, doc, list, (List<Object>) value);
} else {
final Set<Object> set = new HashSet<Object>();
setDocFieldValue(f.getName(), set, OType.EMBEDDEDSET);
value = new OObjectEnumLazySet(genericType, doc, set, (Set<Object>) value);
}
} else if (!(value instanceof OObjectLazyMultivalueElement)) {
boolean embedded = OObjectEntitySerializer.isEmbeddedField(self.getClass(), f.getName());
if (value instanceof List) {
OType type = embedded ? OType.EMBEDDEDLIST : OType.LINKLIST;
List<OIdentifiable> docList = doc.field(f.getName(), type);
if (docList == null) {
if (embedded)
docList = new OTrackedList<OIdentifiable>(doc);
else
docList = new ORecordLazyList(doc);
setDocFieldValue(f.getName(), docList, type);
} else if (isFieldUpdate) {
docList.clear();
}
value = new OObjectLazyList(self, docList, value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
} else if (value instanceof Set) {
OType type = embedded ? OType.EMBEDDEDSET : OType.LINKSET;
Set<OIdentifiable> docSet = doc.field(f.getName(), type);
if (docSet == null) {
if (embedded)
docSet = new OTrackedSet<OIdentifiable>(doc);
else
docSet = new ORecordLazySet(doc);
setDocFieldValue(f.getName(), docSet, type);
} else if (isFieldUpdate) {
docSet.clear();
}
value = new OObjectLazySet(self, docSet, (Set<?>) value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
}
}
if (!((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).isLazyLoading())
((OObjectLazyMultivalueElement) value).detach(false);
return value;
}
use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.
the class OObjectSerializerHelper method toStream.
/**
* Serialize the user POJO to a ORecordDocument instance.
*
* @param iPojo
* User pojo to serialize
* @param iRecord
* Record where to update
* @param iObj2RecHandler
*/
public static ODocument toStream(final Object iPojo, final ODocument iRecord, final OEntityManager iEntityManager, final OClass schemaClass, final OUserObject2RecordHandler iObj2RecHandler, final ODatabaseObject db, final boolean iSaveOnlyDirty) {
if (iSaveOnlyDirty && !iRecord.isDirty())
return iRecord;
final long timer = Orient.instance().getProfiler().startChrono();
final Integer identityRecord = System.identityHashCode(iRecord);
if (OSerializationThreadLocal.INSTANCE.get().contains(identityRecord))
return iRecord;
OSerializationThreadLocal.INSTANCE.get().add(identityRecord);
OProperty schemaProperty;
final Class<?> pojoClass = iPojo.getClass();
final List<Field> properties = getClassFields(pojoClass);
// CHECK FOR ID BINDING
final Field idField = fieldIds.get(pojoClass);
if (idField != null) {
Object id = getFieldValue(iPojo, idField.getName());
if (id != null) {
// FOUND
if (id instanceof ORecordId) {
ORecordInternal.setIdentity(iRecord, (ORecordId) id);
} else if (id instanceof Number) {
// TREATS AS CLUSTER POSITION
((ORecordId) iRecord.getIdentity()).setClusterId(schemaClass.getDefaultClusterId());
((ORecordId) iRecord.getIdentity()).setClusterPosition(((Number) id).longValue());
} else if (id instanceof String)
((ORecordId) iRecord.getIdentity()).fromString((String) id);
else if (id.getClass().equals(Object.class))
ORecordInternal.setIdentity(iRecord, (ORecordId) id);
else
OLogManager.instance().warn(OObjectSerializerHelper.class, "@Id field has been declared as %s while the supported are: ORID, Number, String, Object", id.getClass());
}
}
// CHECK FOR VERSION BINDING
final Field vField = fieldVersions.get(pojoClass);
boolean versionConfigured = false;
if (vField != null) {
versionConfigured = true;
Object ver = getFieldValue(iPojo, vField.getName());
final int version = convertVersion(ver);
ORecordInternal.setVersion(iRecord, version);
}
if (db.isMVCC() && !versionConfigured && db.getTransaction() instanceof OTransactionOptimistic)
throw new OTransactionException("Cannot involve an object of class '" + pojoClass + "' in an Optimistic Transaction commit because it does not define @Version or @OVersion and therefore cannot handle MVCC");
// SET OBJECT CLASS
iRecord.setClassName(schemaClass != null ? schemaClass.getName() : null);
String fieldName;
Object fieldValue;
// CALL BEFORE MARSHALLING
invokeCallback(iPojo, iRecord, OBeforeSerialization.class);
for (Field p : properties) {
fieldName = p.getName();
if (idField != null && fieldName.equals(idField.getName()))
continue;
if (vField != null && fieldName.equals(vField.getName()))
continue;
fieldValue = serializeFieldValue(getFieldType(iPojo, fieldName), getFieldValue(iPojo, fieldName));
schemaProperty = schemaClass != null ? schemaClass.getProperty(fieldName) : null;
if (fieldValue != null) {
if (isEmbeddedObject(iPojo.getClass(), fieldValue.getClass(), fieldName, iEntityManager)) {
// AUTO CREATE SCHEMA PROPERTY
if (schemaClass == null) {
db.getMetadata().getSchema().createClass(iPojo.getClass());
iRecord.setClassNameIfExists(iPojo.getClass().getSimpleName());
}
if (schemaProperty == null) {
OType t = OType.getTypeByClass(fieldValue.getClass());
if (t == null)
t = OType.EMBEDDED;
schemaProperty = iRecord.getSchemaClass().createProperty(fieldName, t);
}
}
}
fieldValue = typeToStream(fieldValue, schemaProperty != null ? schemaProperty.getType() : null, iEntityManager, iObj2RecHandler, db, iRecord, iSaveOnlyDirty);
iRecord.field(fieldName, fieldValue);
}
iObj2RecHandler.registerUserObject(iPojo, iRecord);
// CALL AFTER MARSHALLING
invokeCallback(iPojo, iRecord, OAfterSerialization.class);
OSerializationThreadLocal.INSTANCE.get().remove(identityRecord);
Orient.instance().getProfiler().stopChrono("Object.toStream", "Serialize object to stream", timer);
return iRecord;
}
Aggregations