com.orientechnologies.orient.core.record.impl
Class ODocument

Package class diagram package ODocument
java.lang.Object
  extended by com.orientechnologies.orient.core.record.ORecordAbstract<T>
      extended by com.orientechnologies.orient.core.record.ORecordSchemaAwareAbstract<java.lang.Object>
          extended by com.orientechnologies.orient.core.record.impl.ODocument
All Implemented Interfaces:
OIdentifiable, ORecordElement, ORecord<java.lang.Object>, ORecordInternal<java.lang.Object>, ORecordSchemaAware<java.lang.Object>, OSerializableStream, java.io.Serializable, java.lang.Comparable<OIdentifiable>, java.lang.Iterable<java.util.Map.Entry<java.lang.String,java.lang.Object>>, java.util.Comparator<OIdentifiable>

public class ODocument
extends ORecordSchemaAwareAbstract<java.lang.Object>
implements java.lang.Iterable<java.util.Map.Entry<java.lang.String,java.lang.Object>>

Document representation to handle values dynamically. Can be used in schema-less, schema-mixed and schema-full modes. Fields can be added at run-time. Instances can be reused across calls by using the reset() before to re-use.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.orientechnologies.orient.core.db.record.ORecordElement
ORecordElement.STATUS
 
Field Summary
static char[] INDEX_SEPARATOR
           
static byte RECORD_TYPE
           
 
Constructor Summary
ODocument()
          Internal constructor used on unmarshalling.
ODocument(byte[] iSource)
          Creates a new instance by the raw stream usually read from the database.
ODocument(java.util.Map<? extends java.lang.Object,java.lang.Object> iFieldMap)
          Fills a document passing a map of key/values where the key is the field name and the value the field's value.
ODocument(java.lang.Object[] iFields)
          Fills a document passing the field array in form of pairs of field name and value.
ODocument(OClass iClass)
          Creates a new instance in memory of the specified schema class.
ODocument(ODatabaseRecord iDatabase)
          Creates a new instance and binds to the specified database.
ODocument(ODatabaseRecord iDatabase, ORID iRID)
          Creates a new instance in memory linked by the Record Id to the persistent one.
ODocument(ODatabaseRecord iDatabase, java.lang.String iClassName)
          Creates a new instance in memory of the specified class.
ODocument(ODatabaseRecord iDatabase, java.lang.String iClassName, ORID iRID)
          Creates a new instance in memory of the specified class, linked by the Record Id to the persistent one.
ODocument(java.lang.String iFieldName, java.lang.Object iFieldValue, java.lang.Object... iFields)
          Fills a document passing the field names/values pair, where the first pair is mandatory.
 
Method Summary
 ODocument addOwner(ORecordElement iOwner)
          Internal.
 ODocument clear()
          Clears all the field values and types.
 boolean containsField(java.lang.String iFieldName)
          Checks if a field exists.
 ODocument copy()
          Copies the current instance to a new one.
 boolean detach()
          Removes all the dependencies with other records.
 boolean equals(java.lang.Object obj)
           
<RET> RET
field(java.lang.String iFieldName)
          Reads the field value.
<RET> RET
field(java.lang.String iFieldName, java.lang.Class<?> iFieldType)
          Reads the field value forcing the return type.
 ODocument field(java.lang.String iFieldName, java.lang.Object iPropertyValue)
          Writes the field value.
 ODocument field(java.lang.String iFieldName, java.lang.Object iPropertyValue, OType iFieldType)
          Writes the field value forcing the type.
<RET> RET
field(java.lang.String iFieldName, OType iFieldType)
          Reads the field value forcing the return type.
 java.lang.String[] fieldNames()
          Returns the set of field names.
 int fields()
          Returns the number of fields in memory.
 ODocument fields(java.lang.String iFieldName, java.lang.Object iFieldValue, java.lang.Object... iFields)
          Fill a document passing the field names/values
 OType fieldType(java.lang.String iFieldName)
          Returns the forced field type if any.
 java.lang.Object[] fieldValues()
          Returns the array of field values.
 ODocument flatCopy()
          Internal only.
 ODocument fromStream(byte[] iRecordBuffer)
          Unmarshalls the object.
 void fromString(java.lang.String iValue)
          Fills the ODocument directly with the string representation of the document itself.
 java.lang.String[] getDirtyFields()
          Returns the original value of a field before it has been changed.
 java.lang.Object getOriginalValue(java.lang.String iFieldName)
          Returns the original value of a field before it has been changed.
 byte getRecordType()
          Internal.
 boolean hasOwners()
          Returns true if the record has some owner.
 boolean hasSameContentOf(ODocument iOther)
           
 boolean isEmbedded()
           
 boolean isEmpty()
           
 boolean isLazyLoad()
           
 boolean isOrdered()
           
 boolean isTrackingChanges()
           
 java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.Object>> iterator()
          Returns the iterator against only the changed fields if tracking was enabled
 ODocument load(java.lang.String iFetchPlan)
          Loads the record using a fetch plan.
 ODocument load(java.lang.String iFetchPlan, boolean iIgnoreCache)
          Loads the record using a fetch plan.
 ODocument merge(java.util.Map<java.lang.String,java.lang.Object> iOther, boolean iAddOnlyMode, boolean iMergeSingleItemsOfMultiValueFields)
          Merge current document with the document passed as parameter.
 ODocument merge(ODocument iOther, boolean iConflictsOtherWins, boolean iMergeSingleItemsOfMultiValueFields)
          Merge current document with the document passed as parameter.
 void onAfterIdentityChanged(ORecord<?> iRecord)
          Internal only.
 void onBeforeIdentityChanged(ORID iRID)
          Internal only.
 ORecord<?> placeholder()
          Returns an empty record as place-holder of the current.
<RET> RET
rawField(java.lang.String iFieldName)
           
 java.lang.Object removeField(java.lang.String iFieldName)
          Removes a field.
 ODocument removeOwner(ORecordElement iRecordElement)
           
 ODocument reset()
          Resets the record values and class type to being reused.
 boolean setDatabase(ODatabaseRecord iDatabase)
          Sets the current database.
 ORecordAbstract<java.lang.Object> setDirty()
          Propagates the dirty status to the owner, if any.
 ODocument setFieldType(java.lang.String iFieldName, OType iFieldType)
          Sets the field type.
 void setLazyLoad(boolean iLazyLoad)
           
 ODocument setOrdered(boolean iOrdered)
           
 ODocument setTrackingChanges(boolean iTrackingChanges)
          Enabled or disabled the tracking of changes in the document.
 java.lang.String toString()
          Dumps the instance as string.
 ODocument unload()
          Unloads current record.
 void unsetDirty()
          Internal only.
 
Methods inherited from class com.orientechnologies.orient.core.record.ORecordSchemaAwareAbstract
deserializeFields, fill, getClassName, getSchemaClass, remove, save, save, setClassName, setClassNameIfExists, toStream, toStream, validate, validateField
 
Methods inherited from class com.orientechnologies.orient.core.record.ORecordAbstract
compare, compareTo, copyTo, delete, fill, fromJSON, getDatabase, getIdentity, getInternalStatus, getRecord, getSize, getVersion, hashCode, isDirty, isPinned, load, pin, reload, reload, reload, removeListener, setIdentity, setIdentity, setInternalStatus, setListener, setVersion, toJSON, toJSON, unpin
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.orientechnologies.orient.core.record.ORecordInternal
fill, setIdentity, setIdentity, setVersion
 
Methods inherited from interface com.orientechnologies.orient.core.record.ORecord
delete, fromJSON, getDatabase, getIdentity, getSize, getVersion, isDirty, isPinned, load, pin, reload, toJSON, toJSON, unpin
 
Methods inherited from interface com.orientechnologies.orient.core.db.record.ORecordElement
getInternalStatus, setInternalStatus
 
Methods inherited from interface com.orientechnologies.orient.core.db.record.OIdentifiable
getRecord
 
Methods inherited from interface java.lang.Comparable
compareTo
 
Methods inherited from interface java.util.Comparator
compare
 

Field Detail

RECORD_TYPE

public static final byte RECORD_TYPE
See Also:
Constant Field Values

INDEX_SEPARATOR

public static final char[] INDEX_SEPARATOR
Constructor Detail

ODocument

public ODocument()
Internal constructor used on unmarshalling.


ODocument

public ODocument(byte[] iSource)
Creates a new instance by the raw stream usually read from the database. New instances are not persistent until ORecordSchemaAwareAbstract.save() is called.

Parameters:
iSource - Raw stream

ODocument

public ODocument(ODatabaseRecord iDatabase)
Creates a new instance and binds to the specified database. New instances are not persistent until ORecordSchemaAwareAbstract.save() is called.

Parameters:
iDatabase - Database instance

ODocument

public ODocument(ODatabaseRecord iDatabase,
                 ORID iRID)
Creates a new instance in memory linked by the Record Id to the persistent one. New instances are not persistent until ORecordSchemaAwareAbstract.save() is called.

Parameters:
iDatabase - Database instance
iRID - Record Id

