Skip to content

Commit

Permalink
Reworked the serialisation of entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddie Carpenter committed Jun 19, 2024
1 parent 2a76e2a commit 2f9a875
Show file tree
Hide file tree
Showing 69 changed files with 2,630 additions and 1,067 deletions.
8 changes: 8 additions & 0 deletions jpalite-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry</artifactId>
Expand Down
26 changes: 26 additions & 0 deletions jpalite-core/src/main/java/io/jpalite/CachingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.jpalite;

import jakarta.persistence.PersistenceException;

public class CachingException extends PersistenceException
{
public CachingException()
{
super();
}

public CachingException(String message)
{
super(message);
}

public CachingException(String message, Throwable cause)
{
super(message, cause);
}

public CachingException(Throwable cause)
{
super(cause);
}
}
16 changes: 1 addition & 15 deletions jpalite-core/src/main/java/io/jpalite/ConverterClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ public interface ConverterClass
*/
String getName();

/**
* The class of the converter
*
* @return The class
*/
Class<?> getConvertClass();

/**
* A check to see if the converter should be auto applied
*
Expand All @@ -47,19 +40,12 @@ public interface ConverterClass
*/
//We need to use the raw type here as the converter is not generic
@SuppressWarnings("java:S1452")
TradeSwitchConvert<?, ?> getConverter();
FieldConvertType<?, ?> getConverter();

/**
* The attribute type
*
* @return The attribute type
*/
Class<?> getAttributeType();

/**
* The database type
*
* @return The database type
*/
Class<?> getDbType();
}
74 changes: 24 additions & 50 deletions jpalite-core/src/main/java/io/jpalite/EntityCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,38 @@

import jakarta.annotation.Nonnull;
import jakarta.persistence.Cache;
import jakarta.transaction.*;
import jakarta.transaction.SystemException;

import java.util.List;
import java.time.Instant;

public interface EntityCache extends Cache
{
/**
* Create a new transaction and associate it with the current thread.
*
* @throws jakarta.transaction.NotSupportedException Thrown if the thread is already
* associated with a transaction and the Transaction Manager
* implementation does not support nested transactions.
* @throws jakarta.transaction.SystemException Thrown if the transaction manager
* encounters an unexpected error condition.
* @throws jakarta.transaction.SystemException Thrown if the transaction manager
* encounters an unexpected error condition.
*/
public void begin() throws NotSupportedException, SystemException;
public void begin() throws SystemException;

/**
* Complete the transaction associated with the current thread. When this
* method completes, the thread is no longer associated with a transaction.
*
* @throws jakarta.transaction.RollbackException Thrown to indicate that
* the transaction has been rolled back rather than committed.
* @throws jakarta.transaction.HeuristicMixedException Thrown to indicate that a heuristic
* decision was made and that some relevant updates have been committed
* while others have been rolled back.
* @throws jakarta.transaction.HeuristicRollbackException Thrown to indicate that a
* heuristic decision was made and that all relevant updates have been
* rolled back.
* @throws SecurityException Thrown to indicate that the thread is
* not allowed to commit the transaction.
* @throws IllegalStateException Thrown if the current thread is
* not associated with a transaction.
* @throws SystemException Thrown if the transaction manager
* encounters an unexpected error condition.
* @throws SystemException Thrown if the transaction manager
* encounters an unexpected error condition.
*/
void commit() throws RollbackException,
HeuristicMixedException, HeuristicRollbackException, SecurityException,
IllegalStateException, SystemException;
void commit() throws SystemException;

/**
* Roll back the transaction associated with the current thread. When this
* method completes, the thread is no longer associated with a
* transaction.
*
* @throws SecurityException Thrown to indicate that the thread is
* not allowed to roll back the transaction.
* @throws IllegalStateException Thrown if the current thread is
* not associated with a transaction.
* @throws SystemException Thrown if the transaction manager
* encounters an unexpected error condition.
* @throws SystemException Thrown if the transaction manager
* encounters an unexpected error condition.
*/
void rollback() throws IllegalStateException, SecurityException,
SystemException;
void rollback() throws SystemException;

/**
* Search the cache for an entity using the primary key.
Expand All @@ -83,17 +61,6 @@ void rollback() throws IllegalStateException, SecurityException,
*/
<T> T find(Class<T> entityType, Object primaryKey);

/**
* Search for the entity in the cache using the where clause
*
* @param entityType
* @param query
* @param <T>
* @return
*/
@Nonnull
<T> List<T> search(Class<T> entityType, String query);

/**
* Add an entity to the cache.
*
Expand All @@ -106,21 +73,28 @@ void rollback() throws IllegalStateException, SecurityException,
*
* @param entity The entity to update or add
*/
void update(JPAEntity entity);
void replace(JPAEntity entity);


/**
* Detach an entity from the cache and mark the entity as DETACHED
* Return the time for when an entity-type was last updated
*
* @param entity the entity to detach
* @param entityType The entity type
* @return The instance when the entity was last updated
*/
void remove(JPAEntity entity);
@Nonnull
<T> Instant getLastModified(Class<T> entityType);

/**
* Return the time for when an entity-type was last updated
*
* @param entityType The entity type
* @param <T>
* @return The time since epoch the entity was updated
* @deprecated Replaced by {{@link #getLastModified(Class)}}
*/
<T> long lastModified(Class<T> entityType);
@Deprecated(forRemoval = true, since = "3.0.0")
default <T> long lastModified(Class<T> entityType)
{
return getLastModified(entityType).toEpochMilli();
}
}
4 changes: 2 additions & 2 deletions jpalite-core/src/main/java/io/jpalite/EntityField.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public interface EntityField
*
* @return The attribute converter class. If no converter is set, null is returned
*/
@SuppressWarnings("java:S3740")
@SuppressWarnings({"java:S3740", "rawtypes"})
// Suppress warning for generic types
TradeSwitchConvert getConverterClass();
FieldConvertType getConverter();
}
36 changes: 22 additions & 14 deletions jpalite-core/src/main/java/io/jpalite/EntityMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,20 @@ public interface EntityMetaData<T>
@Nullable
@SuppressWarnings("java:S1452")
//generic wildcard is required
EntityMetaData<?> getIPrimaryKeyMetaData();
EntityMetaData<?> getPrimaryKeyMetaData();

