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

gephi / graphstore / #626

23 Oct 2026 05:10PM UTC coverage: 91.69% (-0.001%) from 91.691%
#626

push

web-flow
Read the graph type once when getting a view graph (#333)

* Read the graph type once when getting a view graph

GraphModelImpl.getGraph(view) read the graph type, then
GraphViewStore.getDirectedGraph read it again in checkDirectedAllowed. When
the graph turned undirected in between, a non-main view threw a
RuntimeException for a call that has a valid answer. It now delegates to
GraphViewStore.getGraph, which reads the type once.

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

* Remove the regression test

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

---------

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

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

12170 of 13273 relevant lines covered (91.69%)

0.92 hits per line

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

95.92
/src/main/java/org/gephi/graph/impl/GraphViewStore.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.ints.IntRBTreeSet;
19
import it.unimi.dsi.fastutil.ints.IntSortedSet;
20
import java.util.function.Predicate;
21
import org.gephi.graph.api.DirectedSubgraph;
22
import org.gephi.graph.api.Edge;
23
import org.gephi.graph.api.Graph;
24
import org.gephi.graph.api.GraphView;
25
import org.gephi.graph.api.Interval;
26
import org.gephi.graph.api.Node;
27
import org.gephi.graph.api.Subgraph;
28
import org.gephi.graph.api.UndirectedSubgraph;
29

30
public class GraphViewStore {
31

32
    // Const
33
    protected static final int NULL_VIEW = -1;
34
    // Config
35
    protected static final int DEFAULT_VIEWS = 0;
36
    // Data
37
    protected final IntSortedSet garbageQueue;
38
    protected final GraphStore graphStore;
39
    protected GraphViewImpl[] views;
40
    protected int length;
41
    // Visible view
42
    protected volatile GraphView visibleView;
43

44
    public GraphViewStore(GraphStore graphStore) {
1 ✔
45
        if (graphStore == null) {
1 ✔
46
            throw new NullPointerException();
1 ✔
47
        }
48
        this.graphStore = graphStore;
1 ✔
49
        this.views = new GraphViewImpl[DEFAULT_VIEWS];
1 ✔
50
        this.garbageQueue = new IntRBTreeSet();
1 ✔
51
        this.visibleView = graphStore.mainGraphView;
1 ✔
52
    }
1 ✔
53

54
    public GraphViewImpl createView() {
55
        return createView(true, true);
1 ✔
56
    }
57

58
    public GraphViewImpl createView(Predicate<Node> nodeFilter, Predicate<Edge> edgeFilter) {
59
        graphStore.autoWriteLock();
1 ✔
60
        try {
61
            GraphViewImpl graphView = new GraphViewImpl(graphStore, nodeFilter, edgeFilter);
1 ✔
62
            addView(graphView);
1 ✔
63
            return graphView;
1 ✔
64
        } finally {
65
            graphStore.autoWriteUnlock();
1 ✔
66
        }
67
    }
68

69
    public GraphViewImpl createView(boolean nodes, boolean edges) {
70
        graphStore.autoWriteLock();
1 ✔
71
        try {
72
            GraphViewImpl graphView = new GraphViewImpl(graphStore, nodes, edges);
1 ✔
73
            addView(graphView);
1 ✔
74
            return graphView;
1 ✔
75
        } finally {
76
            graphStore.autoWriteUnlock();
1 ✔
77
        }
78
    }
79

80
    public GraphViewImpl createView(GraphView view) {
81
        checkNonNullViewObject(view);
1 ✔
82
        return createView(view, view.isNodeView(), view.isEdgeView());
1 ✔
83
    }
84

85
    public GraphViewImpl createView(GraphView view, boolean nodes, boolean edges) {
86
        checkNonNullViewObject(view);
1 ✔
87
        if (view.isMainView()) {
1 ✔
88
            graphStore.autoWriteLock();
1 ✔
89
            try {
90
                GraphViewImpl graphView = new GraphViewImpl(graphStore, nodes, edges);
1 ✔
91
                graphView.fill();
1 ✔
92
                addView(graphView);
1 ✔
93
                return graphView;
1 ✔
94
            } finally {
95
                graphStore.autoWriteUnlock();
1 ✔
96
            }
97
        } else {
98
            checkGraphViewObject(view);
1 ✔
99

100
            graphStore.autoWriteLock();
1 ✔
101
            try {
102
                checkViewExist((GraphViewImpl) view);
1 ✔
103
                GraphViewImpl graphView = new GraphViewImpl((GraphViewImpl) view, nodes, edges);
1 ✔
104
                addView(graphView);
1 ✔
105
                return graphView;
1 ✔
106
            } finally {
107
                graphStore.autoWriteUnlock();
1 ✔
108
            }
109
        }
110
    }
111

112
    public void destroyView(GraphView view) {
113
        checkNonNullViewObject(view);
1 ✔
114
        if (view.isMainView()) {
1 ✔
115
            throw new IllegalArgumentException("Can't delete the main view");
1 ✔
116
        }
117
        checkGraphViewObject(view);
1 ✔
118

119
        graphStore.autoWriteLock();
1 ✔
120
        try {
121
            checkViewExist((GraphViewImpl) view);
1 ✔
122
            TimeIndexStore nodeTimeStore = graphStore.timeStore.nodeIndexStore;
1 ✔
123
            if (nodeTimeStore != null) {
1 ✔
124
                nodeTimeStore.deleteViewIndex(((GraphViewImpl) view).getDirectedGraph());
1 ✔
125
            }
126

127
            TimeIndexStore edgeTimeStore = graphStore.timeStore.edgeIndexStore;
1 ✔
128
            if (edgeTimeStore != null) {
1 ✔
129
                edgeTimeStore.deleteViewIndex(((GraphViewImpl) view).getDirectedGraph());
1 ✔
130
            }
131

132
            IndexStore<Node> nodeIndexStore = graphStore.nodeTable.store.indexStore;
1 ✔
133
            if (nodeIndexStore != null) {
1 ✔
134
                nodeIndexStore.deleteViewIndex(((GraphViewImpl) view).getDirectedGraph());
1 ✔
135
            }
136

137
            IndexStore<Edge> edgeIndexStore = graphStore.edgeTable.store.indexStore;
1 ✔
138
            if (edgeIndexStore != null) {
1 ✔
139
                edgeIndexStore.deleteViewIndex(((GraphViewImpl) view).getDirectedGraph());
1 ✔
140
            }
141

142
            removeView((GraphViewImpl) view);
1 ✔
143
        } finally {
144
            graphStore.autoWriteUnlock();
1 ✔
145
        }
146
    }
1 ✔
147

148
    public void setTimeInterval(GraphView view, Interval interval) {
149
        checkNonNullViewObject(view);
1 ✔
150
        if (view.isMainView()) {
1 ✔
151
            throw new IllegalArgumentException("Can't set the time interval of the main view");
1 ✔
152
        }
153
        checkGraphViewObject(view);
1 ✔
154

155
        graphStore.autoWriteLock();
1 ✔
156
        try {
157
            checkViewExist((GraphViewImpl) view);
1 ✔
158
            GraphViewImpl graphView = (GraphViewImpl) view;
1 ✔
159
            graphView.setTimeInterval(interval);
1 ✔
160
        } finally {
161
            graphStore.autoWriteUnlock();
1 ✔
162
        }
163
    }
1 ✔
164

165
    public boolean contains(GraphView view) {
166
        graphStore.autoReadLock();
1 ✔
167
        try {
168
            checkNonNullViewObject(view);
1 ✔
169
            checkGraphViewObject(view);
1 ✔
170
            GraphViewImpl viewImpl = (GraphViewImpl) view;
1 ✔
171
            int id = viewImpl.storeId;
1 ✔
172
            if (id != NULL_VIEW && id < length && views[id] == view) {
1 ✔
173
                return true;
1 ✔
174
            }
175
            return false;
1 ✔
176
        } finally {
177
            graphStore.autoReadUnlock();
1 ✔
178
        }
179
    }
180

181
    public int size() {
182
        return length - garbageQueue.size();
1 ✔
183
    }
184

185
    public Subgraph getGraph(GraphView view) {
186
        checkNonNullViewObject(view);
1 ✔
187
        checkGraphViewObject(view);
1 ✔
188

189
        if (graphStore.isUndirected()) {
1 ✔
190
            if (view.isMainView()) {
1 ✔
191
                return graphStore.undirectedDecorator;
1 ✔
192
            }
193
            return ((GraphViewImpl) view).getUndirectedGraph();
1 ✔
194
        } else {
195
            if (view.isMainView()) {
1 ✔
196
                return graphStore;
1 ✔
197
            }
198
            return ((GraphViewImpl) view).getDirectedGraph();
1 ✔
199
        }
200
    }
201

202
    public DirectedSubgraph getDirectedGraph(GraphView view) {
203
        checkNonNullViewObject(view);
1 ✔
204
        checkGraphViewObject(view);
1 ✔
205

206
        if (view.isMainView()) {
1 ✔
UNCOV
207
            return graphStore;
×
208
        }
209

210
        checkDirectedAllowed();
1 ✔
211
        return ((GraphViewImpl) view).getDirectedGraph();
1 ✔
212
    }
213

214
    public UndirectedSubgraph getUndirectedGraph(GraphView view) {
215
        checkNonNullViewObject(view);
1 ✔
216
        checkGraphViewObject(view);
1 ✔
217

218
        if (view.isMainView()) {
1 ✔
UNCOV
219
            return graphStore.undirectedDecorator;
×
220
        }
221
        return ((GraphViewImpl) view).getUndirectedGraph();
1 ✔
222
    }
223

224
    public GraphView getVisibleView() {
225
        return visibleView;
1 ✔
226
    }
227

228
    public void setVisibleView(GraphView view) {
229
        if (view == null || view == graphStore.mainGraphView) {
1 ✔
230
            visibleView = graphStore.mainGraphView;
1 ✔
231
        } else {
232
            checkGraphViewObject(view);
1 ✔
233
            checkViewExist((GraphViewImpl) view);
1 ✔
234
            visibleView = view;
1 ✔
235
        }
236
    }
1 ✔
237

238
    public GraphObserverImpl createGraphObserver(Graph graph, boolean withDiff) {
239
        GraphViewImpl graphViewImpl = (GraphViewImpl) graph.getView();
1 ✔
240
        graphStore.autoReadLock();
1 ✔
241
        try {
242
            checkViewExist(graphViewImpl);
1 ✔
243
            return graphViewImpl.createGraphObserver(graph, withDiff);
1 ✔
244
        } finally {
245
            graphStore.autoReadUnlock();
1 ✔
246
        }
247
    }
248

249
    public void destroyGraphObserver(GraphObserverImpl graphObserver) {
250
        GraphViewImpl graphViewImpl = (GraphViewImpl) graphObserver.graph.getView();
1 ✔
251
        if (graphObserver.graphStore != this.graphStore) {
1 ✔
252
            throw new RuntimeException("This observer doesn't belong to this store");
1 ✔
253
        }
254

255
        graphViewImpl.destroyGraphObserver(graphObserver);
1 ✔
256
    }
1 ✔
257

258
    protected void addNode(NodeImpl node) {
259
        for (GraphViewImpl view : views) {
1 ✔
260
            // Edge-only views include all nodes
261
            if (view != null && !view.nodeView) {
1 ✔
262
                view.incrementNodeVersion();
1 ✔
263
            }
264
        }
265
    }
1 ✔
266

267
    protected void removeNode(NodeImpl node) {
268
        for (GraphViewImpl view : views) {
1 ✔
269
            if (view != null) {
1 ✔
270
                if (view.nodeView) {
1 ✔
271
                    view.removeNode(node);
1 ✔
272
                } else {
273
                    // Edge-only views include all nodes, and incident edges are removed from them beforehand
274
                    view.incrementNodeVersion();
1 ✔
275
                }
276
            }
277
        }
278
    }
1 ✔
279

280
    protected void addEdge(EdgeImpl edge) {
281
        for (GraphViewImpl view : views) {
1 ✔
282
            if (view != null) {
1 ✔
283
                if (view.nodeView && !view.edgeView) {
1 ✔
284
                    view.addEdgeInNodeView(edge);
1 ✔
285
                }
286
            }
287
        }
288
    }
1 ✔
289

290
    protected void setEdgeType(EdgeImpl edge, int oldType, boolean wasMutual) {
291
        for (GraphViewImpl view : views) {
1 ✔
292
            if (view != null) {
1 ✔
293
                if (view.containsEdge(edge)) {
1 ✔
294
                    view.setEdgeType(edge, oldType, wasMutual);
1 ✔
295
                }
296
            }
297
        }
298
    }
1 ✔
299

300
    protected void removeEdge(EdgeImpl edge) {
301
        for (GraphViewImpl view : views) {
1 ✔
302
            if (view != null) {
1 ✔
303
                view.removeEdgeFromStore(edge);
1 ✔
304
            }
305
        }
306
    }
1 ✔
307

308
    protected int addView(final GraphViewImpl view) {
309
        checkNonNullViewObject(view);
1 ✔
310

311
        int id;
312
        if (!garbageQueue.isEmpty()) {
1 ✔
313
            id = garbageQueue.firstInt();
1 ✔
314
            garbageQueue.remove(id);
1 ✔
315
        } else {
316
            id = length++;
1 ✔
317
            ensureArraySize(id);
1 ✔
318
        }
319
        views[id] = view;
1 ✔
320
        view.storeId = id;
1 ✔
321
        return id;
1 ✔
322
    }
323

324
    protected void removeView(final GraphViewImpl view) {
325
        checkViewExist(view);
1 ✔
326

327
        int id = view.storeId;
1 ✔
328
        views[id] = null;
1 ✔
329
        garbageQueue.add(id);
1 ✔
330
        view.storeId = NULL_VIEW;
1 ✔
331

332
        view.destroyAllObservers();
1 ✔
333

334
        // Check if not visible view
335
        if (visibleView == view) {
1 ✔
336
            visibleView = graphStore.mainGraphView;
1 ✔
337
        }
338
    }
1 ✔
339

340
    private void ensureArraySize(int index) {
341
        if (index >= views.length) {
1 ✔
342
            GraphViewImpl[] newArray = new GraphViewImpl[index + 1];
1 ✔
343
            System.arraycopy(views, 0, newArray, 0, views.length);
1 ✔
344
            views = newArray;
1 ✔
345
        }
346
    }
1 ✔
347

348
    public int deepHashCode() {
349
        int hash = 5;
1 ✔
350
        for (GraphViewImpl view : this.views) {
1 ✔
351
            hash = 67 * hash + (view != null ? view.deepHashCode() : 0);
1 ✔
352
        }
353
        hash = 67 * hash + this.length;
1 ✔
354
        return hash;
1 ✔
355
    }
356

357
    public boolean deepEquals(GraphViewStore obj) {
358
        if (obj == null) {
1 ✔
359
            return false;
×
360
        }
361
        if (this.length != obj.length) {
1 ✔
362
            return false;
×
363
        }
364
        int l = this.views.length;
1 ✔
365
        if (l != obj.views.length) {
1 ✔
366
            return false;
×
367
        }
368
        for (int i = 0; i < l; i++) {
1 ✔
369
            GraphViewImpl e1 = this.views[i];
1 ✔
370
            GraphViewImpl e2 = obj.views[i];
1 ✔
371

372
            if (e1 == e2) {
1 ✔
373
                continue;
1 ✔
374
            }
375
            if (e1 == null) {
1 ✔
376
                return false;
×
377
            }
378
            if (!e1.deepEquals(e2)) {
1 ✔
379
                return false;
×
380
            }
381
        }
382

383
        return true;
1 ✔
384
    }
385

386
    protected void checkNonNullViewObject(final GraphView o) {
387
        if (o == null) {
1 ✔
388
            throw new NullPointerException();
1 ✔
389
        }
390
    }
1 ✔
391

392
    protected void checkGraphViewObject(final GraphView o) {
393
        if (!o.isMainView() && !(o instanceof GraphViewImpl)) {
1 ✔
394
            throw new IllegalArgumentException("The view is not from this implementation");
1 ✔
395
        }
396
    }
1 ✔
397

398
    protected void checkViewExist(final GraphViewImpl view) {
399
        int id = view.storeId;
1 ✔
400
        if (id == NULL_VIEW || id >= length || views[id] != view) {
1 ✔
401
            throw new IllegalArgumentException("The view doesn't exist");
1 ✔
402
        }
403
    }
1 ✔
404

405
    private void checkDirectedAllowed() {
406
        if (graphStore.isUndirected()) {
1 ✔
407
            throw new RuntimeException("Can't get a directed subgraph from an undirected graph");
×
408
        }
409
    }
1 ✔
410
}
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