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

gephi / graphstore / #655

25 Oct 2026 01:15PM UTC coverage: 91.954% (+0.06%) from 91.898%
#655

push

web-flow
Set an attribute only while its column is in the store (#361)

* Set an attribute only while its column is in the store

Element.setAttribute and removeAttribute, including the variants with a
timestamp or an interval, checked the column before writing without the table
lock, so a concurrent removeColumn made the write fail with an
ArrayIndexOutOfBoundsException, the index update fail with a
NullPointerException, or left the value in the cleared slot, where a column
added later read a value of the wrong type. The value is now written only if
the column is still in the store, checked under the attributes lock, and
removeColumn removes the column before clearing the values. Otherwise the
setter throws the IllegalArgumentException of a removed column. An index
update finds no index for a column added or removed concurrently, and removing
a column that is not the registered one fails before anything is changed.

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

* Look the column up by key under the table lock when removing it

removeColumn(String) resolved the key before taking the graph and table locks, so a
column removed and added again in between could be removed instead of the one named.
Also adds a test for removing a time attribute of an element whose attributes are
shorter than the column index.

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

---------

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

33 of 34 new or added lines in 4 files covered. (97.06%)

12389 of 13473 relevant lines covered (91.95%)

0.92 hits per line

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

94.12
/src/main/java/org/gephi/graph/impl/ColumnStore.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.objects.ObjectOpenHashSet;
21
import it.unimi.dsi.fastutil.shorts.ShortRBTreeSet;
22
import it.unimi.dsi.fastutil.shorts.ShortSortedSet;
23
import java.util.ArrayList;
24
import java.util.Iterator;
25
import java.util.List;
26
import java.util.Set;
27
import org.gephi.graph.api.Column;
28
import org.gephi.graph.api.ColumnIterable;
29
import org.gephi.graph.api.Element;
30
import org.gephi.graph.api.Origin;
31

32
public class ColumnStore<T extends Element> implements ColumnIterable {
33

34
    // Config
35
    protected final static int MAX_SIZE = 65534;
36
    // Const
37
    protected final static int NULL_ID = -1;
38
    protected final static short NULL_SHORT = Short.MIN_VALUE;
39
    // Configuration
40
    protected final ConfigurationImpl configuration;
41
    // GraphStore
42
    protected final GraphStore graphStore;
43
    // Element
44
    protected final Class<T> elementType;
45
    // Columns
46
    protected final Object2ShortMap<String> idMap;
47
    protected final ColumnImpl[] columns;
48
    protected final ShortSortedSet garbageQueue;
49
    // Index
50
    protected final IndexStore<T> indexStore;
51
    // Version
52
    protected final List<TableObserverImpl> observers;
53
    // Locking (optional)
54
    protected final TableLockImpl lock;
55
    // Variables
56
    protected int length;
57

58
    public ColumnStore(Class<T> elementType, boolean indexed) {
59
        this(null, elementType);
1 ✔
60
    }
1 ✔
61

62
    public ColumnStore(GraphStore graphStore, Class<T> elementType) {
1 ✔
63
        if (MAX_SIZE >= Short.MAX_VALUE - Short.MIN_VALUE + 1) {
64
            throw new RuntimeException("Column Store size can't exceed 65534");
65
        }
66
        this.graphStore = graphStore;
1 ✔
67
        if (graphStore == null) {
1 ✔
68
            // Used for testing only
69
            configuration = new ConfigurationImpl();
1 ✔
70
        } else {
71
            configuration = graphStore.configuration;
1 ✔
72
        }
73
        this.lock = configuration.isEnableAutoLocking() ? new TableLockImpl() : null;
1 ✔
74
        this.garbageQueue = new ShortRBTreeSet();
1 ✔
75
        this.idMap = new Object2ShortOpenHashMap<>(MAX_SIZE);
1 ✔
76
        this.columns = new ColumnImpl[MAX_SIZE];
1 ✔
77
        this.elementType = elementType;
1 ✔
78
        this.indexStore = new IndexStore<>(this);
1 ✔
79
        idMap.defaultReturnValue(NULL_SHORT);
1 ✔
80
        this.observers = new ArrayList<>();
1 ✔
81
    }
1 ✔
82

83
    public void addColumn(final Column column) {
84
        checkNonNullColumnObject(column);
1 ✔
85
        checkIndexStatus(column);
1 ✔
86

87
        graphReadLock();
1 ✔
88
        lock();
1 ✔
89
        try {
90
            final ColumnImpl columnImpl = (ColumnImpl) column;
1 ✔
91
            short id = idMap.getShort(columnImpl.getId());
1 ✔
92
            if (id == NULL_SHORT) {
1 ✔
93
                if (!garbageQueue.isEmpty()) {
1 ✔
94
                    id = garbageQueue.firstShort();
1 ✔
95
                    garbageQueue.remove(id);
1 ✔
96
                } else {
97
                    id = intToShort(length);
1 ✔
98
                    if (length >= MAX_SIZE) {
1 ✔
99
                        throw new RuntimeException("Maximum number of columns reached at " + MAX_SIZE);
×
100
                    }
101
                    length++;
1 ✔
102
                }
103
                idMap.put(column.getId(), id);
1 ✔
104
                int intIndex = shortToInt(id);
1 ✔
105
                columnImpl.setStoreId(intIndex);
1 ✔
106
                columns[intIndex] = columnImpl;
1 ✔
107
                if (indexStore != null) {
1 ✔
108
                    indexStore.addColumn(columnImpl);
1 ✔
109
                }
110

111
                // Index attributes
112
                if (graphStore != null && columnImpl.table != null) {
1 ✔
113
                    for (Element e : graphStore.getElements(columnImpl.table)) {
1 ✔
114
                        e.setAttribute(column, column.getDefaultValue());
1 ✔
115
                        if (indexStore != null) {
1 ✔
116
                            // Setting the value only updates the indexes the element is in
117
                            indexStore.index(columnImpl, (T) e);
1 ✔
118
                        }
119
                    }
1 ✔
120
                }
121
            } else {
1 ✔
122
                throw new IllegalArgumentException("The column " + column.getId() + " already exist");
1 ✔
123
            }
124
        } finally {
125
            unlock();
1 ✔
126
            graphReadUnlock();
1 ✔
127
        }
128
    }
1 ✔
129

130
    public void removeColumn(final Column column) {
131
        checkNonNullColumnObject(column);
1 ✔
132

133
        graphReadLock();
1 ✔
134
        lock();
1 ✔
135
        try {
136
            final ColumnImpl columnImpl = (ColumnImpl) column;
1 ✔
137

138
            short id = idMap.getShort(column.getId());
1 ✔
139
            if (id == NULL_SHORT || columns[shortToInt(id)] != columnImpl) {
1 ✔
140
                throw new IllegalArgumentException("The column doesnt exist");
1 ✔
141
            }
142
            // Removed before clearing the values, setters only write while the column is in the store
143
            idMap.removeShort(column.getId());
1 ✔
144
            int intId = shortToInt(id);
1 ✔
145
            columns[intId] = null;
1 ✔
146

147
            // Clean attributes
148
            if (graphStore != null && columnImpl.table != null) {
1 ✔
149
                boolean timeMapColumn = columnImpl.isDynamicAttribute();
1 ✔
150
                for (Element e : graphStore.getElements(columnImpl.table)) {
1 ✔
151
                    ElementImpl element = (ElementImpl) e;
1 ✔
152
                    Object oldValue = element.attributes.setAttribute(column, null);
1 ✔
153
                    if (timeMapColumn) {
1 ✔
154
                        element.removeFromTimeIndex(oldValue);
1 ✔
155
                    }
156
                }
1 ✔
157
            }
158

159
            garbageQueue.add(id);
1 ✔
160
            if (indexStore != null) {
1 ✔
161
                indexStore.removeColumn((ColumnImpl) column);
1 ✔
162
            }
163
            columnImpl.setStoreId(NULL_ID);
1 ✔
164
        } finally {
165
            unlock();
1 ✔
166
            graphReadUnlock();
1 ✔
167
        }
168
    }
1 ✔
169

170
    public void removeColumn(final String key) {
171
        checkNonNullObject(key);
1 ✔
172

173
        graphReadLock();
1 ✔
174
        lock();
1 ✔
175
        try {
176
            ColumnImpl col = getColumn(key);
1 ✔
177
            if (col == null) {
1 ✔
NEW
178
                throw new IllegalArgumentException("The column doesnt exist");
×
179
            }
180
            removeColumn(col);
1 ✔
181
        } finally {
182
            unlock();
1 ✔
183
            graphReadUnlock();
1 ✔
184
        }
185
    }
1 ✔
186

187
    public int getColumnIndex(final String key) {
188
        checkNonNullObject(key);
1 ✔
189
        short id = idMap.getShort(key.toLowerCase());
1 ✔
190
        if (id == NULL_SHORT) {
1 ✔
191
            throw new IllegalArgumentException("The column doesnt exist");
1 ✔
192
        }
193
        return shortToInt(id);
1 ✔
194
    }
195

196
    public ColumnImpl getColumnByIndex(final int index) {
197
        if (index < 0 || index >= columns.length) {
1 ✔
198
            throw new IllegalArgumentException("The column doesnt exist");
×
199
        }
200
        ColumnImpl a = columns[index];
1 ✔
201
        if (a == null) {
1 ✔
202
            throw new IllegalArgumentException("The column doesnt exist");
1 ✔
203
        }
204
        return a;
1 ✔
205
    }
206

207
    public ColumnImpl getColumn(final String key) {
208
        checkNonNullObject(key);
1 ✔
209
        short id = idMap.getShort(key.toLowerCase());
1 ✔
210
        if (id == NULL_SHORT) {
1 ✔
211
            return null;
1 ✔
212
        }
213
        return columns[shortToInt(id)];
1 ✔
214
    }
215

216
    public boolean hasColumn(String key) {
217
        checkNonNullObject(key);
1 ✔
218
        return idMap.containsKey(key.toLowerCase());
1 ✔
219
    }
220

221
    @Override
222
    public Iterator<Column> iterator() {
223
        return new ColumnStoreIterator();
1 ✔
224
    }
225

226
    @Override
227
    public ColumnImpl[] toArray() {
228
        lock();
1 ✔
229
        try {
230
            ColumnImpl[] cols = new ColumnImpl[size()];
1 ✔
231
            int j = 0;
1 ✔
232
            for (int i = 0; i < length; i++) {
1 ✔
233
                ColumnImpl c = columns[i];
1 ✔
234
                if (c != null) {
1 ✔
235
                    cols[j++] = c;
1 ✔
236
                }
237
            }
238
            return cols;
1 ✔
239
        } finally {
240
            unlock();
1 ✔
241
        }
242
    }
243

244
    @Override
245
    public List<Column> toList() {
246
        lock();
1 ✔
247
        try {
248
            List<Column> cols = new ArrayList<>(size());
1 ✔
249
            for (int i = 0; i < length; i++) {
1 ✔
250
                ColumnImpl c = columns[i];
1 ✔
251
                if (c != null) {
1 ✔
252
                    cols.add(c);
1 ✔
253
                }
254
            }
255
            return cols;
1 ✔
256
        } finally {
257
            unlock();
1 ✔
258
        }
259
    }
260

261
    @Override
262
    public void doBreak() {
263
        unlock();
1 ✔
264
    }
1 ✔
265

266
    public Set<String> getColumnKeys() {
267
        lock();
1 ✔
268
        try {
269
            return new ObjectOpenHashSet<>(idMap.keySet());
1 ✔
270
        } finally {
271
            unlock();
1 ✔
272
        }
273
    }
274

275
    public int size() {
276
        return length - garbageQueue.size();
1 ✔
277
    }
278

279
    public int size(Origin origin) {
280
        checkNonNullObject(origin);
1 ✔
281
        lock();
1 ✔
282
        try {
283
            int res = 0;
1 ✔
284
            for (int i = 0; i < length; i++) {
1 ✔
285
                ColumnImpl c = columns[i];
1 ✔
286
                if (c != null && c.origin.equals(origin)) {
1 ✔
287
                    res++;
1 ✔
288
                }
289
            }
290
            return res;
1 ✔
291
        } finally {
292
            unlock();
1 ✔
293
        }
294
    }
295

296
    protected TableObserverImpl createTableObserver(TableImpl table, boolean withDiff) {
297
        if (observers != null) {
1 ✔
298
            lock();
1 ✔
299
            try {
300
                TableObserverImpl observer = new TableObserverImpl(table, withDiff);
1 ✔
301
                observers.add(observer);
1 ✔
302

303
                return observer;
1 ✔
304
            } finally {
305
                unlock();
1 ✔
306
            }
307
        }
308
        return null;
×
309
    }
310

311
    protected void destroyTablesObserver(TableObserverImpl observer) {
312
        if (observers != null) {
1 ✔
313
            lock();
1 ✔
314
            try {
315
                observers.remove(observer);
1 ✔
316
                observer.destroyObserver();
1 ✔
317
            } finally {
318
                unlock();
1 ✔
319
            }
320
        }
321
    }
1 ✔
322

323
    short intToShort(final int id) {
324
        return (short) (id + Short.MIN_VALUE + 1);
1 ✔
325
    }
326

327
    int shortToInt(final short id) {
328
        return id - Short.MIN_VALUE - 1;
1 ✔
329
    }
330

331
    void lock() {
332
        if (lock != null) {
1 ✔
333
            lock.lock();
1 ✔
334
        }
335
    }
1 ✔
336

337
    void unlock() {
338
        if (lock != null) {
1 ✔
339
            lock.unlock();
1 ✔
340
        }
341
    }
1 ✔
342

343
    // The graph lock is taken before the table lock, as when elements are added
344
    private void graphReadLock() {
345
        if (graphStore != null) {
1 ✔
346
            graphStore.autoReadLock();
1 ✔
347
        }
348
    }
1 ✔
349

350
    private void graphReadUnlock() {
351
        if (graphStore != null) {
1 ✔
352
            graphStore.autoReadUnlock();
1 ✔
353
        }
354
    }
1 ✔
355

356
    void checkNonNullObject(final Object o) {
357
        if (o == null) {
1 ✔
358
            throw new NullPointerException();
×
359
        }
360
    }
1 ✔
361

362
    void checkNonNullColumnObject(final Object o) {
363
        if (o == null) {
1 ✔
364
            throw new NullPointerException();
1 ✔
365
        }
366
        if (!(o instanceof ColumnImpl)) {
1 ✔
367
            throw new ClassCastException("Must be ColumnImpl object");
1 ✔
368
        }
369
    }
1 ✔
370

371
    void checkIndexStatus(final Column column) {
372
        if (indexStore == null && column.isIndexed()) {
1 ✔
373
            throw new IllegalArgumentException("Can't add an indexed column to a non indexed store");
×
374
        }
375
    }
1 ✔
376

377
    private final class ColumnStoreIterator implements Iterator<Column> {
378

379
        private int index;
380
        private ColumnImpl pointer;
381

382
        public ColumnStoreIterator() {
1 ✔
383
            lock();
1 ✔
384
        }
1 ✔
385

386
        @Override
387
        public boolean hasNext() {
388
            if (pointer != null) {
1 ✔
389
                return true;
×
390
            }
391
            while (index < length && (pointer = columns[index++]) == null) {
1 ✔
392
            }
393
            if (pointer == null) {
1 ✔
394
                unlock();
1 ✔
395
                return false;
1 ✔
396
            }
397
            return true;
1 ✔
398
        }
399

400
        @Override
401
        public ColumnImpl next() {
402
            ColumnImpl c = pointer;
1 ✔
403
            pointer = null;
1 ✔
404
            return c;
1 ✔
405
        }
406

407
        @Override
408
        public void remove() {
409
            throw new UnsupportedOperationException("Not supported");
1 ✔
410
        }
411
    }
412

413
    public boolean deepEquals(ColumnStore<T> obj) {
414
        if (obj == null) {
1 ✔
415
            return false;
×
416
        }
417
        if (this.elementType != obj.elementType && (this.elementType == null || !this.elementType
1 ✔
418
                .equals(obj.elementType))) {
×
419
            return false;
×
420
        }
421
        Iterator<Column> itr1 = this.iterator();
1 ✔
422
        Iterator<Column> itr2 = obj.iterator();
1 ✔
423
        boolean itr1Closed = false;
1 ✔
424
        boolean itr2Closed = false;
1 ✔
425
        try {
426
            while (itr1.hasNext()) {
1 ✔
427
                if (!itr2.hasNext()) {
1 ✔
428
                    itr2Closed = true;
×
429
                    return false;
×
430
                }
431
                Column c1 = itr1.next();
1 ✔
432
                Column c2 = itr2.next();
1 ✔
433
                if (!c1.equals(c2)) {
1 ✔
434
                    return false;
1 ✔
435
                }
436
            }
1 ✔
437
            itr1Closed = true;
1 ✔
438
            if (itr2.hasNext()) {
1 ✔
439
                return false;
×
440
            }
441
            itr2Closed = true;
1 ✔
442
            return true;
1 ✔
443
        } finally {
444
            if (!itr1Closed) {
1 ✔
445
                this.doBreak();
1 ✔
446
            }
447
            if (!itr2Closed) {
1 ✔
448
                obj.doBreak();
1 ✔
449
            }
450
        }
451
    }
452

453
    public int deepHashCode() {
454
        int hash = 3;
1 ✔
455
        hash = 11 * hash + (this.elementType != null ? this.elementType.hashCode() : 0);
1 ✔
456
        lock();
1 ✔
457
        try {
458
            for (int i = 0; i < length; i++) {
1 ✔
459
                ColumnImpl c = columns[i];
1 ✔
460
                if (c != null) {
1 ✔
461
                    hash = 11 * hash + c.deepHashCode();
1 ✔
462
                }
463
            }
464
        } finally {
465
            unlock();
1 ✔
466
        }
467
        // TODO what about timestampmap
468
        return hash;
1 ✔
469
    }
470
}
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