/**
* Return a list of all the defined id fields
*
* @return Set of id fields
* @return List of id fields
*/
@Nonnull
List<EntityField> getIdFields();

/**
* True if the entity have more than one ID field
* True if the entity has more than one ID field
*
* @return true if there are more than one if fields in the entity
* @return true if there are more than one if field in the entity
*/
boolean hasMultipleIdFields();

Expand All @@ -160,14 +160,24 @@ public interface EntityMetaData<T>
*/
boolean isCacheable();

/**
* Retrieves the idle time of the entity. The units are dependent the value in {@link #getCacheTimeUnit}
*
* @return The idle time in.
*/
long getIdleTime();

/**
* Retrieves the time unit used for caching.
*
* @return The {@link TimeUnit} used for caching.
*/
TimeUnit getCacheTimeUnit();

/**
* True if the entity have a version field
* Checks if the entity has a version field.
*
* @return
* @return True if the entity has a version field, false otherwise.
*/
boolean hasVersionField();

Expand All @@ -179,14 +189,12 @@ public interface EntityMetaData<T>
EntityField getVersionField();

/**
* Return a comma delimited string with columns
*/
String getColumns();

/**
* The protobuf protocal file for the entity
* Deprecated since version 3.0.0 and marked for removal.
* Returns the columns associated with the entity.
*
* @return The proto file as a string
* @return The columns as a comma-delimited string.
* @deprecated This method is deprecated and will be removed in a future version.
*/
String getProtoFile();
@Deprecated(since = "3.0.0", forRemoval = true)
String getColumns();
}//EntityMetaData
44 changes: 20 additions & 24 deletions jpalite-core/src/main/java/io/jpalite/EntityMetaDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

Expand All @@ -49,33 +48,27 @@ public class EntityMetaDataManager
loadEntities();
}

private static int loadEntities()
@SuppressWarnings({"rawtypes", "unchecked"})
private static void loadEntities()
{
lock.lock();
try {
if (!registryLoaded) {
try {
REGISTRY_CONVERTERS.clear();

ClassLoader loader = Thread.currentThread().getContextClassLoader();

long start = System.currentTimeMillis();
Enumeration<URL> urls = loader.getResources("META-INF/io.jpalite.converters");
while (urls.hasMoreElements()) {
URL location = urls.nextElement();
try (InputStream inputStream = location.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {

String line = reader.readLine();
while (line != null) {
ConverterClass convertClass = new ConverterClassImpl(loader.loadClass(line));
REGISTRY_CONVERTERS.put(convertClass.getAttributeType(), convertClass);
line = reader.readLine();
}//while
}//try
}//while
ServiceLoader<FieldConvertType> converterLoader = ServiceLoader.load(FieldConvertType.class);
for (FieldConvertType<?, ?> converter : converterLoader) {
registerConverter(converter);
}

LOG.info("Loaded {} converters in {}ms", REGISTRY_CONVERTERS.size(), System.currentTimeMillis() - start);

start = System.currentTimeMillis();
urls = loader.getResources("META-INF/persistenceUnits.properties");
Enumeration<URL> urls = loader.getResources("META-INF/persistenceUnits.properties");
while (urls.hasMoreElements()) {
URL location = urls.nextElement();
try (InputStream inputStream = location.openStream()) {
Expand All @@ -98,11 +91,8 @@ private static int loadEntities()
}//while
LOG.info("Loaded {} entities in {}ms", REGISTRY_ENTITY_CLASSES.size(), System.currentTimeMillis() - start);
}//try
catch (ClassNotFoundException ex) {
throw new PersistenceException("Error loading converter class", ex);
}//catch
catch (IOException ex) {
throw new PersistenceException("Error reading persistenceUnits.properties or org.tradeswitch.converters", ex);
throw new PersistenceException("Error reading persistenceUnits.properties", ex);
}//catch

registryLoaded = true;
Expand All @@ -111,8 +101,6 @@ private static int loadEntities()
finally {
lock.unlock();
}

return REGISTRY_ENTITY_NAMES.size();
}//loadEntities

public static int getEntityCount()
Expand All @@ -131,6 +119,14 @@ public static <T> EntityMetaData<T> getMetaData(Class<?> entityName)
return metaData;
}//getMetaData

public static void registerConverter(@Nonnull FieldConvertType<?, ?> converter)
{
ConverterClass convertClass = new ConverterClassImpl(converter);
if (convertClass.isAutoApply()) {
REGISTRY_CONVERTERS.put(convertClass.getAttributeType(), convertClass);
}
}

public static void register(@Nonnull EntityMetaData<?> metaData)
{
if (REGISTRY_ENTITY_NAMES.containsKey(metaData.getName())) {
Expand Down
Loading

0 comments on commit 2f9a875

Please sign in to comment.