Skip to content

Commit b319e20

Browse files
performance improvement
1 parent 8d9baab commit b319e20

5 files changed

Lines changed: 56 additions & 54 deletions

File tree

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
The MIT License (MIT)
33
4-
Copyright (c) 2010-2019 head systems, ltd
4+
Copyright (c) 2010-2021 head systems, ltd
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in
@@ -25,47 +25,49 @@ this software and associated documentation files (the "Software"), to deal in
2525
package su.interference.core;
2626

2727
import java.util.*;
28-
import java.util.concurrent.CopyOnWriteArrayList;
29-
import java.util.concurrent.ConcurrentHashMap;
3028

3129
/**
3230
* @author Yuriy Glotanov
3331
* @since 1.0
3432
*/
3533

3634
public class ChunkMap {
37-
private final ConcurrentHashMap<Integer, Chunk> hmap;
38-
private final ConcurrentHashMap<ValueSet, List<Chunk>> imap;
35+
private final HashMap<Integer, Chunk> hmap;
36+
private final HashMap<ValueSet, List<Chunk>> imap;
3937
private final List<Chunk> list;
4038
private final Frame frame;
4139
private volatile boolean sorted;
40+
private volatile int used;
4241

4342
public ChunkMap(Frame frame) {
44-
hmap = new ConcurrentHashMap<>();
45-
imap = new ConcurrentHashMap<>();
46-
list = new CopyOnWriteArrayList<>();
43+
hmap = new HashMap<>();
44+
imap = frame instanceof IndexFrame ? new HashMap<>() : null;
45+
list = frame instanceof IndexFrame ? new ArrayList<>() : null;
4746
this.frame = frame;
4847
}
4948

5049
@SuppressWarnings("unchecked")
51-
public synchronized void sort() {
52-
Collections.sort(list);
53-
sorted = true;
50+
protected synchronized void sort() {
51+
if (frame instanceof IndexFrame) {
52+
Collections.sort(list);
53+
sorted = true;
54+
}
5455
}
5556

56-
public synchronized void add(Chunk c) {
57+
protected synchronized void add(Chunk c) {
5758
hmap.put(c.getHeader().getPtr(), c);
5859
if (frame instanceof IndexFrame) {
5960
if (imap.get(c.getDcs()) == null) {
6061
imap.put(c.getDcs(), new ArrayList<>());
6162
}
6263
imap.get(c.getDcs()).add(c);
64+
list.add(c);
6365
}
64-
list.add(c);
6566
sorted = false;
67+
used = used + c.getBytesAmount();
6668
}
6769

68-
public synchronized void check() {
70+
protected synchronized void check() {
6971
for (Map.Entry<Integer, Chunk> entry : hmap.entrySet()) {
7072
Chunk c = entry.getValue();
7173
if (c.getHeader().getPtr() != entry.getKey()) {
@@ -74,26 +76,29 @@ public synchronized void check() {
7476
}
7577
}
7678

77-
public synchronized List<Chunk> getChunks() {
78-
return list;
79+
protected synchronized Collection<Chunk> getChunks() {
80+
if (frame instanceof IndexFrame) {
81+
return list;
82+
}
83+
return hmap.values();
7984
}
8085

81-
public synchronized Chunk getByPtr(int i) {
86+
protected synchronized Chunk getByPtr(int i) {
8287
return hmap.get(i);
8388
}
8489

85-
public synchronized Chunk get(int i) {
90+
// index only
91+
protected synchronized Chunk get(int i) {
8692
return list.get(i);
8793
}
8894

8995
//for unique indexes
90-
public synchronized List<Chunk> getByKey(ValueSet key) {
96+
protected synchronized List<Chunk> getByKey(ValueSet key) {
9197
return imap.get(key);
9298
}
9399

94-
public synchronized void removeByPtr(int i) {
95-
final boolean x = list.remove(hmap.get(i));
96-
final Chunk c = (Chunk)hmap.remove(i);
100+
protected synchronized void removeByPtr(int i) {
101+
final Chunk c = hmap.remove(i);
97102
if (frame instanceof IndexFrame) {
98103
int i_ = 0;
99104
for (int i__ = 0; i__ < imap.get(c.getDcs()).size(); i__++) {
@@ -102,14 +107,17 @@ public synchronized void removeByPtr(int i) {
102107
}
103108
}
104109
imap.get(c.getDcs()).remove(i_);
110+
list.remove(hmap.get(i));
105111
}
106112
sorted = false;
107-
if (!x || c == null) {
113+
used = used - c.getBytesAmount();
114+
if (c == null) {
108115
throw new RuntimeException("Internal error during remove object from frame");
109116
}
110117
}
111118

112-
public synchronized void remove(int i) {
119+
// index only
120+
protected synchronized void remove(int i) {
113121
final Chunk c = list.get(i);
114122
list.remove(i);
115123
hmap.remove(c.getHeader().getPtr());
@@ -122,21 +130,29 @@ public synchronized void remove(int i) {
122130
}
123131
imap.get(c.getDcs()).remove(i_);
124132
}
133+
used = used - c.getBytesAmount();
125134
sorted = false;
126135
}
127136

128-
public synchronized int size() {
137+
//index only
138+
protected synchronized int size() {
129139
return list.size();
130140
}
131141

132-
public synchronized void clear() {
142+
protected synchronized void clear() {
133143
hmap.clear();
134144
list.clear();
135145
imap.clear();
146+
used = 0;
136147
sorted = false;
137148
}
138149

139-
public synchronized boolean isSorted() {
150+
protected synchronized boolean isSorted() {
140151
return sorted;
141152
}
153+
154+
protected int getUsed() {
155+
return used;
156+
}
157+
142158
}

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ this software and associated documentation files (the "Software"), to deal in
3030
import su.interference.exception.*;
3131
import su.interference.serialize.ByteString;
3232

33-
import java.util.ArrayList;
34-
import java.util.HashMap;
35-
import java.util.List;
36-
import java.util.Map;
33+
import java.util.*;
3734
import java.util.concurrent.ConcurrentHashMap;
3835

3936
/**
@@ -69,7 +66,6 @@ public class Frame implements Comparable {
6966
public static final int ROW_HEADER_SIZE = 16;
7067
public static final int INDEX_HEADER_SIZE = 32;
7168
public static final int MIN_FRAME_SIZE = 256; //test implementation only
72-
public static final int MAX_FRAME_SIZE = 65535;
7369
public static final int MAX_PTR_VALUE = Integer.MAX_VALUE;
7470
public static final int DATA_FRAME = 1;
7571
public static final int INDEX_FRAME = 2;
@@ -374,7 +370,7 @@ public int compareTo(Object obj) {
374370
return 0;
375371
}
376372

377-
public int getFrameFree () {
373+
protected int getFrameFree () {
378374
return getFrameSize() - getBytesAmount();
379375
}
380376

@@ -430,7 +426,6 @@ public synchronized int insertChunk (Chunk c, Session s, boolean check, boolean
430426
data.add(c);
431427
return ptr;
432428
}
433-
434429
}
435430

436431
public synchronized int updateChunk(DataChunk chunk, Object o, Session s, LLT llt) throws Exception {
@@ -478,7 +473,7 @@ public synchronized void deleteChunk (int ptr, Session s, LLT llt) {
478473
header.setState(Header.RECORD_DELETED_STATE);
479474
}
480475

481-
public synchronized List<Chunk> getChunks() {
476+
public synchronized Collection<Chunk> getChunks() {
482477
return data.getChunks();
483478
}
484479

@@ -615,13 +610,8 @@ public HashMap getLiveTransactions() {
615610
return rtran;
616611
}
617612

618-
public int getBytesAmount() {
619-
int used = FRAME_HEADER_SIZE;
620-
final int size = data.getChunks().size();
621-
for (int i=0; i<size; i++) {
622-
used = used + data.getChunks().get(i).getBytesAmount();
623-
}
624-
return used;
613+
private int getBytesAmount() {
614+
return FRAME_HEADER_SIZE + data.getUsed();
625615
}
626616

627617
public int getBytesAmountWoHead() {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ private static boolean poolNotEmpty() {
106106

107107
public void add(Frame b) {
108108
b.getFrameData().setUnsynced();
109-
frames.put(b.getFrameData().getFrameId(), b.getFrameData());
109+
if (frames.get(b.getFrameData().getFrameId()) == null) {
110+
frames.put(b.getFrameData().getFrameId(), b.getFrameData());
111+
}
110112
}
111113

112114
public void add(FrameData b) {
113115
b.setUnsynced();
114-
frames.put(b.getFrameId(), b);
116+
if (frames.get(b.getFrameId()) == null) {
117+
frames.put(b.getFrameId(), b);
118+
}
115119
}
116120

117121
public void commit() throws Exception {

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,6 @@ public synchronized int getFrameUsed() {
274274
return this.getUsed() + tdiff;
275275
}
276276

277-
//real current non-tran amount of chunk bytes
278-
public int getCUsed() {
279-
if (frame == null) {
280-
return -1;
281-
}
282-
return frame.getBytesAmount();
283-
}
284-
285277
public int getFrameFree() {
286278
return size - Frame.FRAME_HEADER_SIZE - getFrameUsed();
287279
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ private java.lang.reflect.Field getTableIdField() {
525525
return null;
526526
}
527527

528-
public boolean isIdFieldNoCheck() throws ClassNotFoundException, MalformedURLException {
529-
java.lang.reflect.Field f = getTableIdField();
528+
public boolean isIdFieldNoCheck() {
529+
java.lang.reflect.Field f = getIdField();
530530
if (f != null) {
531531
NoCheck a = f.getAnnotation(NoCheck.class);
532532
return a != null;

0 commit comments

Comments
 (0)