• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gephi / graphstore / #630

23 Oct 2026 06:52PM UTC coverage: 91.761% (+0.004%) from 91.757%
#630

push

web-flow
Read the edge type count without taking the graph lock (#337)

getEdgeTypeCount() took the graph read lock to read EdgeTypeStore.size(),
which is derived from the length counter and the garbage queue. EdgeTypeStore
has no lock of its own, so the call waited behind a held or queued graph
write lock, and without the lock it could see a count of zero while the store
is cleared.

EdgeTypeStore now publishes the size in a volatile field once the length and
the garbage queue are up to date at the end of each change, including after
deserialization, and getEdgeTypeCount() reads it.

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>

21 of 22 new or added lines in 3 files covered. (95.45%)

12240 of 13339 relevant lines covered (91.76%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.33
/src/main/java/org/gephi/graph/impl/EdgeTypeStore.java
1
/*
2
 * Copyright 2012-2013 Gephi Consortium
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
package org.gephi.graph.impl;
17

18
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
19
import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
20
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
21
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
22
import it.unimi.dsi.fastutil.shorts.ShortRBTreeSet;
23
import it.unimi.dsi.fastutil.shorts.ShortSortedSet;
24
import java.util.Arrays;
25
import org.gephi.graph.impl.utils.MapDeepEquals;
26

27
public class EdgeTypeStore {
28

29
    // Const
30
    protected final static int NULL_TYPE = -1;
31
    protected final static int NULL_LABEL = 0;
32
    private final static short NULL_SHORT = Short.MIN_VALUE;
33
    // Config
34
    public final static int MAX_SIZE = 65534;
35
    // Data
36
    protected final ConfigurationImpl configuration;
37
    protected final Object2ShortMap labelMap;
38
    protected final Short2ObjectMap idMap;
39
    protected final ShortSortedSet garbageQueue;
40
    protected int length;
41
    // Number of registered types, published for reads without the graph lock
42
    private volatile int size;
43

44
    public EdgeTypeStore() {
45
        this(new ConfigurationImpl());
1 ✔
46
    }
1 ✔
47

48
    public EdgeTypeStore(ConfigurationImpl config) {
1 ✔
49
        if (MAX_SIZE >= Short.MAX_VALUE - Short.MIN_VALUE + 1) {
50
            throw new RuntimeException("Edge Type Store size can't exceed 65534");
51
        }
52
        this.configuration = config;
1 ✔
53
        this.garbageQueue = new ShortRBTreeSet();
1 ✔
54
        this.labelMap = new Object2ShortOpenHashMap(MAX_SIZE);
1 ✔
55
        this.idMap = new Short2ObjectOpenHashMap(MAX_SIZE);
1 ✔
56
        labelMap.defaultReturnValue(NULL_SHORT);
1 ✔
57

58
        // Add null type
59
        short id = intToShort(NULL_LABEL);
1 ✔
60
        length++;
1 ✔
61
        labelMap.put(null, id);
1 ✔
62
        idMap.put(id, null);
1 ✔
63
        updateSize();
1 ✔
64
    }
1 ✔
65

66
    public int getId(final Object label) {
67
        short id = labelMap.getShort(label);
1 ✔
68
        if (id == NULL_SHORT) {
1 ✔
69
            return NULL_TYPE;
1 ✔
70
        }
71
        return shortToInt(id);
1 ✔
72
    }
73

74
    public Object getLabel(final int id) {
75
        checkValidId(id);
1 ✔
76
        checkIdExists(id);
1 ✔
77

78
        return idMap.get(intToShort(id));
1 ✔
79
    }
80

81
    public void registerEdgeType(int type) {
82
        if (!contains(type)) {
1 ✔
83
            if (configuration.isEnableAutoEdgeTypeRegistration()) {
1 ✔
84
                addType(String.valueOf(type), type);
1 ✔
85
            } else {
86
                throw new UnsupportedOperationException(
1 ✔
87
                        "The type " + type + " doesn't exist, and edge type auto registration is disabled (from Configuration)");
88
            }
89
        }
90
    }
1 ✔
91

92
    public int addType(final Object label) {
93
        checkType(label);
1 ✔
94

95
        short id = labelMap.getShort(label);
1 ✔
96
        if (id == NULL_SHORT) {
1 ✔
97
            if (!garbageQueue.isEmpty()) {
1 ✔
98
                id = garbageQueue.firstShort();
1 ✔
99
                garbageQueue.remove(id);
1 ✔
100
            } else {
101
                id = intToShort(length);
1 ✔
102
                if (length >= MAX_SIZE) {
1 ✔
103
                    throw new RuntimeException("Maximum number of edge types reached at " + MAX_SIZE);
1 ✔
104
                }
105
                length++;
1 ✔
106
            }
107
            labelMap.put(label, id);
1 ✔
108
            idMap.put(id, label);
1 ✔
109
            updateSize();
1 ✔
110
        }
111
        return shortToInt(id);
1 ✔
112
    }
113

114
    public boolean addType(final Object label, final int id) {
115
        short givenId = intToShort(id);
1 ✔
116
        short foundId = labelMap.getShort(label);
1 ✔
117
        if (foundId != NULL_SHORT && foundId != givenId) {
1 ✔
118
            throw new RuntimeException("This label '" + label + "' is already assigned to a different id");
1 ✔
119
        } else if (idMap.containsKey(givenId)) {
1 ✔
120
            if ((label == null && idMap.get(givenId) == null) || (idMap.get(givenId) != null && idMap.get(givenId)
1 ✔
121
                    .equals(label))) {
1 ✔
122
                return false;
1 ✔
123
            } else {
124
                throw new RuntimeException("This id '" + id + "' is already assigned to a different label");
1 ✔
125
            }
126
        }
127
        try {
128
            if (garbageQueue.contains(givenId)) {
1 ✔
129
                garbageQueue.remove(givenId);
1 ✔
130
                labelMap.put(label, givenId);
1 ✔
131
                idMap.put(givenId, label);
1 ✔
132
            } else {
133
                short insertId = intToShort(length++);
1 ✔
134
                while (insertId < givenId) {
1 ✔
135
                    garbageQueue.add(insertId);
1 ✔
136
                    insertId = intToShort(length++);
1 ✔
137
                    if (length >= MAX_SIZE) {
1 ✔
NEW
138
                        throw new RuntimeException("Maximum number of edge types reached at " + MAX_SIZE);
×
139
                    }
140
                }
141
                labelMap.put(label, givenId);
1 ✔
142
                idMap.put(givenId, label);
1 ✔
143
            }
144
        } finally {
145
            // The loop can throw after changing length and garbageQueue
146
            updateSize();
1 ✔
147
        }
148
        return true;
1 ✔
149
    }
150

151
    public int removeType(final Object label) {
152
        short id = labelMap.removeShort(label);
1 ✔
153
        if (id == NULL_SHORT) {
1 ✔
154
            return NULL_TYPE;
1 ✔
155
        }
156
        idMap.remove(id);
1 ✔
157
        garbageQueue.add(id);
1 ✔
158
        updateSize();
1 ✔
159

160
        int intId = shortToInt(id);
1 ✔
161
        return intId;
1 ✔
162
    }
163

164
    public Object removeType(final int type) {
165
        checkValidId(type);
1 ✔
166

167
        short id = intToShort(type);
1 ✔
168
        Object label = idMap.remove(id);
1 ✔
169
        if (label != null) {
1 ✔
170
            labelMap.remove(label);
1 ✔
171
            garbageQueue.add(id);
1 ✔
172
            updateSize();
1 ✔
173
        }
174
        return label;
1 ✔
175
    }
176

177
    public Object[] getLabels() {
178
        return labelMap.keySet().toArray();
1 ✔
179
    }
180

181
    protected short[] getIds() {
182
        return labelMap.values().toShortArray();
1 ✔
183
    }
184

185
    public int[] getIdsAsInts() {
186
        short[] s = getIds();
1 ✔
187
        int[] res = new int[s.length];
1 ✔
188
        for (int i = 0; i < s.length; i++) {
1 ✔
189
            res[i] = shortToInt(s[i]);
1 ✔
190
        }
191
        return res;
1 ✔
192
    }
193

194
    protected short[] getGarbage() {
195
        return garbageQueue.toShortArray();
1 ✔
196
    }
197

198
    public boolean contains(final Object label) {
199
        return labelMap.containsKey(label);
1 ✔
200
    }
201

202
    public boolean contains(final int id) {
203
        checkValidId(id);
1 ✔
204

205
        return idMap.containsKey(intToShort(id));
1 ✔
206
    }
207

208
    public void clear() {
209
        labelMap.clear();
1 ✔
210
        idMap.clear();
1 ✔
211
        garbageQueue.clear();
1 ✔
212
        length = 0;
1 ✔
213

214
        // Add null type
215
        short id = intToShort(NULL_LABEL);
1 ✔
216
        length++;
1 ✔
217
        labelMap.put(null, id);
1 ✔
218
        idMap.put(id, null);
1 ✔
219
        updateSize();
1 ✔
220
    }
1 ✔
221

222
    public int size() {
223
        return size;
1 ✔
224
    }
225

226
    // Publishes the size once length and garbageQueue are up to date
227
    void updateSize() {
228
        size = length - garbageQueue.size();
1 ✔
229
    }
1 ✔
230

231
    private short intToShort(final int id) {
232
        return (short) (id + Short.MIN_VALUE + 1);
1 ✔
233
    }
234

235
    private int shortToInt(final short id) {
236
        return id - Short.MIN_VALUE - 1;
1 ✔
237
    }
238

239
    static void checkValidId(final int id) {
240
        if (id < 0 || id >= MAX_SIZE) {
1 ✔
241
            throw new IllegalArgumentException("The type must be included between 0 and " + (MAX_SIZE - 1));
1 ✔
242
        }
243
    }
1 ✔
244

245
    private void checkType(final Object o) {
246
        if (o != null) {
1 ✔
247
            Class cl = o.getClass();
1 ✔
248
            if (!(cl.equals(Integer.class) || cl.equals(String.class) || cl.equals(Float.class) || cl
1 ✔
249
                    .equals(Double.class) || cl.equals(Short.class) || cl.equals(Byte.class) || cl
×
250
                            .equals(Long.class) || cl.equals(Character.class) || cl.equals(Boolean.class))) {
×
251
                throw new IllegalArgumentException("The type id is " + cl
×
252
                        .getCanonicalName() + " but must be a primitive type (int, string, long...)");
×
253
            }
254
            if (!configuration.getEdgeLabelType().equals(o.getClass())) {
1 ✔
255
                throw new IllegalArgumentException("The expected type was " + configuration.getEdgeLabelType()
1 ✔
256
                        .getCanonicalName() + " and " + o.getClass().getCanonicalName() + " was found");
1 ✔
257
            }
258
        }
259
    }
1 ✔
260

261
    private void checkIdExists(final int id) {
262
        if (!idMap.containsKey(intToShort(id))) {
1 ✔
263
            throw new IllegalArgumentException("The id " + id + " doesn' exist");
1 ✔
264
        }
265
    }
1 ✔
266

267
    public int deepHashCode() {
268
        int hash = 3;
1 ✔
269
        short[] keys = idMap.keySet().toShortArray();
1 ✔
270
        Arrays.sort(keys);
1 ✔
271
        for (int i = 0; i < keys.length; i++) {
1 ✔
272
            Short s = keys[i];
1 ✔
273
            Object o = idMap.get(s);
1 ✔
274
            hash = 67 * hash + (o != null ? o.hashCode() : 0);
1 ✔
275
            hash = 67 * hash + s.hashCode();
1 ✔
276
        }
277
        return hash;
1 ✔
278
    }
279

280
    public boolean deepEquals(Object obj) {
281
        if (obj == null) {
1 ✔
282
            return false;
×
283
        }
284
        if (getClass() != obj.getClass()) {
1 ✔
285
            return false;
×
286
        }
287
        final EdgeTypeStore other = (EdgeTypeStore) obj;
1 ✔
288
        if (!MapDeepEquals.mapDeepEquals(labelMap, other.labelMap)) {
1 ✔
289
            return false;
1 ✔
290
        }
291
        if (!MapDeepEquals.mapDeepEquals(idMap, other.idMap)) {
1 ✔
292
            return false;
×
293
        }
294
        return true;
1 ✔
295
    }
296
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc