improvements to two competing enums: CacheConcurrencyStrategy + AccessType

This commit is contained in:
Gavin 2022-12-28 01:15:26 +01:00
parent 3569efcf7a
commit 16be6a821b
4 changed files with 57 additions and 47 deletions

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.annotations;
import org.hibernate.AssertionFailure;
import org.hibernate.cache.spi.access.AccessType;
/**
@ -49,7 +50,7 @@ public enum CacheConcurrencyStrategy {
*
* @see org.hibernate.cfg.AvailableSettings#DEFAULT_CACHE_CONCURRENCY_STRATEGY
*/
NONE( null ),
NONE,
/**
* Read-only access to the shared second-level cache.
@ -62,7 +63,7 @@ public enum CacheConcurrencyStrategy {
*
* @see AccessType#READ_ONLY
*/
READ_ONLY( AccessType.READ_ONLY ),
READ_ONLY,
/**
* Read/write access to the shared second-level cache with no
@ -84,7 +85,7 @@ public enum CacheConcurrencyStrategy {
*
* @see AccessType#NONSTRICT_READ_WRITE
*/
NONSTRICT_READ_WRITE( AccessType.NONSTRICT_READ_WRITE ),
NONSTRICT_READ_WRITE,
/**
* Read/write access to the shared second-level cache using
@ -115,7 +116,7 @@ public enum CacheConcurrencyStrategy {
*
* @see AccessType#READ_WRITE
*/
READ_WRITE( AccessType.READ_WRITE ),
READ_WRITE,
/**
* Transactional access to the shared second-level cache.
@ -130,22 +131,29 @@ public enum CacheConcurrencyStrategy {
*
* @see AccessType#TRANSACTIONAL
*/
TRANSACTIONAL( AccessType.TRANSACTIONAL );
private final AccessType accessType;
CacheConcurrencyStrategy(AccessType accessType) {
this.accessType = accessType;
}
TRANSACTIONAL;
/**
* Get the {@link AccessType} corresponding to this concurrency strategy.
*
* @return The corresponding concurrency strategy. Note that this will
* return {@code null} for {@link #NONE}
* return {@code null} for {@link #NONE}.
*/
public AccessType toAccessType() {
return accessType;
switch ( this ) {
case NONE:
return null;
case READ_ONLY:
return AccessType.READ_ONLY;
case NONSTRICT_READ_WRITE:
return AccessType.NONSTRICT_READ_WRITE;
case READ_WRITE:
return AccessType.READ_WRITE;
case TRANSACTIONAL:
return AccessType.TRANSACTIONAL;
default:
throw new AssertionFailure( "unknown CacheConcurrencyStrategy" );
}
}
/**
@ -154,29 +162,25 @@ public enum CacheConcurrencyStrategy {
* @param accessType The access type to convert
*
* @return The corresponding enum value. {@link #NONE} is returned by
* default if unable to recognize the {@code accessType} or if the
* {@code accessType} is {@code null}.
* default if unable to recognize the {@code accessType} or
* if the {@code accessType} is {@code null}.
*/
public static CacheConcurrencyStrategy fromAccessType(AccessType accessType) {
if ( null == accessType ) {
if ( accessType == null ) {
return NONE;
}
switch ( accessType ) {
case READ_ONLY: {
return READ_ONLY;
}
case READ_WRITE: {
return READ_WRITE;
}
case NONSTRICT_READ_WRITE: {
return NONSTRICT_READ_WRITE;
}
case TRANSACTIONAL: {
return TRANSACTIONAL;
}
default: {
return NONE;
else {
switch ( accessType ) {
case READ_ONLY:
return READ_ONLY;
case READ_WRITE:
return READ_WRITE;
case NONSTRICT_READ_WRITE:
return NONSTRICT_READ_WRITE;
case TRANSACTIONAL:
return TRANSACTIONAL;
default:
return NONE;
}
}
}
@ -198,6 +202,7 @@ public enum CacheConcurrencyStrategy {
}
private boolean isMatch(String name) {
final AccessType accessType = toAccessType();
return accessType != null && accessType.getExternalName().equalsIgnoreCase( name )
|| name().equalsIgnoreCase( name );
}

View File

@ -33,7 +33,7 @@ import org.hibernate.dialect.Dialect;
* We must also consider the physical representation of the zoned datetime
* in the database table.
* <p>
* The {@link #DEFAULT default strategy} guarantees that a round trip
* The {@linkplain #DEFAULT default strategy} guarantees that a round trip
* preserves the instant. Whether the zone or offset is preserved depends
* on whether the underlying database has a {@code timestamp with time zone}
* type which preserves offsets:

View File

@ -9,27 +9,34 @@ package org.hibernate.cache.spi.access;
import java.util.Locale;
/**
* The types of access strategies available.
* Enumerates the policies for managing concurrent access to the shared
* second-level cache.
*
* @author Steve Ebersole
*/
public enum AccessType {
/**
* Read-only access. Data may be added and removed, but not mutated.
* Read-only access. Data may be added and removed, but not mutated.
*/
READ_ONLY( "read-only" ),
/**
* Read and write access (strict). Data may be added, removed and mutated.
* Read and write access. Data may be added, removed and mutated.
* A "soft" lock on the cached item is used to manage concurrent
* access during mutation.
*/
READ_WRITE( "read-write" ),
/**
* Read and write access (non-strict). Data may be added, removed and mutated. The non-strictness comes from
* the fact that locks are not maintained as tightly as in {@link #READ_WRITE}, which leads to better throughput
* but may also lead to inconsistencies.
* Read and write access. Data may be added, removed and mutated.
* The cached item is invalidated before and after transaction
* completion to manage concurrent access during mutation. This
* strategy is more vulnerable to inconsistencies than
* {@link #READ_WRITE}, but may allow higher throughput.
*/
NONSTRICT_READ_WRITE( "nonstrict-read-write" ),
/**
* A read and write strategy where isolation/locking is maintained in conjunction with a JTA transaction.
* Read and write access. Data may be added, removed and mutated.
* Some sort of hard lock is maintained in conjunction with a
* JTA transaction.
*/
TRANSACTIONAL( "transactional" );
@ -40,7 +47,7 @@ public enum AccessType {
}
/**
* Get the corresponding externalized name for this value.
* Get the external name of this value.
*
* @return The corresponding externalized name.
*/
@ -54,13 +61,11 @@ public enum AccessType {
}
/**
* Resolve an AccessType from its external name.
* Resolve an {@link AccessType} from its external name.
*
* @param externalName The external representation to resolve
*
* @return The access type.
*
* @throws UnknownAccessTypeException If the externalName was not recognized.
* @return The {@link AccessType} represented by the given external name
* @throws UnknownAccessTypeException if the external name was not recognized
*
* @see #getExternalName()
*/

View File

@ -23,7 +23,7 @@ import org.hibernate.Session;
* tends to build up in the first-level cache of session, sessions should
* typically not be associated with long-lived scopes.
* <p>
* The most typical example of s scope with which a current session might
* The most typical example of a scope with which a current session might
* be associated is the HTTP request context in a web application.
* <p>
* An implementation of this interface must: