Package com.orientechnologies.orient.core.record.impl

Examples of com.orientechnologies.orient.core.record.impl.ODocument


    final long begin = System.currentTimeMillis();

    String dateAsString = database.getStorage().getConfiguration().getDateFormatInstance().format(begin);

    ODocument doc = new ODocument(database, "Order");
    doc.field("context", "testPrecision");
    doc.field("date", new Date(), OType.DATE);
    doc.save();

    List<ODocument> result = database.command(
        new OSQLSynchQuery<ODocument>("select * from Order where date >= ? and context = 'testPrecision'")).execute(dateAsString);

    Assert.assertEquals(result.size(), 1);
View Full Code Here


    database.close();
  }

  @Test
  public void testDateTypes() throws ParseException {
    ODocument doc = new ODocument();
    doc.field("context", "test");
    doc.field("date", System.currentTimeMillis(), OType.DATE);

    Assert.assertTrue(doc.field("date") instanceof Date);

  }
View Full Code Here

  public static final String                            NAME      = "ORecordDocument2csv";
  public static final ORecordSerializerSchemaAware2CSV  INSTANCE  = new ORecordSerializerSchemaAware2CSV();

  @Override
  public ORecordSchemaAware<?> newObject(ODatabaseRecord iDatabase, String iClassName) {
    return new ODocument(iDatabase, iClassName);
  }
