Skip to content

Commit 31f7315

Browse files
committed
Use identifiers index position as hash key to cache the mtabix block values instead of the identifiers hash. Fixes #156
1 parent f4490ba commit 31f7315

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

org.gitools.matrix/src/main/java/org/gitools/matrix/model/mtabixmatrix/MTabixBlockValues.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,35 @@
2121
*/
2222
package org.gitools.matrix.model.mtabixmatrix;
2323

24+
import edu.upf.bg.mtabix.MTabixConfig;
25+
import edu.upf.bg.mtabix.parse.IKeyParser;
2426
import gnu.trove.map.TLongDoubleMap;
2527
import gnu.trove.map.hash.TLongDoubleHashMap;
2628

29+
import java.util.HashMap;
30+
import java.util.List;
31+
import java.util.Map;
32+
2733
public class MTabixBlockValues {
2834

35+
private IKeyParser keyParser;
36+
private Map<Integer, Map<String, Integer>> identifierToPosition;
2937
private TLongDoubleMap map;
3038

31-
public MTabixBlockValues() {
39+
public MTabixBlockValues(IKeyParser keyParser, Map<Integer, List<String>> identifiers) {
3240
super();
41+
3342
this.map = new TLongDoubleHashMap();
43+
this.keyParser = keyParser;
44+
this.identifierToPosition = new HashMap<>();
45+
for (Map.Entry<Integer, List<String>> e : identifiers.entrySet()) {
46+
Map<String, Integer> pos = new HashMap<>();
47+
List<String> ids = e.getValue();
48+
for (int i=0; i < ids.size(); i++) {
49+
pos.put(ids.get(i), i);
50+
}
51+
this.identifierToPosition.put(e.getKey(), pos);
52+
}
3453
}
3554

3655
public Double get(String... identifiers) {
@@ -48,9 +67,9 @@ public void put(long key, double value) {
4867
map.put(key, value);
4968
}
5069

51-
public static long hash(String... identifiers) {
52-
int a = identifiers[0].hashCode();
53-
int b = identifiers[1].hashCode();
70+
public long hash(String... identifiers) {
71+
int a = identifierToPosition.get(keyParser.getKeys()[0]).get(identifiers[0]);
72+
int b = identifierToPosition.get(keyParser.getKeys()[1]).get(identifiers[1]);
5473
return (long) a << 32 | b & 0xFFFFFFFFL;
5574
}
5675
}

org.gitools.matrix/src/main/java/org/gitools/matrix/model/mtabixmatrix/MTabixMatrix.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,18 @@ private synchronized MTabixBlockValues loadBlock(Long filePointer, int column) t
130130

131131
long block = getBlockAddress(filePointer);
132132
LOGGER.info("Loading block '" + Long.toHexString(block) + "' column " + column);
133-
MTabixBlockValues matrix = new MTabixBlockValues();
133+
MTabixBlockValues matrix = new MTabixBlockValues(this.index.getConfig().getKeyParser(), this.index.getIndexIdentifiers());
134134
dataStream.seek(filePointer);
135135

136136
while ((getBlockAddress(dataStream.getFilePointer()) == block) && ((lineLength = dataStream.readLine(line)) != -1)) {
137137

138138
final Double value = DoubleTranslator.get().stringToValue(parseLine(line, column, lineLength));
139139
if (value!=null) {
140-
int a = parseHash(line, 1, lineLength);
141-
int b = parseHash(line, 0, lineLength);
142-
long key = (long) a << 32 | b & 0xFFFFFFFFL;
140+
String id1 = parseLine(line, 1, lineLength);
141+
String id0 = parseLine(line, 0, lineLength);
142+
long key = matrix.hash(id1, id0);
143143
matrix.put(key, value);
144144
}
145-
146145
}
147146

148147
return matrix;
@@ -199,22 +198,6 @@ public String parseLine(byte[] str, int pos, int length) {
199198
return new String(str, start, size);
200199
}
201200

202-
public int parseHash(byte[] str, int pos, int length) {
203-
204-
int size = parseTab(str, pos, length);
205-
206-
if (size == -1) {
207-
return 0;
208-
}
209-
210-
int hash = 0;
211-
for (int i=start; i < end; i++) {
212-
hash = 31 * hash + str[i];
213-
}
214-
215-
return hash;
216-
}
217-
218201
private class BlockLoader extends CacheLoader<Long, MTabixBlockValues> {
219202

220203
private int column;

0 commit comments

Comments
 (0)