Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/su/interference/core/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public interface Chunk extends Comparable {
byte[] getChunk();
void setChunk(byte[] chunk);
int getBytesAmount();
Comparable getId (Session s) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException;
ValueSet getDcs();
Object getEntity() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException;
Object getUndoEntity() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException;
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/su/interference/core/ChunkIdComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
The MIT License (MIT)

Copyright (c) 2010-2019 head systems, ltd

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

package su.interference.core;

import su.interference.persistent.Session;
import java.util.Comparator;

/**
* @author Yuriy Glotanov
* @since 1.0
*/

public class ChunkIdComparator implements Comparator<Chunk> {
private Session s;

public ChunkIdComparator(Session s) {
this.s = s;
}

public int compare(Chunk c1, Chunk c2) {
try {
return c1.getId(s).compareTo(c2.getId(s));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
71 changes: 59 additions & 12 deletions src/main/java/su/interference/core/DataChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public class DataChunk implements Chunk {
//cache-dependency parameters
private volatile byte[] chunk;
private volatile ValueSet dcs; //datacolumn set
private byte[] id;
private Comparable id;
private byte[] serializedId;
private Object entity;
private Object undoentity;
private DataChunk source;
Expand Down Expand Up @@ -111,8 +112,8 @@ public DataObject getT() {
return t;
}

public byte[] getId (Session s) throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedEncodingException, InternalException {
if (id==null) {
public Comparable getId (Session s) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
if (serializedId==null) {
if (entity==null) {
getEntity();
}
Expand All @@ -130,17 +131,50 @@ public byte[] getId (Session s) throws InvocationTargetException, NoSuchMethodEx
if (sa!=null) {
Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
Object v = z.invoke(entity, null);
id = sr.serialize(f[i].getType().getName(), v);
id = (Comparable) v;
} else {
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});
Object v = z.invoke(entity, new Object[]{s});
id = sr.serialize(f[i].getType().getName(), v);
id = (Comparable) v;
}
}
}
}
return id;
}

public byte[] getSerializedId (Session s) throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedEncodingException, InternalException {
if (serializedId==null) {
if (entity==null) {
getEntity();
}
Class c = entity.getClass();
final TransEntity ta = (TransEntity)c.getAnnotation(TransEntity.class);
final SystemEntity sa = (SystemEntity)c.getAnnotation(SystemEntity.class);
if (ta!=null) {
//for Transactional Wrapper Entity we must get superclass (original Entity class)
c = c.getSuperclass();
}
Field[] f = c.getDeclaredFields();
for (int i=0; i<f.length; i++) {
final Id a = f[i].getAnnotation(Id.class);
if (a!=null) {
if (sa!=null) {
Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
Object v = z.invoke(entity, null);
id = (Comparable) v;
serializedId = sr.serialize(f[i].getType().getName(), v);
} else {
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});
Object v = z.invoke(entity, new Object[]{s});
id = (Comparable) v;
serializedId = sr.serialize(f[i].getType().getName(), v);
}
}
}
}
return serializedId;
}