ODocument

public ODocument(ODatabaseRecord iDatabase,
                 java.lang.String iClassName,
                 ORID iRID)
Creates a new instance in memory of the specified class, linked by the Record Id to the persistent one. New instances are not persistent until ORecordSchemaAwareAbstract.save() is called.

Parameters:
iDatabase - Database instance
iClassName - Class name
iRID - Record Id

ODocument

public ODocument(ODatabaseRecord iDatabase,
                 java.lang.String iClassName)
Creates a new instance in memory of the specified class. New instances are not persistent until ORecordSchemaAwareAbstract.save() is called.

Parameters:
iDatabase - Database instance
iClassName - Class name

ODocument

public ODocument(OClass iClass)
Creates a new instance in memory of the specified schema class. New instances are not persistent until ORecordSchemaAwareAbstract.save() is called. The database reference is taken by the OClass instance received.

Parameters:
iClass - OClass instance

ODocument

public ODocument(java.lang.Object[] iFields)
Fills a document passing the field array in form of pairs of field name and value.

Parameters:
iFields - Array of field pairs

ODocument

public ODocument(java.util.Map<? extends java.lang.Object,java.lang.Object> iFieldMap)
Fills a document passing a map of key/values where the key is the field name and the value the field's value.

Parameters:
iFieldMap - Map of Object/Object

ODocument

public ODocument(java.lang.String iFieldName,
                 java.lang.Object iFieldValue,
                 java.lang.Object... iFields)
Fills a document passing the field names/values pair, where the first pair is mandatory.

Method Detail

copy

public ODocument copy()
Copies the current instance to a new one. Hasn't been choose the clone() to let ODocument return type. Once copied the new instance has the same identity and values but all the internal structure are totally independent by the source.

Specified by:
copy in interface ORecord<java.lang.Object>
Returns:
The Object instance itself giving a "fluent interface". Useful to call multiple methods in chain.

flatCopy

public ODocument flatCopy()
Description copied from interface: ORecordInternal
Internal only. Executes a flat copy of the record.

Specified by:
flatCopy in interface ORecordInternal<java.lang.Object>
Overrides:
flatCopy in class ORecordAbstract<java.lang.Object>
See Also:
ORecord.copy()

placeholder

public ORecord<?> placeholder()
Returns an empty record as place-holder of the current. Used when a record is requested, but only the identity is needed.

Returns:

detach

public boolean detach()
Description copied from interface: ORecord
Removes all the dependencies with other records. All the relationships remain in form of RecordID. If some links contain dirty records, the detach can't be complete and this method returns false.

Specified by:
detach in interface ORecord<java.lang.Object>
Overrides:
detach in class ORecordAbstract<java.lang.Object>
Returns:
True if the document has been successfully detached, otherwise false.

load

public ODocument load(java.lang.String iFetchPlan)
Loads the record using a fetch plan. Example:

doc.load( "*:3" ); // LOAD THE DOCUMENT BY EARLY FETCHING UP TO 3rd LEVEL OF CONNECTIONS

Parameters:
iFetchPlan - Fetch plan to use

load

public ODocument load(java.lang.String iFetchPlan,
                      boolean iIgnoreCache)
Loads the record using a fetch plan. Example:

doc.load( "*:3", true ); // LOAD THE DOCUMENT BY EARLY FETCHING UP TO 3rd LEVEL OF CONNECTIONS IGNORING THE CACHE

Parameters:
iIgnoreCache - Ignore the cache or use it

hasSameContentOf

public boolean hasSameContentOf(ODocument iOther)

toString

public java.lang.String toString()
Dumps the instance as string.

Overrides:
toString in class ORecordAbstract<java.lang.Object>

fromString

public void fromString(java.lang.String iValue)
Fills the ODocument directly with the string representation of the document itself. Use it for faster insertion but pay attention to respect the OrientDB record format.

record.reset();
record.setClassName("Account");
record.fromString(new String("Account@id:" + data.getCyclesDone() + ",name:'Luca',surname:'Garulli',birthDate:" + date.getTime()
+ ",salary:" + 3000f + i));
record.save();

Parameters:
iValue -

fieldNames

public java.lang.String[] fieldNames()
Returns the set of field names.

Specified by:
fieldNames in interface ORecordSchemaAware<java.lang.Object>
Returns:
Set of string containing the field names

fieldValues

public java.lang.Object[] fieldValues()
Returns the array of field values.

Specified by:
fieldValues in interface ORecordSchemaAware<java.lang.Object>
Returns:
Object array of the field values

rawField

