Interface ODatabase<T>

All Superinterfaces:
AutoCloseable, Closeable, OBackupable
All Known Subinterfaces:
ODatabaseDocument, ODatabaseDocumentInternal, ODatabaseInternal<T>, ODatabaseObject, ODatabaseSession
All Known Implementing Classes:
ODatabaseDocumentAbstract, ODatabaseDocumentDistributed, ODatabaseDocumentDistributedPooled, ODatabaseDocumentEmbedded, ODatabaseDocumentEmbeddedPooled, ODatabaseDocumentRemote, ODatabaseDocumentRemotePooled, ODatabaseWrapperAbstract, OObjectDatabaseTx

public interface ODatabase<T> extends OBackupable, Closeable
Generic Database interface. Represents the lower level of the Database providing raw API to access to the raw records.
Limits:
  • Maximum records per cluster/class = 9.223.372.036 Billions: 2^63 = 9.223.372.036.854.775.808 records
  • Maximum records per database = 302.231.454.903.657 Billions: 2^15 clusters x 2^63 records = (2^78) 32.768 * 9,223.372.036.854.775.808 = 302.231,454.903.657.293.676.544 records
  • Maximum storage per database = 19.807.040.628.566.084 Terabytes: 2^31 data-segments x 2^63 bytes = (2^94) 2.147.483.648 x 9,223.372.036.854.775.808 Exabytes = 19.807,040.628.566.084.398.385.987.584 Yottabytes
