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

gephi / graphstore / #511

09 Oct 2025 08:00PM UTC coverage: 90.947% (+0.005%) from 90.942%
#511

push

web-flow
Column no-index gets it version incremented (#252)

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

12 existing lines in 1 file now uncovered.

11583 of 12736 relevant lines covered (90.95%)

0.91 hits per line

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

92.51
/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 org.gephi.graph.api.DirectedSubgraph;
21
import org.gephi.graph.api.Edge;
22
import org.gephi.graph.api.Graph;
23
import org.gephi.graph.api.GraphView;
24
import org.gephi.graph.api.Interval;
25
import org.gephi.graph.api.Node;
26
import org.gephi.graph.api.Subgraph;
27
import org.gephi.graph.api.UndirectedSubgraph;
28

29
public class GraphViewStore {
30

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

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

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

57
    public GraphViewImpl createView(boolean nodes, boolean edges) {
58
        graphStore.autoWriteLock();
1✔
59
        try {
60
            GraphViewImpl graphView = new GraphViewImpl(graphStore, nodes, edges);
1✔
61
            addView(graphView);
1✔
62
            return graphView;
1✔
63
        } finally {
64
            graphStore.autoWriteUnlock();
1✔
65
        }
66
    }
67

68
    public GraphViewImpl createView(GraphView view) {
69
        return createView(view, true, true);
1✔
70
    }
71

72
    public GraphViewImpl createView(GraphView view, boolean nodes, boolean edges) {
73
        if (view.isMainView()) {
1✔
74
            graphStore.autoWriteLock();
1✔
75
            try {
76
                GraphViewImpl graphView = new GraphViewImpl(graphStore, nodes, edges);
1✔
77
                graphView.fill();
1✔
78
                addView(graphView);
1✔
79
                return graphView;
1✔
80
            } finally {
81
                graphStore.autoWriteUnlock();
1✔
82
            }
83
        } else {
84
            checkNonNullViewObject(view);
1✔
85
            checkGraphViewObject(view);
1✔
86
            checkViewExist((GraphViewImpl) view);
1✔
87

88
            graphStore.autoWriteLock();
1✔
89
            try {
90
                GraphViewImpl graphView = new GraphViewImpl((GraphViewImpl) view, nodes, edges);
1✔
91
                addView(graphView);
1✔
92
                return graphView;
1✔
93
            } finally {
94
                graphStore.autoWriteUnlock();
1✔
95
            }
96
        }
97
    }
98

99
    public void destroyView(GraphView view) {
100
        if (view.isMainView()) {
1✔
101
            throw new IllegalArgumentException("Can't delete the main view");
1✔
102
        }
103
        graphStore.autoWriteLock();
1✔
104
        try {
105
            checkNonNullViewObject(view);
1✔
106
            checkGraphViewObject(view);
1✔
107

108
            TimeIndexStore nodeTimeStore = graphStore.timeStore.nodeIndexStore;
1✔
109
            if (nodeTimeStore != null) {
1✔
110
                nodeTimeStore.deleteViewIndex(((GraphViewImpl) view).getDirectedGraph());
1✔
111
            }
112

113
            TimeIndexStore edgeTimeStore = graphStore.timeStore.edgeIndexStore;
1✔
114
            if (edgeTimeStore != null) {
1✔
115
                edgeTimeStore.deleteViewIndex(((GraphViewImpl) view).getDirectedGraph());
1✔
116
            }
117

118
            IndexStore<Node> nodeIndexStore = graphStore.nodeTable.store.indexStore;
1✔
119
            if (nodeIndexStore != null) {
1✔
120
                nodeIndexStore.deleteViewIndex(((GraphViewImpl) view).getDirectedGraph());
1✔
121
            }
122

123
            IndexStore<Edge> edgeIndexStore = graphStore.edgeTable.store.indexStore;
1✔
124
            if (edgeIndexStore != null) {
1✔
125
                edgeIndexStore.deleteViewIndex(((GraphViewImpl) view).getDirectedGraph());
1✔
126
            }
127

128
            removeView((GraphViewImpl) view);
1✔
129
        } finally {
130
            graphStore.autoWriteUnlock();
1✔
131
        }
132
    }
1✔
133

134
    public void setTimeInterval(GraphView view, Interval interval) {
135
        checkNonNullViewObject(view);
1✔
136
        checkViewExist((GraphViewImpl) view);
1✔
137

138
        graphStore.autoWriteLock();
1✔
139
        try {
140
            GraphViewImpl graphView = (GraphViewImpl) view;
1✔
141
            graphView.setTimeInterval(interval);
1✔
142
        } finally {
143
            graphStore.autoWriteUnlock();
1✔
144
        }
145
    }
1✔
146

147
    public boolean contains(GraphView view) {
148
        graphStore.autoReadLock();
1✔
149
        try {
150
            checkNonNullViewObject(view);
1✔
151
            GraphViewImpl viewImpl = (GraphViewImpl) view;
1✔
152
            int id = viewImpl.storeId;
1✔
153
            if (id != NULL_VIEW && id < length && views[id] == view) {
1✔
154
                return true;
1✔
155
            }
156
            return false;
1✔
157
        } finally {
158
            graphStore.autoReadUnlock();
1✔
159
        }
160
    }
161

162
    public int size() {
163
        return length - garbageQueue.size();
1✔
164
    }
165

166
    public Subgraph getGraph(GraphView view) {
167
        checkNonNullViewObject(view);
1✔
168
        checkGraphViewObject(view);
1✔
169

170
        if (graphStore.isUndirected()) {
1✔
171
            if (view.isMainView()) {
1✔
UNCOV
172
                return graphStore.undirectedDecorator;
×
173
            }
174
            return ((GraphViewImpl) view).getUndirectedGraph();
1✔
175
        } else {
176
            if (view.isMainView()) {
1✔
UNCOV
177
                return graphStore;
×
178
            }
179
            return ((GraphViewImpl) view).getDirectedGraph();
1✔
180
        }
181
    }
182

183
    public DirectedSubgraph getDirectedGraph(GraphView view) {
184
        checkNonNullViewObject(view);
1✔
185
        checkGraphViewObject(view);
1✔
186

187
        if (view.isMainView()) {
1✔
188
            return graphStore;
1✔
189
        }
190

191
        checkDirectedAllowed();
1✔
192
        return ((GraphViewImpl) view).getDirectedGraph();
1✔
193
    }
194

195
    public UndirectedSubgraph getUndirectedGraph(GraphView view) {
196
        checkNonNullViewObject(view);
1✔
197
        checkGraphViewObject(view);
1✔
198

199
        if (view.isMainView()) {
1✔
UNCOV
200
            return graphStore.undirectedDecorator;
×
201
        }
202
        return ((GraphViewImpl) view).getUndirectedGraph();
1✔
203
    }
204

205
    public GraphView getVisibleView() {
206
        return visibleView;
1✔
207
    }
208

209
    public void setVisibleView(GraphView view) {
210
        if (view == null || view == graphStore.mainGraphView) {
1✔
211
            visibleView = graphStore.mainGraphView;
1✔
212
        } else {
213
            checkNonNullViewObject(view);
1✔
214
            checkViewExist((GraphViewImpl) view);
1✔
215
            visibleView = view;
1✔
216
        }
217
    }
1✔
218

219
    public GraphObserverImpl createGraphObserver(Graph graph, boolean withDiff) {
220
        GraphViewImpl graphViewImpl = (GraphViewImpl) graph.getView();
1✔
221
        checkViewExist(graphViewImpl);
1✔
222

223
        return graphViewImpl.createGraphObserver(graph, withDiff);
1✔
224
    }
225

226
    public void destroyGraphObserver(GraphObserverImpl graphObserver) {
227
        GraphViewImpl graphViewImpl = (GraphViewImpl) graphObserver.graph.getView();
1✔
228
        if (graphObserver.graphStore != this.graphStore) {
1✔
229
            throw new RuntimeException("This observer doesn't belong to this store");
1✔
230
        }
231

232
        graphViewImpl.destroyGraphObserver(graphObserver);
1✔
233
    }
1✔
234

235
    protected void addNode(NodeImpl node) {
236
        if (views.length > 0) {
1✔
237
            for (GraphViewImpl view : views) {
1✔
238
                if (view != null) {
1✔
239
                    view.ensureNodeVectorSize(node);
1✔
240
                }
241
            }
242
        }
243
    }
1✔
244

245
    protected void removeNode(NodeImpl node) {
246
        if (views.length > 0) {
1✔
247
            for (GraphViewImpl view : views) {
1✔
248
                if (view != null) {
1✔
249
                    view.removeNode(node);
1✔
250
                }
251
            }
252
        }
253
    }
1✔
254

255
    protected void addEdge(EdgeImpl edge) {
256
        if (views.length > 0) {
1✔
257
            for (GraphViewImpl view : views) {
1✔
258
                if (view != null) {
1✔
259
                    view.ensureEdgeVectorSize(edge);
1✔
260

261
                    if (view.nodeView && !view.edgeView) {
1✔
262
                        view.addEdgeInNodeView(edge);
1✔
263
                    }
264
                }
265
            }
266
        }
267
    }
1✔
268

269
    protected void setEdgeType(EdgeImpl edge, int oldType, boolean wasMutual) {
270
        if (views.length > 0) {
1✔
271
            for (GraphViewImpl view : views) {
1✔
272
                if (view != null) {
1✔
273
                    if ((view.nodeView && !view.edgeView) || (view.edgeView && view.containsEdge(edge))) {
1✔
274
                        view.setEdgeType(edge, oldType, wasMutual);
1✔
275
                    }
276
                }
277
            }
278
        }
279
    }
1✔
280

281
    protected void removeEdge(EdgeImpl edge) {
282
        if (views.length > 0) {
1✔
283
            for (GraphViewImpl view : views) {
1✔
284
                if (view != null) {
1✔
285
                    view.removeEdge(edge);
1✔
286
                }
287
            }
288
        }
289
    }
1✔
290

291
    protected int addView(final GraphViewImpl view) {
292
        checkNonNullViewObject(view);
1✔
293

294
        int id;
295
        if (!garbageQueue.isEmpty()) {
1✔
296
            id = garbageQueue.firstInt();
1✔
297
            garbageQueue.remove(id);
1✔
298
        } else {
299
            id = length++;
1✔
300
            ensureArraySize(id);
1✔
301
        }
302
        views[id] = view;
1✔
303
        view.storeId = id;
1✔
304
        return id;
1✔
305
    }
306

307
    protected void removeView(final GraphViewImpl view) {
308
        checkViewExist(view);
1✔
309

310
        int id = view.storeId;
1✔
311
        views[id] = null;
1✔
312
        garbageQueue.add(id);
1✔
313
        view.storeId = NULL_VIEW;
1✔
314

315
        view.destroyAllObservers();
1✔
316

317
        // Check if not visible view
318
        if (visibleView == view) {
1✔
319
            visibleView = graphStore.mainGraphView;
1✔
320
        }
321
    }
1✔
322

323
    private void ensureArraySize(int index) {
324
        if (index >= views.length) {
1✔
325
            GraphViewImpl[] newArray = new GraphViewImpl[index + 1];
1✔
326
            System.arraycopy(views, 0, newArray, 0, views.length);
1✔
327
            views = newArray;
1✔
328
        }
329
    }
1✔
330

331
    public int deepHashCode() {
UNCOV
332
        int hash = 5;
×
UNCOV
333
        for (GraphViewImpl view : this.views) {
×
334
            hash = 67 * hash + view.deepHashCode();
×
335
        }
UNCOV
336
        hash = 67 * hash + this.length;
×
337
        return hash;
×
338
    }
339

340
    public boolean deepEquals(GraphViewStore obj) {
341
        if (obj == null) {
1✔
UNCOV
342
            return false;
×
343
        }
344
        if (this.length != obj.length) {
1✔
UNCOV
345
            return false;
×
346
        }
347
        int l = this.views.length;
1✔
348
        if (l != obj.views.length) {
1✔
UNCOV
349
            return false;
×
350
        }
351
        for (int i = 0; i < l; i++) {
1✔
352
            GraphViewImpl e1 = this.views[i];
1✔
353
            GraphViewImpl e2 = obj.views[i];
1✔
354

355
            if (e1 == e2) {
1✔
356
                continue;
1✔
357
            }
358
            if (e1 == null) {
1✔
UNCOV
359
                return false;
×
360
            }
361
            if (!e1.deepEquals(e2)) {
1✔
UNCOV
362
                return false;
×
363
            }
364
        }
365

366
        return true;
1✔
367
    }
368

369
    protected void checkNonNullViewObject(final GraphView o) {
370
        if (o == null) {
1✔
371
            throw new NullPointerException();
1✔
372
        }
373
    }
1✔
374

375
    protected void checkGraphViewObject(final GraphView o) {
376
        if (!o.isMainView() && !(o instanceof GraphViewImpl)) {
1✔
377
            throw new IllegalArgumentException("The view is not from this implementation");
1✔
378
        }
379
    }
1✔
380

381
    protected void checkViewExist(final GraphViewImpl view) {
382
        int id = view.storeId;
1✔
383
        if (id == NULL_VIEW || id >= length || views[id] != view) {
1✔
384
            throw new IllegalArgumentException("The view doesn't exist");
1✔
385
        }
386
    }
1✔
387

388
    private void checkDirectedAllowed() {
389
        if (graphStore.isUndirected()) {
1✔
UNCOV
390
            throw new RuntimeException("Can't get a directed subgraph from an undirected graph");
×
391
        }
392
    }
1✔
393
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc