Skip to content

Commit 58c1e74

Browse files
Merge pull request #19 from yuriy-glotanov/master
pre-release 2020.1 beta with CEP implementation
2 parents f157ec1 + 06570b7 commit 58c1e74

26 files changed

Lines changed: 746 additions & 131 deletions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public interface Chunk extends Comparable {
4343
byte[] getChunk();
4444
void setChunk(byte[] chunk);
4545
int getBytesAmount();
46+
Comparable getId (Session s) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException;
4647
ValueSet getDcs();
4748
Object getEntity() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException;
4849
Object getUndoEntity() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2010-2019 head systems, ltd
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
23+
*/
24+
25+
package su.interference.core;
26+
27+
import su.interference.persistent.Session;
28+
import java.util.Comparator;
29+
30+
/**
31+
* @author Yuriy Glotanov
32+
* @since 1.0
33+
*/
34+
35+
public class ChunkIdComparator implements Comparator<Chunk> {
36+
private Session s;
37+
38+
public ChunkIdComparator(Session s) {
39+
this.s = s;
40+
}
41+
42+
public int compare(Chunk c1, Chunk c2) {
43+
try {
44+
return c1.getId(s).compareTo(c2.getId(s));
45+
} catch (Exception e) {
46+
throw new RuntimeException(e);
47+
}
48+
}
49+
50+
}

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

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public class DataChunk implements Chunk {
5757
//cache-dependency parameters
5858
private volatile byte[] chunk;
5959
private volatile ValueSet dcs; //datacolumn set
60-
private byte[] id;
60+
private Comparable id;
61+
private byte[] serializedId;
6162
private Object entity;
6263
private Object undoentity;
6364
private DataChunk source;
@@ -111,8 +112,8 @@ public DataObject getT() {
111112
return t;
112113
}
113114

114-
public byte[] getId (Session s) throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedEncodingException, InternalException {
115-
if (id==null) {
115+
public Comparable getId (Session s) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
116+
if (serializedId==null) {
116117
if (entity==null) {
117118
getEntity();
118119
}
@@ -130,17 +131,50 @@ public byte[] getId (Session s) throws InvocationTargetException, NoSuchMethodEx
130131
if (sa!=null) {
131132
Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
132133
Object v = z.invoke(entity, null);
133-
id = sr.serialize(f[i].getType().getName(), v);
134+
id = (Comparable) v;
134135
} else {
135136
Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), new Class<?>[]{Session.class});
136137
Object v = z.invoke(entity, new Object[]{s});
137-
id = sr.serialize(f[i].getType().getName(), v);
138+
id = (Comparable) v;
138139
}
139140
}
140141
}
141142
}
142143
return id;
143144
}
145+
146+
public byte[] getSerializedId (Session s) throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedEncodingException, InternalException {
147+
if (serializedId==null) {
148+
if (entity==null) {
149+
getEntity();
150+
}
151+
Class c = entity.getClass();
152+
final TransEntity ta = (TransEntity)c.getAnnotation(TransEntity.class);
153+
final SystemEntity sa = (SystemEntity)c.getAnnotation(SystemEntity.class);
154+
if (ta!=null) {
155+
//for Transactional Wrapper Entity we must get superclass (original Entity class)
156+
c = c.getSuperclass();
157+
}
158+
Field[] f = c.getDeclaredFields();
159+
for (int i=0; i<f.length; i++) {
160+
final Id a = f[i].getAnnotation(Id.class);
161+
if (a!=null) {
162+
if (sa!=null) {
163+
Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
164+
Object v = z.invoke(entity, null);
165+
id = (Comparable) v;
166+
serializedId = sr.serialize(f[i].getType().getName(), v);
167+
} else {
168+
Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), new Class<?>[]{Session.class});
169+
Object v = z.invoke(entity, new Object[]{s});
170+
id = (Comparable) v;
171+
serializedId = sr.serialize(f[i].getType().getName(), v);
172+
}
173+
}
174+
}
175+
}
176+
return serializedId;
177+
}
144178

