Class Session
Permazen
-specific state during a CLI session.
Instances operate in one of three modes; see SessionMode
.
Instances are not thread safe.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Callback interface used bySession.performSessionAction()
andSession.performSessionActionWithCurrentTransaction()
.static interface
Extension ofSession.TransactionalAction
that indicates the transaction should be retried if aRetryTransactionException
is thrown.static interface
Extension ofSession.Action
that indicates the action requires an open transaction.static interface
Extension ofSession.TransactionalAction
that indicates the action provides custom transaction options. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Default error message prefix.static final int
Default value for the initial retry delay (in milliseconds).static final int
Default value for the maximum number of retry attempts.static final int
Default value for the maximum retry delay (in milliseconds).protected final Logger
-
Constructor Summary
ConstructorDescriptionConstructor forSessionMode.CORE_API
.Session
(org.dellroad.jct.core.ConsoleSession<?, ?> consoleSession, KVDatabase kvdb) Constructor forSessionMode.KEY_VALUE
.Constructor forSessionMode.PERMAZEN
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
closeTransaction
(boolean commit) Close a transaction.Get the associatedDatabase
, if any.Get a description of the database.getError()
Get the error output for this CLI session.Get prefix to use when displaying error messages.int
Get the initial retry delay when aSession.RetryableTransactionalAction
is given toperformSessionAction()
.Get the associatedKVDatabase
.Get the openKVTransaction
currently associated with this instance.int
Configure the maximum retry delay when aSession.RetryableTransactionalAction
is given toperformSessionAction()
.int
Get the maximum number of allowed retries when aSession.RetryableTransactionalAction
is given toperformSessionAction()
.getMode()
Get this instance'sSessionMode
.Get the standard output for this CLI session.Get the associatedPermazen
, if any.Get the openPermazenTransaction
currently associated with this instance.Get theSchemaModel
configured for this instance.Get the openTransaction
currently associated with this instance.Get theValidationMode
associated with this instance.boolean
Get whether the recording of new schemas should be allowed.boolean
Get whether the garbage collection of old schemas is enabled.boolean
Get whether new transactions should be marked read-only.boolean
Determine whether there is a transaction already associated with this instance.boolean
boolean
openTransaction
(Map<String, ?> options) Open a transaction.boolean
performSessionAction
(Session.Action action) Perform the given action in the context of this session.boolean
Associate the currentPermazenTransaction
with this instance, if not already associated, while performing the given action.protected void
Handle an exception thrown during execution of a command.void
setAllowNewSchema
(boolean allowNewSchema) void
setDatabaseDescription
(String databaseDescription) void
setErrorMessagePrefix
(String prefix) Set prefix to use when displaying error messages.void
setGarbageCollectSchemas
(boolean garbageCollectSchemas) void
setInitialRetryDelay
(int initialRetryDelay) void
setMaximumRetryDelay
(int maximumRetryDelay) void
setMaxRetries
(int maxRetries) void
setMode
(SessionMode mode) Change this instance'sSessionMode
.void
setReadOnly
(boolean readOnly) void
Mark the current transaction to be rolled back.void
setSchemaModel
(SchemaModel schemaModel) void
setValidationMode
(ValidationMode validationMode) void
setVerbose
(boolean verbose) protected boolean
Determine whether a stack trace should be included in error messages by default for the given exception.
-
Field Details
-
DEFAULT_MAX_RETRIES
public static final int DEFAULT_MAX_RETRIESDefault value for the maximum number of retry attempts.- See Also:
-
DEFAULT_INITIAL_RETRY_DELAY
public static final int DEFAULT_INITIAL_RETRY_DELAYDefault value for the initial retry delay (in milliseconds).- See Also:
-
DEFAULT_MAXIMUM_RETRY_DELAY
public static final int DEFAULT_MAXIMUM_RETRY_DELAYDefault value for the maximum retry delay (in milliseconds).- See Also:
-
DEFAULT_ERROR_MESSAGE_PREFIX
Default error message prefix.- See Also:
-
log
-
-
Constructor Details
-
Session
Constructor forSessionMode.KEY_VALUE
.- Parameters:
consoleSession
- console sessionkvdb
- key/value database- Throws:
IllegalArgumentException
- if either parameter is null
-
Session
Constructor forSessionMode.CORE_API
.- Parameters:
consoleSession
- console sessiondb
- core database- Throws:
IllegalArgumentException
- if either parameter is null
-
Session
Constructor forSessionMode.PERMAZEN
.- Parameters:
consoleSession
- console sessionpdb
- database- Throws:
IllegalArgumentException
- if either parameter is null
-
-
Method Details
-
getMode
Get this instance'sSessionMode
.- Returns:
- this instance's
SessionMode
.
-
setMode
Change this instance'sSessionMode
.- Parameters:
mode
- newSessionMode
- Throws:
IllegalArgumentException
- ifmode
is nullIllegalArgumentException
- ifmode
requires aPermazen
(i.e.,SessionMode.PERMAZEN
), orDatabase
(i.e.,SessionMode.CORE_API
) instance, but none was provided at construction
-
getKVDatabase
Get the associatedKVDatabase
.- Returns:
- the associated
KVDatabase
-
getDatabase
Get the associatedDatabase
, if any.- Returns:
- the associated
Database
or null if this instance is not inSessionMode.PERMAZEN
orSessionMode.CORE_API
-
getPermazen
Get the associatedPermazen
, if any.- Returns:
- the associated
Permazen
or null if this instance is not inSessionMode.PERMAZEN
-
getKVTransaction
Get the openKVTransaction
currently associated with this instance.- Returns:
- the open
KVTransaction
in which to do work - Throws:
IllegalStateException
- ifperformSessionAction()
is not currently being invoked
-
getTransaction
Get the openTransaction
currently associated with this instance.- Returns:
- the open
Transaction
in which to do work - Throws:
IllegalStateException
- ifperformSessionAction()
is not currently being invokedIllegalStateException
- if this instance is not in modeSessionMode.CORE_API
orSessionMode.PERMAZEN
-
getPermazenTransaction
Get the openPermazenTransaction
currently associated with this instance.This method just invokes
PermazenTransaction.getCurrent()
and returns the result.- Returns:
- the open
PermazenTransaction
in which to do work - Throws:
IllegalStateException
- ifperformSessionAction()
is not currently being invokedIllegalStateException
- if this instance is not in modeSessionMode.PERMAZEN
-
getSchemaModel
Get theSchemaModel
configured for this instance. If this is left unconfigured, after the first transaction it will be updated with the schema model actually used.In
SessionMode.KEY_VALUE
, this always returns null.- Returns:
- the schema model used by this session if available, otherwise null
-
setSchemaModel
-
getDatabaseDescription
Get a description of the database.- Returns:
- a short description of the underlying database
-
setDatabaseDescription
-
getValidationMode
Get theValidationMode
associated with this instance. If this is left unconfigured,ValidationMode.AUTOMATIC
is used for new transactions.This property is only relevant in
SessionMode.PERMAZEN
.- Returns:
- the validation mode used by this session
-
setValidationMode
-
isAllowNewSchema
public boolean isAllowNewSchema()Get whether the recording of new schemas should be allowed. Default value is false.This setting is ignored except in
SessionMode.CORE_API
.- Returns:
- whether this session allows recording a new schema version
-
setAllowNewSchema
public void setAllowNewSchema(boolean allowNewSchema) -
isGarbageCollectSchemas
public boolean isGarbageCollectSchemas()Get whether the garbage collection of old schemas is enabled. Default value is false.This setting is ignored except in
SessionMode.CORE_API
.- Returns:
- whether this session allows recording a new schema version
-
setGarbageCollectSchemas
public void setGarbageCollectSchemas(boolean garbageCollectSchemas) -
isReadOnly
public boolean isReadOnly()Get whether new transactions should be marked read-only. Default value is false.- Returns:
- whether this session creates read-only transactions
-
setReadOnly
public void setReadOnly(boolean readOnly) -
getMaxRetries
public int getMaxRetries()Get the maximum number of allowed retries when aSession.RetryableTransactionalAction
is given toperformSessionAction()
.Default value is
DEFAULT_MAX_RETRIES
.- Returns:
- maximum number of retry attempts, or zero if retries are disabled
- See Also:
-
setMaxRetries
public void setMaxRetries(int maxRetries) -
getInitialRetryDelay
public int getInitialRetryDelay()Get the initial retry delay when aSession.RetryableTransactionalAction
is given toperformSessionAction()
.Default value is
DEFAULT_INITIAL_RETRY_DELAY
.- Returns:
- initial retry delay in milliseconds
- See Also:
-
setInitialRetryDelay
public void setInitialRetryDelay(int initialRetryDelay) -
getMaximumRetryDelay
public int getMaximumRetryDelay()Configure the maximum retry delay when aSession.RetryableTransactionalAction
is given toperformSessionAction()
.Default value is
DEFAULT_MAXIMUM_RETRY_DELAY
.- Returns:
- maximum retry delay in milliseconds
- See Also:
-
setMaximumRetryDelay
public void setMaximumRetryDelay(int maximumRetryDelay) -
getOutput
Get the standard output for this CLI session.- Returns:
- output writer
-
getError
Get the error output for this CLI session.- Returns:
- error writer
-
reportException
Handle an exception thrown during execution of a command.- Parameters:
e
- exception that occurred
-
showStackTrace
Determine whether a stack trace should be included in error messages by default for the given exception.- Parameters:
e
- error- Returns:
- true to include stack trace
-
getErrorMessagePrefix
Get prefix to use when displaying error messages.Default is "Error: ".
- Returns:
- error message prefix
-
setErrorMessagePrefix
Set prefix to use when displaying error messages.- Parameters:
prefix
- error message prefix
-
isVerbose
public boolean isVerbose() -
setVerbose
public void setVerbose(boolean verbose) -
performSessionActionWithCurrentTransaction
public boolean performSessionActionWithCurrentTransaction(Session.Action action) throws InterruptedException Associate the currentPermazenTransaction
with this instance, if not already associated, while performing the given action.If
action
throws anException
, it will be caught and handled byreportException()
and then false returned.This instance must be in
SessionMode.PERMAZEN
, there must be aPermazenTransaction
open and associated with the current thread, and this instance must not already have a differentPermazenTransaction
associated with it (it may already have the samePermazenTransaction
associated with it). ThePermazenTransaction
will be left open when this method returns.This method safely handles re-entrant invocation.
- Parameters:
action
- action to perform- Returns:
- true if
action
completed successfully, false ifaction
threw an exception - Throws:
IllegalStateException
- if there is a different open transaction already associated with this instanceIllegalStateException
- if this instance is not in modeSessionMode.PERMAZEN
InterruptedException
- if the current thread is interruptedIllegalArgumentException
- ifaction
is null
-
performSessionAction
Perform the given action in the context of this session.If
action
is aSession.TransactionalAction
, and there is no transaction currently associated with this instance, a new transaction will be created, held open whileaction
executes, then committed; any transaction options fromaction
implementingSession.TransactionalActionWithOptions
will be appied. Otherwise,action
is just executed directly.In either case, if
action
throws anException
, it will be caught and handled byreportException()
and then false returned.If there is already a transaction currently associated with this instance, it is left open while
action
executes and upon return.If
action
is aSession.RetryableTransactionalAction
, and a newly created transaction throws aRetryTransactionException
, it will be retried automatically up to the configured maximum number of retry attempts. An exponential back-off algorithm is used: after the first failed attempt, the current thread sleeps for the initial retry delay. After each subsequent failed attempt, the retry delay is doubled, up to the limit imposed by the configured maximum retry delay.- Parameters:
action
- action to perform, possibly within a transaction- Returns:
- true if
action
completed successfully, false if a transaction could not be created oraction
threw an exception - Throws:
InterruptedException
- if the current thread is interruptedIllegalArgumentException
- ifaction
is null
-
isTransactionOpen
public boolean isTransactionOpen()Determine whether there is a transaction already associated with this instance.- Returns:
- true if a transaction is already open
-
setRollbackOnly
public void setRollbackOnly()Mark the current transaction to be rolled back.- Throws:
IllegalStateException
- if there is no transaction associated with this instance
-
openTransaction
Open a transaction.For experts only.
- Throws:
Exception
-
closeTransaction
Close a transaction.For experts only.
- Throws:
Exception
-