Skip to content

Commit 0cc5462

Browse files
performance optimize: persist(), indexes
1 parent 9628141 commit 0cc5462

11 files changed

Lines changed: 171 additions & 100 deletions

File tree

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+
ValueSet getDcs();
4647
Object getEntity() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException;
4748
Object getUndoEntity() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException;
4849
void updateEntity(Object o) throws InternalException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, MalformedURLException;

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,26 @@ this software and associated documentation files (the "Software"), to deal in
3535

3636
public class ChunkMap {
3737
private final ConcurrentHashMap<Integer, Chunk> hmap;
38+
private final ConcurrentHashMap<ValueSet, Chunk> imap;
3839
private final List<Chunk> list;
40+
private volatile boolean sorted;
3941

4042
public ChunkMap() {
41-
hmap = new ConcurrentHashMap<Integer, Chunk>();
42-
list = new CopyOnWriteArrayList<Chunk>();
43+
hmap = new ConcurrentHashMap<>();
44+
imap = new ConcurrentHashMap<>();
45+
list = new CopyOnWriteArrayList<>();
4346
}
4447

4548
public void sort() {
4649
Collections.sort(list);
50+
sorted = true;
4751
}
4852

4953
public void add(Chunk c) {
5054
hmap.put(c.getHeader().getPtr(), c);
55+
imap.put(c.getDcs(), c);
5156
list.add(c);
57+
sorted = false;
5258
}
5359

5460
public List<Chunk> getChunks() {
@@ -63,10 +69,16 @@ public Chunk get(int i) {
6369
return list.get(i);
6470
}
6571

72+
public Chunk getByKey(ValueSet key) {
73+
return imap.get(key);
74+
}
75+
6676
public void removeByPtr(int i) {
6777
final boolean x = list.remove(hmap.get(i));
68-
final Object o = hmap.remove(i);
69-
if (!x || o == null) {
78+
final Chunk c = (Chunk)hmap.remove(i);
79+
imap.remove(c.getDcs());
80+
sorted = false;
81+
if (!x || c == null) {
7082
throw new RuntimeException("Internal error during remove object from frame");
7183
}
7284
}
@@ -75,6 +87,8 @@ public void remove(int i) {
7587
final Chunk c = list.get(i);
7688
list.remove(i);
7789
hmap.remove(c.getHeader().getPtr());
90+
imap.remove(c.getDcs());
91+
sorted = false;
7892
}
7993

8094
public int size() {
@@ -84,6 +98,11 @@ public int size() {
8498
public void clear() {
8599
hmap.clear();
86100
list.clear();
101+
imap.clear();
102+
sorted = false;
87103
}
88104

105+
public boolean isSorted() {
106+
return sorted;
107+
}
89108
}

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,25 @@ public class DataChunk implements Chunk {
6565
private final CustomSerializer sr = new CustomSerializer();
6666

6767
//returns datacolumn set
68-
public ValueSet getDcs() throws ClassNotFoundException, IllegalAccessException, InternalException, MalformedURLException {
68+
public ValueSet getDcs() {
6969
if (dcs==null) {
70-
final Field[] f = t.getFields();
71-
final Object[] vs = new Object[f.length];
72-
for (int i=0; i<f.length; i++) {
73-
final int m = f[i].getModifiers();
74-
final Transient ta = f[i].getAnnotation(Transient.class);
75-
if (ta==null) {
76-
if (Modifier.isPrivate(m)) {
77-
f[i].setAccessible(true);
70+
try {
71+
final Field[] f = this.t == null ? this.entity.getClass().getFields() : t.getFields();
72+
final List<Object> vs = new ArrayList<>();
73+
for (int i = 0; i < f.length; i++) {
74+
final int m = f[i].getModifiers();
75+
final Transient ta = f[i].getAnnotation(Transient.class);
76+
if (ta == null) {
77+
if (Modifier.isPrivate(m)) {
78+
f[i].setAccessible(true);
79+
}
80+
vs.add(f[i].get(entity));
7881
}
79-
vs[i] = f[i].get(entity);
8082
}
83+
dcs = new ValueSet(vs.toArray(new Object[]{}));
84+
} catch (Exception e) {
85+
throw new RuntimeException(e);
8186
}
82-
dcs = new ValueSet(vs);
8387
}
8488
return dcs;
8589
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ public interface DataObject {
6060
long getIdValue(Session s, LLT llt) throws Exception;
6161
long getIncValue(Session s, LLT llt) throws Exception;
6262
void incFrameAmount ();
63+
Class getSc();
64+
Class getGenericClass();
6365
boolean isNoTran() throws ClassNotFoundException, MalformedURLException;
6466
boolean isIndex() throws ClassNotFoundException, MalformedURLException;
6567
Object newInstance() throws IOException, InternalException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException;
6668
Object getInstance() throws IOException, InternalException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException;
6769
void usedSpace (FrameData bd, int used, boolean persist, Session s, LLT llt);
6870
void addIndexValue (DataChunk dc) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException;
69-
Field[] getColumns();
7071
java.lang.reflect.Field[] getFields() throws ClassNotFoundException, InternalException, MalformedURLException;
72+
java.lang.reflect.Field getIdField();
73+
String getIdFieldType();
74+
String getIdFieldGetter();
7175
FrameData allocateFrame(DataFile df, DataObject t, Session s, LLT llt) throws Exception;
7276

7377
}

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

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@ this software and associated documentation files (the "Software"), to deal in
3232
*/
3333

3434
public class IndexElement implements Comparable {
35-
private IndexElementKey key;
36-
private Object element;
35+
final private IndexElementKey key;
36+
private Object element;
37+
final private boolean ex;
3738

38-
public IndexElement (IndexElementKey key, Object element) {
39+
public IndexElement (IndexElementKey key, Object element, boolean ex) {
3940
this.key = key;
4041
this.element = element;
42+
this.ex = ex;
4143
}
4244

4345
public int compareTo(final Object obj) {
4446
final IndexElement j = (IndexElement)obj;
4547
final int res = this.getKey().compareTo(j.getKey());
46-
final String clname = this.getElement().getClass().getSimpleName();
47-
if (clname.equals("Integer")&&res==0) {
48+
if (this.ex && res == 0) {
4849
if ((Integer)this.getElement() < (Integer)j.getElement()) { return -1; } else if ((Integer)this.getElement() > (Integer)j.getElement()) { return 1; }
4950
return 0;
5051
}
@@ -55,10 +56,6 @@ public IndexElementKey getKey() {
5556
return key;
5657
}
5758

58-
public void setKey(IndexElementKey key) {
59-
this.key = key;
60-
}
61-
6259
public Object getElement() {
6360
return element;
6461
}
@@ -68,26 +65,7 @@ public void setElement(Object element) {
6865
}
6966

7067
public String ElementToString () {
71-
String clname = this.element.getClass().getSimpleName();
72-
if (clname.equals("Integer")) {
73-
return ""+ (Integer)this.element;
74-
}
75-
if (clname.equals("Long")) {
76-
return ""+ (Long)this.element;
77-
}
78-
if (clname.equals("Float")) {
79-
return ""+ (Float)this.element;
80-
}
81-
if (clname.equals("Double")) {
82-
return ""+ (Double)this.element;
83-
}
84-
if (clname.equals("String")) {
85-
return ""+ (String)this.element;
86-
}
87-
if (clname.equals("Date")) {
88-
return ""+ (Date)this.element;
89-
}
90-
return "Datatype not recognized";
68+
return "" + this.element;
9169
}
9270

9371
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ private boolean isFill(DataChunk ie) {
208208
}
209209

210210
public ValueSet sort() throws ClassNotFoundException, IllegalAccessException, InternalException, MalformedURLException {
211-
this.data.sort();
211+
if (!this.data.isSorted()) {
212+
this.data.sort();
213+
}
212214
this.sorted = true;
213215
if (this.data.size()>0) {
214216
return ((DataChunk)this.data.get(this.data.size()-1)).getDcs();
@@ -255,13 +257,16 @@ public synchronized ArrayList<Long> getChildElementsPtr(ValueSet value) throws C
255257
}
256258

257259
//return first element which found - for unique indexes
258-
public DataChunk getObjectByKey(ValueSet key) throws ClassNotFoundException, IllegalAccessException, InternalException, MalformedURLException {
260+
public DataChunk getObjectByKey(ValueSet key) {
261+
/*
259262
for (Chunk ie : this.data.getChunks()) {
260263
if (((DataChunk)ie).getDcs().equals(key)) {
261264
return (DataChunk)ie;
262265
}
263266
}
264267
return null;
268+
*/
269+
return (DataChunk) this.data.getByKey(key);
265270
}
266271

267272
//return all element which found - for non-unique indexes

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public synchronized void add (String id, Object element) {
6060
}
6161

6262
public synchronized void add (IndexElementKey key, Object element) {
63-
IndexElement e = new IndexElement(key, element);
63+
IndexElement e = new IndexElement(key, element, false);
6464

6565
boolean cnue = true;
6666
IndexElementList target = list.get(start);
@@ -92,9 +92,9 @@ public synchronized void add (IndexElementKey key, Object element) {
9292
addElementList(newlist);
9393
prevtg = target;
9494
if (newlist.isDivided()) {
95-
e = new IndexElement(newlist.getMaxValue(), new Integer(newlist.getPtr()));
95+
e = new IndexElement(newlist.getMaxValue(), new Integer(newlist.getPtr()), true);
9696
} else {
97-
e = new IndexElement(prevtg.getMaxValue(), new Integer(prevtg.getPtr()));
97+
e = new IndexElement(prevtg.getMaxValue(), new Integer(prevtg.getPtr()), true);
9898
}
9999
if (prevtg.getParent()==0) { //add parent ElementList - always type 2 (node)
100100
target = new IndexElementList(2);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ private static void registerMetrics() throws Exception {
400400
Metrics.register(Metrics.TIMER, "executeQuery");
401401
Metrics.register(Metrics.TIMER, "deallocateQuery");
402402
Metrics.register(Metrics.TIMER, "syncFrames");
403+
Metrics.register(Metrics.TIMER, "persistGetChunk");
404+
Metrics.register(Metrics.TIMER, "persistInsertChunk");
405+
Metrics.register(Metrics.TIMER, "persistInsertIndex");
403406
Metrics.register(Metrics.HISTOGRAM, "recordRCount");
404407
Metrics.register(Metrics.HISTOGRAM, "recordLCount");
405408
Metrics.register(Metrics.HISTOGRAM, "syncQueue");

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

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

2525
package su.interference.core;
2626

27-
import java.util.Date;
28-
2927
/**
3028
* @author Yuriy Glotanov
3129
* @since 1.0
@@ -57,26 +55,9 @@ public int compare (Object obj, int thr) {
5755
final ValueSet j = (ValueSet)obj;
5856

5957
for (int i=0; i<vs.length; i++) {
60-
String clname = vs[i].getClass().getSimpleName();
61-
if (clname.equals("Integer")) {
62-
if ((Integer)this.vs[i] < (Integer)j.getValueSet()[i]) { return -1; } else if ((Integer)this.vs[i] > (Integer)j.getValueSet()[i]) { return 1; }
63-
}
64-
if (clname.equals("Long")) {
65-
if ((Long)this.vs[i] < (Long)j.getValueSet()[i]) { return -1; } else if ((Long)this.vs[i] > (Long)j.getValueSet()[i]) { return 1; }
66-
}
67-
if (clname.equals("Float")) {
68-
if ((Float)this.vs[i] < (Float)j.getValueSet()[i]) { return -1; } else if ((Float)this.vs[i] > (Float)j.getValueSet()[i]) { return 1; }
69-
}
70-
if (clname.equals("Double")) {
71-
if ((Double)this.vs[i] < (Double)j.getValueSet()[i]) { return -1; } else if ((Double)this.vs[i] > (Double)j.getValueSet()[i]) { return 1; }
72-
}
73-
if (clname.equals("String")) {
74-
int c = (((String)this.vs[i]).compareTo((String)j.getValueSet()[i]));
75-
if (c!=0) { return c; }
76-
}
77-
if (clname.equals("Date")) {
78-
int c = (((Date)this.vs[i]).compareTo((Date)j.getValueSet()[i]));
79-
if (c!=0) { return c; }
58+
final int ct = ((Comparable)this.vs[i]).compareTo(j.getValueSet()[i]);
59+
if (ct != 0) {
60+
return ct;
8061
}
8162
if (i==thr-1) {
8263
break;
@@ -90,5 +71,11 @@ public boolean equals (Object obj) {
9071
return this.compareTo(j)==0?true:false;
9172
}
9273

74+
public int hashCode() {
75+
int hashCode = 1;
76+
for (Object o : vs)
77+
hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode());
78+
return hashCode;
79+
}
9380

9481
}

0 commit comments

Comments
 (0)