EntityCache优化synchronized
This commit is contained in:
@@ -10,6 +10,7 @@ import java.lang.reflect.Array;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.*;
|
import java.util.concurrent.atomic.*;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import java.util.stream.*;
|
import java.util.stream.*;
|
||||||
@@ -41,11 +42,13 @@ public final class EntityCache<T> {
|
|||||||
//Flipper.sort转换成Comparator的缓存
|
//Flipper.sort转换成Comparator的缓存
|
||||||
private final Map<String, Comparator<T>> sortComparators = new ConcurrentHashMap<>();
|
private final Map<String, Comparator<T>> sortComparators = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private final ReentrantLock tableLock = new ReentrantLock();
|
||||||
|
|
||||||
//Entity类
|
//Entity类
|
||||||
private final Class<T> type;
|
private final Class<T> type;
|
||||||
|
|
||||||
//接口返回的对象是否需要复制一份
|
//接口返回的对象是否需要复制一份
|
||||||
private final boolean needcopy;
|
private final boolean needCopy;
|
||||||
|
|
||||||
//Entity构建器
|
//Entity构建器
|
||||||
private final Creator<T> creator;
|
private final Creator<T> creator;
|
||||||
@@ -100,7 +103,7 @@ public final class EntityCache<T> {
|
|||||||
direct = ve2.direct();
|
direct = ve2.direct();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.needcopy = !direct;
|
this.needCopy = !direct;
|
||||||
this.newReproduce = Reproduce.create(type, type, (m) -> {
|
this.newReproduce = Reproduce.create(type, type, (m) -> {
|
||||||
try {
|
try {
|
||||||
java.lang.reflect.Field field = type.getDeclaredField(m);
|
java.lang.reflect.Field field = type.getDeclaredField(m);
|
||||||
@@ -216,7 +219,7 @@ public final class EntityCache<T> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
T rs = map.get(pk);
|
T rs = map.get(pk);
|
||||||
return rs == null ? null : (needcopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
return rs == null ? null : (needCopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T[] finds(Serializable... pks) {
|
public T[] finds(Serializable... pks) {
|
||||||
@@ -230,7 +233,7 @@ public final class EntityCache<T> {
|
|||||||
T[] array = arrayer.apply(ids.length);
|
T[] array = arrayer.apply(ids.length);
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
T rs = map.get(ids[i]);
|
T rs = map.get(ids[i]);
|
||||||
array[i] = rs == null ? null : (needcopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
array[i] = rs == null ? null : (needCopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
} else if (t == long[].class) {
|
} else if (t == long[].class) {
|
||||||
@@ -238,7 +241,7 @@ public final class EntityCache<T> {
|
|||||||
T[] array = arrayer.apply(ids.length);
|
T[] array = arrayer.apply(ids.length);
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
T rs = map.get(ids[i]);
|
T rs = map.get(ids[i]);
|
||||||
array[i] = rs == null ? null : (needcopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
array[i] = rs == null ? null : (needCopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
@@ -246,7 +249,7 @@ public final class EntityCache<T> {
|
|||||||
T[] array = arrayer.apply(pks.length);
|
T[] array = arrayer.apply(pks.length);
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
T rs = map.get(pks[i]);
|
T rs = map.get(pks[i]);
|
||||||
array[i] = rs == null ? null : (needcopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
array[i] = rs == null ? null : (needCopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
@@ -260,7 +263,7 @@ public final class EntityCache<T> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (selects == null) {
|
if (selects == null) {
|
||||||
return (needcopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
return (needCopy ? newReproduce.apply(this.creator.create(), rs) : rs);
|
||||||
}
|
}
|
||||||
T t = this.creator.create();
|
T t = this.creator.create();
|
||||||
for (Attribute attr : this.info.attributes) {
|
for (Attribute attr : this.info.attributes) {
|
||||||
@@ -295,7 +298,7 @@ public final class EntityCache<T> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (selects == null) {
|
if (selects == null) {
|
||||||
if (needcopy) {
|
if (needCopy) {
|
||||||
rs = newReproduce.apply(ctr.create(), rs);
|
rs = newReproduce.apply(ctr.create(), rs);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -323,7 +326,7 @@ public final class EntityCache<T> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (selects == null) {
|
if (selects == null) {
|
||||||
return (needcopy ? newReproduce.apply(this.creator.create(), opt.get()) : opt.get());
|
return (needCopy ? newReproduce.apply(this.creator.create(), opt.get()) : opt.get());
|
||||||
}
|
}
|
||||||
T rs = opt.get();
|
T rs = opt.get();
|
||||||
T t = this.creator.create();
|
T t = this.creator.create();
|
||||||
@@ -703,7 +706,7 @@ public final class EntityCache<T> {
|
|||||||
}
|
}
|
||||||
final List<T> rs = new ArrayList<>();
|
final List<T> rs = new ArrayList<>();
|
||||||
if (selects == null) {
|
if (selects == null) {
|
||||||
Consumer<? super T> action = x -> rs.add(needcopy ? newReproduce.apply(creator.create(), x) : x);
|
Consumer<? super T> action = x -> rs.add(needCopy ? newReproduce.apply(creator.create(), x) : x);
|
||||||
if (comparator != null) {
|
if (comparator != null) {
|
||||||
stream.forEachOrdered(action);
|
stream.forEachOrdered(action);
|
||||||
} else {
|
} else {
|
||||||
@@ -801,8 +804,11 @@ public final class EntityCache<T> {
|
|||||||
if (rs == null) {
|
if (rs == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
synchronized (rs) {
|
tableLock.lock(); //表锁, 可优化成行锁
|
||||||
|
try {
|
||||||
this.chgReproduce.apply(rs, entity);
|
this.chgReproduce.apply(rs, entity);
|
||||||
|
} finally {
|
||||||
|
tableLock.unlock();
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -815,10 +821,13 @@ public final class EntityCache<T> {
|
|||||||
if (rs == null) {
|
if (rs == null) {
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
synchronized (rs) {
|
tableLock.lock(); //表锁, 可优化成行锁
|
||||||
|
try {
|
||||||
for (Attribute attr : attrs) {
|
for (Attribute attr : attrs) {
|
||||||
attr.set(rs, attr.get(entity));
|
attr.set(rs, attr.get(entity));
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
tableLock.unlock();
|
||||||
}
|
}
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
@@ -828,12 +837,15 @@ public final class EntityCache<T> {
|
|||||||
return (T[]) Array.newInstance(type, 0);
|
return (T[]) Array.newInstance(type, 0);
|
||||||
}
|
}
|
||||||
T[] rms = this.list.stream().filter(node.createPredicate(this)).toArray(len -> (T[]) Array.newInstance(type, len));
|
T[] rms = this.list.stream().filter(node.createPredicate(this)).toArray(len -> (T[]) Array.newInstance(type, len));
|
||||||
for (T rs : rms) {
|
tableLock.lock(); //表锁, 可优化成行锁
|
||||||
synchronized (rs) {
|
try {
|
||||||
|
for (T rs : rms) {
|
||||||
for (Attribute attr : attrs) {
|
for (Attribute attr : attrs) {
|
||||||
attr.set(rs, attr.get(entity));
|
attr.set(rs, attr.get(entity));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
tableLock.unlock();
|
||||||
}
|
}
|
||||||
return rms;
|
return rms;
|
||||||
}
|
}
|
||||||
@@ -868,11 +880,14 @@ public final class EntityCache<T> {
|
|||||||
if (rs == null) {
|
if (rs == null) {
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
synchronized (rs) {
|
tableLock.lock(); //表锁, 可优化成行锁
|
||||||
|
try {
|
||||||
for (int i = 0; i < attrs.size(); i++) {
|
for (int i = 0; i < attrs.size(); i++) {
|
||||||
ColumnValue cv = values.get(i);
|
ColumnValue cv = values.get(i);
|
||||||
updateColumn(attrs.get(i), rs, cv.getExpress(), cv.getValue());
|
updateColumn(attrs.get(i), rs, cv.getExpress(), cv.getValue());
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
tableLock.unlock();
|
||||||
}
|
}
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
@@ -890,13 +905,16 @@ public final class EntityCache<T> {
|
|||||||
stream = stream.limit(flipper.getLimit());
|
stream = stream.limit(flipper.getLimit());
|
||||||
}
|
}
|
||||||
T[] rms = stream.filter(node.createPredicate(this)).toArray(len -> (T[]) Array.newInstance(type, len));
|
T[] rms = stream.filter(node.createPredicate(this)).toArray(len -> (T[]) Array.newInstance(type, len));
|
||||||
for (T rs : rms) {
|
tableLock.lock(); //表锁, 可优化成行锁
|
||||||
synchronized (rs) {
|
try {
|
||||||
|
for (T rs : rms) {
|
||||||
for (int i = 0; i < attrs.size(); i++) {
|
for (int i = 0; i < attrs.size(); i++) {
|
||||||
ColumnValue cv = values.get(i);
|
ColumnValue cv = values.get(i);
|
||||||
updateColumn(attrs.get(i), rs, cv.getExpress(), cv.getValue());
|
updateColumn(attrs.get(i), rs, cv.getExpress(), cv.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
tableLock.unlock();
|
||||||
}
|
}
|
||||||
return rms;
|
return rms;
|
||||||
}
|
}
|
||||||
@@ -909,8 +927,11 @@ public final class EntityCache<T> {
|
|||||||
if (rs == null) {
|
if (rs == null) {
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
synchronized (rs) {
|
tableLock.lock(); //表锁, 可优化成行锁
|
||||||
|
try {
|
||||||
return updateColumn(attr, rs, ColumnExpress.ORR, orvalue);
|
return updateColumn(attr, rs, ColumnExpress.ORR, orvalue);
|
||||||
|
} finally {
|
||||||
|
tableLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -922,8 +943,11 @@ public final class EntityCache<T> {
|
|||||||
if (rs == null) {
|
if (rs == null) {
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
synchronized (rs) {
|
tableLock.lock(); //表锁, 可优化成行锁
|
||||||
|
try {
|
||||||
return updateColumn(attr, rs, ColumnExpress.AND, andvalue);
|
return updateColumn(attr, rs, ColumnExpress.AND, andvalue);
|
||||||
|
} finally {
|
||||||
|
tableLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -935,8 +959,11 @@ public final class EntityCache<T> {
|
|||||||
if (rs == null) {
|
if (rs == null) {
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
synchronized (rs) {
|
tableLock.lock(); //表锁, 可优化成行锁
|
||||||
|
try {
|
||||||
return updateColumn(attr, rs, ColumnExpress.INC, incvalue);
|
return updateColumn(attr, rs, ColumnExpress.INC, incvalue);
|
||||||
|
} finally {
|
||||||
|
tableLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -948,8 +975,11 @@ public final class EntityCache<T> {
|
|||||||
if (rs == null) {
|
if (rs == null) {
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
synchronized (rs) {
|
tableLock.lock(); //表锁, 可优化成行锁
|
||||||
|
try {
|
||||||
return updateColumn(attr, rs, ColumnExpress.DEC, incvalue);
|
return updateColumn(attr, rs, ColumnExpress.DEC, incvalue);
|
||||||
|
} finally {
|
||||||
|
tableLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user