Skip to content

Commit

Permalink
Merge pull request #34 from vzakharchenko/LazyList
Browse files Browse the repository at this point in the history
LazyList fix
  • Loading branch information
vzakharchenko committed Mar 26, 2020
2 parents 45af551 + 81f0433 commit 1ffdf6d
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 182 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;
import com.github.vzakharchenko.dynamic.orm.core.query.QueryContextImpl;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.CacheBuilder;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.RawCacheBuilder;
import com.querydsl.sql.RelationalPath;
import org.apache.commons.collections4.CollectionUtils;

Expand Down Expand Up @@ -40,7 +41,8 @@ public List<MODEL> getModelList() {
return Collections.EMPTY_LIST;
}
CacheBuilder<MODEL> cacheBuilder = ormQueryFactory.modelCacheBuilder(qTable, modelClass);
return ModelLazyListFactory.buildModelLazyList(listPrimaryKey, cacheBuilder);
return ModelLazyListFactory.buildModelLazyList(listPrimaryKey,
(RawCacheBuilder<MODEL>) cacheBuilder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.CacheBuilder;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.RawCacheBuilder;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.List;

Expand All @@ -15,20 +13,21 @@
public class ModelLazyList<MODEL extends DMLModel> extends AbstractList<MODEL> {
private static final int MAX_LAZYLIST_CACHE_SIZE = 100;
private final List<CompositeKey> listIds;
private final CacheBuilder<MODEL> cacheBuilder;
private final RawCacheBuilder<MODEL> cacheBuilder;

protected ModelLazyList(List<CompositeKey> listIds, CacheBuilder<MODEL> cacheBuilder) {
protected ModelLazyList(List<CompositeKey> listIds,
RawCacheBuilder<MODEL> cacheBuilder) {
super();
this.listIds = listIds;
this.cacheBuilder = cacheBuilder;
}

@Override
public MODEL get(int index) {
Serializable key = listIds.get(index);
if (!((RawCacheBuilder) cacheBuilder).isPresentInCache(key)) {
CompositeKey key = listIds.get(index);
if (!cacheBuilder.isPresentInCache(key)) {
int toIndex = index + MAX_LAZYLIST_CACHE_SIZE;
cacheBuilder.findAllByIds(listIds.subList(
cacheBuilder.findAllOfMapByIds(listIds.subList(
index, toIndex < listIds.size() ? toIndex : listIds.size()));
}
return cacheBuilder.findOneById(listIds.get(index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;
import com.github.vzakharchenko.dynamic.orm.core.query.QueryContextImpl;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.CacheBuilder;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.RawCacheBuilder;
import com.querydsl.sql.RelationalPath;

import java.util.List;
Expand All @@ -14,8 +14,9 @@
public abstract class ModelLazyListFactory {

public static <MODEL extends DMLModel> List<MODEL> buildModelLazyList(
List<CompositeKey> keys, CacheBuilder<MODEL> cacheBuilder) {
return new ModelLazyList<MODEL>(keys, cacheBuilder);
List<CompositeKey> keys,
RawCacheBuilder<MODEL> cacheBuilder) {
return new ModelLazyList<>(keys, cacheBuilder);
}

public static <MODEL extends DMLModel> LazyList<MODEL> buildLazyList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.OrmQueryFactory;
import com.github.vzakharchenko.dynamic.orm.core.cache.CachedAllData;
import com.github.vzakharchenko.dynamic.orm.core.cache.CachedColumn;
import com.github.vzakharchenko.dynamic.orm.core.cache.CachedColumnWithValue;
import com.github.vzakharchenko.dynamic.orm.core.cache.MapModel;
import com.github.vzakharchenko.dynamic.orm.core.dynamic.QDynamicTable;
import com.github.vzakharchenko.dynamic.orm.core.dynamic.dml.DynamicTableModel;
Expand All @@ -15,7 +13,6 @@
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -44,20 +41,12 @@ public static CachedAllData buildAllDataCache(RelationalPath<?> qTable) {
return new CachedAllData(qTable);
}

public static <TYPE extends Serializable> CachedColumnWithValue buildCachedColumnWithValue(
Path<TYPE> column, TYPE value) {
return new CachedColumnWithValue(column, value);
}

public static CachedColumn buildCachedColumn(Path column) {
return new CachedColumn(column);
}

public static CompositeKey buildPrimaryKeyCacheModel(
DMLModel model, RelationalPath<?> qTable) {
return PrimaryKeyHelper
.getPrimaryKeyValues(model, qTable);
}
// public static CompositeKey buildPrimaryKeyCacheModel(
// DMLModel model, RelationalPath<?> qTable) {
// return PrimaryKeyHelper
// .getPrimaryKeyValues(model, qTable);
// }

public static <MODEL extends DMLModel> MODEL newInstance(
RelationalPath<?> qTable, Class<MODEL> modelClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.RawModel;
import com.github.vzakharchenko.dynamic.orm.core.query.crud.UpdateModelBuilder;
import com.google.common.collect.Sets;
import com.querydsl.core.types.Path;
import com.querydsl.sql.PrimaryKey;
import com.querydsl.sql.RelationalPath;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.Assert;

import java.io.Serializable;
Expand Down Expand Up @@ -51,10 +53,20 @@ public static PrimaryKey<?> getPrimaryKey(RelationalPath qTable) {
return (PrimaryKey<?>) qTable.getPrimaryKey();
}

public static CompositeKey validateCompositeKey(CompositeKey compositeKey,
RelationalPath<?> qTable) {
Assert.notNull(qTable.getPrimaryKey(),
qTable + " does not have Primary Key");
Assert.isTrue(CollectionUtils.isEqualCollection(
compositeKey.getCompositeMap().keySet(),
Sets.newHashSet(qTable.getPrimaryKey().getLocalColumns())),
qTable + " contains another Primary Key.");
return compositeKey;
}

public static CompositeKey getCompositeKey(Serializable value, RelationalPath<?> qTable) {
if (value instanceof CompositeKey) {
return (CompositeKey) value;
return validateCompositeKey((CompositeKey) value, qTable);
} else {
return getOnePrimaryKey(qTable, value);
}
Expand Down Expand Up @@ -152,6 +164,10 @@ public static CompositeKey getOnePrimaryKey(
RelationalPath qTable,
Serializable value) {
List<? extends Path<?>> primaryKeyColumns = getPrimaryKeyColumns(qTable);
Assert.isTrue(!primaryKeyColumns.isEmpty(),
qTable + " does not have Primary Key");
Assert.isTrue(primaryKeyColumns.size() == 1,
qTable + " has composite Primary key");
Path<?> column = primaryKeyColumns.get(0);
return CompositeKeyBuilder
.create(qTable).addPrimaryKey(column, value).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
*/
public abstract class AbstractCacheBuilder<MODEL extends DMLModel>
implements CacheBuilder<MODEL>, RawCacheBuilder {
implements RawCacheBuilder<MODEL> {

public final SoftDelete<?> softDelete;
protected final Class<MODEL> modelClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,8 @@ public MODEL findOneById(Serializable key) {

@Override
public List<MODEL> findAllByIds(List<? extends Serializable> keys) {

List<MODEL> returnedModels = new ArrayList<>(keys.size());

Map<CompositeKey, MapModel> listOfMapByIds = findAllOfMapByIds(
PrimaryKeyHelper.getCompositeKeys(keys, qTable));

for (Serializable key : keys) {
returnedModels.add(CacheHelper.buildModel(modelClass, listOfMapByIds.get(key)));
}

return returnedModels;
return ModelLazyListFactory
.buildModelLazyList(PrimaryKeyHelper.getCompositeKeys(keys, qTable), this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.vzakharchenko.dynamic.orm.core.query.cache;


import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.cache.MapModel;
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;

Expand All @@ -11,7 +12,7 @@
/**
*
*/
public interface RawCacheBuilder {
public interface RawCacheBuilder<MODEL extends DMLModel> extends CacheBuilder<MODEL> {
Map<CompositeKey, MapModel> findAllOfMapByIds(List<CompositeKey> keys);

boolean isPresentInCache(Serializable key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.vzakharchenko.dynamic.orm.core.transaction.cache;

import com.github.vzakharchenko.dynamic.orm.core.cache.CachedAllData;
import com.github.vzakharchenko.dynamic.orm.core.helper.CacheHelper;
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;
import org.springframework.cache.Cache;

Expand Down Expand Up @@ -67,21 +67,21 @@ public void cacheEvict(Serializable key) {
@Override
public void deleteModel(CompositeKey key) {
cacheEvict(key);
cacheEvict(new CachedAllData(key.getTable()));
cacheEvict(CacheHelper.buildAllDataCache(key.getTable()));
deletedObjects.put(key, key);
insertedObjects.remove(key);
updatedObjects.remove(key);
}

@Override
public void insertModel(CompositeKey key) {
cacheEvict(new CachedAllData(key.getTable()));
cacheEvict(CacheHelper.buildAllDataCache(key.getTable()));
insertedObjects.put(key, key);
}

@Override
public void updateModel(CompositeKey key) {
cacheEvict(new CachedAllData(key.getTable()));
cacheEvict(CacheHelper.buildAllDataCache(key.getTable()));
cacheEvict(key);
updatedObjects.put(key, key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void testSelectSmartCacheWrongType() {

}

@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "Primary key value is null")
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "TEST_COMPOSITE_PK_TABLE has composite Primary key")
public void testSelectSmartCacheFailedJustValue() {
TestTableCompositePrimaryKey data1 = createStaticDml(1, "key1", 11);
// insert data
Expand All @@ -135,7 +135,7 @@ public void testSelectSmartCacheFailedJustValue() {
.findAllByIds(Arrays.asList(1));
}

@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "Primary key value is null")
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "TEST_COMPOSITE_PK_TABLE contains another Primary Key.")
public void testSelectSmartCacheOnlyOneKey() {
TestTableCompositePrimaryKey data1 = createStaticDml(1, "key1", 11);
// insert data
Expand Down
Loading

0 comments on commit 1ffdf6d

Please sign in to comment.