Author:
Luca Garulli (l.garulli--(at)--orientdb.com)
  • Method Details

    • activateOnCurrentThread

      ODatabase activateOnCurrentThread()
      Activate current database instance on current thread. Call this method before using the database if you switch between multiple databases instances on the same thread or if you pass them across threads.
    • isActiveOnCurrentThread

      boolean isActiveOnCurrentThread()
      Returns true if the current database instance is active on current thread, otherwise false.
    • reload

      void reload()
      Reloads the database information like the cluster list.
    • getConfiguration

      OContextConfiguration getConfiguration()
      Returns the database configuration settings. If defined, any database configuration overwrites the global one.
      Returns:
      OContextConfiguration
    • close

      void close()
      Closes an opened database, if the database is already closed does nothing, if a transaction is active will be rollback.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • getStatus

      ODatabase.STATUS getStatus()
      Returns the current status of database.
    • setStatus

      @Deprecated <DB extends ODatabase> DB setStatus(ODatabase.STATUS iStatus)
      Deprecated.
      Set the current status of database. deprecated since 2.2
    • getSize

      long getSize()
      Returns the total size of the records in the database.
    • getName

      String getName()
      Returns the database name.
      Returns:
      Name of the database
    • getURL

      String getURL()
      Returns the database URL.
      Returns:
      URL of the database
    • getLocalCache

      OLocalRecordCache getLocalCache()
      Returns the level1 cache. Cannot be null.
      Returns:
      Current cache.
    • getDefaultClusterId

      int getDefaultClusterId()
      Returns the default cluster id. If not specified all the new entities will be stored in the default cluster.
      Returns:
      The default cluster id
    • getClusters

      int getClusters()
      Returns the number of clusters.
      Returns:
      Number of the clusters
    • existsCluster

      boolean existsCluster(String iClusterName)
      Returns true if the cluster exists, otherwise false.
      Parameters:
      iClusterName - Cluster name
      Returns:
      true if the cluster exists, otherwise false
    • getClusterNames

      Collection<String> getClusterNames()
      Returns all the names of the clusters.
      Returns:
      Collection of cluster names.
    • getClusterIdByName

      int getClusterIdByName(String iClusterName)
      Returns the cluster id by name.
      Parameters:
      iClusterName - Cluster name
      Returns:
      The id of searched cluster.
    • getClusterNameById

      String getClusterNameById(int iClusterId)
      Returns the cluster name by id.
      Parameters:
      iClusterId - Cluster id
      Returns:
      The name of searched cluster.
    • getClusterRecordSizeByName

      @Deprecated long getClusterRecordSizeByName(String iClusterName)
      Deprecated.
      Returns the total size of records contained in the cluster defined by its name.
      Parameters:
      iClusterName - Cluster name
      Returns:
      Total size of records contained.
    • getClusterRecordSizeById

      @Deprecated long getClusterRecordSizeById(int iClusterId)
      Deprecated.
      Returns the total size of records contained in the cluster defined by its id.
      Parameters:
      iClusterId - Cluster id
      Returns:
      The name of searched cluster.
    • isClosed

      boolean isClosed()
      Checks if the database is closed.
      Returns:
      true if is closed, otherwise false.
    • truncateCluster

      void truncateCluster(String clusterName)
      Removes all data in the cluster with given name. As result indexes for this class will be rebuilt.
      Parameters:
      clusterName - Name of cluster to be truncated.
    • countClusterElements

      long countClusterElements(int iCurrentClusterId)
      Counts all the entities in the specified cluster id.
      Parameters:
      iCurrentClusterId - Cluster id
      Returns:
      Total number of entities contained in the specified cluster
    • countClusterElements

      long countClusterElements(int[] iClusterIds)
      Counts all the entities in the specified cluster ids.
      Parameters:
      iClusterIds - Array of cluster ids Cluster id
      Returns:
      Total number of entities contained in the specified clusters
    • countClusterElements

      long countClusterElements(String iClusterName)
      Counts all the entities in the specified cluster name.
      Parameters:
      iClusterName - Cluster name
      Returns:
      Total number of entities contained in the specified cluster
    • addCluster

      int addCluster(String iClusterName, Object... iParameters)
      Adds a new cluster.
      Parameters:
      iClusterName - Cluster name
      iParameters - Additional parameters to pass to the factories
      Returns:
      Cluster id
    • addBlobCluster

      int addBlobCluster(String iClusterName, Object... iParameters)
      Adds a new cluster for store blobs.
      Parameters:
      iClusterName - Cluster name
      iParameters - Additional parameters to pass to the factories
      Returns:
      Cluster id
    • getBlobClusterIds

      Set<Integer> getBlobClusterIds()
      Retrieve the set of defined blob cluster.
      Returns:
      the set of defined blob cluster ids.
    • addCluster

      int addCluster(String iClusterName, int iRequestedId)
      Adds a new cluster.
      Parameters:
      iClusterName - Cluster name
      iRequestedId - requested id of the cluster
      Returns:
      Cluster id
    • dropCluster

      boolean dropCluster(String iClusterName)
      Drops a cluster by its name. Physical clusters will be completely deleted
      Parameters:
      iClusterName - the name of the cluster
      Returns:
      true if has been removed, otherwise false
    • dropCluster

      boolean dropCluster(int iClusterId)
      Drops a cluster by its id. Physical clusters will be completely deleted.
      Parameters:
      iClusterId - id of cluster to delete
      Returns:
      true if has been removed, otherwise false
    • setProperty

      @Deprecated Object setProperty(String iName, Object iValue)
      Deprecated.
      use OrientDBConfig.builder().setConfig(propertyName, propertyValue).build(); instead if you use >=3.0 API.
      Sets a property value
      Parameters:
      iName - Property name
      iValue - new value to set
      Returns:
      The previous value if any, otherwise null
    • getProperty

      @Deprecated Object getProperty(String iName)
      Deprecated.
      use getConfiguration() instead if you use >=3.0 API.
      Gets the property value.
      Parameters:
      iName - Property name
      Returns:
      The previous value if any, otherwise null
    • getProperties

      Deprecated.
      use getConfiguration() instead if you use >=3.0 API.
      Returns an iterator of the property entries
    • get

      Object get(ODatabase.ATTRIBUTES iAttribute)
      Returns a database attribute value
      Parameters:
      iAttribute - Attributes between #ATTRIBUTES enum
      Returns:
      The attribute value
    • set

      <DB extends ODatabase> DB set(ODatabase.ATTRIBUTES iAttribute, Object iValue)
      Sets a database attribute value
      Parameters:
      iAttribute - Attributes between #ATTRIBUTES enum
      iValue - Value to set
      Returns:
      underlying
    • registerListener

      @Deprecated void registerListener(ODatabaseListener iListener)
      Deprecated.
      Registers a listener to the database events.
      Parameters:
      iListener - the listener to register
    • unregisterListener

      @Deprecated void unregisterListener(ODatabaseListener iListener)
      Deprecated.
      Unregisters a listener to the database events.
      Parameters:
      iListener - the listener to unregister
    • getRecordMetadata

      @Deprecated ORecordMetadata getRecordMetadata(ORID rid)
      Deprecated.
    • freeze

      void freeze()
      Flush cached storage content to the disk.

      After this call users can perform only idempotent calls like read records and select/traverse queries. All write-related operations will queued till release() command will be called.

      Given command waits till all on going modifications in indexes or DB will be finished.

      IMPORTANT: This command is not reentrant.

      See Also:
    • release

      void release()
      Allows to execute write-related commands on DB. Called after freeze() command.
      See Also:
    • freeze

      void freeze(boolean throwException)
      Flush cached storage content to the disk.

      After this call users can perform only select queries. All write-related commands will queued till release() command will be called or exception will be thrown on attempt to modify DB data. Concrete behaviour depends on throwException parameter.

      IMPORTANT: This command is not reentrant.

      Parameters:
      throwException - If true OModificationOperationProhibitedException exception will be thrown in case of write command will be performed.
    • newInstance

      <RET> RET newInstance()
      Creates a new entity instance.
      Returns:
      The new instance.
    • getDictionary

      @Deprecated ODictionary<T> getDictionary()
      Deprecated.
      Manual indexes are prohibited and will be removed
      Returns the Dictionary manual index.
      Returns:
      ODictionary instance
    • getUser

      OSecurityUser getUser()
      Returns the current user logged into the database.
      See Also:
    • getClass

      default OClass getClass(String className)
      retrieves a class from the schema
      Parameters:
      className - The class name
      Returns:
      The object representing the class in the schema. Null if the class does not exist.
    • createClass

      default OClass createClass(String className, String... superclasses) throws OSchemaException
      Creates a new class in the schema
      Parameters:
      className - the class name
      superclasses - a list of superclasses for the class (can be empty)
      Returns:
      the class with the given name
      Throws:
      OSchemaException - if a class with this name already exists or if one of the superclasses does not exist.
    • load

      <RET extends T> RET load(T iObject)
      Loads the entity and return it.
      Parameters:
      iObject - The entity to load. If the entity was already loaded it will be reloaded and all the changes will be lost.
      Returns:
    • load

      <RET extends T> RET load(T iObject, String iFetchPlan)
      Loads a record using a fetch plan.
      Parameters:
      iObject - Record to load
      iFetchPlan - Fetch plan used
      Returns:
      The record received
    • lock

      <RET extends T> RET lock(ORID recordId) throws OLockException
      Pessimistic lock a record.

      In case of lock inside the transaction the lock will be release by the commit operation, In case of lock outside a transaction unlock need to be call manually.

      Parameters:
      recordId - the id of the record that need to be locked
      Returns:
      the record updated to the last state after the lock.
      Throws:
      OLockException - In case of deadlock detected
    • lock

      <RET extends T> RET lock(ORID recordId, long timeout, TimeUnit timeoutUnit) throws OLockException
      Pessimistic lock a record.
      Parameters:
      recordId - the id of the record that need to be locked
      timeout - for the record locking
      timeoutUnit - relative for the timeout
      Returns:
      the record updated to the last state after the lock.
      Throws:
      OLockException - In case of deadlock detected
    • unlock

      void unlock(ORID recordId) throws OLockException
      Pessimistic unlock
      Parameters:
      recordId - the id of the record to unlock
      Throws:
      OLockException - if the record is not locked.
    • load

      <RET extends T> RET load(T iObject, String iFetchPlan, boolean iIgnoreCache)
      Loads a record using a fetch plan.
      Parameters:
      iObject - Record to load
      iFetchPlan - Fetch plan used
      iIgnoreCache - Ignore cache or use it
      Returns:
      The record received
    • reload

      <RET extends T> RET reload(T iObject, String iFetchPlan, boolean iIgnoreCache)
      Force the reloading of the entity.
      Parameters:
      iObject - The entity to load. If the entity was already loaded it will be reloaded and all the changes will be lost.
      iFetchPlan - Fetch plan used
      iIgnoreCache - Ignore cache or use it
      Returns:
      The loaded entity
    • reload

      <RET extends T> RET reload(T iObject, String iFetchPlan, boolean iIgnoreCache, boolean force)
      Force the reloading of the entity.
      Parameters:
      iObject - The entity to load. If the entity was already loaded it will be reloaded and all the changes will be lost.
      iFetchPlan - Fetch plan used
      iIgnoreCache - Ignore cache or use it
      force - Force to reload record even if storage has the same record as reloaded record, it is useful if fetch plan is not null and alongside with root record linked records will be reloaded.
      Returns:
      The loaded entity
    • load

      <RET extends T> RET load(ORID recordId)
      Loads the entity by the Record ID.
      Parameters:
      recordId - The unique record id of the entity to load.
      Returns:
      The loaded entity
    • load

      <RET extends T> RET load(ORID iRecordId, String iFetchPlan)
      Loads the entity by the Record ID using a fetch plan.
      Parameters:
      iRecordId - The unique record id of the entity to load.
      iFetchPlan - Fetch plan used
      Returns:
      The loaded entity
    • load

      <RET extends T> RET load(ORID iRecordId, String iFetchPlan, boolean iIgnoreCache)
      Loads the entity by the Record ID using a fetch plan and specifying if the cache must be ignored.
      Parameters:
      iRecordId - The unique record id of the entity to load.
      iFetchPlan - Fetch plan used
      iIgnoreCache - Ignore cache or use it
      Returns:
      The loaded entity
    • save

      <RET extends T> RET save(T iObject)
      Saves an entity in synchronous mode. If the entity is not dirty, then the operation will be ignored. For custom entity implementations assure to set the entity as dirty.
      Parameters:
      iObject - The entity to save
      Returns:
      The saved entity.
    • save

      <RET extends T> RET save(T iObject, String iClusterName)
      Saves an entity in the specified cluster in synchronous mode. If the entity is not dirty, then the operation will be ignored. For custom entity implementations assure to set the entity as dirty. If the cluster does not exist, an error will be thrown.
      Parameters:
      iObject - The entity to save
      iClusterName - Name of the cluster where to save
      Returns:
      The saved entity.
    • delete

      ODatabase<T> delete(T iObject)
      Deletes an entity from the database in synchronous mode.
      Parameters:
      iObject - The entity to delete.
      Returns:
      The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
    • delete

      ODatabase<T> delete(ORID iRID)
      Deletes the entity with the received RID from the database.
      Parameters:
      iRID - The RecordID to delete.
      Returns:
      The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
    • delete

      ODatabase<T> delete(ORID iRID, int iVersion)
      Deletes the entity with the received RID from the database.
      Parameters:
      iRID - The RecordID to delete.
      iVersion - for MVCC
      Returns:
      The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
    • getTransaction

      OTransaction getTransaction()
      Return active transaction. Cannot be null. If no transaction is active, then a OTransactionNoTx instance is returned.
      Returns:
      OTransaction implementation
    • begin

      ODatabase<T> begin()
      Begins a new transaction. By default the type is OPTIMISTIC. If a previous transaction is running a nested call counter is incremented. A transaction once begun has to be closed by calling the commit() or rollback().
      Returns:
      The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
    • begin

      ODatabase<T> begin(OTransaction.TXTYPE iStatus)
      Begins a new transaction specifying the transaction type. If a previous transaction is running a nested call counter is incremented. A transaction once begun has to be closed by calling the commit() or rollback().
      Returns:
      The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
    • commit

      Commits the current transaction. The approach is all or nothing. All changes will be permanent following the storage type. If the operation succeed all the entities changed inside the transaction context will be effective. If the operation fails, all the changed entities will be restored in the data store.
      Returns:
      Throws:
      OTransactionException
    • commit

      ODatabase<T> commit(boolean force) throws OTransactionException
      Throws:
      OTransactionException
    • rollback

      ODatabase<T> rollback() throws OTransactionException
      Aborts the current running transaction. All the pending changed entities will be restored in the data store.
      Returns:
      Throws:
      OTransactionException
    • rollback

      ODatabase<T> rollback(boolean force) throws OTransactionException
      Throws:
      OTransactionException
    • query

      Executes an SQL query. The result set has to be closed after usage

      Sample usage:

      OResultSet rs = db.query("SELECT FROM V where name = ?", "John"); while(rs.hasNext()){ OResult item = rs.next(); ... } rs.close();

      Parameters:
      query - the query string
      args - query parameters (positional)
      Returns:
      the query result set
      Throws:
      OCommandSQLParsingException
      OCommandExecutionException
    • query

      Executes an SQL query (idempotent). The result set has to be closed after usage

      Sample usage:

      Map<String, Object&gt params = new HashMapMap<&gt(); params.put("name", "John"); OResultSet rs = db.query("SELECT FROM V where name = :name", params); while(rs.hasNext()){ OResult item = rs.next(); ... } rs.close();

      Parameters:
      query - the query string
      args - query parameters (named)
      Returns:
      Throws:
      OCommandSQLParsingException
      OCommandExecutionException
    • command

      Executes a generic (idempotent or non idempotent) command. The result set has to be closed after usage

      Sample usage:

      OResultSet rs = db.command("INSERT INTO Person SET name = ?", "John"); ... rs.close();

      Parameters:
      query -
      args - query arguments
      Returns:
      Throws:
      OCommandSQLParsingException
      OCommandExecutionException
    • command

      Executes a generic (idempotent or non idempotent) command. The result set has to be closed after usage

      Sample usage:

      Map<String, Object&gt params = new HashMapMap<&gt(); params.put("name", "John"); OResultSet rs = db.query("INSERT INTO Person SET name = :name", params); ... rs.close();

      Parameters:
      query -
      args -
      Returns:
      Throws:
      OCommandSQLParsingException
      OCommandExecutionException
    • execute

      default OResultSet execute(String language, String script, Object... args) throws OCommandExecutionException, OCommandScriptException
      Execute a script in a specified query language. The result set has to be closed after usage

      Sample usage:

      String script = "INSERT INTO Person SET name = 'foo', surname = ?;"+ "INSERT INTO Person SET name = 'bar', surname = ?;"+ "INSERT INTO Person SET name = 'baz', surname = ?;";

      OResultSet rs = db.execute("sql", script, "Surname1", "Surname2", "Surname3"); ... rs.close();

      Parameters:
      language -
      script -
      args -
      Returns:
      Throws:
      OCommandExecutionException
      OCommandScriptException
    • execute

      default OResultSet execute(String language, String script, Map<String,?> args) throws OCommandExecutionException, OCommandScriptException
      Execute a script of a specified query language The result set has to be closed after usage

      Sample usage:

      Map<String, Object&gt params = new HashMapMap<&gt(); params.put("surname1", "Jones"); params.put("surname2", "May"); params.put("surname3", "Ali");

      String script = "INSERT INTO Person SET name = 'foo', surname = :surname1;"+ "INSERT INTO Person SET name = 'bar', surname = :surname2;"+ "INSERT INTO Person SET name = 'baz', surname = :surname3;";

      OResultSet rs = db.execute("sql", script, params); ... rs.close();

      Parameters:
      language -
      script -
      args -
      Returns:
      Throws:
      OCommandExecutionException
      OCommandScriptException
    • getMetadata

      OMetadata getMetadata()
      Return the OMetadata instance. Cannot be null.
      Returns:
      The OMetadata instance.
    • registerHook

      <DB extends ODatabase<?>> DB registerHook(ORecordHook iHookImpl)
      Registers a hook to listen all events for Records.
      Parameters:
      iHookImpl - ORecordHook implementation
      Returns:
      The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
    • registerHook

      <DB extends ODatabase<?>> DB registerHook(ORecordHook iHookImpl, ORecordHook.HOOK_POSITION iPosition)
    • getHooks

      Retrieves all the registered hooks.
      Returns:
      A not-null unmodifiable map of ORecordHook and position instances. If there are no hooks registered, the Map is empty.
    • unregisterHook

      <DB extends ODatabase<?>> DB unregisterHook(ORecordHook iHookImpl)
      Unregisters a previously registered hook.
      Parameters:
      iHookImpl - ORecordHook implementation
      Returns:
      The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain. deprecated since 2.2
    • getListeners

      Iterable<ODatabaseListener> getListeners()
      Retrieves all the registered listeners.
      Returns:
      An iterable of ODatabaseListener instances.
    • getType

      String getType()
    • getConflictStrategy

      @Deprecated ORecordConflictStrategy getConflictStrategy()
      Deprecated.
      Returns the current record conflict strategy.
    • setConflictStrategy

      @Deprecated <DB extends ODatabase<?>> DB setConflictStrategy(String iStrategyName)
      Deprecated.
      Overrides record conflict strategy selecting the strategy by name.
      Parameters:
      iStrategyName - ORecordConflictStrategy strategy name
      Returns:
      The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
    • setConflictStrategy

      @Deprecated <DB extends ODatabase<?>> DB setConflictStrategy(ORecordConflictStrategy iResolver)
      Deprecated.
      Overrides record conflict strategy.
      Parameters:
      iResolver - ORecordConflictStrategy implementation
      Returns:
      The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
    • incrementalBackup

      String incrementalBackup(String path) throws UnsupportedOperationException
      Performs incremental backup of database content to the selected folder. This is thread safe operation and can be done in normal operational mode.

      If it will be first backup of data full content of database will be copied into folder otherwise only changes after last backup in the same folder will be copied.

      Parameters:
      path - Path to backup folder.
      Returns:
      File name of the backup
      Throws:
      UnsupportedOperationException
      Since:
      2.2
    • live

      OLiveQueryMonitor live(String query, OLiveQueryResultListener listener, Map<String,?> args)
      Subscribe a query as a live query for future create/update event with the referred conditions
      Parameters:
      query - live query
      listener - the listener that receive the query results
      args - the live query args
    • live

      OLiveQueryMonitor live(String query, OLiveQueryResultListener listener, Object... args)
      Subscribe a query as a live query for future create/update event with the referred conditions
      Parameters:
      query - live query
      listener - the listener that receive the query results
      args - the live query args
    • executeWithRetry

      default <RET> RET executeWithRetry(int nRetries, Function<ODatabaseSession,RET> function) throws IllegalStateException, IllegalArgumentException, ONeedRetryException, UnsupportedOperationException
      Tries to execute a lambda in a transaction, retrying it if an ONeedRetryException is thrown.

      If the DB does not have an active transaction, after the execution you will still be out of tx.

      If the DB has an active transaction, then the transaction has to be empty (no operations executed yet) and after the execution you will be in a new transaction.

      Type Parameters:
      RET - the return type of the lambda
      Parameters:
      nRetries - the maximum number of retries (> 0)
      function - a lambda containing application code to execute in a commit/retry loop
      Returns:
      The result of the execution of the lambda
      Throws:
      IllegalStateException - if there are operations in the current transaction
      ONeedRetryException - if the maximum number of retries is executed and all failed with an ONeedRetryException
      IllegalArgumentException - if nRetries is <= 0
      UnsupportedOperationException - if this type of database does not support automatic commit/retry