Skip to content

Commit

Permalink
#29 сделал возможность составного ключа с генерацией long integer внутри
Browse files Browse the repository at this point in the history
  • Loading branch information
serezakorotaev committed Dec 17, 2023
1 parent b825829 commit 5f95515
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package apps.amaralus.qa.platform.dataset.model;

import apps.amaralus.qa.platform.rocksdb.key.CompoundKey;
import apps.amaralus.qa.platform.rocksdb.sequence.GeneratedSequence;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.keyvalue.annotation.KeySpace;

@Data
@KeySpace("alias")
//todo сделать составной ключ name+project
public class AliasModel {

@Id
@GeneratedSequence("alias-sequence")
private long id;
private Key key;
private String name;
private long dataset;
private String propertyName;
private String project;

@Getter
@Setter
@CompoundKey
public static final class Key {
long id;
String project;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

public interface AliasRepository extends KeyValueRepository<AliasModel, Long> {

List<AliasModel> findAllByProject(String project);
List<AliasModel> findAllByKey_Project(String project);

Optional<AliasModel> findByNameAndProject(String name, String project);
Optional<AliasModel> findByNameAndKey_Project(String name, String project);

List<AliasModel> findAllByNameAndProject(String name, String project);
List<AliasModel> findAllByNameAndKey_Project(String name, String project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Alias save(Alias alias) {

public Alias updateAliasName(String newName, String oldName, String project) {

var alias = aliasRepository.findByNameAndProject(oldName, project)
var alias = aliasRepository.findByNameAndKey_Project(oldName, project)
.map(aliasModel -> {
aliasModel.setName(newName);
return aliasRepository.save(aliasModel);
Expand All @@ -43,15 +43,15 @@ public Alias updateAliasName(String newName, String oldName, String project) {
}

public void deleteAliasByName(String name, String project) {
aliasRepository.deleteAll(aliasRepository.findAllByNameAndProject(name, project));
aliasRepository.deleteAll(aliasRepository.findAllByNameAndKey_Project(name, project));
}

public void deleteAllByProject(String project) {
aliasRepository.deleteAll(aliasRepository.findAllByProject(project));
aliasRepository.deleteAll(aliasRepository.findAllByKey_Project(project));
}

public Optional<Alias> getAliasByName(String aliasName, String project) {
return aliasRepository.findByNameAndProject(aliasName, project)
return aliasRepository.findByNameAndKey_Project(aliasName, project)
.map(aliasMapper::mapToD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@
import apps.amaralus.qa.platform.dataset.model.AliasModel;
import apps.amaralus.qa.platform.mapper.GenericMapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface AliasMapper extends GenericMapper<Alias, AliasModel> {

@Override
@Mapping(target = "key.id", source = "id")
@Mapping(target = "key.project", source = "project")
AliasModel mapToM(Alias alias);

@Override
@Mapping(target = "id", source = "key.id")
@Mapping(target = "project", source = "key.project")
Alias mapToD(AliasModel aliasModel);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package apps.amaralus.qa.platform.rocksdb;

import apps.amaralus.qa.platform.rocksdb.key.CompoundKey;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -38,7 +39,13 @@ else if (id instanceof Long l)
return longToBytes(l);
else if (id instanceof String s)
return s.getBytes(StandardCharsets.UTF_8);
else
else if (id.getClass().isAnnotationPresent(CompoundKey.class)) {
try {
return mapper.writer().writeValueAsBytes(id);
} catch (JsonProcessingException e) {
throw new RocksDbRuntimeException(e);
}
} else
throw new UnsupportedOperationException("Id type [" + id.getClass().getName() + "] is unsupported!");
}

Expand All @@ -53,8 +60,15 @@ else if (idClass.isAssignableFrom(Long.TYPE))
return bytesToLong(bytes);
else if (idClass.isAssignableFrom(String.class))
return new String(bytes, StandardCharsets.UTF_8);
else
else if (idClass.isAnnotationPresent(CompoundKey.class)) {
try {
return mapper.reader().readValue(bytes, idClass);
} catch (IOException e) {
throw new RocksDbRuntimeException(e);
}
} else
throw new UnsupportedOperationException("Id type [" + idClass.getName() + "] is unsupported!");

}

private byte[] intToBytes(int data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package apps.amaralus.qa.platform.rocksdb.key;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;

@Retention(RetentionPolicy.RUNTIME)
@Target(value = TYPE)
public @interface CompoundKey {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package apps.amaralus.qa.platform.rocksdb.sequence;

import apps.amaralus.qa.platform.rocksdb.key.CompoundKey;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Set;

@Aspect
Expand All @@ -20,7 +24,8 @@ public class AutoIncrementSequenceAspect {
private final MappingContext<?, ?> mappingContext;
private final Set<Class<?>> sequences;

public AutoIncrementSequenceAspect(SequenceGenerator sequenceGenerator, MappingContext<?, ?> mappingContext) {
public AutoIncrementSequenceAspect(SequenceGenerator sequenceGenerator,
MappingContext<?, ?> mappingContext) {
this.sequenceGenerator = sequenceGenerator;
this.mappingContext = mappingContext;
sequences = sequenceGenerator.getSequencesClasses();
Expand Down Expand Up @@ -52,19 +57,45 @@ private void handleEntity(Object entity) {
}
}

@SneakyThrows
@SuppressWarnings("java:S2589")
private void incrementSequence(PersistentPropertyAccessor<?> accessor, PersistentProperty<?> persistentProperty) {
var propertyValue = accessor.getProperty(persistentProperty);
// null or int or long equality
if (propertyValue == null || propertyValue.equals(0) || propertyValue.equals(0L)) {
long nextValue = sequenceGenerator.increment(getSequenceName(persistentProperty));

// possible int casting
if (persistentProperty.getType().equals(Integer.class)
|| persistentProperty.getType().equals(Integer.TYPE))
accessor.setProperty(persistentProperty, (int) nextValue);
else
accessor.setProperty(persistentProperty, nextValue);


if (propertyValue != null && propertyValue.getClass().isAnnotationPresent(CompoundKey.class)) {
incrementCompoundKey(persistentProperty, propertyValue);
} else {
if (propertyValue == null || propertyValue.equals(0) || propertyValue.equals(0L)) {
long nextValue = sequenceGenerator.increment(getSequenceName(persistentProperty));

// possible int casting
if (persistentProperty.getType().equals(Integer.class)
|| persistentProperty.getType().equals(Integer.TYPE))
accessor.setProperty(persistentProperty, (int) nextValue);
else
accessor.setProperty(persistentProperty, nextValue);
}
}
}

@SuppressWarnings("java:S3011")
private void incrementCompoundKey(PersistentProperty<?> persistentProperty, Object propertyValue) throws IllegalAccessException {

for (Field field : propertyValue.getClass().getDeclaredFields()) {

if (!field.getType().equals(Long.class)
&& !field.getType().equals(Long.TYPE)
&& !field.getType().equals(Integer.class)
&& !field.getType().equals(Integer.TYPE))
throw new InvalidPropertyException(persistentProperty.getOwner().getType(), persistentProperty.getName(), "Sequence only supports long or integer types");

field.setAccessible(true);

field.set(
propertyValue,
sequenceGenerator.increment(getSequenceName(persistentProperty))
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apps.amaralus.qa.platform.rocksdb.sequence;

import apps.amaralus.qa.platform.rocksdb.RocksDbKeyValueAdapter;
import apps.amaralus.qa.platform.rocksdb.key.CompoundKey;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.data.mapping.PersistentProperty;
Expand Down Expand Up @@ -115,7 +116,8 @@ private void validateProperty(PersistentProperty<?> persistentProperty) {
if (!propertyType.equals(Long.class)
&& !propertyType.equals(Long.TYPE)
&& !propertyType.equals(Integer.class)
&& !propertyType.equals(Integer.TYPE))
&& !propertyType.equals(Integer.TYPE)
&& !propertyType.isAnnotationPresent(CompoundKey.class))
throw new InvalidPropertyException(sequenceClass, persistentProperty.getName(), "Sequence only supports long or integer types");
}

Expand Down

0 comments on commit 5f95515

Please sign in to comment.