145179
public int getBytesAmount() {
146180
return this.getChunk().length+this.getHeader().getHeader().length;
@@ -243,11 +277,13 @@ public DataChunk (Object o, Session s, RowId r) throws IOException, InvocationTa
243277
if (sa!=null) {
244278
final Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
245279
final Object v = z.invoke(o, null);
246-
id = sr.serialize(f[i].getType().getName(), v);
280+
id = (Comparable) v;
281+
serializedId = sr.serialize(f[i].getType().getName(), v);
247282
} else {
248283
final Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), new Class<?>[]{Session.class});
249284
final Object v = z.invoke(o, new Object[]{s});
250-
id = sr.serialize(f[i].getType().getName(), v);
285+
id = (Comparable) v;
286+
serializedId = sr.serialize(f[i].getType().getName(), v);
251287
}
252288
}
253289
}
@@ -322,13 +358,16 @@ public DataChunk (byte[] b, DataObject t, RowHeader h, DataChunk source) throws
322358
//All var length types is non-primitive
323359
final byte[] data = bs.substring(s+v, s+v+(Types.isVarType(cs[i])?bs.getIntFromBytes(bs.substring(s,s+v)):Types.getLength(cs[i])));
324360
if (a!=null) {
325-
id = data;
361+
serializedId = data;
326362
}
327363
try {
328364
if (Modifier.isPrivate(m)) {
329365
cs[i].setAccessible(true);
330366
}
331367
dcs.getValueSet()[i] = sr.deserialize(data, cs[i]);
368+
if (a!=null) {
369+
id = (Comparable) dcs.getValueSet()[i];
370+
}
332371
} catch (UnsupportedEncodingException e) {
333372
dcs.getValueSet()[i] = "UnsupportedEncodingException";
334373
}
@@ -353,13 +392,16 @@ public DataChunk (String h, FrameData bd) throws ClassNotFoundException, Instant
353392
//All var length types is non-primitive
354393
final byte[] data = bs.substring(s+v, s+v+(Types.isVarType(cs[i])?bs.getIntFromBytes(bs.substring(s,s+v)):Types.getLength(cs[i])));
355394
if (a!=null) {
356-
id = data;
395+
serializedId = data;
357396
}
358397
try {
359398
if (Modifier.isPrivate(m)) {
360399
cs[i].setAccessible(true);
361400
}
362401
dcs.getValueSet()[i] = sr.deserialize(data, cs[i]);
402+
if (a!=null) {
403+
id = (Comparable) dcs.getValueSet()[i];
404+
}
363405
} catch (UnsupportedEncodingException e) {
364406
dcs.getValueSet()[i] = "UnsupportedEncodingException";
365407
}
@@ -387,13 +429,16 @@ public DataChunk (String h, Table t, Transaction tx) throws ClassNotFoundExcepti
387429
//All var length types is non-primitive
388430
final byte[] data = bs.substring(s+v, s+v+(Types.isVarType(cs[i])?bs.getIntFromBytes(bs.substring(s,s+v)):Types.getLength(cs[i])));
389431
if (a!=null) {
390-
id = data;
432+
serializedId = data;
391433
}
392434
try {
393435
if (Modifier.isPrivate(m)) {
394436
cs[i].setAccessible(true);
395437
}
396438
dcs.getValueSet()[i] = sr.deserialize(data, cs[i]);
439+
if (a!=null) {
440+
id = (Comparable) dcs.getValueSet()[i];
441+
}
397442
} catch (UnsupportedEncodingException e) {
398443
dcs.getValueSet()[i] = "UnsupportedEncodingException";
399444
}
@@ -450,11 +495,13 @@ public byte[] flush (Session s) throws IOException, InvocationTargetException, N
450495
if (sa!=null) {
451496
final Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
452497
final Object v = z.invoke(this.entity, null);
453-
id = sr.serialize(f[i].getType().getName(), v);
498+
id = (Comparable) v;
499+
serializedId = sr.serialize(f[i].getType().getName(), v);
454500
} else {
455501
final Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), new Class<?>[]{Session.class});
456502
final Object v = z.invoke(this.entity, new Object[]{s});
457-
id = sr.serialize(f[i].getType().getName(), v);
503+
id = (Comparable) v;
504+
serializedId = sr.serialize(f[i].getType().getName(), v);
458505
}
459506
}
460507
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ this software and associated documentation files (the "Software"), to deal in
3131
import org.slf4j.LoggerFactory;
3232
import su.interference.persistent.*;
3333
import su.interference.exception.*;
34+
import su.interference.sql.ContainerFrame;
35+
import su.interference.sql.FrameApi;
3436
import su.interference.sql.SQLCursor;
3537
import su.interference.transport.TransportSyncTask;
3638

@@ -60,14 +62,20 @@ private synchronized boolean syncFramesFromQueue() throws Exception {
6062
final int famt = Storage.getStorage().getFiles()==null?0:Storage.getStorage().getFiles().size();
6163
logger.debug("sync procedure was started with frames amount="+LLT.getFrames().size());
6264

63-
final ArrayList<SyncFrame> frames = new ArrayList<SyncFrame>();
64-
final ArrayList<FreeFrame> fframes = new ArrayList<FreeFrame>();
65+
final ArrayList<SyncFrame> frames = new ArrayList<>();
66+
final Map<Integer, List<FrameApi>> frames_ = new HashMap<>();
67+
final ArrayList<FreeFrame> fframes = new ArrayList<>();
6568
final Session s = Session.getDntmSession();
66-
for (Map.Entry entry : LLT.getFrames().entrySet()) {
69+
70+
for (Map.Entry<Long, Frame> entry : LLT.getFrames().entrySet()) {
6771
FreeFrame fb = null;
6872
try {
69-
frames.add(new SyncFrame((Frame) entry.getValue(), s, fb));
70-
SQLCursor.addStreamFrame(((Frame) entry.getValue()).getFrameData());
73+
final Frame f = entry.getValue();
74+
frames.add(new SyncFrame(f, s, fb));
75+
if (frames_.get(f.getObjectId()) == null) {
76+
frames_.put(f.getObjectId(), new ArrayList<>());
77+
}
78+
frames_.get(f.getObjectId()).add(f.getFrameData());
7179
} catch (MissingSyncFrameException e) {
7280
logger.debug("Unable to sync frame "+((Frame) entry.getValue()).getPtr()+" because removed by freeing");
7381
}
@@ -76,6 +84,10 @@ private synchronized boolean syncFramesFromQueue() throws Exception {
7684
}
7785
}
7886

87+
for (Map.Entry<Integer, List<FrameApi>> entry: frames_.entrySet()) {
88+
SQLCursor.addStreamFrame(new ContainerFrame(entry.getKey(), entry.getValue()));
89+
}
90+
7991
SyncTask[] tasklist = new SyncTask[famt];
8092

8193
int cnt = 0;

src/main/java/su/interference/persistent/FrameData.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,20 @@ public IndexFrame getIndexFrame() throws IOException, ClassNotFoundException, In
194194
return (IndexFrame) frame;
195195
}
196196

197-
public synchronized ArrayList<Object> getFrameEntities(Session s)
198-
throws IOException, ClassNotFoundException, InternalException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
199-
final ArrayList<Object> res = new ArrayList<Object>();
197+
public synchronized ArrayList<Chunk> getFrameChunks(Session s)
198+
throws IOException, ClassNotFoundException, InternalException, IllegalAccessException, InstantiationException {
200199
if (getDataObject().isIndex()) {
201-
for (Chunk c : getIndexFrame().getFrameChunks(s)) {
202-
res.add(((DataChunk)c).getEntity());
203-
}
200+
return getIndexFrame().getFrameChunks(s);
204201
} else {
205-
for (Chunk c : getDataFrame().getFrameChunks(s)) {
206-
res.add(((DataChunk)c).getEntity());
207-
}
202+
return getDataFrame().getFrameChunks(s);
203+
}
204+
}
205+
206+
public synchronized ArrayList<Object> getFrameEntities(Session s)
207+
throws IOException, ClassNotFoundException, InternalException, IllegalAccessException, InstantiationException {
208+
final ArrayList<Object> res = new ArrayList<>();
209+
for (Chunk c : getFrameChunks(s)) {
210+
res.add(((DataChunk)c).getEntity());
208211
}
209212
return res;
210213
}

src/main/java/su/interference/persistent/Session.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public Table registerTable (String n, Session s) throws Exception {
176176
return registerTable (n, s, null, null, null, false);
177177
}
178178

179-
public Table registerTable (String n, Session s, ArrayList<SQLColumn> cols, java.lang.reflect.Field[] flds, Table pt, boolean ixflag) throws Exception {
179+
public Table registerTable (String n, Session s, List<SQLColumn> cols, java.lang.reflect.Field[] flds, Table pt, boolean ixflag) throws Exception {
180180
final ClassLoader cl = this.getClass().getClassLoader();
181181
final POJOProxyFactory ppf = POJOProxyFactory.getInstance();
182182
final RSProxyFactory rpf = RSProxyFactory.getInstance();

src/main/java/su/interference/persistent/Table.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ public DataChunk getChunkByEntity (Object o, Session s) throws IOException, Invo
14001400
final List<FrameData> bds = Instance.getInstance().getTableById(this.getObjectId()).getFrames();
14011401
for (FrameData b : bds) {
14021402
for (Chunk dc : b.getDataFrame().getFrameChunks(s)) {
1403-
if (Arrays.equals(id, ((DataChunk)dc).getId(s))) {
1403+
if (Arrays.equals(id, ((DataChunk)dc).getSerializedId(s))) {
14041404
return (DataChunk)dc;
14051405
}
14061406
}
@@ -1430,7 +1430,7 @@ public DataChunk getChunkByEntity (Object o, Session s) throws IOException, Invo
14301430
final List<FrameData> bds = Instance.getInstance().getTableById(this.getObjectId()).getFrames();
14311431
for (FrameData b : bds) {
14321432
for (Chunk dc : b.getDataFrame().getFrameChunks(s)) {
1433-
if (Arrays.equals(id, ((DataChunk) dc).getId(s))) {
1433+
if (Arrays.equals(id, ((DataChunk) dc).getSerializedId(s))) {
14341434
return (DataChunk) dc;
14351435
}
14361436
}
@@ -1476,7 +1476,7 @@ public DataChunk getChunkById (long id, Session s) throws IOException, Invocatio
14761476
List<FrameData> bds = Instance.getInstance().getTableById(this.getObjectId()).getFrames();
14771477
for (FrameData b : bds) {
14781478
for (Chunk c : b.getDataFrame().getFrameChunks(s)) {
1479-
if (Arrays.equals(iid, ((DataChunk) c).getId(s))) {
1479+
if (Arrays.equals(iid, ((DataChunk) c).getSerializedId(s))) {
14801480
return (DataChunk) c;
14811481
}
14821482
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ this software and associated documentation files (the "Software"), to deal in
2424

2525
package su.interference.proxy;
2626

27+
import su.interference.core.DataChunk;
28+
import su.interference.persistent.Session;
29+
2730
import java.lang.reflect.InvocationTargetException;
2831
import java.lang.reflect.Method;
2932

@@ -34,9 +37,37 @@ this software and associated documentation files (the "Software"), to deal in
3437

3538
public class GenericResultImpl implements GenericResult {
3639

40+
private DataChunk dataChunk;
41+
42+
public DataChunk getDataChunk(Session s) throws Exception {
43+
if (dataChunk == null) {
44+
dataChunk = new DataChunk(this, s);
45+
}
46+
return dataChunk;
47+
}
48+
3749
public Object getValueByName(String name) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
3850
Method m = this.getClass().getMethod("get"+name.substring(0,1).toUpperCase()+name.substring(1,name.length()), null);
3951
return m.invoke(this, null);
4052
}
4153

54+
@Override
55+
public String toString() {
56+
final Method[] ms = this.getClass().getMethods();
57+
final StringBuffer sb = new StringBuffer();
58+
try {
59+
for (Method m : ms) {
60+
if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
61+
sb.append(m.getName().substring(3).substring(0, 1).toLowerCase());
62+
sb.append(m.getName().substring(3).substring(1));
63+
sb.append(":");
64+
sb.append(m.invoke(this, null));
65+
}
66+
}
67+
} catch (Exception e) {
68+
e.printStackTrace();
69+
}
70+
return sb.toString();
71+
}
72+
4273
}

0 commit comments

Comments
 (0)