Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  package com.sleepycat.je.tree;
  
Caches the VLSN sequence for the LN entries in a BIN, when VLSN preservation and caching are configured. A VLSN is added to the cache when an LN is evicted from a BIN. When the LN is resident, there is no need for caching because the LN contains the VLSN. See BIN.setTarget. This strategy works because an LN is always cached during a read or write operation, and only evicted after that based on eviction policies.
 
 public abstract class VLSNCache {
 
     public abstract long get(int idx);
     public abstract VLSNCache set(int idxlong valIN parent);
     public abstract VLSNCache copy(int fromint toint n);
     public abstract long getMemorySize();

    
An EMPTY_CACHE is used initially for each BIN until the need arises to add to the cache. The cache will remain empty if LNs are never evicted or version caching is not configured, which is always the case for standalone JE.
 
     public static VLSNCache EMPTY_CACHE = new VLSNCache() {

        
The EMPTY_CACHE always return a null VLSN sequence.
 
         @Override
         public long get(int idx) {
             return ..getSequence();
         }

        
When adding to the cache the EMPTY_CACHE is mutated into a DefaultCache.
 
         @Override
         public VLSNCache set(int idxlong valIN parent) {
 
             if (!parent.getDatabase().getDbEnvironment().getCacheVLSN()) {
                 return this;
             }
 
             if (val == ..getSequence()) {
                 return this;
             }
 
             final VLSNCache newCache = new DefaultCache
                 (parent.getMaxEntries(),
                  parent.getDatabase().
                         getDbEnvironment().
                         getCachedVLSNMinLength());
 
             parent.updateMemorySize(getMemorySize(), newCache.getMemorySize());
 
             return newCache.set(idxvalparent);
         }
 
         @Override
         public VLSNCache copy(int fromint toint n) {
             return this;
         }

        
An EMPTY_CACHE has no JE cache memory overhead because there is only one global instance.
 
         @Override
         public long getMemorySize() {
             return 0;
         }
     };
 
     public static class DefaultCache extends VLSNCache {

        
Maximum value of a VLSN indexed by number of bytes.
 
         private static long[] MAX_VLSN = {
             0x0L,
             0xFFL,
             0xFFFFL,
             0xFFFFFFL,
             0xFFFFFFFFL,
             0xFFFFFFFFFFL,
             0xFFFFFFFFFFFFL,
             0xFFFFFFFFFFFFFFL,
             0x7FFFFFFFFFFFFFFFL,
        };
        private final byte[] byteArray;
        private final int bytesPerVlsn;
        public DefaultCache(int capacityint nBytes) {
            assert capacity >= 1;
            assert nBytes >= 1;
            assert nBytes <= 8;
             = nBytes;
             = new byte[capacity * ];
        }
        /* Only for use by the Sizeof utility. */
        public DefaultCache(@SuppressWarnings("unused"SizeofMarker marker) {
             = 0;
             = null;
        }
        @Override
        public long get(int idx) {
            int i = idx * ;
            final int end = i + ;
            long val = ([i] & 0xFF);
            for (i += 1; i < endi += 1) {
                val <<= 8;
                val |= ([i] & 0xFF);
            }
            if (val == 0) {
                return ..getSequence();
            }
            return val;
        }

        
Mutates to a DefaultCache with a larger number of bytes if necessary to hold the given value.
        @Override
        public DefaultCache set(int idxlong valIN parent) {
            assert idx >= 0;
            assert idx < . / ;
            assert val != 0;
            if (val == ..getSequence()) {
                val = 0;
            }
            /*
             * If the value can't be represented using bytesPerVlsn, mutate
             * to a cache with a larger number of bytes.
             */
            if (val > []) {
                final int capacity = . / ;
                DefaultCache newCache =
                    new DefaultCache(capacity + 1);
                parent.updateMemorySize(getMemorySize(),
                                        newCache.getMemorySize());
                /*
                 * Set new value in new cache, and copy other values from old
                 * cache.
                 */
                newCache = newCache.set(idxvalparent);
                for (int i = 0; i < capacityi += 1) {
                    if (i != idx) {
                        newCache = newCache.set(iget(idx), parent);
                    }
                }
                return newCache;
            }
            /* Set VLSN sequence in this cache. */
            int i = ((idx + 1) * ) - 1;
            final int end = i - ;
            [i] = (byte) (val & 0xFF);
            for (i -= 1; i > endi -= 1) {
                val >>= 8;
                [i] = (byte) (val & 0xFF);
            }
            assert ((val & 0xFFFFFFFFFFFFFF00L) == 0) : val;
            return this;
        }
        @Override
        public DefaultCache copy(int fromint toint n) {
            System.arraycopy(,
                             from * ,
                             ,
                             to * ,
                             n * );
            return this;
        }
        @Override
        public long getMemorySize() {
            return . +
                   MemoryBudget.byteArraySize(.);
        }
    }
New to GrepCode? Check out our FAQ X