001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2014-2015 ForgeRock AS.
015 */
016package org.opends.server.backends.pluggable.spi;
017
018import java.io.Closeable;
019import java.util.Set;
020
021import org.forgerock.opendj.config.server.ConfigException;
022import org.opends.server.types.BackupConfig;
023import org.opends.server.types.BackupDirectory;
024import org.opends.server.types.DirectoryException;
025import org.opends.server.types.RestoreConfig;
026
027/**
028 * This interface abstracts the underlying storage engine,
029 * isolating the pluggable backend generic code from a particular storage engine implementation.
030 */
031public interface Storage extends Closeable
032{
033
034  /**
035   * Starts the import operation.
036   * @return a new Importer object which must be closed to release all resources
037   * @throws ConfigException
038   *           if there is a problem with the configuration
039   * @throws StorageRuntimeException
040   *           if a problem occurs with the underlying storage engine
041   * @see #close() to release all resources once import is finished
042   */
043  Importer startImport() throws ConfigException, StorageRuntimeException;
044
045  /**
046   * Opens the storage engine to allow executing operations on it.
047   *
048   * @param accessMode
049   *           Specify the access mode to this storage.
050   * @throws NullPointerException
051   *           if accessMode is null.
052   * @throws Exception
053   *           if a problem occurs with the underlying storage engine
054   * @see #close() to release all resources once import is finished
055   */
056  void open(AccessMode accessMode) throws Exception;
057
058  /**
059   * Executes a read operation. In case of a read operation rollback, implementations must ensure
060   * the read operation is retried until it succeeds.
061   *
062   * @param <T>
063   *          type of the value returned
064   * @param readOperation
065   *          the read operation to execute
066   * @return the value read by the read operation
067   * @throws Exception
068   *           if a problem occurs with the underlying storage engine
069   */
070  <T> T read(ReadOperation<T> readOperation) throws Exception;
071
072  /**
073   * Executes a write operation. In case of a write operation rollback, implementations must ensure
074   * the write operation is retried until it succeeds.
075   *
076   * @param writeOperation
077   *          the write operation to execute
078   * @throws Exception
079   *           if a problem occurs with the underlying storage engine
080   */
081  void write(WriteOperation writeOperation) throws Exception;
082
083  /**
084   * Remove all files for a backend of this storage.
085   *
086   * @throws StorageRuntimeException if removal fails
087   */
088  void removeStorageFiles() throws StorageRuntimeException;
089
090  /**
091   * Returns the current status of the storage.
092   *
093   * @return the current status of the storage
094   */
095  StorageStatus getStorageStatus();
096
097  /**
098   * Returns {@code true} if this storage supports backup and restore.
099   *
100   * @return {@code true} if this storage supports backup and restore.
101   */
102  boolean supportsBackupAndRestore();
103
104  /**
105   * Creates a backup for this storage.
106   *
107   * @param backupConfig
108   *          The configuration to use when performing the backup.
109   * @throws DirectoryException
110   *           If a Directory Server error occurs.
111   */
112  void createBackup(BackupConfig backupConfig) throws DirectoryException;
113
114  /**
115   * Removes a backup for this storage.
116   *
117   * @param backupDirectory
118   *          The backup directory structure with which the specified backup is
119   *          associated.
120   * @param backupID
121   *          The backup ID for the backup to be removed.
122   * @throws DirectoryException
123   *           If it is not possible to remove the specified backup.
124   */
125  void removeBackup(BackupDirectory backupDirectory, String backupID) throws DirectoryException;
126
127  /**
128   * Restores a backup for this storage.
129   *
130   * @param restoreConfig
131   *          The configuration to use when performing the restore.
132   * @throws DirectoryException
133   *           If a Directory Server error occurs.
134   */
135  void restoreBackup(RestoreConfig restoreConfig) throws DirectoryException;
136
137  /**
138   * Lists the trees that exist in this storage.
139   *
140   * @return a set of {@link TreeName}s representing the trees that exist in this storage
141   */
142  Set<TreeName> listTrees();
143
144  @Override
145  void close();
146}