public <RET> RET rawField(java.lang.String iFieldName)

field

public <RET> RET field(java.lang.String iFieldName)
Reads the field value.

Specified by:
field in interface ORecordSchemaAware<java.lang.Object>
Parameters:
iFieldName - field name
Returns:
field value if defined, otherwise null

field

public <RET> RET field(java.lang.String iFieldName,
                       java.lang.Class<?> iFieldType)
Reads the field value forcing the return type. Use this method to force return of ORID instead of the entire document by passing ORID.class as iFieldType.

Parameters:
iFieldName - field name
iFieldType - Forced type.
Returns:
field value if defined, otherwise null

field

public <RET> RET field(java.lang.String iFieldName,
                       OType iFieldType)
Reads the field value forcing the return type. Use this method to force return of binary data.

Specified by:
field in interface ORecordSchemaAware<java.lang.Object>
Parameters:
iFieldName - field name
iFieldType - Forced type.
Returns:
field value if defined, otherwise null

field

public ODocument field(java.lang.String iFieldName,
                       java.lang.Object iPropertyValue)
Writes the field value.

Specified by:
field in interface ORecordSchemaAware<java.lang.Object>
Parameters:
iFieldName - field name
iPropertyValue - field value
Returns:
The Record instance itself giving a "fluent interface". Useful to call multiple methods in chain.

fields

public ODocument fields(java.lang.String iFieldName,
                        java.lang.Object iFieldValue,
                        java.lang.Object... iFields)
Fill a document passing the field names/values


field

public ODocument field(java.lang.String iFieldName,
                       java.lang.Object iPropertyValue,
                       OType iFieldType)
Writes the field value forcing the type.

Specified by:
field in interface ORecordSchemaAware<java.lang.Object>
Parameters:
iFieldName - field name
iPropertyValue - field value
iFieldType - Forced type (not auto-determined)
Returns:
The Record instance itself giving a "fluent interface". Useful to call multiple methods in chain.

removeField

public java.lang.Object removeField(java.lang.String iFieldName)
Removes a field.

Specified by:
removeField in interface ORecordSchemaAware<java.lang.Object>
Parameters:
iFieldName - Field name
Returns:
The old value contained in the remove field

merge

public ODocument merge(ODocument iOther,
                       boolean iConflictsOtherWins,
                       boolean iMergeSingleItemsOfMultiValueFields)
Merge current document with the document passed as parameter. If the field already exists then the conflicts are managed based on the value of the parameter 'iConflictsOtherWins'.

Parameters:
iOther - Other ODocument instance to merge
iConflictsOtherWins - if true, the other document wins in case of conflicts, otherwise the current document wins
iMergeSingleItemsOfMultiValueFields -
Returns:

merge

public ODocument merge(java.util.Map<java.lang.String,java.lang.Object> iOther,
                       boolean iAddOnlyMode,
                       boolean iMergeSingleItemsOfMultiValueFields)
Merge current document with the document passed as parameter. If the field already exists then the conflicts are managed based on the value of the parameter 'iConflictsOtherWins'.

Parameters:
iOther - Other ODocument instance to merge
iAddOnlyMode - if true, the other document properties will be always added. If false, the missed propertie in the "other" document will be removed by original too
iMergeSingleItemsOfMultiValueFields -
Returns:

getDirtyFields

public java.lang.String[] getDirtyFields()
Returns the original value of a field before it has been changed.

Parameters:
iFieldName - Property name to retrieve the original value

getOriginalValue

public java.lang.Object getOriginalValue(java.lang.String iFieldName)
Returns the original value of a field before it has been changed.

Parameters:
iFieldName - Property name to retrieve the original value

iterator

public java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.Object>> iterator()
Returns the iterator against only the changed fields if tracking was enabled

Specified by:
iterator in interface java.lang.Iterable<java.util.Map.Entry<java.lang.String,java.lang.Object>>

setDatabase

public boolean setDatabase(ODatabaseRecord iDatabase)
Description copied from interface: ORecordElement
Sets the current database.

Specified by:
setDatabase in interface ORecordElement
Overrides:
setDatabase in class ORecordAbstract<java.lang.Object>
Returns:
true if the database is changed, otherwise false

containsField

public boolean containsField(java.lang.String iFieldName)
Checks if a field exists.

Specified by:
containsField in interface ORecordSchemaAware<java.lang.Object>
Parameters:
iFieldName - Field name
Returns:
True if exists, otherwise false.

getRecordType

public byte getRecordType()
Internal.

Specified by:
getRecordType in interface ORecordInternal<java.lang.Object>

hasOwners

