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

gephi / graphstore / #653

24 Oct 2026 08:16PM UTC coverage: 91.896% (+0.01%) from 91.886%
#653

push

web-flow
Keep the column index consistent when an attribute is set concurrently (#360)

Element.setAttribute writes the value and then updates the index without the
table lock, while adding and removing nodes, view changes and view index fills
read the value and update the index on their own. In between, the index update
threw a NullPointerException on the missing old value, or left the element
under a stale value or in the index after its removal. An index now only moves
an element it holds under the old value, a removal finds the element under any
value, and each update checks the value again afterwards to move the element
to the current one. addColumn indexes the existing elements itself, and fill
rebuilds the view indexes like not.

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

80 of 83 new or added lines in 6 files covered. (96.39%)

1 existing line in 1 file now uncovered.

12360 of 13450 relevant lines covered (91.9%)

0.92 hits per line

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

98.59
/src/main/java/org/gephi/graph/impl/IndexStore.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 it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
20
import java.util.Iterator;
21
import java.util.Map;
22
import java.util.Map.Entry;
23
import org.gephi.graph.api.Column;
24
import org.gephi.graph.api.Edge;
25
import org.gephi.graph.api.Element;
26
import org.gephi.graph.api.ElementIterable;
27
import org.gephi.graph.api.Graph;
28
import org.gephi.graph.api.GraphView;
29
import org.gephi.graph.api.Node;
30

31
public class IndexStore<T extends Element> {
32

33
    protected final ColumnStore<T> columnStore;
34
    protected final TableLockImpl lock;
35
    protected final IndexImpl<T> mainIndex;
36
    protected final Map<GraphView, IndexImpl<T>> viewIndexes;
37

38
    public IndexStore(ColumnStore<T> columnStore) {
1 ✔
39
        this.columnStore = columnStore;
1 ✔
40
        this.mainIndex = new IndexImpl<>(columnStore);
1 ✔
41
        this.viewIndexes = new Object2ObjectOpenHashMap<>();
1 ✔
42
        this.lock = columnStore.lock;
1 ✔
43
    }
1 ✔
44

45
    // Table locked
46
    protected void addColumn(ColumnImpl col) {
47
        mainIndex.addColumn(col);
1 ✔
48
        for (IndexImpl<T> index : viewIndexes.values()) {
1 ✔
49
            index.addColumn(col);
1 ✔
50
        }
1 ✔
51
    }
1 ✔
52

53
    // Table locked
54
    protected void removeColumn(ColumnImpl col) {
55
        mainIndex.removeColumn(col);
1 ✔
56
        for (IndexImpl<T> index : viewIndexes.values()) {
1 ✔
57
            index.removeColumn(col);
1 ✔
58
        }
1 ✔
59
    }
1 ✔
60

61
    protected boolean hasColumn(ColumnImpl col) {
62
        return mainIndex.hasColumn(col);
1 ✔
63
    }
64

65
    protected IndexImpl getIndex(Graph graph) {
66
        GraphView view = graph.getView();
1 ✔
67
        if (view.isMainView()) {
1 ✔
68
            return mainIndex;
1 ✔
69
        }
70
        synchronized (viewIndexes) {
1 ✔
71
            IndexImpl<T> viewIndex = viewIndexes.get(view);
1 ✔
72
            if (viewIndex != null) {
1 ✔
73
                return viewIndex;
1 ✔
74
            }
75
        }
1 ✔
76
        // The graph lock is taken first, as indexing the view requires it, then the table lock. The view index monitor
77
        // is only held for the lookup and the put, as set() takes it while the table lock is held by addColumn.
78
        graph.readLock();
1 ✔
79
        lock();
1 ✔
80
        try {
81
            if (((GraphViewImpl) view).isDestroyed()) {
1 ✔
82
                throw new IllegalArgumentException("The view doesn't exist");
1 ✔
83
            }
84
            IndexImpl<T> viewIndex;
85
            synchronized (viewIndexes) {
1 ✔
86
                viewIndex = viewIndexes.get(view);
1 ✔
87
            }
1 ✔
88
            if (viewIndex == null) {
1 ✔
89
                viewIndex = createViewIndex(graph);
1 ✔
90
            }
91
            return viewIndex;
1 ✔
92
        } finally {
93
            unlock();
1 ✔
94
            graph.readUnlock();
1 ✔
95
        }
96
    }
97

98
    protected IndexImpl createViewIndex(Graph graph) {
99
        if (graph.getView().isMainView()) {
1 ✔
100
            throw new IllegalArgumentException("Can't create a view index for the main view");
1 ✔
101
        }
102
        lock();
1 ✔
103
        try {
104
            IndexImpl viewIndex = new IndexImpl<>(columnStore, graph);
1 ✔
105
            ColumnImpl[] columns = columnStore.toArray();
1 ✔
106
            viewIndex.addAllColumns(columns);
1 ✔
107
            // set() only takes the monitor, so it has to wait until the index is filled
108
            synchronized (viewIndexes) {
1 ✔
109
                viewIndexes.put(graph.getView(), viewIndex);
1 ✔
110
                try {
111
                    indexView(graph);
1 ✔
112
                } catch (RuntimeException | Error e) {
1 ✔
113
                    viewIndexes.remove(graph.getView());
1 ✔
114
                    throw e;
1 ✔
115
                }
1 ✔
116
            }
1 ✔
117

118
            return viewIndex;
1 ✔
119
        } finally {
120
            unlock();
1 ✔
121
        }
122
    }
123

124
    protected void deleteViewIndex(Graph graph) {
125
        if (graph.getView().isMainView()) {
1 ✔
126
            throw new IllegalArgumentException("Can't delete a view index for the main view");
1 ✔
127
        }
128
        lock();
1 ✔
129
        try {
130
            IndexImpl<T> index;
131
            // Same monitor as set(), which iterates the view indexes without the table lock
132
            synchronized (viewIndexes) {
1 ✔
133
                index = viewIndexes.remove(graph.getView());
1 ✔
134
            }
1 ✔
135
            if (index != null) {
1 ✔
136
                index.destroy();
1 ✔
137
            }
138
        } finally {
139
            unlock();
1 ✔
140
        }
141
    }
1 ✔
142

143
    public Object set(Column column, Object oldValue, Object value, T element) {
144
        value = mainIndex.set(column, oldValue, value, element);
1 ✔
145

146
        if (!viewIndexes.isEmpty()) {
1 ✔
147
            synchronized (viewIndexes) {
1 ✔
148
                for (Entry<GraphView, IndexImpl<T>> entry : viewIndexes.entrySet()) {
1 ✔
149
                    // Reads the view directly, the graph lock can't be taken while holding the monitor
150
                    if (inView((GraphViewImpl) entry.getKey(), element)) {
1 ✔
151
                        entry.getValue().set(column, oldValue, value, element);
1 ✔
152
                    }
153
                }
1 ✔
154
            }
1 ✔
155
        }
156

157
        return value;
1 ✔
158
    }
159

160
    // Like containsNode and containsEdge, but reads the store id once as the element can be removed meanwhile
161
    private static boolean inView(GraphViewImpl view, Element element) {
162
        if (element instanceof Node) {
1 ✔
163
            int storeId = ((NodeImpl) element).getStoreId();
1 ✔
164
            return storeId != NodeStore.NULL_ID && (!view.nodeView || view.nodeBitVector.get(storeId));
1 ✔
165
        }
NEW
166
        int storeId = ((EdgeImpl) element).getStoreId();
×
NEW
167
        return storeId != EdgeStore.NULL_ID && view.edgeBitVector.get(storeId);
×
168
    }
169

170
    public void clear(T element) {
171
        ElementImpl elementImpl = (ElementImpl) element;
1 ✔
172

173
        lock();
1 ✔
174
        try {
175
            final int length = columnStore.length;
1 ✔
176
            final ColumnImpl[] cols = columnStore.columns;
1 ✔
177
            for (int i = 0; i < length; i++) {
1 ✔
178
                Column c = cols[i];
1 ✔
179
                if (c != null) {
1 ✔
180
                    Object value = elementImpl.getAttribute(c);
1 ✔
181
                    mainIndex.remove(c, value, element);
1 ✔
182
                    if (!viewIndexes.isEmpty()) {
1 ✔
183
                        synchronized (viewIndexes) {
1 ✔
184
                            for (Entry<GraphView, IndexImpl<T>> entry : viewIndexes.entrySet()) {
1 ✔
185
                                GraphViewImpl graphView = (GraphViewImpl) entry.getKey();
1 ✔
186
                                boolean inView = element instanceof Node ? graphView.containsNode((Node) element)
1 ✔
187
                                        : graphView.containsEdge((Edge) element);
1 ✔
188
                                if (inView) {
1 ✔
189
                                    entry.getValue().remove(c, value, element);
1 ✔
190
                                }
191
                            }
1 ✔
192
                        }
1 ✔
193
                    }
194
                }
195
            }
196
        } finally {
197
            unlock();
1 ✔
198
        }
199
    }
1 ✔
200

201
    public void index(T element) {
202
        ElementImpl elementImpl = (ElementImpl) element;
1 ✔
203
        lock();
1 ✔
204
        try {
205

206
            final int length = columnStore.length;
1 ✔
207
            final ColumnImpl[] cols = columnStore.columns;
1 ✔
208
            for (int i = 0; i < length; i++) {
1 ✔
209
                Column c = cols[i];
1 ✔
210
                if (c != null) {
1 ✔
211
                    Object value = elementImpl.getAttribute(c);
1 ✔
212
                    Object indexedValue = mainIndex.put(c, value, element);
1 ✔
213
                    elementImpl.attributes.replaceAttribute(c.getIndex(), value, indexedValue);
1 ✔
214
                    indexCurrentValue(mainIndex, c, indexedValue, elementImpl);
1 ✔
215
                    // Only edge-only views contain a node as it's added to the store. Edges are indexed in node-only
216
                    // views when they are added to them.
217
                    if (element instanceof Node && !viewIndexes.isEmpty()) {
1 ✔
218
                        synchronized (viewIndexes) {
1 ✔
219
                            for (Entry<GraphView, IndexImpl<T>> entry : viewIndexes.entrySet()) {
1 ✔
220
                                GraphViewImpl graphView = (GraphViewImpl) entry.getKey();
1 ✔
221
                                if (graphView.containsNode((Node) element)) {
1 ✔
222
                                    entry.getValue().put(c, indexedValue, element);
1 ✔
223
                                    indexCurrentValue(entry.getValue(), c, indexedValue, elementImpl);
1 ✔
224
                                }
225
                            }
1 ✔
226
                        }
1 ✔
227
                    }
228
                }
229
            }
230
        } finally {
231
            unlock();
1 ✔
232
        }
233
    }
1 ✔
234

235
    // Table locked
236
    protected void index(ColumnImpl column, T element) {
237
        ElementImpl elementImpl = (ElementImpl) element;
1 ✔
238
        Object value = elementImpl.getAttribute(column);
1 ✔
239
        mainIndex.put(column, value, element);
1 ✔
240
        indexCurrentValue(mainIndex, column, value, elementImpl);
1 ✔
241
        if (!viewIndexes.isEmpty()) {
1 ✔
242
            synchronized (viewIndexes) {
1 ✔
243
                for (Entry<GraphView, IndexImpl<T>> entry : viewIndexes.entrySet()) {
1 ✔
244
                    GraphViewImpl graphView = (GraphViewImpl) entry.getKey();
1 ✔
245
                    boolean inView = element instanceof Node ? graphView.containsNode((Node) element)
1 ✔
246
                            : graphView.containsEdge((Edge) element);
1 ✔
247
                    if (inView) {
1 ✔
248
                        entry.getValue().put(column, value, element);
1 ✔
249
                        indexCurrentValue(entry.getValue(), column, value, elementImpl);
1 ✔
250
                    }
251
                }
1 ✔
252
            }
1 ✔
253
        }
254
    }
1 ✔
255

256
    public void indexView(Graph graph) {
257
        final IndexImpl viewIndex = viewIndexes.get(graph.getView());
1 ✔
258
        if (viewIndex != null) {
1 ✔
259
            graph.readLock();
1 ✔
260
            try {
261
                ElementIterable<?> elements = null;
1 ✔
262
                if (columnStore.elementType.equals(Node.class)) {
1 ✔
263
                    elements = graph.getNodes();
1 ✔
264
                } else if (columnStore.elementType.equals(Edge.class)) {
1 ✔
265
                    elements = graph.getEdges();
1 ✔
266
                }
267

268
                if (elements != null) {
1 ✔
269
                    Iterator<T> iterator = (Iterator<T>) elements.iterator();
1 ✔
270
                    // The iterator holds a graph read lock until it is exhausted
271
                    boolean exhausted = false;
1 ✔
272
                    try {
273
                        while (iterator.hasNext()) {
1 ✔
274
                            ElementImpl element = (ElementImpl) iterator.next();
1 ✔
275

276
                            final ColumnImpl[] cols = columnStore.columns;
1 ✔
277
                            int length = columnStore.length;
1 ✔
278
                            for (int i = 0; i < length; i++) {
1 ✔
279
                                Column c = cols[i];
1 ✔
280
                                if (c != null) {
1 ✔
281
                                    Object value = element.getAttribute(c);
1 ✔
282
                                    viewIndex.put(c, value, element);
1 ✔
283
                                    indexCurrentValue(viewIndex, c, value, element);
1 ✔
284
                                }
285
                            }
286
                        }
1 ✔
287
                        exhausted = true;
1 ✔
288
                    } finally {
289
                        if (!exhausted) {
1 ✔
290
                            elements.doBreak();
1 ✔
291
                        }
292
                    }
293
                }
294
            } finally {
295
                graph.readUnlock();
1 ✔
296
            }
297
        }
298
    }
1 ✔
299

300
    public void indexInView(T element, GraphView view) {
301
        ElementImpl elementImpl = (ElementImpl) element;
1 ✔
302
        lock();
1 ✔
303
        try {
304
            IndexImpl<T> index = viewIndexes.get(view);
1 ✔
305
            if (index != null) {
1 ✔
306
                final int length = columnStore.length;
1 ✔
307
                final ColumnImpl[] cols = columnStore.columns;
1 ✔
308
                for (int i = 0; i < length; i++) {
1 ✔
309
                    Column c = cols[i];
1 ✔
310
                    if (c != null) {
1 ✔
311
                        Object value = elementImpl.getAttribute(c);
1 ✔
312
                        index.put(c, value, element);
1 ✔
313
                        indexCurrentValue(index, c, value, elementImpl);
1 ✔
314
                    }
315
                }
316
            }
317
        } finally {
318
            unlock();
1 ✔
319
        }
320
    }
1 ✔
321

322
    // A set between reading the value and putting it finds no element to move, so it's moved to the current value
323
    private static void indexCurrentValue(IndexImpl index, Column column, Object indexedValue, ElementImpl element) {
324
        Object value;
325
        while ((value = element.attributes.getAttribute(column)) != indexedValue) {
1 ✔
326
            index.set(column, indexedValue, value, element);
1 ✔
327
            indexedValue = value;
1 ✔
328
        }
329
    }
1 ✔
330

331
    public void clearInView(T element, GraphView view) {
332
        ElementImpl elementImpl = (ElementImpl) element;
1 ✔
333
        lock();
1 ✔
334
        try {
335
            IndexImpl<T> index = viewIndexes.get(view);
1 ✔
336
            if (index != null) {
1 ✔
337
                final int length = columnStore.length;
1 ✔
338
                final ColumnImpl[] cols = columnStore.columns;
1 ✔
339
                for (int i = 0; i < length; i++) {
1 ✔
340
                    Column c = cols[i];
1 ✔
341
                    if (c != null) {
1 ✔
342
                        Object value = elementImpl.getAttribute(c);
1 ✔
343
                        index.remove(c, value, element);
1 ✔
344
                    }
345
                }
346
            }
347
        } finally {
348
            unlock();
1 ✔
349
        }
350
    }
1 ✔
351

352
    public void clear(GraphView view) {
353
        lock();
1 ✔
354
        try {
355
            IndexImpl<T> index = viewIndexes.get(view);
1 ✔
356
            if (index != null) {
1 ✔
357
                index.clear();
×
358
            }
359
        } finally {
360
            unlock();
1 ✔
361
        }
362
    }
1 ✔
363

364
    public void clear() {
365
        lock();
1 ✔
366
        try {
367
            mainIndex.clear();
1 ✔
368
            for (IndexImpl index : viewIndexes.values()) {
1 ✔
369
                index.clear();
1 ✔
370
            }
1 ✔
371
        } finally {
372
            unlock();
1 ✔
373
        }
374
    }
1 ✔
375

376
    private void lock() {
377
        if (lock != null) {
1 ✔
378
            lock.lock();
1 ✔
379
        }
380
    }
1 ✔
381

382
    private void unlock() {
383
        if (lock != null) {
1 ✔
384
            lock.unlock();
1 ✔
385
        }
386
    }
1 ✔
387
}
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