Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.cafs; |
| 2 | |
| 3 | import static aQute.lib.io.IO.*; |
| 4 | |
| 5 | import java.io.*; |
| 6 | import java.nio.channels.*; |
| 7 | import java.security.*; |
| 8 | import java.util.*; |
| 9 | import java.util.concurrent.atomic.*; |
| 10 | import java.util.zip.*; |
| 11 | |
| 12 | import aQute.lib.index.*; |
| 13 | import aQute.libg.cryptography.*; |
| 14 | |
| 15 | /** |
| 16 | * CAFS implements a SHA-1 based file store. The basic idea is that every file |
| 17 | * in the universe has a unique SHA-1. Hard to believe but people smarter than |
| 18 | * me have come to that conclusion. This class maintains a compressed store of |
| 19 | * SHA-1 identified files. So if you have the SHA-1, you can get the contents. |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 20 | * This makes it easy to store a SHA-1 instead of the whole file or maintain a |
| 21 | * naming scheme. An added advantage is that it is always easy to verify you get |
| 22 | * the right stuff. The SHA-1 Content Addressable File Store is the core |
| 23 | * underlying idea in Git. |
| 24 | */ |
| 25 | public class CAFS implements Closeable, Iterable<SHA1> { |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 26 | final static byte[] CAFS; |
| 27 | final static byte[] CAFE; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 28 | final static String INDEXFILE = "index.idx"; |
| 29 | final static String STOREFILE = "store.cafs"; |
| 30 | final static String ALGORITHM = "SHA-1"; |
| 31 | final static int KEYLENGTH = 20; |
| 32 | final static int HEADERLENGTH = 4 // CAFS |
| 33 | + 4 // flags |
| 34 | + 4 // compressed length |
| 35 | + 4 // uncompressed length |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | + KEYLENGTH // key |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | + 2 // header checksum |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 38 | ; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 39 | |
| 40 | final File home; |
| 41 | Index index; |
| 42 | RandomAccessFile store; |
| 43 | FileChannel channel; |
| 44 | |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 45 | static { |
| 46 | try { |
| 47 | CAFS = "CAFS".getBytes("UTF-8"); |
| 48 | CAFE = "CAFE".getBytes("UTF-8"); |
| 49 | } catch (Throwable e) { |
| 50 | throw new ExceptionInInitializerError(e); |
| 51 | } |
| 52 | } |
| 53 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 54 | /** |
| 55 | * Constructor for a Content Addressable File Store |
| 56 | * |
| 57 | * @param home |
| 58 | * @param create |
| 59 | * @throws Exception |
| 60 | */ |
| 61 | public CAFS(File home, boolean create) throws Exception { |
| 62 | this.home = home; |
| 63 | if (!home.isDirectory()) { |
| 64 | if (create) { |
| 65 | home.mkdirs(); |
| 66 | } else |
| 67 | throw new IllegalArgumentException("CAFS requires a directory with create=false"); |
| 68 | } |
| 69 | |
| 70 | index = new Index(new File(home, INDEXFILE), KEYLENGTH); |
| 71 | store = new RandomAccessFile(new File(home, STOREFILE), "rw"); |
| 72 | channel = store.getChannel(); |
| 73 | if (store.length() < 0x100) { |
| 74 | if (create) { |
| 75 | store.write(CAFS); |
| 76 | for (int i = 1; i < 64; i++) |
| 77 | store.writeInt(0); |
| 78 | channel.force(true); |
| 79 | } else |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 80 | throw new IllegalArgumentException("Invalid store file, length is too short " + store); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 81 | System.err.println(store.length()); |
| 82 | } |
| 83 | store.seek(0); |
| 84 | if (!verifySignature(store, CAFS)) |
| 85 | throw new IllegalArgumentException("Not a valid signature: CAFS at start of file"); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Store an input stream in the CAFS while calculating and returning the |
| 91 | * SHA-1 code. |
| 92 | * |
| 93 | * @param in |
| 94 | * The input stream to store. |
| 95 | * @return The SHA-1 code. |
| 96 | * @throws Exception |
| 97 | * if anything goes wrong |
| 98 | */ |
| 99 | public SHA1 write(InputStream in) throws Exception { |
| 100 | |
| 101 | Deflater deflater = new Deflater(); |
| 102 | MessageDigest md = MessageDigest.getInstance(ALGORITHM); |
| 103 | DigestInputStream din = new DigestInputStream(in, md); |
| 104 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 105 | DeflaterOutputStream dout = new DeflaterOutputStream(bout, deflater); |
| 106 | copy(din, dout); |
| 107 | |
| 108 | synchronized (store) { |
| 109 | // First check if it already exists |
| 110 | SHA1 sha1 = new SHA1(md.digest()); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 111 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 112 | long search = index.search(sha1.digest()); |
| 113 | if (search > 0) |
| 114 | return sha1; |
| 115 | |
| 116 | byte[] compressed = bout.toByteArray(); |
| 117 | |
| 118 | // we need to append this file to our store, |
| 119 | // which requires a lock. However, we are in a race |
| 120 | // so others can get the lock between us getting |
| 121 | // the length and someone else getting the lock. |
| 122 | // So we must verify after we get the lock that the |
| 123 | // length was unchanged. |
| 124 | FileLock lock = null; |
| 125 | try { |
| 126 | long insertPoint; |
| 127 | int recordLength = compressed.length + HEADERLENGTH; |
| 128 | |
| 129 | while (true) { |
| 130 | insertPoint = store.length(); |
| 131 | lock = channel.lock(insertPoint, recordLength, false); |
| 132 | |
| 133 | if (store.length() == insertPoint) |
| 134 | break; |
| 135 | |
| 136 | // We got the wrong lock, someone else |
| 137 | // got in between reading the length |
| 138 | // and locking |
| 139 | lock.release(); |
| 140 | } |
| 141 | int totalLength = deflater.getTotalIn(); |
| 142 | store.seek(insertPoint); |
| 143 | update(sha1.digest(), compressed, totalLength); |
| 144 | index.insert(sha1.digest(), insertPoint); |
| 145 | return sha1; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 146 | } |
| 147 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 148 | if (lock != null) |
| 149 | lock.release(); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Read the contents of a sha 1 key. |
| 156 | * |
| 157 | * @param sha1 |
| 158 | * The key |
| 159 | * @return An Input Stream on the content or null of key not found |
| 160 | * @throws Exception |
| 161 | */ |
| 162 | public InputStream read(final SHA1 sha1) throws Exception { |
| 163 | synchronized (store) { |
| 164 | long offset = index.search(sha1.digest()); |
| 165 | if (offset < 0) |
| 166 | return null; |
| 167 | |
| 168 | byte[] readSha1; |
| 169 | byte[] buffer; |
| 170 | store.seek(offset); |
| 171 | if (!verifySignature(store, CAFE)) |
| 172 | throw new IllegalArgumentException("No signature"); |
| 173 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 174 | int flags = store.readInt(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 175 | int compressedLength = store.readInt(); |
| 176 | int uncompressedLength = store.readInt(); |
| 177 | readSha1 = new byte[KEYLENGTH]; |
| 178 | store.read(readSha1); |
| 179 | SHA1 rsha1 = new SHA1(readSha1); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 180 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 181 | if (!sha1.equals(rsha1)) |
| 182 | throw new IOException("SHA1 read and asked mismatch: " + sha1 + " " + rsha1); |
| 183 | |
| 184 | short crc = store.readShort(); // Read CRC |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 185 | if (crc != checksum(flags, compressedLength, uncompressedLength, readSha1)) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 186 | throw new IllegalArgumentException("Invalid header checksum: " + sha1); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 187 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 188 | buffer = new byte[compressedLength]; |
| 189 | store.readFully(buffer); |
| 190 | return getSha1Stream(sha1, buffer, uncompressedLength); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | public boolean exists(byte[] sha1) throws Exception { |
| 195 | return index.search(sha1) >= 0; |
| 196 | } |
| 197 | |
| 198 | public void reindex() throws Exception { |
| 199 | long length; |
| 200 | synchronized (store) { |
| 201 | length = store.length(); |
| 202 | if (length < 0x100) |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 203 | throw new IllegalArgumentException("Store file is too small, need to be at least 256 bytes: " + store); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | RandomAccessFile in = new RandomAccessFile(new File(home, STOREFILE), "r"); |
| 207 | try { |
| 208 | byte[] signature = new byte[4]; |
| 209 | in.readFully(signature); |
| 210 | if (!Arrays.equals(CAFS, signature)) |
| 211 | throw new IllegalArgumentException("Store file does not start with CAFS: " + in); |
| 212 | |
| 213 | in.seek(0x100); |
| 214 | File ixf = new File(home, "index.new"); |
| 215 | Index index = new Index(ixf, KEYLENGTH); |
| 216 | |
| 217 | while (in.getFilePointer() < length) { |
| 218 | long entry = in.getFilePointer(); |
| 219 | SHA1 sha1 = verifyEntry(in); |
| 220 | index.insert(sha1.digest(), entry); |
| 221 | } |
| 222 | |
| 223 | synchronized (store) { |
| 224 | index.close(); |
| 225 | File indexFile = new File(home, INDEXFILE); |
| 226 | ixf.renameTo(indexFile); |
| 227 | this.index = new Index(indexFile, KEYLENGTH); |
| 228 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 229 | } |
| 230 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 231 | in.close(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | public void close() throws IOException { |
| 236 | synchronized (store) { |
| 237 | try { |
| 238 | store.close(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 239 | } |
| 240 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 241 | index.close(); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | private SHA1 verifyEntry(RandomAccessFile in) throws IOException, NoSuchAlgorithmException { |
| 247 | byte[] signature = new byte[4]; |
| 248 | in.readFully(signature); |
| 249 | if (!Arrays.equals(CAFE, signature)) |
| 250 | throw new IllegalArgumentException("File is corrupted: " + in); |
| 251 | |
| 252 | /* int flags = */in.readInt(); |
| 253 | int compressedSize = in.readInt(); |
| 254 | int uncompressedSize = in.readInt(); |
| 255 | byte[] key = new byte[KEYLENGTH]; |
| 256 | in.readFully(key); |
| 257 | SHA1 sha1 = new SHA1(key); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 258 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 259 | byte[] buffer = new byte[compressedSize]; |
| 260 | in.readFully(buffer); |
| 261 | |
| 262 | InputStream xin = getSha1Stream(sha1, buffer, uncompressedSize); |
| 263 | xin.skip(uncompressedSize); |
| 264 | xin.close(); |
| 265 | return sha1; |
| 266 | } |
| 267 | |
| 268 | private boolean verifySignature(DataInput din, byte[] org) throws IOException { |
| 269 | byte[] read = new byte[org.length]; |
| 270 | din.readFully(read); |
| 271 | return Arrays.equals(read, org); |
| 272 | } |
| 273 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 274 | private InputStream getSha1Stream(final SHA1 sha1, byte[] buffer, final int total) throws NoSuchAlgorithmException { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 275 | ByteArrayInputStream in = new ByteArrayInputStream(buffer); |
| 276 | InflaterInputStream iin = new InflaterInputStream(in) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 277 | int count = 0; |
| 278 | final MessageDigest digestx = MessageDigest.getInstance(ALGORITHM); |
| 279 | final AtomicBoolean calculated = new AtomicBoolean(); |
| 280 | |
| 281 | @Override |
| 282 | public int read(byte[] data, int offset, int length) throws IOException { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 283 | int size = super.read(data, offset, length); |
| 284 | if (size <= 0) |
| 285 | eof(); |
| 286 | else { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 287 | count += size; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 288 | this.digestx.update(data, offset, size); |
| 289 | } |
| 290 | return size; |
| 291 | } |
| 292 | |
| 293 | public int read() throws IOException { |
| 294 | int c = super.read(); |
| 295 | if (c < 0) |
| 296 | eof(); |
| 297 | else { |
| 298 | count++; |
| 299 | this.digestx.update((byte) c); |
| 300 | } |
| 301 | return c; |
| 302 | } |
| 303 | |
| 304 | void eof() throws IOException { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 305 | if (calculated.getAndSet(true)) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 306 | return; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 307 | |
| 308 | if (count != total) |
| 309 | throw new IOException("Counts do not match. Expected to read: " + total + " Actually read: " |
| 310 | + count); |
| 311 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 312 | SHA1 calculatedSha1 = new SHA1(digestx.digest()); |
| 313 | if (!sha1.equals(calculatedSha1)) |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 314 | throw (new IOException("SHA1 caclulated and asked mismatch, asked: " + sha1 + ", \nfound: " |
| 315 | + calculatedSha1)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | public void close() throws IOException { |
| 319 | eof(); |
| 320 | super.close(); |
| 321 | } |
| 322 | }; |
| 323 | return iin; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Update a record in the store, assuming the store is at the right |
| 328 | * position. |
| 329 | * |
| 330 | * @param sha1 |
| 331 | * The checksum |
| 332 | * @param compressed |
| 333 | * The compressed length |
| 334 | * @param totalLength |
| 335 | * The uncompressed length |
| 336 | * @throws IOException |
| 337 | * The exception |
| 338 | */ |
| 339 | private void update(byte[] sha1, byte[] compressed, int totalLength) throws IOException { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 340 | // System.err.println("pos: " + store.getFilePointer()); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 341 | store.write(CAFE); // 00-03 Signature |
| 342 | store.writeInt(0); // 04-07 Flags for the future |
| 343 | store.writeInt(compressed.length); // 08-11 Length deflated data |
| 344 | store.writeInt(totalLength); // 12-15 Length |
| 345 | store.write(sha1); // 16-35 |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 346 | store.writeShort(checksum(0, compressed.length, totalLength, sha1)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 347 | store.write(compressed); |
| 348 | channel.force(false); |
| 349 | } |
| 350 | |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 351 | short checksum(int flags, int compressedLength, int totalLength, byte[] sha1) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 352 | CRC32 crc = new CRC32(); |
| 353 | crc.update(flags); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 354 | crc.update(flags >> 8); |
| 355 | crc.update(flags >> 16); |
| 356 | crc.update(flags >> 24); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 357 | crc.update(compressedLength); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 358 | crc.update(compressedLength >> 8); |
| 359 | crc.update(compressedLength >> 16); |
| 360 | crc.update(compressedLength >> 24); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 361 | crc.update(totalLength); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 362 | crc.update(totalLength >> 8); |
| 363 | crc.update(totalLength >> 16); |
| 364 | crc.update(totalLength >> 24); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 365 | crc.update(sha1); |
| 366 | return (short) crc.getValue(); |
| 367 | } |
| 368 | |
| 369 | public Iterator<SHA1> iterator() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 370 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 371 | return new Iterator<SHA1>() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 372 | long position = 0x100; |
| 373 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 374 | public boolean hasNext() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 375 | synchronized (store) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 376 | try { |
| 377 | return position < store.length(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 378 | } |
| 379 | catch (IOException e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 380 | throw new RuntimeException(e); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | public SHA1 next() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 386 | synchronized (store) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 387 | try { |
| 388 | store.seek(position); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 389 | byte[] signature = new byte[4]; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 390 | store.readFully(signature); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 391 | if (!Arrays.equals(CAFE, signature)) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 392 | throw new IllegalArgumentException("No signature"); |
| 393 | |
| 394 | int flags = store.readInt(); |
| 395 | int compressedLength = store.readInt(); |
| 396 | int totalLength = store.readInt(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 397 | byte[] sha1 = new byte[KEYLENGTH]; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 398 | store.readFully(sha1); |
| 399 | short crc = store.readShort(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 400 | if (crc != checksum(flags, compressedLength, totalLength, sha1)) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 401 | throw new IllegalArgumentException("Header checksum fails"); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 402 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 403 | position += HEADERLENGTH + compressedLength; |
| 404 | return new SHA1(sha1); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 405 | } |
| 406 | catch (IOException e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 407 | throw new RuntimeException(e); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 408 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | public void remove() { |
| 413 | throw new UnsupportedOperationException("Remvoe not supported, CAFS is write once"); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 414 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 415 | }; |
| 416 | } |
| 417 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 418 | public boolean isEmpty() throws IOException { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 419 | synchronized (store) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 420 | return store.getFilePointer() <= 256; |
| 421 | } |
| 422 | } |
| 423 | } |