|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. |
| 6 | + */ |
| 7 | +package org.hibernate.persister.entity.mutation; |
| 8 | + |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.BitSet; |
| 11 | + |
| 12 | +import org.hibernate.sql.model.MutationTarget; |
| 13 | +import org.hibernate.sql.model.TableMapping; |
| 14 | + |
| 15 | +/** |
| 16 | + * Represents a Set of TableMapping(s); table mappings are |
| 17 | + * identified by an ordered unique id: the order in which |
| 18 | + * they are updated within the scope of a particular persister. |
| 19 | + * This makes it possible to store a set of them as a bitset, |
| 20 | + * which is typically more efficient than using a {@link java.util.Set}. |
| 21 | + * These table ids come from {@link org.hibernate.sql.model.TableMapping#getRelativePosition}. |
| 22 | + * <p>N.B. Make sure to not store TableMappings from different |
| 23 | + * persisters, as their unique identifiers will overlap: |
| 24 | + * we'll only verify a mismatch if assertions are enabled.</p> |
| 25 | + */ |
| 26 | +public final class TableSet { |
| 27 | + |
| 28 | + private BitSet bits; |
| 29 | + private Object[] checks; //Meant for assertions only |
| 30 | + |
| 31 | + public void add(final TableMapping tableMapping) { |
| 32 | + if ( bits == null ) { |
| 33 | + bits = new BitSet(); |
| 34 | + } |
| 35 | + assert addForChecks( tableMapping ); |
| 36 | + bits.set( tableMapping.getRelativePosition() ); |
| 37 | + } |
| 38 | + |
| 39 | + public boolean isEmpty() { |
| 40 | + return bits == null; |
| 41 | + } |
| 42 | + |
| 43 | + public boolean contains(final TableMapping tableMapping) { |
| 44 | + assert matchRead( tableMapping ); |
| 45 | + return bits != null && bits.get( tableMapping.getRelativePosition() ); |
| 46 | + } |
| 47 | + |
| 48 | + //Meant for assertions only |
| 49 | + private boolean matchRead(final TableMapping tableMapping) { |
| 50 | + if ( bits != null ) { |
| 51 | + final int index = tableMapping.getRelativePosition(); |
| 52 | + if ( bits.get( index ) ) { |
| 53 | + return checks[index] == tableMapping; |
| 54 | + } |
| 55 | + } |
| 56 | + return true; //to make the assertion happy |
| 57 | + } |
| 58 | + |
| 59 | + //Meant for assertions only |
| 60 | + private boolean addForChecks(final TableMapping tableMapping) { |
| 61 | + final int position = tableMapping.getRelativePosition(); |
| 62 | + ensureCapacity( position ); |
| 63 | + if ( checks[position] != null ) { |
| 64 | + //pre-existing in the set: verify it's the same one. |
| 65 | + if ( checks[position] != tableMapping ) { |
| 66 | + return false;//fail the assertion |
| 67 | + } |
| 68 | + } |
| 69 | + checks[position] = tableMapping; |
| 70 | + return true; //to make the assertion happy |
| 71 | + } |
| 72 | + |
| 73 | + //Meant for assertions only |
| 74 | + private void ensureCapacity(final int position) { |
| 75 | + final int increments = 3; //Needs to be at least 1. |
| 76 | + if ( checks == null ) { |
| 77 | + checks = new Object[position + increments]; |
| 78 | + } |
| 79 | + else if ( checks.length <= position ) { |
| 80 | + checks = Arrays.copyOf( checks, position + increments ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments