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.cleaner;
  
 
Stores a sorted list of LSN offsets in a packed short representation. Each stored value is the difference between two consecutive offsets. The stored values are stored as one or more shorts where each short holds 0x7fff values. Shorts are in LSB order. The value is negated if more shorts for the same offset follow; this works because offsets are always positive values.
 
 public class PackedOffsets implements Loggable {
 
     private short[] data;
     private int size;

    
Creates an empty object.
 
     public PackedOffsets() {
 
         /*
          * Verify assumption in FileSummaryLN that a new PackedOffsets instance
          * has no extra extra memory that must be budgeted.
          */
         assert getExtraMemorySize() == 0;
     }

    
Returns an iterator over all offsets.
 
     Iterator iterator() {
         return new Iterator();
     }

    
Packs the given offsets, replacing any offsets stored in this object.
 
     public void pack(long[] offsets) {
 
         /* Allocate a maximum sized new data array. */
         short[] newData = new short[offsets.length * 3];
 
         /* Pack the sorted offsets. */
         Arrays.sort(offsets);
         int dataIndex = 0;
         long priorVal = 0;
         for (int i = 0; i < offsets.lengthi += 1) {
             long val = offsets[i];
             dataIndex = append(newDatadataIndexval - priorVal);
             priorVal = val;
         }
 
         /* Copy in the exact sized new data. */
          = new short[dataIndex];
         System.arraycopy(newData, 0, , 0, dataIndex);
          = offsets.length;
     }

    
Returns the unpacked offsets.
 
     long[] toArray() {
         long[] offsets = new long[];
         int index = 0;
         Iterator iter = iterator();
         while (iter.hasNext()) {
             offsets[index++] = iter.next();
         }
         assert index == ;
         return offsets;
     }

    
Copies the given value as a packed long to the array starting at the given index. Returns the index of the next position in the array.
 
     private int append(short[] toint indexlong val) {
 
         assert val >= 0;
 
         while (true) {
             short s = (short) (val & 0x7fff);
             val >>>= 15;
             if (val > 0) {
                 to[index++] = (short) (-1 - s);
            } else {
                to[index++] = s;
                break;
            }
        }
        return index;
    }

    
An iterator over all offsets.
    class Iterator {
        private int index;
        private long priorVal;
        private Iterator() {
        }
        boolean hasNext() {
            return  != null &&  < .;
        }
        long next() {
            long val = ;
            for (int shift = 0;; shift += 15) {
                long s = [++];
                if (s < 0) {
                    val += (-1 - s) << shift;
                } else {
                    val += s << shift;
                    break;
                }
            }
             = val;
            return val;
        }
    }

    
Return the extra memory used by this object when the pack() method has been called to allocate the data array.
    public int getExtraMemorySize() {
        if ( != null) {
            return MemoryBudget.shortArraySize(.);
        } else {
            return 0;
        }
    }

    
    public int getLogSize() {
        int len = ( != null) ? . : 0;
        return  (LogUtils.getPackedIntLogSize() +
                 LogUtils.getPackedIntLogSize(len) +
                 (len * .));
    }

    
    public void writeToLog(ByteBuffer buf) {
        LogUtils.writePackedInt(buf);
        if ( != null) {
            LogUtils.writePackedInt(buf.);
            for (int i = 0; i < .i += 1) {
                LogUtils.writeShort(buf[i]);
            }
        } else {
            LogUtils.writePackedInt(buf, 0);
        }
    }

    
    public void readFromLog(ByteBuffer bufint entryVersion) {
        boolean unpacked = (entryVersion < 6);
         = LogUtils.readInt(bufunpacked);
        int len = LogUtils.readInt(bufunpacked);
        if (len > 0) {
             = new short[len];
            for (int i = 0; i < leni += 1) {
                [i] = LogUtils.readShort(buf);
            }
        }
    }

    
    public void dumpLog(StringBuilder bufboolean verbose) {
        if ( > 0) {
            Iterator i = iterator();
            buf.append("<offsets size=\"");
            buf.append();
            buf.append("\">");
            while (i.hasNext()) {
                buf.append("0x");
                buf.append(Long.toHexString(i.next()));
                buf.append(' ');
            }
            buf.append("</offsets>");
        } else {
            buf.append("<offsets size=\"0\"/>");
        }
    }

    
    public long getTransactionId() {
        return -1;
    }

    

See also:
com.sleepycat.je.log.Loggable.logicalEquals(com.sleepycat.je.log.Loggable) Always return false, this item should never be compared.
    public boolean logicalEquals(Loggable other) {
        return false;
    }
    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        dumpLog(buftrue);
        return buf.toString();
    }
New to GrepCode? Check out our FAQ X