View Full Code Here

  protected StringBuilder toString(ORecordInternal<?> iRecord, final StringBuilder iOutput, final String iFormat,
      final OUserObject2RecordHandler iObjHandler, final Set<Integer> iMarshalledRecords, final boolean iOnlyDelta) {
    if (!(iRecord instanceof ODocument))
      throw new OSerializationException("Can't marshall a record of type " + iRecord.getClass().getSimpleName() + " to CSV");

    final ODocument record = (ODocument) iRecord;

    // CHECK IF THE RECORD IS PENDING TO BE MARSHALLED
    final Integer identityRecord = System.identityHashCode(record);
    if (iMarshalledRecords != null)
      if (iMarshalledRecords.contains(identityRecord)) {
        return iOutput;
      } else
        iMarshalledRecords.add(identityRecord);

    final ODatabaseRecord database = ODatabaseRecordThreadLocal.INSTANCE.get();

    if (!iOnlyDelta && record.getSchemaClass() != null) {
      // MARSHALL THE CLASSNAME
      iOutput.append(record.getSchemaClass().getStreamableName());
      iOutput.append(OStringSerializerHelper.CLASS_SEPARATOR);
    }

    OProperty prop;
    OType type;
    OClass linkedClass;
    OType linkedType;
    String fieldClassName;
    int i = 0;

    final String[] fieldNames = iOnlyDelta && record.isTrackingChanges() ? record.getDirtyFields() : record.fieldNames();

    // MARSHALL ALL THE FIELDS OR DELTA IF TRACKING IS ENABLED
    for (String fieldName : fieldNames) {
      Object fieldValue = record.rawField(fieldName);
      if (i > 0)
        iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);

      // SEARCH FOR A CONFIGURED PROPERTY
      prop = record.getSchemaClass() != null ? record.getSchemaClass().getProperty(fieldName) : null;
      fieldClassName = getClassName(fieldValue);

      type = record.fieldType(fieldName);
      linkedClass = null;
      linkedType = null;

      if (prop != null) {
        // RECOGNIZED PROPERTY
        type = prop.getType();
        linkedClass = prop.getLinkedClass();
        linkedType = prop.getLinkedType();

      } else if (fieldValue != null) {
        // NOT FOUND: TRY TO DETERMINE THE TYPE FROM ITS CONTENT
        if (type == null) {
          if (fieldValue.getClass() == byte[].class)
            type = OType.BINARY;
          else if (database != null && fieldValue instanceof ORecord<?>) {
            if (type == null)
              // DETERMINE THE FIELD TYPE
              if (fieldValue instanceof ODocument && ((ODocument) fieldValue).hasOwners())
                type = OType.EMBEDDED;
              else
                type = OType.LINK;

            linkedClass = getLinkInfo(database, fieldClassName);
          } else if (fieldValue instanceof ORID)
            // DETERMINE THE FIELD TYPE
            type = OType.LINK;

          else if (database != null && database.getDatabaseOwner() instanceof ODatabaseObject
              && ((ODatabaseObject) database.getDatabaseOwner()).getEntityManager().getEntityClass(fieldClassName) != null) {
            // DETERMINE THE FIELD TYPE
            type = OType.LINK;
            linkedClass = getLinkInfo(database, fieldClassName);
          } else if (fieldValue instanceof Date)
            type = OType.DATETIME;
          else if (fieldValue instanceof String)
            type = OType.STRING;
          else if (fieldValue instanceof Integer)
            type = OType.INTEGER;
          else if (fieldValue instanceof Long)
            type = OType.LONG;
          else if (fieldValue instanceof Float)
            type = OType.FLOAT;
          else if (fieldValue instanceof Short)
            type = OType.SHORT;
          else if (fieldValue instanceof Byte)
            type = OType.BYTE;
          else if (fieldValue instanceof Double)
            type = OType.DOUBLE;
        }

        if (fieldValue instanceof Collection<?> || fieldValue.getClass().isArray()) {
          int size = OMultiValue.getSize(fieldValue);

          if (size > 0) {
            final Object firstValue = OMultiValue.getFirstValue(fieldValue);

            if (firstValue != null) {
              if (firstValue instanceof ORID) {
                linkedClass = null;
                linkedType = OType.LINK;
                if (fieldValue instanceof Set<?>)
                  type = OType.LINKSET;
                else
                  type = OType.LINKLIST;
              } else if (database != null
                  && (firstValue instanceof ORecordSchemaAware<?> || (database.getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) database
                      .getDatabaseOwner()).getEntityManager().getEntityClass(getClassName(firstValue)) != null))) {
                linkedClass = getLinkInfo(database, getClassName(firstValue));
                if (type == null) {
                  // LINK: GET THE CLASS
                  linkedType = OType.LINK;

                  if (fieldValue instanceof Set<?>)
                    type = OType.LINKSET;
                  else
                    type = OType.LINKLIST;
                } else
                  linkedType = OType.EMBEDDED;
              } else {
                if (firstValue instanceof Enum<?>)
                  linkedType = OType.STRING;
                else {
                  linkedType = OType.getTypeByClass(firstValue.getClass());

                  if (linkedType != OType.LINK) {
                    // EMBEDDED FOR SURE SINCE IT CONTAINS JAVA TYPES
                    if (linkedType == null) {
                      linkedType = OType.EMBEDDED;
                      // linkedClass = new OClass(firstValue.getClass());
                    }
                  }
                }

                if (type == null)
                  if (fieldValue instanceof Set<?>)
                    type = OType.EMBEDDEDSET;
                  else
                    type = OType.EMBEDDEDLIST;
              }
            }
          } else if (type == null)
            type = OType.EMBEDDEDLIST;

        } else if (fieldValue instanceof Map<?, ?>) {
          if (type == null)
            type = OType.EMBEDDEDMAP;

          if (OMultiValue.getSize(fieldValue) > 0) {
            Object firstValue = OMultiValue.getFirstValue(fieldValue);

            if (firstValue instanceof ORID) {
              linkedClass = null;
              linkedType = OType.LINK;
              type = OType.LINKMAP;
            } else if (database != null
                && (firstValue instanceof ORecordSchemaAware<?> || (database.getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) database
                    .getDatabaseOwner()).getEntityManager().getEntityClass(getClassName(firstValue)) != null))) {
              if (((ORecordInternal<?>) firstValue).getIdentity().isValid())
                type = OType.LINKMAP;

              // LINK: GET THE CLASS
              linkedType = type == OType.EMBEDDEDLIST || type == OType.EMBEDDEDSET || type == OType.EMBEDDEDMAP ? OType.EMBEDDED
                  : OType.LINK;
              linkedClass = getLinkInfo(database, getClassName(firstValue));
            } else {
              linkedType = OType.getTypeByClass(firstValue.getClass());
              if (linkedType == OType.LINK && type == OType.EMBEDDEDMAP)
                type = OType.LINKMAP;
            }
          }
        }
      }

      if (type == OType.TRANSIENT)
        // TRANSIENT FIELD
        continue;

      if (type == null)
        type = OType.EMBEDDED;

      iOutput.append(fieldName);
      iOutput.append(FIELD_VALUE_SEPARATOR);
      fieldToStream((ODocument) iRecord, iRecord.getDatabase(), iOutput, iObjHandler, type, linkedClass, linkedType, fieldName,
          fieldValue, iMarshalledRecords, true);

      i++;
    }

    if (iMarshalledRecords != null)
      iMarshalledRecords.remove(identityRecord);

    // GET THE OVERSIZE IF ANY
    final float overSize;
    if (record.getSchemaClass() != null)
      // GET THE CONFIGURED OVERSIZE SETTED PER CLASS
      overSize = record.getSchemaClass().getOverSize();
    else
      overSize = 0;

    // APPEND BLANKS IF NEEDED
    final int newSize;
    if (record.hasOwners())
      // EMBEDDED: GET REAL SIZE
      newSize = iOutput.length();
    else if (record.getSize() == iOutput.length())
      // IDENTICAL! DO NOTHING
      newSize = record.getSize();
    else if (record.getSize() > iOutput.length()) {
      // APPEND EXTRA SPACES TO FILL ALL THE AVAILABLE SPACE AND AVOID FRAGMENTATION
      newSize = record.getSize();
    } else if (overSize > 0) {
      // APPEND EXTRA SPACES TO GET A LARGER iOutput
      newSize = (int) (iOutput.length() * overSize);
    } else
      // NO OVERSIZE
