Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict read only mode #436

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package one.microstream.persistence.exceptions;

/*-
* #%L
* MicroStream Persistence
* %%
* Copyright (C) 2019 - 2022 MicroStream Software
* %%
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License, v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is
* available at https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
* #L%
*/

public class PersistenceExceptionStoringDisabled extends PersistenceException
{
@Override
public String getMessage()
{
return "Storing is not enabled! " +
(super.getMessage() != null ? " Details: " + super.getMessage() : "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static one.microstream.X.notNull;

import one.microstream.afs.types.WriteController;
import one.microstream.persistence.exceptions.PersistenceException;
import one.microstream.persistence.exceptions.PersistenceExceptionStoringDisabled;


@FunctionalInterface
Expand All @@ -36,7 +36,7 @@ public default void validateIsStoringEnabled()
return;
}

throw new PersistenceException("Storing is not enabled.");
throw new PersistenceExceptionStoringDisabled();
}

public default boolean isStoringEnabled()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package one.microstream.storage.types;

/*-
* #%L
* MicroStream Storage
* %%
* Copyright (C) 2019 - 2022 MicroStream Software
* %%
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License, v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is
* available at https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
* #L%
*/

import org.slf4j.Logger;

import one.microstream.afs.types.WriteController;
import one.microstream.util.logging.Logging;

/**
* {@link StorageWriteController} implementation that
* allows to switch between the supplied and a read only
* StorageWriteController. If the read only mode is disabled
* the supplied StorageWriteController will be used.
*
* <br><br>
* Setup:
*
* <pre> {@code
*
* final EmbeddedStorageFoundation<?> foundation = EmbeddedStorage.Foundation();
*
* final StorageWriteControllerReadOnlyMode storageWriteController =
* new StorageWriteControllerReadOnlyMode(foundation.getWriteController());
*
* foundation.setWriteController(storageWriteController);
*
* final EmbeddedStorageManager storage = foundation.start();
* }
* </pre>
*
*/
public class StorageWriteControllerReadOnlyMode implements StorageWriteController
{
private final static Logger logger = Logging.getLogger(StorageWriteController.class);

///////////////////////////////////////////////////////////////////////////
// instance fields //
////////////////////

private WriteController writeController;
private final WriteController defaultController;
private final WriteController readOnlyController;


///////////////////////////////////////////////////////////////////////////
// constructors //
/////////////////

/**
* Create a new instance of a StorageWriteControllerReadOnlyMode.
* The constructed instance will have the read only mode ENABLED.
*
* @param writeController the {@link StorageWriteController} instance
* that is used if the read only mode is disabled.
*/
public StorageWriteControllerReadOnlyMode(final WriteController writeController)
{
super();
this.defaultController = writeController;
this.readOnlyController = WriteController.Disabled();
this.writeController = this.readOnlyController;

logger.info("Created SwitchableStorageWriteController with Storage read-only mode enabled: {}!",
fh-ms marked this conversation as resolved.
Show resolved Hide resolved
this.writeController == this.readOnlyController
);
}


///////////////////////////////////////////////////////////////////////////
// methods //
////////////

public void enableReadOnly(final boolean enableReadOnly)
fh-ms marked this conversation as resolved.
Show resolved Hide resolved
{
if(enableReadOnly)
{
this.writeController = this.readOnlyController;
}
else
{
this.writeController = this.defaultController;
}

logger.info("Read-only mode enabled: {}", this.writeController == this.readOnlyController);
}

public boolean readOnlyEnabled()
fh-ms marked this conversation as resolved.
Show resolved Hide resolved
{
return this.writeController == this.readOnlyController;
}

@Override
public void validateIsWritable()
{
this.writeController.validateIsWritable();
}

@Override
public boolean isWritable()
{
return this.writeController.isWritable();
}

@Override
public boolean isFileCleanupEnabled()
{
return this.writeController.isWritable();
}

@Override
public boolean isBackupEnabled()
{
//must return true to start the backup handler even if writing
//is not enabled during startup!
return this.defaultController.isWritable();
}

@Override
public boolean isDeletionDirectoryEnabled()
{
return this.writeController.isWritable();
}

@Override
public boolean isFileDeletionEnabled()
{
return this.writeController.isWritable();
}

}