srecord noeq PicturesByMD5(File dir) { settable S extension = ".jpeg"; transient Lock lock = lock(); // current count of images (calculated lazily) transient new VarWithNotify imageCount; File imageFile(S md5) { ret newFile(dir, assertMD5(md5) + extension); } bool has(S md5) { ret isFile(imageFile(md5)); } BufferedImage get(S md5) { ret decodeImage(imageFile(md5)); } swappable BufferedImage decodeImage(File f) { ret loadImage2(f); } bool put(S md5, byte[] imageData) { // double-checked locking, yay! if (has(md5)) false; lock lock; if (has(md5)) false; saveBinaryFile(imageFile(md5), imageData); if (imageCount.has()) imageCount.set(imageCount()+1); true; } // -actually scans the directory // -only sees files with the correct extension int freshImageCount() { lock lock; ret setAndReturn(imageCount, l(freshImageFiles())); } L freshImageFiles() { ret listFilesWithExtension(dir, extension); } // get current image count in an efficient way // -does not notice when other objects/processes // add or remove images in the directory int imageCount() { lock lock; if (!imageCount.has()) freshImageCount(); ret imageCount!; } IVarWithNotify varImageCount() { ret imageCount; } }