Skip to content

Commit

Permalink
Draft changes for generic type
Browse files Browse the repository at this point in the history
  • Loading branch information
anidotnet committed Oct 7, 2023
1 parent 6a24753 commit 0ee58f5
Show file tree
Hide file tree
Showing 103 changed files with 1,029 additions and 606 deletions.
1 change: 1 addition & 0 deletions nitrite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
<exclude>**/*DBNull.java</exclude>
<exclude>**/*DBValue.java</exclude>
<exclude>**/*UnknownType.java</exclude>
<exclude>**/*Reflector.java</exclude>
<exclude>**/*AndFilter.java</exclude>
<exclude>**/*OrFilter.java</exclude>
<exclude>**/*BetweenFilter.java</exclude>
Expand Down
115 changes: 93 additions & 22 deletions nitrite/src/main/java/org/dizitart/no2/Nitrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import org.dizitart.no2.collection.NitriteCollection;
import org.dizitart.no2.common.Constants;
import org.dizitart.no2.common.util.ObjectUtils;
import org.dizitart.no2.common.EntityType;
import org.dizitart.no2.exceptions.NitriteIOException;
import org.dizitart.no2.exceptions.ValidationException;
import org.dizitart.no2.repository.ObjectRepository;
import org.dizitart.no2.repository.EntityDecorator;
import org.dizitart.no2.repository.ObjectRepository;
import org.dizitart.no2.store.NitriteStore;
import org.dizitart.no2.store.StoreMetaData;
import org.dizitart.no2.transaction.Session;
Expand All @@ -40,11 +40,10 @@
* Nitrite is a lightweight, embedded, and self-contained Java NoSQL database.
* It provides an easy-to-use API to store and retrieve data. Nitrite stores
* data in the form of documents and supports indexing on fields within
* the documents to provide efficient search capabilities. Nitrite supports
* the documents to provide efficient search capabilities. Nitrite supports
* transactions, and provides a simple and efficient way to persist data.
*
* <p>
* Nitrite is thread-safe and can be used in a multi-threaded environment
* Nitrite is thread-safe and can be used in a multithreaded environment
* without any issues. Nitrite is designed to be embedded within the application
* and does not require any external setup or installation.
*
Expand Down Expand Up @@ -114,7 +113,10 @@ static NitriteBuilder builder() {
* @return the repository containing objects of type {@link T}.
* @see ObjectRepository
*/
<T> ObjectRepository<T> getRepository(Class<T> type);
default <T> ObjectRepository<T> getRepository(Class<T> type) {
return getRepository(new EntityType<>() {
});
}

/**
* Opens a type-safe object repository with a key identifier from the store.
Expand All @@ -129,7 +131,38 @@ static NitriteBuilder builder() {
* @return the repository containing objects of type {@link T}.
* @see ObjectRepository
*/
<T> ObjectRepository<T> getRepository(Class<T> type, String key);
default <T> ObjectRepository<T> getRepository(Class<T> type, String key) {
return getRepository(new EntityType<>() {
}, key);
}

/**
* Opens a type-safe object repository for the specified entity type from the
* store. If the repository does not exist it will be created automatically and
* returned. If a repository is already opened, it is returned as is.
* <p>
* The returned repository is thread-safe for concurrent use.
*
* @param entityType the entity type for which the repository is to be returned
* @param <T> the type of the entity
* @return the {@link ObjectRepository} instance for the specified entity type
*/
<T> ObjectRepository<T> getRepository(EntityType<T> entityType);

/**
* Opens a type-safe object repository for the specified entity type and a key
* identifier from the store. If the repository does not exist it will be
* created automatically and returned. If a repository is already opened, it is
* returned as is.
* <p>
* The returned repository is thread-safe for concurrent use.
*
* @param entityType the entity type for which the repository is to be returned
* @param <T> the type of the entity
* @param key the key, which will be appended to the repositories name.
* @return the {@link ObjectRepository} instance for the specified entity type
*/
<T> ObjectRepository<T> getRepository(EntityType<T> entityType, String key);

