mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor tidy after #1863. Add NaturalKeyEntrySimple for single property natural key lookup.
This commit is contained in:
@@ -1,77 +1,17 @@
|
||||
package io.ebeaninternal.api;
|
||||
|
||||
|
||||
import io.ebean.Pairs;
|
||||
import io.ebeaninternal.server.deploy.BeanNaturalKey;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Natural key entry with name value pairs for each of the properties making up the key.
|
||||
* An entry for natural key lookup.
|
||||
*/
|
||||
public class NaturalKeyEntry {
|
||||
|
||||
private final Map<String,Object> map = new HashMap<>();
|
||||
private final Object key;
|
||||
private Object inValue;
|
||||
|
||||
/**
|
||||
* Used when query query just has a series of EQ expressions (no IN clause).
|
||||
*/
|
||||
NaturalKeyEntry(BeanNaturalKey naturalKey, List<NaturalKeyEq> eqList) {
|
||||
load(eqList);
|
||||
this.key = calculateKey(naturalKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create when query uses an IN clause.
|
||||
*/
|
||||
NaturalKeyEntry(BeanNaturalKey naturalKey, List<NaturalKeyEq> eqList, String inProperty, Object inValue) {
|
||||
load(eqList);
|
||||
if (inProperty != null) {
|
||||
map.put(inProperty, inValue);
|
||||
this.inValue = inValue;
|
||||
}
|
||||
this.key = calculateKey(naturalKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create when query uses an IN PAIRS clause.
|
||||
*/
|
||||
NaturalKeyEntry(BeanNaturalKey naturalKey, List<NaturalKeyEq> eqList,
|
||||
String inMapProperty0, String inMapProperty1, Pairs.Entry pair) {
|
||||
load(eqList);
|
||||
map.put(inMapProperty0, pair.getA());
|
||||
map.put(inMapProperty1, pair.getB());
|
||||
this.inValue = pair;
|
||||
this.key = calculateKey(naturalKey);
|
||||
}
|
||||
|
||||
private void load(List<NaturalKeyEq> eqList) {
|
||||
if (eqList != null) {
|
||||
for (NaturalKeyEq eq : eqList) {
|
||||
map.put(eq.property, eq.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Object calculateKey(BeanNaturalKey naturalKey) {
|
||||
return naturalKey.calculateKey(map);
|
||||
}
|
||||
public interface NaturalKeyEntry {
|
||||
|
||||
/**
|
||||
* Return the natural cache key (String concatenation of values).
|
||||
*/
|
||||
public Object key() {
|
||||
return key;
|
||||
}
|
||||
Object key();
|
||||
|
||||
/**
|
||||
* Return the inValue (used to remove from IN clause of original query).
|
||||
*/
|
||||
Object getInValue() {
|
||||
return inValue;
|
||||
}
|
||||
Object getInValue();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package io.ebeaninternal.api;
|
||||
|
||||
|
||||
import io.ebean.Pairs;
|
||||
import io.ebeaninternal.server.deploy.BeanNaturalKey;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Natural key entry with name value pairs for each of the properties making up the key.
|
||||
*/
|
||||
class NaturalKeyEntryBasic implements NaturalKeyEntry {
|
||||
|
||||
private final Map<String,Object> map = new HashMap<>();
|
||||
private final Object key;
|
||||
private Object inValue;
|
||||
|
||||
/**
|
||||
* Used when query query just has a series of EQ expressions (no IN clause).
|
||||
*/
|
||||
NaturalKeyEntryBasic(BeanNaturalKey naturalKey, List<NaturalKeyEq> eqList) {
|
||||
load(eqList);
|
||||
this.key = calculateKey(naturalKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create when query uses an IN clause.
|
||||
*/
|
||||
NaturalKeyEntryBasic(BeanNaturalKey naturalKey, List<NaturalKeyEq> eqList, String inProperty, Object inValue) {
|
||||
load(eqList);
|
||||
if (inProperty != null) {
|
||||
map.put(inProperty, inValue);
|
||||
this.inValue = inValue;
|
||||
}
|
||||
this.key = calculateKey(naturalKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create when query uses an IN PAIRS clause.
|
||||
*/
|
||||
NaturalKeyEntryBasic(BeanNaturalKey naturalKey, List<NaturalKeyEq> eqList,
|
||||
String inMapProperty0, String inMapProperty1, Pairs.Entry pair) {
|
||||
load(eqList);
|
||||
map.put(inMapProperty0, pair.getA());
|
||||
map.put(inMapProperty1, pair.getB());
|
||||
this.inValue = pair;
|
||||
this.key = calculateKey(naturalKey);
|
||||
}
|
||||
|
||||
private void load(List<NaturalKeyEq> eqList) {
|
||||
if (eqList != null) {
|
||||
for (NaturalKeyEq eq : eqList) {
|
||||
map.put(eq.property, eq.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Object calculateKey(BeanNaturalKey naturalKey) {
|
||||
return naturalKey.calculateKey(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object key() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getInValue() {
|
||||
return inValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.ebeaninternal.api;
|
||||
|
||||
class NaturalKeyEntrySimple implements NaturalKeyEntry {
|
||||
|
||||
private final Object key;
|
||||
|
||||
NaturalKeyEntrySimple(Object key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object key() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getInValue() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -103,26 +103,46 @@ public class NaturalKeyQueryData<T> {
|
||||
|
||||
this.set = new NaturalKeySet();
|
||||
if (inValues != null) {
|
||||
// a findList() with an IN clause so we project
|
||||
// for every IN value a natural key combination
|
||||
for (Object inValue : inValues) {
|
||||
set.add(new NaturalKeyEntry(naturalKey, eqList, inProperty, inValue));
|
||||
}
|
||||
addInValues();
|
||||
} else if (inPairs != null) {
|
||||
// a findList() with an IN Map clause so we project
|
||||
// for every IN value a natural key combination
|
||||
for (Pairs.Entry entry : inPairs) {
|
||||
set.add(new NaturalKeyEntry(naturalKey, eqList, inProperty0, inProperty1, entry));
|
||||
}
|
||||
|
||||
addInPairs();
|
||||
} else {
|
||||
// only one - a findOne()
|
||||
set.add(new NaturalKeyEntry(naturalKey, eqList));
|
||||
addEqualsKey();
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
private void addInPairs() {
|
||||
// a findList() with an IN Map clause so we project
|
||||
// for every IN value a natural key combination
|
||||
for (Pairs.Entry entry : inPairs) {
|
||||
set.add(new NaturalKeyEntryBasic(naturalKey, eqList, inProperty0, inProperty1, entry));
|
||||
}
|
||||
}
|
||||
|
||||
private void addInValues() {
|
||||
if (eqList == null) {
|
||||
// a single property IN expression
|
||||
for (Object inValue : inValues) {
|
||||
set.add(new NaturalKeyEntrySimple(inValue));
|
||||
}
|
||||
} else {
|
||||
// IN expression + EQ expression(s)
|
||||
for (Object inValue : inValues) {
|
||||
set.add(new NaturalKeyEntryBasic(naturalKey, eqList, inProperty, inValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addEqualsKey() {
|
||||
if (eqList.size() == 1) {
|
||||
// a single property EQ expression
|
||||
set.add(new NaturalKeyEntrySimple(eqList.get(0).value));
|
||||
} else {
|
||||
set.add(new NaturalKeyEntryBasic(naturalKey, eqList));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the properties match the natural key properties.
|
||||
*/
|
||||
|
||||
@@ -9,7 +9,7 @@ public class NaturalKeySet {
|
||||
|
||||
private final Map<Object, NaturalKeyEntry> map = new LinkedHashMap<>();
|
||||
|
||||
public NaturalKeySet() {
|
||||
NaturalKeySet() {
|
||||
}
|
||||
|
||||
public int size() {
|
||||
@@ -24,8 +24,7 @@ public class NaturalKeySet {
|
||||
return map.keySet();
|
||||
}
|
||||
|
||||
public Object getInValue(Object naturalKey) {
|
||||
NaturalKeyEntry entry = map.get(naturalKey);
|
||||
return entry.getInValue();
|
||||
Object getInValue(Object naturalKey) {
|
||||
return map.get(naturalKey).getInValue();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user