Skip to content

Commit 5b557bc

Browse files
Merge pull request #17 from yuriy-glotanov/master
improve performance of sql engine
2 parents 750109c + ca41411 commit 5b557bc

12 files changed

Lines changed: 58 additions & 43 deletions

src/main/java/su/interference/core/Frame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public synchronized ArrayList<Chunk> getFrameChunks (Session s) throws IOExcepti
527527
if (dataObject==null) {
528528
dataObject = Instance.getInstance().getTableById(this.objectId);
529529
}
530-
if (dataObject.isNoTran()) {
530+
if (dataObject.isNoTran() || s.isStream()) {
531531

532532
for (Chunk c : data.getChunks()) {
533533
if (c.getHeader().getState()==Header.RECORD_NORMAL_STATE) {

src/main/java/su/interference/proxy/POJOProxyFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public synchronized Class register (String name) throws ClassNotFoundException,
215215
sb.append(sparam);
216216
sb.append("; }\n");
217217
}
218-
sb.append(" if (tran==null||(tran!=null&&tran.getTransType()==su.interference.persistent.Transaction.TRAN_THR)||(tran!=null&&tran.getTransId() == ");
218+
sb.append(" if (session.isStream()||tran==null||(tran!=null&&tran.getTransType()==su.interference.persistent.Transaction.TRAN_THR)||(tran!=null&&tran.getTransId() == ");
219219
sb.append("session.getTransaction().getTransId())) {\n");
220220
sb.append(" return super.get");
221221
sb.append(f[i].getName().substring(0,1).toUpperCase());

src/main/java/su/interference/sql/FrameApiJoin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ public List<Object> getResult() {
116116

117117
public void setResult(List<Object> result) {
118118
this.result = result;
119+
}
120+
121+
public void setResultWithCountDown(List<Object> result) {
122+
this.result = result;
119123
latch.countDown();
120124
}
121125

src/main/java/su/interference/sql/FrameJoinTask.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public List<Object> call() throws Exception {
152152
if (hmap.skipCheckNC()) {
153153
res.add(j);
154154
} else {
155-
if (nc.checkNC(j, sqlcid, last)) {
155+
if (nc.checkNC(j, sqlcid, last, s)) {
156156
res.add(j);
157157
}
158158
}
@@ -193,7 +193,7 @@ public List<Object> call() throws Exception {
193193
if (hmap.skipCheckNC()) {
194194
res.add(j);
195195
} else {
196-
if (nc.checkNC(j, sqlcid, last)) {
196+
if (nc.checkNC(j, sqlcid, last, s)) {
197197
res.add(j);
198198
}
199199
}
@@ -211,7 +211,7 @@ public List<Object> call() throws Exception {
211211
if (hmap.skipCheckNC()) {
212212
res.add(j);
213213
} else {
214-
if (nc.checkNC(j, sqlcid, last)) {
214+
if (nc.checkNC(j, sqlcid, last, s)) {
215215
res.add(j);
216216
}
217217
}
@@ -221,12 +221,12 @@ public List<Object> call() throws Exception {
221221
//one table loop
222222
if (r == null) {
223223
//todo need to cast o1 to RS type
224-
//if (nc.checkNC(o1, sqlcid, last)) {
224+
if (nc.checkNC(o1, sqlcid, last, s)) {
225225
res.add(o1); //target table is null -> result class is null -> returns generic entities
226-
//}
226+
}
227227
} else {
228228
Object j = joinDataRecords(r, c1, c2, t1, t2, o1, null, cols, c1rs, s);
229-
if (nc.checkNC(j, sqlcid, last)) {
229+
if (nc.checkNC(j, sqlcid, last, s)) {
230230
res.add(j);
231231
}
232232
}
@@ -235,7 +235,7 @@ public List<Object> call() throws Exception {
235235
//nested loop
236236
for (Object o2 : drs2) {
237237
Object j = joinDataRecords(r, c1, c2, t1, t2, o1, o2, cols, c1rs, s);
238-
if (nc.checkNC(j, sqlcid, last)) {
238+
if (nc.checkNC(j, sqlcid, last, s)) {
239239
res.add(j);
240240
}
241241
}

src/main/java/su/interference/sql/NestedCondition.java

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ this software and associated documentation files (the "Software"), to deal in
2424

2525
package su.interference.sql;
2626

27+
import su.interference.core.EntityContainer;
2728
import su.interference.persistent.Session;
2829
import su.interference.persistent.Table;
2930
import su.interference.sqlexception.*;
3031
import su.interference.core.Types;
3132
import su.interference.exception.InternalException;
3233

3334
import java.io.IOException;
35+
import java.lang.reflect.Method;
3436
import java.util.*;
3537
import java.io.UnsupportedEncodingException;
3638
import java.lang.reflect.Field;
@@ -53,7 +55,10 @@ public class NestedCondition extends Condition {
5355
private int type; // 1 - AND, 2 - OR
5456
private boolean empty;
5557

56-
public boolean checkNC (Object o, int sqlcid, boolean last) throws UnsupportedEncodingException, InternalException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
58+
public boolean checkNC (Object o, int sqlcid, boolean last, Session s) throws UnsupportedEncodingException, InternalException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
59+
final Class cl = o.getClass();
60+
final Class cl_ = Arrays.asList(o.getClass().getInterfaces()).contains(EntityContainer.class) ? o.getClass().getSuperclass() : o.getClass();
61+
final boolean rs_ = !Arrays.asList(o.getClass().getInterfaces()).contains(EntityContainer.class);
5762
boolean res = false;
5863
if (empty) {
5964
return true;
@@ -65,20 +70,20 @@ public boolean checkNC (Object o, int sqlcid, boolean last) throws UnsupportedEn
6570
boolean cres = false;
6671
if (c.getClass().getName().equals("su.interference.sql.NestedCondition")) {
6772
final NestedCondition nc = (NestedCondition)c;
68-
cres = nc.checkNC(o, sqlcid, last);
73+
cres = nc.checkNC(o, sqlcid, last, s);
6974
}
7075
if (c.getClass().getName().equals("su.interference.sql.JoinCondition")) {
7176
JoinCondition jc = (JoinCondition)c;
7277
if ((jc.getId()==sqlcid)||(jc.getId()==0&&last)) {
73-
cres = sqlEquals(o, jc);
78+
cres = sqlEquals(cl, cl_, o, jc, rs_, s);
7479
} else {
7580
cres = true;
7681
}
7782
}
7883
if (c.getClass().getName().equals("su.interference.sql.ValueCondition")) {
7984
final ValueCondition vc = (ValueCondition)c;
8085
if ((vc.getId()==sqlcid)||(vc.getId()==0&&last)) {
81-
cres = sqlEquals(o, vc);
86+
cres = sqlEquals(cl, cl_, o, vc, rs_, s);
8287
} else {
8388
cres = true;
8489
}
@@ -93,28 +98,26 @@ public boolean checkNC (Object o, int sqlcid, boolean last) throws UnsupportedEn
9398
return res;
9499
}
95100

96-
public boolean sqlEquals (Object o, ValueCondition vc) throws UnsupportedEncodingException, InternalException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
97-
final Class c = o.getClass();
101+
private boolean sqlEquals (Class c, Class c_, Object o, ValueCondition vc, boolean rs_, Session s) throws UnsupportedEncodingException, InternalException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
98102
boolean res = false;
99103

100-
for (int j=0; j<c.getDeclaredFields().length; j++) {
101-
final Field f = c.getDeclaredFields()[j];
104+
for (int j=0; j<c_.getDeclaredFields().length; j++) {
105+
final Field f = c_.getDeclaredFields()[j];
102106
if (f.getName().equals(vc.getConditionColumn().getAlias())) {
103-
res = sqlEquals(o, f, null, vc.getValues(), vc.getCondition());
107+
res = sqlEquals(c, o, f, null, vc.getValues(), vc.getCondition(), rs_, s);
104108
}
105109
}
106110

107111
return res;
108112
}
109113

110-
public boolean sqlEquals (Object o, JoinCondition jc) throws InternalException, UnsupportedEncodingException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
111-
final Class c = o.getClass();
114+
private boolean sqlEquals (Class c, Class c_, Object o, JoinCondition jc, boolean rs_, Session s) throws InternalException, UnsupportedEncodingException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
112115
boolean res = false;
113116

114117
Field lf = null;
115118
Field rf = null;
116-
for (int j=0; j<c.getDeclaredFields().length; j++) {
117-
Field f = c.getDeclaredFields()[j];
119+
for (int j=0; j<c_.getDeclaredFields().length; j++) {
120+
Field f = c_.getDeclaredFields()[j];
118121
if (f.getName().equals(jc.getConditionColumn().getAlias())) {
119122
lf = f;
120123
}
@@ -123,16 +126,15 @@ public boolean sqlEquals (Object o, JoinCondition jc) throws InternalException,
123126
}
124127
}
125128
if (lf!=null&&rf!=null) {
126-
res = sqlEquals(o, lf, rf, null, jc.getCondition());
129+
res = sqlEquals(c, o, lf, rf, null, jc.getCondition(), rs_, s);
127130
}
128131

129132
return res;
130133

131134
}
132135

133136
//rf used for join condition, ro - for value condition
134-
public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype) throws InternalException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
135-
final Class c = o.getClass();
137+
private boolean sqlEquals (Class c, Object o, Field lf, Field rf, Object[] ro, int ctype, boolean rs_, Session s) throws InternalException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
136138
final String t1 = lf.getType().getName();
137139
final String t2 = rf==null?t1:rf.getType().getName();
138140

@@ -141,14 +143,14 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
141143
}
142144

143145
if (t1.equals(Types.t_string)&&t2.equals(Types.t_string)) {
144-
final String d1 = (String)c.getMethod("get"+lf.getName().substring(0,1).toUpperCase()+lf.getName().substring(1,lf.getName().length()), null).invoke(o, null);
146+
final String d1 = (String)invoke(c, o, lf.getName(), rs_, s);
145147
final String[] d2 = new String[rf==null?ro.length:1];
146148
if (rf==null) {
147149
for (int i=0;i<ro.length;i++) {
148150
d2[i] = (String)ro[i];
149151
}
150152
} else {
151-
d2[0] = (String)c.getMethod("get"+rf.getName().substring(0,1).toUpperCase()+rf.getName().substring(1,rf.getName().length()), null).invoke(o,null);
153+
d2[0] = (String)invoke(c, o, rf.getName(), rs_, s);
152154
}
153155
if ((ctype==Condition.C_EQUAL)||(ctype==Condition.C_IN)) {
154156
for (int i=0; i<d2.length; i++) {
@@ -184,14 +186,14 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
184186
}
185187
}
186188
if (t1.equals(Types.t_date)&&t2.equals(Types.t_date)) {
187-
final Date d1 = (Date)c.getMethod("get"+lf.getName().substring(0,1).toUpperCase()+lf.getName().substring(1,lf.getName().length()), null).invoke(o, null);
189+
final Date d1 = (Date)invoke(c, o, lf.getName(), rs_, s);
188190
final Date[] d2 = new Date[rf==null?ro.length:1];
189191
if (rf==null) {
190192
for (int i=0;i<ro.length;i++) {
191193
d2[i] = (Date)ro[i];
192194
}
193195
} else {
194-
d2[0] = (Date)c.getMethod("get"+rf.getName().substring(0,1).toUpperCase()+rf.getName().substring(1,rf.getName().length()), null).invoke(o,null);
196+
d2[0] = (Date)invoke(c, o, rf.getName(), rs_, s);
195197
}
196198
if ((ctype==Condition.C_EQUAL)||(ctype==Condition.C_IN)) {
197199
for (int i=0; i<d2.length; i++) {
@@ -215,14 +217,14 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
215217
Long d1 = new Long(0);
216218
Long[] d2 = new Long[rf==null?ro.length:1];
217219
if ((t1.equals(Types.p_int)||t1.equals(Types.t_int)||t1.equals(Types.c_int))&&(t2.equals(Types.p_int)||t2.equals(Types.t_int)||t2.equals(Types.c_int))) {
218-
Integer dd = (Integer)c.getMethod("get"+lf.getName().substring(0,1).toUpperCase()+lf.getName().substring(1,lf.getName().length()), null).invoke(o,null);
220+
Integer dd = (Integer)invoke(c, o, lf.getName(), rs_, s);
219221
d1 = new Long(dd);
220222
if (rf==null) {
221223
for (int i=0;i<ro.length;i++) {
222224
d2[i] = new Long((Integer)ro[i]);
223225
}
224226
} else {
225-
Integer dr = (Integer)c.getMethod("get"+rf.getName().substring(0,1).toUpperCase()+rf.getName().substring(1,rf.getName().length()), null).invoke(o,null);
227+
Integer dr = (Integer)invoke(c, o, rf.getName(), rs_, s);
226228
d2[0] = new Long(dr);
227229
}
228230
}
@@ -284,21 +286,21 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
284286
Double d1 = new Double(0);
285287
Double[] d2 = new Double[rf==null?ro.length:1];
286288
if ((t1.equals(Types.p_float)||t1.equals(Types.t_float))&&(t2.equals(Types.p_float)||t2.equals(Types.t_float))) {
287-
Float dd = (Float)c.getMethod("get"+lf.getName().substring(0,1).toUpperCase()+lf.getName().substring(1,lf.getName().length()), null).invoke(o,null);
289+
Float dd = (Float)invoke(c, o, lf.getName(), rs_, s);
288290
d1 = new Double(dd);
289291
if (rf==null) {
290292
for (int i=0;i<ro.length;i++) {
291293
//RO object created by ValueCondition constructor, and already cast to 8-byte type (Double)
292294
d2[i] = (Double)ro[i];
293295
}
294296
} else {
295-
Float dr = (Float)c.getMethod("get"+rf.getName().substring(0,1).toUpperCase()+rf.getName().substring(1,rf.getName().length()), null).invoke(o,null);
297+
Float dr = (Float)invoke(c, o, rf.getName(), rs_, s);
296298
d2[0] = new Double(dr);
297299
}
298300
}
299301
if ((t1.equals(Types.p_double)||t1.equals(Types.t_double))&&(t2.equals(Types.p_double)||t2.equals(Types.t_double))) {
300-
d1 = (Double)c.getMethod("get"+lf.getName().substring(0,1).toUpperCase()+lf.getName().substring(1,lf.getName().length()), null).invoke(o, null);
301-
d2 = rf==null?(Double[])ro:new Double[]{(Double)c.getMethod("get"+rf.getName().substring(0,1).toUpperCase()+rf.getName().substring(1,rf.getName().length()), null).invoke(o,null)};
302+
d1 = (Double)invoke(c, o, lf.getName(), rs_, s);
303+
d2 = rf==null?(Double[])ro:new Double[]{(Double)invoke(c, o, rf.getName(), rs_, s)};
302304
}
303305
if ((ctype==Condition.C_EQUAL)||(ctype==Condition.C_IN)) {
304306
for (int i=0; i<d2.length; i++) {
@@ -353,6 +355,12 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
353355
return false;
354356
}
355357

358+
private Object invoke(Class c, Object o, String fname, boolean rs_, Session s) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
359+
final String mtname = "get"+fname.substring(0,1).toUpperCase()+fname.substring(1,fname.length());
360+
final Method m = rs_ ? c.getMethod(mtname, null) : c.getMethod(mtname, Session.class);
361+
return rs_ ? m.invoke(o, null) : m.invoke(o, s);
362+
}
363+
356364
public NestedCondition (String cdd, SQLStatement sql, ArrayList<SQLTable> tables) throws Exception {
357365
int nl = 0;
358366
String cc = "";

src/main/java/su/interference/sql/RemoteTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public Boolean call() throws Exception {
6666
}
6767
throw new InternalException();
6868
}
69-
Metrics.get("recordRCount").put(rs.size());
7069
for (FrameApiJoin j : rs) {
71-
joins.get(j.getKey()).setResult(j.getResult());
70+
joins.get(j.getKey()).setResultWithCountDown(j.getResult());
71+
Metrics.get("recordRCount").put(j.getResult().size());
7272
}
7373
} else {
7474
for (Map.Entry<String, FrameApiJoin> entry : joins.entrySet()) {

src/main/java/su/interference/sql/SQLCursor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ public SQLCursor (int id, FrameIterator lbi, FrameIterator rbi, NestedCondition
105105
target = this.peristent ? s.registerTable(cur.getResultTargetName(), s, rscols, null, null, ixflag && last) : new ResultList(cur.getSqlStmt().getEntityTable());
106106
} else if (cur.getType() == Cursor.STREAM_TYPE) {
107107
target = new StreamQueue();
108-
} else
109-
{
108+
} else {
110109
target = this.peristent ? s.registerTable("su.interference.persistent.R$" + UUID.randomUUID().toString().replace('-', '$'), s, rscols, null, null, ixflag && last) : new ResultList(cur.getSqlStmt().getEntityTable());
111110
}
112111
current = new FrameHolder(target);

src/main/java/su/interference/sql/ValueCondition.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ public ValueCondition (SQLColumn cc, int c, String value, NestedCondition nc)
126126
} else { //integer
127127
String ct = cc.getColumn().getType().getName();
128128
if (ct.equals(Types.t_int)||ct.equals(Types.p_int)||ct.equals(Types.c_int)) {
129-
this.values = new Object[1];
130-
this.values[0] = Integer.parseInt(val);
129+
this.values[i] = Integer.parseInt(val);
131130
} else if (ct.equals(Types.t_long)||ct.equals(Types.p_long)||ct.equals(Types.c_long)) {
132-
this.values = new Object[1];
133-
this.values[0] = Long.parseLong(val);
131+
this.values[i] = Long.parseLong(val);
134132
} else {
135133
throw new InvalidConditionValue();
136134
}

src/main/java/su/interference/transport/CommandEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ this software and associated documentation files (the "Software"), to deal in
3535

3636
public class CommandEvent extends TransportEventImpl implements PersistentEvent {
3737

38+
private final static long serialVersionUID = 4363987969365366565L;
3839
public static final int COMMIT = 1;
3940
public static final int ROLLBACK = 2;
4041
public static final int MAX_COMMAND = 10;

src/main/java/su/interference/transport/MgmtEvent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ this software and associated documentation files (the "Software"), to deal in
3333
*/
3434

3535
public class MgmtEvent extends TransportEventImpl {
36+
37+
private final static long serialVersionUID = 436398796431755011L;
3638
private final static Logger logger = LoggerFactory.getLogger(MgmtEvent.class);
3739
public final static int MGMT_STARTUP = 1;
3840
public final static int MGMT_SHUTDOWN = 2;

0 commit comments

Comments
 (0)