View Full Code Here

    if (iContent.length() == 0)
      return iRecord;

    // UNMARSHALL THE CLASS NAME
    final ODocument record = (ODocument) iRecord;

    final int posFirstValue = iContent.indexOf(OStringSerializerHelper.ENTRY_SEPARATOR);
    int pos = iContent.indexOf(OStringSerializerHelper.CLASS_SEPARATOR);
    if (pos > -1 && (pos < posFirstValue || posFirstValue == -1)) {
      record.setClassNameIfExists(iContent.substring(0, pos));
      iContent = iContent.substring(pos + 1);
    } else
      record.setClassNameIfExists(null);

    final List<String> fields = OStringSerializerHelper.smartSplit(iContent, OStringSerializerHelper.RECORD_SEPARATOR);

    String field;
    String fieldName = null;
    String fieldValue;
    OType type = null;
    OClass linkedClass;
    OType linkedType;
    OProperty prop;

    // UNMARSHALL ALL THE FIELDS
    for (int i = 0; i < fields.size(); ++i) {
      field = fields.get(i).trim();

      boolean uncertainType = false;

      try {
        pos = field.indexOf(FIELD_VALUE_SEPARATOR);
        if (pos > -1) {
          // GET THE FIELD NAME
          fieldName = field.substring(0, pos);

          // GET THE FIELD VALUE
          fieldValue = field.length() > pos + 1 ? field.substring(pos + 1) : null;

          // SEARCH FOR A CONFIGURED PROPERTY
          prop = record.getSchemaClass() != null ? record.getSchemaClass().getProperty(fieldName) : null;
          if (prop != null) {
            // RECOGNIZED PROPERTY
            type = prop.getType();
            linkedClass = prop.getLinkedClass();
            linkedType = prop.getLinkedType();

          } else {
            // SCHEMA PROPERTY NOT FOUND FOR THIS FIELD: TRY TO AUTODETERMINE THE BEST TYPE
            type = record.fieldType(fieldName);
            linkedClass = null;
            linkedType = null;

            // NOT FOUND: TRY TO DETERMINE THE TYPE FROM ITS CONTENT
            if (fieldValue != null && type == null) {
              if (fieldValue.length() > 1 && fieldValue.charAt(0) == '"' && fieldValue.charAt(fieldValue.length() - 1) == '"') {
                type = OType.STRING;
              } else if (fieldValue.charAt(0) == OStringSerializerHelper.COLLECTION_BEGIN
                  && fieldValue.charAt(fieldValue.length() - 1) == OStringSerializerHelper.COLLECTION_END) {
                type = OType.EMBEDDEDLIST;

                final String value = fieldValue.substring(1, fieldValue.length() - 1);

                if (value.length() > 0) {
                  if (value.charAt(0) == OStringSerializerHelper.LINK) {
                    type = OType.LINKLIST;
                    linkedType = OType.LINK;

                    // GET THE CLASS NAME IF ANY
                    int classSeparatorPos = value.indexOf(OStringSerializerHelper.CLASS_SEPARATOR);
                    if (classSeparatorPos > -1) {
                      String className = value.substring(1, classSeparatorPos);
                      if (className != null)
                        linkedClass = iDatabase.getMetadata().getSchema().getClass(className);
                    }
                  } else if (value.charAt(0) == OStringSerializerHelper.PARENTHESIS_BEGIN) {
                    linkedType = OType.EMBEDDED;
                  } else if (Character.isDigit(value.charAt(0)) || value.charAt(0) == '+' || value.charAt(0) == '-') {
                    String[] items = value.split(",");
                    linkedType = getType(items[0]);
                  } else if (value.charAt(0) == '\'' || value.charAt(0) == '"')
                    linkedType = OType.STRING;
                } else
                  uncertainType = true;

              } else if (fieldValue.charAt(0) == OStringSerializerHelper.MAP_BEGIN
                  && fieldValue.charAt(fieldValue.length() - 1) == OStringSerializerHelper.MAP_END) {
                type = OType.EMBEDDEDMAP;
              } else if (fieldValue.charAt(0) == OStringSerializerHelper.LINK)
                type = OType.LINK;
              else if (fieldValue.charAt(0) == OStringSerializerHelper.PARENTHESIS_BEGIN)
                type = OType.EMBEDDED;
              else if (fieldValue.equals("true") || fieldValue.equals("false"))
                type = OType.BOOLEAN;
              else
                type = getType(fieldValue);
            }
          }

          if (type == OType.EMBEDDEDLIST || type == OType.EMBEDDEDSET || type == OType.EMBEDDEDMAP)
            // SAVE THE TYPE AS EMBEDDED
            record.field(fieldName, fieldFromStream(iRecord, type, linkedClass, linkedType, fieldName, fieldValue), type);
          else
            record.field(fieldName, fieldFromStream(iRecord, type, linkedClass, linkedType, fieldName, fieldValue));

          if (uncertainType)
            record.setFieldType(fieldName, null);
        }
      } catch (Exception e) {
        OLogManager.instance().exception("Error on unmarshalling field '%s' with value: ", e, OSerializationException.class,
            fieldName, field);
      }
View Full Code Here

    // FILTER BY PROPERTY VALUES
    final ORecordLazySet result = new ORecordLazySet(underlying);
    if (iEdges != null)
      for (OIdentifiable item : iEdges) {
        final ODocument doc = (ODocument) item;
        for (Entry<String, Object> prop : iProperties.entrySet()) {
          if (prop.getKey() != null && doc.containsField(prop.getKey())) {
            if (prop.getValue() == null) {
              if (doc.field(prop.getKey()) == null)
                // BOTH NULL: ADD IT
                result.add(item);
            } else if (prop.getValue().equals(doc.field(prop.getKey())))
              // SAME VALUE: ADD IT
              result.add(item);
          }
        }
      }
View Full Code Here

      // BIND VALUES
      index.put(fields.get(KEYWORD_KEY), (OIdentifiable) fields.get(KEYWORD_RID));
      return null;
    } else {
      // CREATE NEW DOCUMENT
      ODocument doc = className != null ? new ODocument(database, className) : new ODocument(database);

      OSQLHelper.bindParameters(doc, fields, iArgs);

      if (clusterName != null)
        doc.save(clusterName);
      else
        doc.save();
      return doc;
    }
  }
View Full Code Here

          try {
            rec = it.next();
            if (rec instanceof ODocument) {
              // CHECK IF THE CLASS OF THE DOCUMENT IS INCLUDED
              ODocument doc = (ODocument) rec;
              if (includeClasses != null) {
                if (!includeClasses.contains(doc.getClassName()))
                  continue;
              } else if (excludeClasses != null) {
                if (excludeClasses.contains(doc.getClassName()))
                  continue;
              }
            }

            exportRecord(recordTot, recordNum++, rec);
View Full Code Here

      final Set<Object> docs = document.field(OGraphDatabase.VERTEX_FIELD_IN);

      if (docs != null) {
        // TRANSFORM ALL THE ARCS
        ODocument doc;
        for (Object o : docs) {
          if (o instanceof ODocument)
            doc = (ODocument) o;
          else
            doc = database.getRecordById((ORID) o);

          if (iEdgeLabel != null && !iEdgeLabel.equals(doc.field(OGraphDatabase.LABEL)))
            continue;

          temp.add((OGraphEdge) database.getUserObjectByRecord(doc, null));
        }
      }
View Full Code Here

      final Set<Object> docs = document.field(OGraphDatabase.VERTEX_FIELD_OUT);

      if (docs != null) {
        // TRANSFORM ALL THE ARCS
        ODocument doc;
        for (Object o : docs) {
          if (o instanceof ODocument)
            doc = (ODocument) o;
          else
            doc = database.getRecordById((ORID) o);

          if (iEdgeLabel != null && !iEdgeLabel.equals(doc.field(OGraphDatabase.LABEL)))
            continue;

          temp.add((OGraphEdge) database.getUserObjectByRecord(doc, null));
        }
      }
View Full Code Here

TOP

Related Classes of com.orientechnologies.orient.core.record.impl.ODocument

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.