public int getBytesAmount() {
return this.getChunk().length+this.getHeader().getHeader().length;
Expand Down Expand Up @@ -243,11 +277,13 @@ public DataChunk (Object o, Session s, RowId r) throws IOException, InvocationTa
if (sa!=null) {
final Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
final Object v = z.invoke(o, null);
id = sr.serialize(f[i].getType().getName(), v);
id = (Comparable) v;
serializedId = sr.serialize(f[i].getType().getName(), v);
} else {
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});
final Object v = z.invoke(o, new Object[]{s});
id = sr.serialize(f[i].getType().getName(), v);
id = (Comparable) v;
serializedId = sr.serialize(f[i].getType().getName(), v);
}
}
}
Expand Down Expand Up @@ -322,13 +358,16 @@ public DataChunk (byte[] b, DataObject t, RowHeader h, DataChunk source) throws
//All var length types is non-primitive
final byte[] data = bs.substring(s+v, s+v+(Types.isVarType(cs[i])?bs.getIntFromBytes(bs.substring(s,s+v)):Types.getLength(cs[i])));
if (a!=null) {
id = data;
serializedId = data;
}
try {
if (Modifier.isPrivate(m)) {
cs[i].setAccessible(true);
}
dcs.getValueSet()[i] = sr.deserialize(data, cs[i]);
if (a!=null) {
id = (Comparable) dcs.getValueSet()[i];
}
} catch (UnsupportedEncodingException e) {
dcs.getValueSet()[i] = "UnsupportedEncodingException";
}
Expand All @@ -353,13 +392,16 @@ public DataChunk (String h, FrameData bd) throws ClassNotFoundException, Instant
//All var length types is non-primitive
final byte[] data = bs.substring(s+v, s+v+(Types.isVarType(cs[i])?bs.getIntFromBytes(bs.substring(s,s+v)):Types.getLength(cs[i])));
if (a!=null) {
id = data;
serializedId = data;
}
try {
if (Modifier.isPrivate(m)) {
cs[i].setAccessible(true);
}
dcs.getValueSet()[i] = sr.deserialize(data, cs[i]);
if (a!=null) {
id = (Comparable) dcs.getValueSet()[i];
}
} catch (UnsupportedEncodingException e) {
dcs.getValueSet()[i] = "UnsupportedEncodingException";
}
Expand Down Expand Up @@ -387,13 +429,16 @@ public DataChunk (String h, Table t, Transaction tx) throws ClassNotFoundExcepti
//All var length types is non-primitive
final byte[] data = bs.substring(s+v, s+v+(Types.isVarType(cs[i])?bs.getIntFromBytes(bs.substring(s,s+v)):Types.getLength(cs[i])));
if (a!=null) {
id = data;
serializedId = data;
}
try {
if (Modifier.isPrivate(m)) {
cs[i].setAccessible(true);
}
dcs.getValueSet()[i] = sr.deserialize(data, cs[i]);
if (a!=null) {
id = (Comparable) dcs.getValueSet()[i];
}
} catch (UnsupportedEncodingException e) {
dcs.getValueSet()[i] = "UnsupportedEncodingException";
}
Expand Down Expand Up @@ -450,11 +495,13 @@ public byte[] flush (Session s) throws IOException, InvocationTargetException, N
if (sa!=null) {
final Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
final Object v = z.invoke(this.entity, null);
id = sr.serialize(f[i].getType().getName(), v);
id = (Comparable) v;
serializedId = sr.serialize(f[i].getType().getName(), v);
} else {
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});
final Object v = z.invoke(this.entity, new Object[]{s});
id = sr.serialize(f[i].getType().getName(), v);
id = (Comparable) v;
serializedId = sr.serialize(f[i].getType().getName(), v);
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/su/interference/core/SyncQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ this software and associated documentation files (the "Software"), to deal in
import org.slf4j.LoggerFactory;
import su.interference.persistent.*;
import su.interference.exception.*;
import su.interference.sql.ContainerFrame;
import su.interference.sql.FrameApi;
import su.interference.sql.SQLCursor;
import su.interference.transport.TransportSyncTask;

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

final ArrayList<SyncFrame> frames = new ArrayList<SyncFrame>();
final ArrayList<FreeFrame> fframes = new ArrayList<FreeFrame>();
final ArrayList<SyncFrame> frames = new ArrayList<>();
final Map<Integer, List<FrameApi>> frames_ = new HashMap<>();
final ArrayList<FreeFrame> fframes = new ArrayList<>();
final Session s = Session.getDntmSession();
for (Map.Entry entry : LLT.getFrames().entrySet()) {

for (Map.Entry<Long, Frame> entry : LLT.getFrames().entrySet()) {
FreeFrame fb = null;
try {
frames.add(new SyncFrame((Frame) entry.getValue(), s, fb));
SQLCursor.addStreamFrame(((Frame) entry.getValue()).getFrameData());
final Frame f = entry.getValue();
frames.add(new SyncFrame(f, s, fb));
if (frames_.get(f.getObjectId()) == null) {
frames_.put(f.getObjectId(), new ArrayList<>());
}
frames_.get(f.getObjectId()).add(f.getFrameData());
} catch (MissingSyncFrameException e) {
logger.debug("Unable to sync frame "+((Frame) entry.getValue()).getPtr()+" because removed by freeing");
}
Expand All @@ -76,6 +84,10 @@ private synchronized boolean syncFramesFromQueue() throws Exception {
}
}

for (Map.Entry<Integer, List<FrameApi>> entry: frames_.entrySet()) {
SQLCursor.addStreamFrame(new ContainerFrame(entry.getKey(), entry.getValue()));
}

SyncTask[] tasklist = new SyncTask[famt];

int cnt = 0;
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/su/interference/persistent/FrameData.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,20 @@ public IndexFrame getIndexFrame() throws IOException, ClassNotFoundException, In
return (IndexFrame) frame;
}

public synchronized ArrayList<Object> getFrameEntities(Session s)
throws IOException, ClassNotFoundException, InternalException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
final ArrayList<Object> res = new ArrayList<Object>();
public synchronized ArrayList<Chunk> getFrameChunks(Session s)
throws IOException, ClassNotFoundException, InternalException, IllegalAccessException, InstantiationException {
if (getDataObject().isIndex()) {
for (Chunk c : getIndexFrame().getFrameChunks(s)) {
res.add(((DataChunk)c).getEntity());
}
return getIndexFrame().getFrameChunks(s);
} else {
for (Chunk c : getDataFrame().getFrameChunks(s)) {
res.add(((DataChunk)c).getEntity());
}
return getDataFrame().getFrameChunks(s);
}
}

public synchronized ArrayList<Object> getFrameEntities(Session s)
throws IOException, ClassNotFoundException, InternalException, IllegalAccessException, InstantiationException {
final ArrayList<Object> res = new ArrayList<>();
for (Chunk c : getFrameChunks(s)) {
res.add(((DataChunk)c).getEntity());
}
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/su/interference/persistent/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public Table registerTable (String n, Session s) throws Exception {
return registerTable (n, s, null, null, null, false);
}

public Table registerTable (String n, Session s, ArrayList<SQLColumn> cols, java.lang.reflect.Field[] flds, Table pt, boolean ixflag) throws Exception {
public Table registerTable (String n, Session s, List<SQLColumn> cols, java.lang.reflect.Field[] flds, Table pt, boolean ixflag) throws Exception {
final ClassLoader cl = this.getClass().getClassLoader();
final POJOProxyFactory ppf = POJOProxyFactory.getInstance();
final RSProxyFactory rpf = RSProxyFactory.getInstance();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/su/interference/persistent/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ public DataChunk getChunkByEntity (Object o, Session s) throws IOException, Invo
final List<FrameData> bds = Instance.getInstance().getTableById(this.getObjectId()).getFrames();
for (FrameData b : bds) {
for (Chunk dc : b.getDataFrame().getFrameChunks(s)) {
if (Arrays.equals(id, ((DataChunk)dc).getId(s))) {
if (Arrays.equals(id, ((DataChunk)dc).getSerializedId(s))) {
return (DataChunk)dc;
}
}
Expand Down Expand Up @@ -1430,7 +1430,7 @@ public DataChunk getChunkByEntity (Object o, Session s) throws IOException, Invo
final List<FrameData> bds = Instance.getInstance().getTableById(this.getObjectId()).getFrames();
for (FrameData b : bds) {
for (Chunk dc : b.getDataFrame().getFrameChunks(s)) {
if (Arrays.equals(id, ((DataChunk) dc).getId(s))) {
if (Arrays.equals(id, ((DataChunk) dc).getSerializedId(s))) {
return (DataChunk) dc;
}
}
Expand Down Expand Up @@ -1476,7 +1476,7 @@ public DataChunk getChunkById (long id, Session s) throws IOException, Invocatio
List<FrameData> bds = Instance.getInstance().getTableById(this.getObjectId()).getFrames();
for (FrameData b : bds) {
for (Chunk c : b.getDataFrame().getFrameChunks(s)) {
if (Arrays.equals(iid, ((DataChunk) c).getId(s))) {
if (Arrays.equals(iid, ((DataChunk) c).getSerializedId(s))) {
return (DataChunk) c;
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/su/interference/proxy/GenericResultImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ this software and associated documentation files (the "Software"), to deal in

package su.interference.proxy;

import su.interference.core.DataChunk;
import su.interference.persistent.Session;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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

public class GenericResultImpl implements GenericResult {

private DataChunk dataChunk;

public DataChunk getDataChunk(Session s) throws Exception {
if (dataChunk == null) {
dataChunk = new DataChunk(this, s);
}
return dataChunk;
}

public Object getValueByName(String name) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method m = this.getClass().getMethod("get"+name.substring(0,1).toUpperCase()+name.substring(1,name.length()), null);
return m.invoke(this, null);
}

@Override
public String toString() {
final Method[] ms = this.getClass().getMethods();
final StringBuffer sb = new StringBuffer();
try {
for (Method m : ms) {
if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
sb.append(m.getName().substring(3).substring(0, 1).toLowerCase());
sb.append(m.getName().substring(3).substring(1));
sb.append(":");
sb.append(m.invoke(this, null));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}

}
Loading