/**
* Opens a type-safe object repository using a {@link EntityDecorator}. If the
Expand All @@ -147,9 +180,8 @@ static NitriteBuilder builder() {
/**
* Opens a type-safe object repository using a {@link EntityDecorator} and a key
* identifier from the store. If the repository does not exist it will be
* created
* automatically and returned. If a repository is already opened, it is returned
* as is.
* created automatically and returned. If a repository is already opened, it is
* returned as is.
* <p>
* The returned repository is thread-safe for concurrent use.
*
Expand All @@ -173,7 +205,9 @@ static NitriteBuilder builder() {
* @param <T> the type parameter
* @param type the type
*/
<T> void destroyRepository(Class<T> type);
default <T> void destroyRepository(Class<T> type) {
destroyRepository(new EntityType<T>() {});
}

/**
* Destroys a keyed-{@link ObjectRepository} without opening it first.
Expand All @@ -182,7 +216,26 @@ static NitriteBuilder builder() {
* @param type the type
* @param key the key
*/
<T> void destroyRepository(Class<T> type, String key);
default <T> void destroyRepository(Class<T> type, String key) {
destroyRepository(new EntityType<T>() {}, key);
}

/**
* Destroys the repository for the given entity type.
*
* @param entityType the entity type for which the repository needs to be destroyed.
* @param <T> the type of the entity.
*/
<T> void destroyRepository(EntityType<T> entityType);

/**
* Destroys the repository for the specified entity type and key.
*
* @param entityType the entity type of the repository to be destroyed.
* @param key the key of the repository to be destroyed.
* @param <T> the type of the entity.
*/
<T> void destroyRepository(EntityType<T> entityType, String key);

/**
* Destroys an {@link ObjectRepository} without opening it first.
Expand Down Expand Up @@ -262,11 +315,6 @@ static NitriteBuilder builder() {
*/
StoreMetaData getDatabaseMetaData();

/**
* Creates a {@link Session} for transaction.
*
* @return the session
*/
/**
* Creates a new session for the Nitrite database. A session is a lightweight
* container that holds transactions. Multiple sessions can be created for a
Expand Down Expand Up @@ -300,9 +348,7 @@ default boolean hasCollection(String name) {
* @return true if a repository of the specified type exists, false otherwise
*/
default <T> boolean hasRepository(Class<T> type) {
checkOpened();
String name = findRepositoryName(type, null);
return listRepositories().contains(name);
return hasRepository(new EntityType<T>() {});
}

/**
Expand All @@ -316,10 +362,35 @@ default <T> boolean hasRepository(Class<T> type) {
* entity type; false otherwise
*/
default <T> boolean hasRepository(Class<T> type, String key) {
return hasRepository(new EntityType<T>() {}, key);
}

/**
* Checks if a repository exists for the given entity type in the database.
*
* @param entityType the entity type to check for
* @param <T> the type of the entity
* @return {@code true} if a repository exists for the given entity type; {@code false} otherwise
*/
default <T> boolean hasRepository(EntityType<T> entityType) {
checkOpened();
String name = findRepositoryName(entityType, null);
return listRepositories().contains(name);
}

/**
* Checks if a repository exists for the given entity type and key in the database.
*
* @param entityType the entity type of the repository to check
* @param key the key of the repository to check
* @param <T> the type of the entity
* @return true if a repository with the given entity type and key exists, false otherwise
*/
default <T> boolean hasRepository(EntityType<T> entityType, String key) {
checkOpened();
String entityName = ObjectUtils.getEntityName(type);
String name = findRepositoryName(entityType, null);
return listKeyedRepositories().containsKey(key)
&& listKeyedRepositories().get(key).contains(entityName);
&& listKeyedRepositories().get(key).contains(name);
}

/**
Expand Down
17 changes: 9 additions & 8 deletions nitrite/src/main/java/org/dizitart/no2/NitriteDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.dizitart.no2.collection.CollectionFactory;
import org.dizitart.no2.collection.Document;
import org.dizitart.no2.collection.NitriteCollection;
import org.dizitart.no2.common.EntityType;
import org.dizitart.no2.common.concurrent.LockService;
import org.dizitart.no2.exceptions.NitriteException;
import org.dizitart.no2.exceptions.NitriteIOException;
Expand Down Expand Up @@ -71,15 +72,15 @@ public NitriteCollection getCollection(String name) {
}

@Override
public <T> ObjectRepository<T> getRepository(Class<T> type) {
public <T> ObjectRepository<T> getRepository(EntityType<T> entityType) {
checkOpened();
return repositoryFactory.getRepository(nitriteConfig, type);
return repositoryFactory.getRepository(nitriteConfig, entityType);
}

@Override
public <T> ObjectRepository<T> getRepository(Class<T> type, String key) {
public <T> ObjectRepository<T> getRepository(EntityType<T> entityType, String key) {
checkOpened();
return repositoryFactory.getRepository(nitriteConfig, type, key);
return repositoryFactory.getRepository(nitriteConfig, entityType, key);
}

@Override
Expand All @@ -101,16 +102,16 @@ public void destroyCollection(String name) {
}

@Override
public <T> void destroyRepository(Class<T> type) {
public <T> void destroyRepository(EntityType<T> entityType) {
checkOpened();
String mapName = findRepositoryName(type, null);
String mapName = findRepositoryName(entityType, null);
store.removeMap(mapName);
}

@Override
public <T> void destroyRepository(Class<T> type, String key) {
public <T> void destroyRepository(EntityType<T> entityType, String key) {
checkOpened();
String mapName = findRepositoryName(type, key);
String mapName = findRepositoryName(entityType, key);
store.removeMap(mapName);
}

Expand Down
68 changes: 68 additions & 0 deletions nitrite/src/main/java/org/dizitart/no2/common/EntityType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.dizitart.no2.common;

import lombok.Getter;
import org.dizitart.no2.exceptions.ValidationException;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Objects;

/**
* An abstract class representing the type of entity in Nitrite database.
* It provides the raw type and parameterized type information of the entity.
*
* @param <T> the type of the entity
* @since 4.2
* @author Anindya Chatterjee
*/
@Getter
public abstract class EntityType<T> implements Comparable<EntityType<T>> {
/**
* Represents the parameterized type of entity.
*/
protected final Type type;

/**
* The raw type of the entity.
*/
protected final Class<T> rawType;

@SuppressWarnings("unchecked")
protected EntityType() {
Type superClass = getClass().getGenericSuperclass();
if (superClass instanceof Class<?>) {
throw new ValidationException("EntityType constructed without actual type information");
} else if (!(superClass instanceof ParameterizedType)) {
throw new ValidationException(superClass.getTypeName() + " is not a parameterized type");
}

type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
rawType = type instanceof ParameterizedType
? (Class<T>) ((ParameterizedType) type).getRawType()
: type instanceof TypeVariable
? (Class<T>) ((TypeVariable<?>) type).getBounds()[0]
: (Class<T>) type;
}

@Override
public int compareTo(EntityType<T> o) { return 0; }

@Override
public String toString() {
return type.getTypeName();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EntityType<?> that = (EntityType<?>) o;
return Objects.equals(type, that.type) && Objects.equals(rawType, that.rawType);
}

@Override
public int hashCode() {
return Objects.hash(type, rawType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.dizitart.no2.common.mapper;

import org.dizitart.no2.collection.Document;
import org.dizitart.no2.common.EntityType;

/**
* The {@link EntityConverter} interface is used to convert
Expand All @@ -34,7 +35,7 @@ public interface EntityConverter<T> {
*
* @return the entity type
*/
Class<T> getEntityType();
EntityType<T> getEntityType();

/**
* Converts the entity to a {@link Document}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package org.dizitart.no2.common.mapper;

import org.dizitart.no2.common.EntityType;
import org.dizitart.no2.common.module.NitritePlugin;

/**
* An interface that provides a method to try converting an object of one type to an object of another type.
*
* @author Anindya Chatterjee.
* @since 1.0
* @since 4.2
*/
public interface NitriteMapper extends NitritePlugin {
/**
Expand All @@ -35,5 +36,5 @@ public interface NitriteMapper extends NitritePlugin {
* @param type the type
* @return the target
*/
<Source, Target> Object tryConvert(Source source, Class<Target> type);
<Source, Target> Object tryConvert(Source source, EntityType<Target> type);
}
Loading

0 comments on commit 0ee58f5

Please sign in to comment.