Class Session
Permazen
-specific state during a CLI session, include the current open transaction, if any.
Instances operate in one of three modes; see SessionMode
.
Instances are thread safe.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Callback interface used bySession.performSessionAction()
static interface
Extension ofSession.TransactionalAction
that indicates the transaction should be retried if aRetryKVTransactionException
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.static final class
Information about the a transaction associated with aSession
. -
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) Closed the transaction previously opened and associated with this instance byopenTransaction()
.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 theKVTransaction
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 thePermazenTransaction
currently associated with this instance.Get theSchemaModel
configured for this instance.Get the obsolete schema removal policy.Get theTransaction
currently associated with this instance.Get information about the transaction currently associated with this instance.Get theValidationMode
associated with this instance.boolean
Determine whether there is a transaction associated with this instance.boolean
Get whether the recording of new schemas should be allowed.boolean
Get whether new transactions should be marked read-only.boolean
Get whether to always show stack traces with error messages.Build a new Core APITransactionConfig
based on this instance.openBranchedTransaction
(SessionMode txMode, Map<String, ?> openOptions, Map<String, ?> syncOptions) Open a new branched transaction and associate it with this instance.openTransaction
(SessionMode txMode, Map<String, ?> kvoptions) Open a new transaction in the specified mode and associate it with this instance.boolean
performSessionAction
(Session.Action action) Perform the given action in the context of this session and (optionally) an open transaction.protected void
Handle an exception thrown during execution of a command.void
setAllowNewSchema
(boolean allowNewSchema) void
setDatabaseDescription
(String databaseDescription) void
setErrorMessagePrefix
(String prefix) 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
setSchemaModel
(SchemaModel schemaModel) void
setSchemaRemoval
(TransactionConfig.SchemaRemoval schemaRemoval) 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
-
getTxInfo
Get information about the transaction currently associated with this instance.- Returns:
- info for the transaction associated with this instance
- Throws:
IllegalStateException
- if there is no transaction associated with this instance
-
getKVTransaction
Get theKVTransaction
currently associated with this instance.Equivalent to
getTxInfo
().
getKVTransaction
()
.- Returns:
KVTransaction
associated with this instance- Throws:
IllegalStateException
- if there is no transaction associated with this session
-
getTransaction
Get theTransaction
currently associated with this instance.Equivalent to
getTxInfo
().
getTransaction
()
.- Returns:
Transaction
associated with this instance- Throws:
IllegalStateException
- if there is no transaction associated with this instanceIllegalStateException
- if this instance was not in modeSessionMode.CORE_API
orSessionMode.PERMAZEN
when the transaction was opened
-
getPermazenTransaction
Get thePermazenTransaction
currently associated with this instance.Equivalent to
getTxInfo
().
getPermazenTransaction
()
.- Returns:
PermazenTransaction
associated with this instance- Throws:
IllegalStateException
- if there is no transaction associated with this instanceIllegalStateException
- if this instance was not in modeSessionMode.PERMAZEN
when the transaction was opened
-
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.Default value is
ValidationMode.AUTOMATIC
.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 true.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) -
getSchemaRemoval
Get the obsolete schema removal policy.Default value is
TransactionConfig.SchemaRemoval.CONFIG_CHANGE
This setting is ignored except in
SessionMode.CORE_API
.- Returns:
- if and when to automatically remove unused schemas
-
setSchemaRemoval
-
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.When
isVerbose()
is true, this method is obviated.- 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
-
isVerbose
public boolean isVerbose()Get whether to always show stack traces with error messages.Default is false.
- Returns:
- whether to always show stack traces
-
setVerbose
public void setVerbose(boolean verbose) -
newTransactionConfig
Build a new Core APITransactionConfig
based on this instance.- Returns:
- core API transaction configuration
- Throws:
IllegalStateException
- if noDatabase
was provided at construction
-
performSessionAction
Perform the given action in the context of this session and (optionally) an open transaction.If
action
is not aSession.TransactionalAction
, it is just executed with this instance as the parameter.Otherwise, if there is no transaction currently associated with this instance, a new transaction is created, associated with the current thread while
action
executes, and then committed; ifaction
implementsSession.TransactionalActionWithOptions
then its transaction options will be appied.Otherwise, the already open transaction associated with this instance is associated with the current thread while
action
executes, and left open when this method returns.In any case, if
action
throws anException
, it will be caught and handled byreportException()
and then false returned.If
action
is aSession.RetryableTransactionalAction
, and a newly created transaction throws aRetryKVTransactionException
, 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
-
hasTransaction
public boolean hasTransaction()Determine whether there is a transaction associated with this instance.Equivalent to
getTxInfo()
!= null
.- Returns:
- true if there is an associated transaction
-
openTransaction
Open a new transaction in the specified mode and associate it with this instance.- Parameters:
txMode
- transaction session mode, or null for this session's modekvoptions
- key/value transaction options, or null for none- Returns:
- the newly created
Session.TxInfo
- Throws:
IllegalStateException
- if there is already a transaction associated with this instanceIllegalStateException
- if there is already aPermazenTransaction
associated with the current thread
-
openBranchedTransaction
public Session.TxInfo openBranchedTransaction(SessionMode txMode, Map<String, ?> openOptions, Map<String, ?> syncOptions) Open a new branched transaction and associate it with this instance.- Parameters:
txMode
- transaction session mode, or null for this session's modeopenOptions
-KVDatabase
-specific transaction options for the branch's opening transaction, or null for nonesyncOptions
-KVDatabase
-specific transaction options for the branch's commit transaction, or null for none- Returns:
- the newly created
Session.TxInfo
- Throws:
IllegalStateException
- if there is already a transaction associated with this instanceIllegalStateException
- if there is already aPermazenTransaction
associated with the current thread
-
closeTransaction
public void closeTransaction(boolean commit) Closed the transaction previously opened and associated with this instance byopenTransaction()
.This essentially does the reverse of
openTransaction()
.If
commit
is false, and there is no transaction associated with this instance, this method does nothing.- Parameters:
commit
- true to commit the transaction, false to roll it back- Throws:
IllegalStateException
- ifcommit
is true and there is no transaction associated with this instance
-