1 8 9 package com.sleepycat.je.cleaner; 10 11 import com.sleepycat.je.utilint.Tracer; 12 13 31 public class OffsetList { 32 33 static final int SEGMENT_CAPACITY = 100; 34 35 private Segment head; 36 private Segment tail; 37 private int size; 38 39 public OffsetList() { 40 head = new Segment(); 41 tail = head; 42 } 43 44 47 public boolean add(long value, boolean checkDupOffsets) { 48 49 50 if (checkDupOffsets) { 51 assert (!contains(value)) : 52 Tracer.getStackTrace(new Exception ("Dup Offset " + 53 Long.toHexString(value))); 54 } 55 56 60 Segment oldTail = tail; 61 tail = tail.add(value); 62 size += 1; 63 return tail != oldTail; 64 } 65 66 public int size() { 67 return size; 68 } 69 70 73 boolean merge(OffsetList other) { 74 75 boolean oneSegFreed = true; 76 Segment seg = other.head; 77 while (true) { 78 Segment next = seg.next(); 79 if (next != null) { 80 81 seg.setNext(head); 82 head = seg; 83 seg = next; 84 } else { 85 86 for (int i = 0; i < seg.size(); i += 1) { 87 if (add(seg.get(i), false)) { 88 89 assert oneSegFreed; 90 oneSegFreed = false; 91 } 92 } 93 break; 94 } 95 } 96 return oneSegFreed; 97 } 98 99 104 public long[] toArray() { 105 106 long[] a = new long[size]; 107 int next = 0; 108 109 segments: for (Segment seg = head; seg != null; seg = seg.next()) { 110 for (int i = 0; i < seg.size(); i += 1) { 111 if (next >= a.length) { 112 break segments; 113 } 114 a[next] = seg.get(i); 115 next += 1; 116 } 117 } 118 119 return a; 120 } 121 122 125 boolean contains(long offset) { 126 127 for (Segment seg = head; seg != null; seg = seg.next()) { 128 for (int i = 0; i < seg.size(); i += 1) { 129 if (seg.get(i) == offset) { 130 return true; 131 } 132 } 133 } 134 135 return false; 136 } 137 138 142 public static class Segment { 143 144 private int index; 145 private Segment next; 146 private int[] values; 147 148 149 public Segment() { 150 values = new int[SEGMENT_CAPACITY]; 151 } 152 153 157 Segment add(long value) { 158 if (index < values.length) { 159 160 164 values[index] = (int) value; 165 index += 1; 166 return this; 167 } else { 168 169 174 Segment seg = new Segment(); 175 seg.values[0] = (int) value; 176 seg.index = 1; 177 next = seg; 178 return seg; 179 } 180 } 181 182 185 long get(int i) { 186 return ((long) values[i]) & 0xFFFFFFFF; 187 } 188 189 192 Segment next() { 193 return next; 194 } 195 196 199 void setNext(Segment next) { 200 this.next = next; 201 } 202 203 206 int size() { 207 return index; 208 } 209 } 210 } 211 | Popular Tags |