h2/src/main/org/h2/mvstore/db/TransactionStore.java
/**
         * Get the size of the map as seen by this transaction.
         *
         * @return the size
         */
Get the size of the map as seen by this transaction.
@return the size
Get the size of the map as seen by this transaction . @return the size
public long sizeAsLong() {
            long sizeRaw = map.sizeAsLong();
            MVMap<Long, Object[]> undo = transaction.store.undoLog;
            long undoLogSize;
            synchronized (undo) {
                undoLogSize = undo.sizeAsLong();
            }
            if (undoLogSize == 0) {
                return sizeRaw;
            }
            if (undoLogSize > sizeRaw) {
                long size = 0;
                Cursor<K, VersionedValue> cursor = map.cursor(null);
                while (cursor.hasNext()) {
                    K key = cursor.next();
                    VersionedValue data = cursor.getValue();
                    data = getValue(key, readLogId, data);
                    if (data != null && data.value != null) {
                        size++;
                    }
                }
                return size;
            }
            synchronized (undo) {
                long size = map.sizeAsLong();
                MVMap<Object, Integer> temp = transaction.store.createTempMap();
                try {
                    for (Entry<Long, Object[]> e : undo.entrySet()) {
                        Object[] op = e.getValue();
                        int m = (Integer) op[0];
                        if (m != mapId) {
                            continue;
                        }
                        @SuppressWarnings("unchecked")
                        K key = (K) op[1];
                        if (get(key) == null) {
                            Integer old = temp.put(key, 1);
                            if (old == null) {
                                size--;
                            }
                        }
                    }
                } finally {
                    transaction.store.store.removeMap(temp);
                }
                return size;
            }
        }
public long sizeAsLong ( ) { long sizeRaw = map . sizeAsLong ( ) ; MVMap < Long , Object [ ] > undo = transaction . store . undoLog ; long undoLogSize ; synchronized ( undo ) { undoLogSize = undo . sizeAsLong ( ) ; } if ( undoLogSize == 0 ) { return sizeRaw ; } if ( undoLogSize > sizeRaw ) { long size = 0 ; Cursor < K , VersionedValue > cursor = map . cursor ( null ) ; while ( cursor . hasNext ( ) ) { K key = cursor . next ( ) ; VersionedValue data = cursor . getValue ( ) ; data = getValue ( key , readLogId , data ) ; if ( data != null && data . value != null ) { size ++ ; } } return size ; } synchronized ( undo ) { long size = map . sizeAsLong ( ) ; MVMap < Object , Integer > temp = transaction . store . createTempMap ( ) ; try { for ( Entry < Long , Object [ ] > e : undo . entrySet ( ) ) { Object [ ] op = e . getValue ( ) ; int m = ( Integer ) op [ 0 ] ; if ( m != mapId ) { continue ; } @ SuppressWarnings ( "unchecked" ) K key = ( K ) op [ 1 ] ; if ( get ( key ) == null ) { Integer old = temp . put ( key , 1 ) ; if ( old == null ) { size -- ; } } } } finally { transaction . store . store . removeMap ( temp ) ; } return size ; } }
Get the size of the map as seen by this transaction.
Get the size of the map as seen by this transaction .
// the undo log is larger than the map - // count the entries of the map // the undo log is smaller than the map - // scan the undo log and subtract invisible entries // re-fetch in case any transaction was committed now // a different map - ignore // count each key only once // (there might be multiple changes for the same key)