public boolean hasOwners()
Returns true if the record has some owner.


addOwner

public ODocument addOwner(ORecordElement iOwner)
Internal.

Returns:

removeOwner

public ODocument removeOwner(ORecordElement iRecordElement)

setDirty

public ORecordAbstract<java.lang.Object> setDirty()
Propagates the dirty status to the owner, if any. This happens when the object is embedded in another one.

Specified by:
setDirty in interface ORecordElement
Overrides:
setDirty in class ORecordAbstract<java.lang.Object>
Returns:
The object it self. Useful to call methods in chain.

onBeforeIdentityChanged

public void onBeforeIdentityChanged(ORID iRID)
Description copied from interface: ORecordElement
Internal only.

Specified by:
onBeforeIdentityChanged in interface ORecordElement
Overrides:
onBeforeIdentityChanged in class ORecordAbstract<java.lang.Object>

onAfterIdentityChanged

public void onAfterIdentityChanged(ORecord<?> iRecord)
Description copied from interface: ORecordElement
Internal only.

Specified by:
onAfterIdentityChanged in interface ORecordElement
Overrides:
onAfterIdentityChanged in class ORecordAbstract<java.lang.Object>

fromStream

public ODocument fromStream(byte[] iRecordBuffer)
Description copied from interface: OSerializableStream
Unmarshalls the object. Fills the current object with the values contained in the byte array representation restoring a previous state. Usually byte[] comes from the storage or network.

Specified by:
fromStream in interface OSerializableStream
Overrides:
fromStream in class ORecordAbstract<java.lang.Object>
Parameters:
iRecordBuffer - byte array representation of the object
Returns:
The Object instance itself giving a "fluent interface". Useful to call multiple methods in chain.

unsetDirty

public void unsetDirty()
Description copied from interface: ORecordInternal
Internal only. Unsets the dirty status of the record.

Specified by:
unsetDirty in interface ORecordInternal<java.lang.Object>
Overrides:
unsetDirty in class ORecordAbstract<java.lang.Object>

fieldType

public OType fieldType(java.lang.String iFieldName)
Returns the forced field type if any.

Parameters:
iFieldName -

unload

public ODocument unload()
Description copied from interface: ORecord
Unloads current record. All information are lost but the record identity. At the next access the record will be auto-reloaded. Useful to free memory or to avoid to keep an old version of it.

Specified by:
unload in interface ORecord<java.lang.Object>
Overrides:
unload in class ORecordAbstract<java.lang.Object>
Returns:
The Object instance itself giving a "fluent interface". Useful to call multiple methods in chain.

clear

public ODocument clear()
Clears all the field values and types.

Specified by:
clear in interface ORecord<java.lang.Object>
Overrides:
clear in class ORecordAbstract<java.lang.Object>
Returns:
The Object instance itself giving a "fluent interface". Useful to call multiple methods in chain.

reset

public ODocument reset()
Resets the record values and class type to being reused.

Specified by:
reset in interface ORecord<java.lang.Object>
Overrides:
reset in class ORecordSchemaAwareAbstract<java.lang.Object>
Returns:
The Object instance itself giving a "fluent interface". Useful to call multiple methods in chain.

isLazyLoad

public boolean isLazyLoad()

setLazyLoad

public void setLazyLoad(boolean iLazyLoad)

isTrackingChanges

public boolean isTrackingChanges()

setTrackingChanges

public ODocument setTrackingChanges(boolean iTrackingChanges)
Enabled or disabled the tracking of changes in the document. This is needed by some triggers like OPropertyIndexManager to determine what fields are changed to update indexes.

Parameters:
iTrackingChanges - True to enable it, otherwise false
Returns:

isOrdered

public boolean isOrdered()

setOrdered

public ODocument setOrdered(boolean iOrdered)

equals

public boolean equals(java.lang.Object obj)
Specified by:
equals in interface java.util.Comparator<OIdentifiable>
Overrides:
equals in class ORecordAbstract<java.lang.Object>

fields

public int fields()
Returns the number of fields in memory.

Specified by:
fields in interface ORecordSchemaAware<java.lang.Object>
Returns:
Fields number

isEmpty

public boolean isEmpty()

isEmbedded

public boolean isEmbedded()

setFieldType

public ODocument setFieldType(java.lang.String iFieldName,
                              OType iFieldType)
Sets the field type. This overrides the schema property settings if any.

Parameters:
iFieldName - Field name
iFieldType - Type to set between OType enumaration values


Copyrights 2010-2011 (c) Luca Garulli. All Rights Reserved. This software is released as Open Source using the Apache 2.0 license.