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

gephi / graphstore / #656

25 Oct 2026 01:38PM UTC coverage: 91.912% (-0.04%) from 91.954%
#656

push

web-flow
Return an ElementIterable from the column index get and release its lock (#362)

* Return a snapshot from the column index get and entry iterator

Index.get locked the column index when called and unlocked it when the
iterator was exhausted, so a get that was never iterated or a loop that
broke early kept the lock, and setting an attribute inside the loop could
deadlock with a setter on a view index. Non-indexed, degree and edge type
columns held the graph read lock the same way. get and the entry iterator
now copy the elements under the lock and return the copy.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Return an ElementIterable from the column index get and take the lock when iterating

Index.get took the index lock when called and released it only when the
iterator was exhausted, so a result that was never iterated or a loop that
stopped early kept the lock with no way to release it. Non-indexed, degree
and edge type columns held the graph read lock the same way.

get now returns an ElementIterable, like Graph.getNodes: the lock is taken
when iterator() is called and released when the iterator is exhausted or on
doBreak(). Each iteration takes and releases its own hold, and doBreak after
exhaustion or twice does nothing. The snapshot of the previous commit is
reverted, the entry iterator is unchanged. Code compiled against the
Iterable return type must be recompiled.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Release every open iteration of an index get result and never leak the graph lock

doBreak only released the latest iterator of a result, so nested loops over
the same result kept the outer lock. parallelStream unlocked the lock from a
worker thread. A non-indexed column removed during an iteration of get,
count, values, getMinValue or getMaxValue left the graph read lock held by
the graph iterator.

doBreak now releases every iteration of the result still holding the lock,
parallelStream throws Unsupport... (continued)

145 of 166 new or added lines in 6 files covered. (87.35%)

12466 of 13563 relevant lines covered (91.91%)

0.92 hits per line

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

93.82
/src/main/java/org/gephi/graph/impl/IndexImpl.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

17
package org.gephi.graph.impl;
18

19
import java.util.Collection;
20
import java.util.Collections;
21
import java.util.Map;
22
import java.util.Set;
23
import org.gephi.graph.api.Column;
24
import org.gephi.graph.api.ColumnIndex;
25
import org.gephi.graph.api.Edge;
26
import org.gephi.graph.api.EdgeIterable;
27
import org.gephi.graph.api.Element;
28
import org.gephi.graph.api.ElementIterable;
29
import org.gephi.graph.api.Graph;
30
import org.gephi.graph.api.Index;
31
import org.gephi.graph.api.NodeIterable;
32

33
public class IndexImpl<T extends Element> implements Index<T> {
34

35
    protected final ColumnStore<T> columnStore;
36
    protected final Graph graph;
37
    protected ColumnIndexImpl[] columns;
38
    protected int columnsCount;
39

40
    public IndexImpl(ColumnStore<T> columnStore) {
41
        this(columnStore, columnStore.graphStore);
1 ✔
42
    }
1 ✔
43

44
    public IndexImpl(ColumnStore<T> columnStore, Graph graph) {
1 ✔
45
        this.columnStore = columnStore;
1 ✔
46
        this.graph = graph;
1 ✔
47
        this.columns = new ColumnIndexImpl[0];
1 ✔
48
    }
1 ✔
49

50
    @Override
51
    public Class<T> getIndexClass() {
52
        return columnStore.elementType;
1 ✔
53
    }
54

55
    @Override
56
    public String getIndexName() {
57
        return "index_" + columnStore.elementType.getCanonicalName();
1 ✔
58
    }
59

60
    @Override
61
    public ColumnIndex getColumnIndex(Column column) {
62
        return getIndex(column);
1 ✔
63
    }
64

65
    @Override
66
    public int count(Column column, Object value) {
67
        checkNonNullColumnObject(column);
1 ✔
68

69
        ColumnIndexImpl index = getIndex(column);
1 ✔
70
        if (index != null) {
1 ✔
71
            return index.count(value);
1 ✔
72
        }
73
        return 0;
×
74
    }
75

76
    public int count(String key, Object value) {
77
        checkNonNullObject(key);
1 ✔
78

79
        return count(columnStore.getColumn(key), value);
1 ✔
80
    }
81

82
    @Override
83
    public ElementIterable<T> get(Column column, Object value) {
84
        checkNonNullColumnObject(column);
1 ✔
85

86
        ColumnIndexImpl index = getIndex(column);
1 ✔
87
        if (index != null) {
1 ✔
88
            return index.get(value);
1 ✔
89
        }
90
        return (ElementIterable<T>) (Edge.class.equals(columnStore.elementType) ? EdgeIterable.EMPTY
1 ✔
NEW
91
                : NodeIterable.EMPTY);
×
92
    }
93

94
    public ElementIterable<T> get(String key, Object value) {
95
        checkNonNullObject(key);
1 ✔
96

97
        return get(columnStore.getColumn(key), value);
1 ✔
98
    }
99

100
    @Override
101
    public boolean isSortable(Column column) {
102
        checkNonNullColumnObject(column);
1 ✔
103

104
        ColumnIndexImpl index = getIndex(column);
1 ✔
105
        if (index != null) {
1 ✔
106
            return index.isSortable();
1 ✔
107
        }
108
        return false;
×
109
    }
110

111
    @Override
112
    public Number getMinValue(Column column) {
113
        checkNonNullColumnObject(column);
1 ✔
114

115
        ColumnIndexImpl index = getIndex(column);
1 ✔
116
        if (index != null) {
1 ✔
117
            return index.getMinValue();
1 ✔
118
        }
119
        return null;
×
120
    }
121

122
    @Override
123
    public Number getMaxValue(Column column) {
124
        checkNonNullColumnObject(column);
1 ✔
125

126
        ColumnIndexImpl index = getIndex(column);
1 ✔
127
        if (index != null) {
1 ✔
128
            return index.getMaxValue();
1 ✔
129
        }
130
        return null;
×
131
    }
132

133
    public Iterable<Map.Entry<Object, Set<T>>> get(Column column) {
134
        checkNonNullColumnObject(column);
1 ✔
135

136
        return getIndex(column);
1 ✔
137
    }
138

139
    @Override
140
    public Collection values(Column column) {
141
        checkNonNullColumnObject(column);
1 ✔
142

143
        ColumnIndexImpl index = getIndex(column);
1 ✔
144
        if (index != null) {
1 ✔
145
            return index.values();
1 ✔
146
        }
147
        return Collections.EMPTY_LIST;
×
148
    }
149

150
    @Override
151
    public int countValues(Column column) {
152
        checkNonNullColumnObject(column);
1 ✔
153
        ColumnIndexImpl index = getIndex(column);
1 ✔
154
        if (index != null) {
1 ✔
155
            return index.countValues();
1 ✔
156
        }
157
        return 0;
×
158
    }
159

160
    @Override
161
    public int countElements(Column column) {
162
        checkNonNullColumnObject(column);
1 ✔
163
        ColumnIndexImpl index = getIndex(column);
1 ✔
164
        if (index != null) {
1 ✔
165
            return index.countElements();
1 ✔
166
        }
167
        return 0;
×
168
    }
169

170
    public Object put(String key, Object value, T element) {
171
        checkNonNullObject(key);
1 ✔
172

173
        return put(columnStore.getColumn(key), value, element);
1 ✔
174
    }
175

176
    public Object put(Column column, Object value, T element) {
177
        checkNonNullColumnObject(column);
1 ✔
178

179
        return getIndex(column).putValue(element, value);
1 ✔
180
    }
181

182
    public void remove(String key, Object value, T element) {
183
        checkNonNullObject(key);
1 ✔
184

185
        remove(columnStore.getColumn(key), value, element);
1 ✔
186
    }
1 ✔
187

188
    public void remove(Column column, Object value, T element) {
189
        checkNonNullColumnObject(column);
1 ✔
190

191
        getIndex(column).removeValue(element, value);
1 ✔
192
    }
1 ✔
193

194
    public Object set(String key, Object oldValue, Object value, T element) {
195
        checkNonNullObject(key);
1 ✔
196

197
        return set(columnStore.getColumn(key), oldValue, value, element);
1 ✔
198
    }
199

200
    public Object set(Column column, Object oldValue, Object value, T element) {
201
        checkNonNullColumnObject(column);
1 ✔
202

203
        // No index while the column is added or after it's removed concurrently
204
        ColumnIndexImpl index = getIndex(column);
1 ✔
205
        if (index == null) {
1 ✔
206
            return value;
1 ✔
207
        }
208
        return index.replaceValue(element, oldValue, value);
1 ✔
209
    }
210

211
    public void clear() {
212
        for (ColumnIndexImpl ai : columns) {
1 ✔
213
            if (ai != null) {
1 ✔
214
                ai.clear();
1 ✔
215
            }
216
        }
217
    }
1 ✔
218

219
    protected void addColumn(ColumnImpl col) {
220
        ensureColumnSize(col.storeId);
1 ✔
221
        ColumnIndexImpl index = createIndex(col);
1 ✔
222
        columns[col.storeId] = index;
1 ✔
223
        columnsCount++;
1 ✔
224
    }
1 ✔
225

226
    protected void addAllColumns(ColumnImpl[] cols) {
227
        ensureColumnSize(cols.length);
1 ✔
228
        for (ColumnImpl col : cols) {
1 ✔
229
            ColumnIndexImpl index = createIndex(col);
1 ✔
230
            ensureColumnSize(col.storeId);
1 ✔
231
            columns[col.storeId] = index;
1 ✔
232
            columnsCount++;
1 ✔
233
        }
234
    }
1 ✔
235

236
    protected void removeColumn(ColumnImpl col) {
237
        ColumnIndexImpl index = columns[col.storeId];
1 ✔
238
        index.destroy();
1 ✔
239
        columns[col.storeId] = null;
1 ✔
240
        columnsCount--;
1 ✔
241
    }
1 ✔
242

243
    protected boolean hasColumn(ColumnImpl col) {
244
        int id = col.storeId;
1 ✔
245
        return id != ColumnStore.NULL_ID && columns.length > id && columns[id].getColumn() == col;
1 ✔
246
    }
247

248
    protected ColumnIndexImpl getIndex(Column col) {
249
        int id = col.getIndex();
1 ✔
250
        if (id != ColumnStore.NULL_ID && columns.length > id) {
1 ✔
251
            ColumnIndexImpl index = columns[id];
1 ✔
252
            if (index != null && index.getColumn() == col) {
1 ✔
253
                return index;
1 ✔
254
            }
255
        }
256

257
        // TODO: Make this more robust
258
        if (col.isProperty()) {
1 ✔
259
            DefaultColumnsImpl defaultColumns = columnStore.graphStore.defaultColumns;
1 ✔
260
            if (col == defaultColumns.degreeColumn) {
1 ✔
261
                return new DegreeNoIndexImpl(graph, DegreeNoIndexImpl.DegreeType.DEGREE);
1 ✔
262
            } else if (col == defaultColumns.inDegreeColumn) {
1 ✔
263
                return new DegreeNoIndexImpl(graph, DegreeNoIndexImpl.DegreeType.IN_DEGREE);
1 ✔
264
            } else if (col == defaultColumns.outDegreeColumn) {
1 ✔
265
                return new DegreeNoIndexImpl(graph, DegreeNoIndexImpl.DegreeType.OUT_DEGREE);
1 ✔
266
            } else if (col == defaultColumns.typeColumn) {
1 ✔
267
                return new EdgeTypeNoIndexImpl(graph);
1 ✔
268
            }
269
        }
270
        return null;
1 ✔
271
    }
272

273
    protected ColumnIndexImpl getIndex(String key) {
274
        return getIndex(columnStore.getColumn(key));
1 ✔
275
    }
276

277
    protected void destroy() {
278
        for (ColumnIndexImpl ai : columns) {
1 ✔
279
            if (ai != null) {
1 ✔
280
                ai.destroy();
1 ✔
281
            }
282
        }
283
        columns = new ColumnIndexImpl[0];
1 ✔
284
        columnsCount = 0;
1 ✔
285
    }
1 ✔
286

287
    protected int size() {
288
        return columnsCount;
1 ✔
289
    }
290

291
    ColumnIndexImpl createIndex(ColumnImpl col) {
292
        return col.isIndexed() && ColumnStandardIndexImpl.isSupportedType(col) ? createStandardIndex(col)
1 ✔
293
                : createNoIndex(col, graph);
1 ✔
294
    }
295

296
    ColumnNoIndexImpl createNoIndex(ColumnImpl column, Graph graph) {
297
        return new ColumnNoIndexImpl(column, graph, columnStore.elementType);
1 ✔
298
    }
299

300
    ColumnStandardIndexImpl createStandardIndex(ColumnImpl column) {
301
        if (column.getTypeClass().equals(Byte.class)) {
1 ✔
302
            // Byte
303
            return new ColumnStandardIndexImpl.ByteStandardIndex<T>(column);
1 ✔
304
        } else if (column.getTypeClass().equals(Short.class)) {
1 ✔
305
            // Short
306
            return new ColumnStandardIndexImpl.ShortStandardIndex<T>(column);
1 ✔
307
        } else if (column.getTypeClass().equals(Integer.class)) {
1 ✔
308
            // Integer
309
            return new ColumnStandardIndexImpl.IntegerStandardIndex<T>(column);
1 ✔
310
        } else if (column.getTypeClass().equals(Long.class)) {
1 ✔
311
            // Long
312
            return new ColumnStandardIndexImpl.LongStandardIndex<T>(column);
1 ✔
313
        } else if (column.getTypeClass().equals(Float.class)) {
1 ✔
314
            // Float
315
            return new ColumnStandardIndexImpl.FloatStandardIndex<T>(column);
1 ✔
316
        } else if (column.getTypeClass().equals(Double.class)) {
1 ✔
317
            // Double
318
            return new ColumnStandardIndexImpl.DoubleStandardIndex<T>(column);
1 ✔
319
        } else if (Number.class.isAssignableFrom(column.getTypeClass())) {
1 ✔
320
            // Other numbers
321
            return new ColumnStandardIndexImpl.GenericNumberStandardIndex<T>(column);
1 ✔
322
        } else if (column.getTypeClass().equals(Boolean.class)) {
1 ✔
323
            // Boolean
324
            return new ColumnStandardIndexImpl.BooleanStandardIndex<T>(column);
1 ✔
325
        } else if (column.getTypeClass().equals(Character.class)) {
1 ✔
326
            // Char
327
            return new ColumnStandardIndexImpl.CharStandardIndex<T>(column);
1 ✔
328
        } else if (column.getTypeClass().equals(String.class)) {
1 ✔
329
            // String
330
            return new ColumnStandardIndexImpl.DefaultStandardIndex<T>(column);
1 ✔
331
        } else if (column.getTypeClass().equals(byte[].class)) {
1 ✔
332
            // Byte Array
333
            return new ColumnStandardIndexImpl.ByteArrayStandardIndex<T>(column);
1 ✔
334
        } else if (column.getTypeClass().equals(short[].class)) {
1 ✔
335
            // Short Array
336
            return new ColumnStandardIndexImpl.ShortArrayStandardIndex<T>(column);
1 ✔
337
        } else if (column.getTypeClass().equals(int[].class)) {
1 ✔
338
            // Integer Array
339
            return new ColumnStandardIndexImpl.IntegerArrayStandardIndex<T>(column);
1 ✔
340
        } else if (column.getTypeClass().equals(long[].class)) {
1 ✔
341
            // Long Array
342
            return new ColumnStandardIndexImpl.LongArrayStandardIndex<T>(column);
1 ✔
343
        } else if (column.getTypeClass().equals(float[].class)) {
1 ✔
344
            // Float array
345
            return new ColumnStandardIndexImpl.FloatArrayStandardIndex<T>(column);
1 ✔
346
        } else if (column.getTypeClass().equals(double[].class)) {
1 ✔
347
            // Double array
348
            return new ColumnStandardIndexImpl.DoubleArrayStandardIndex<T>(column);
1 ✔
349
        } else if (column.getTypeClass().equals(boolean[].class)) {
1 ✔
350
            // Boolean array
351
            return new ColumnStandardIndexImpl.BooleanArrayStandardIndex<T>(column);
1 ✔
352
        } else if (column.getTypeClass().equals(char[].class)) {
1 ✔
353
            // Char array
354
            return new ColumnStandardIndexImpl.CharArrayStandardIndex<T>(column);
1 ✔
355
        } else if (column.getTypeClass().equals(String[].class)) {
1 ✔
356
            // String array
357
            return new ColumnStandardIndexImpl.DefaultArrayStandardIndex<T>(column);
1 ✔
358
        } else if (column.getTypeClass().isArray()) {
1 ✔
359
            // Default Array
360
            return new ColumnStandardIndexImpl.DefaultArrayStandardIndex<T>(column);
1 ✔
361
        }
362
        return new ColumnStandardIndexImpl.DefaultStandardIndex<T>(column);
1 ✔
363
    }
364

365
    private void ensureColumnSize(int index) {
366
        if (index >= columns.length) {
1 ✔
367
            ColumnIndexImpl[] newArray = new ColumnIndexImpl[index + 1];
1 ✔
368
            System.arraycopy(columns, 0, newArray, 0, columns.length);
1 ✔
369
            columns = newArray;
1 ✔
370
        }
371
    }
1 ✔
372

373
    void checkNonNullObject(final Object o) {
374
        if (o == null) {
1 ✔
375
            throw new NullPointerException();
×
376
        }
377
    }
1 ✔
378

379
    void checkNonNullColumnObject(final Object o) {
380
        if (o == null) {
1 ✔
381
            throw new NullPointerException();
×
382
        }
383
        if (!(o instanceof ColumnImpl)) {
1 ✔
384
            throw new ClassCastException("Must be ColumnImpl object");
×
385
        }
386
    }
1 ✔
387

388
}
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