/** * Creates write back implementation of {@link FileManagerTransaction}. * The write back implementation isolates modification from underlaying * cache. The changes are made visible by commit. * @param root for which the {@link FileManagerTransaction} should be created. * @return the write back implementation of {@link FileManagerTransaction}. */ public static FileManagerTransaction writeBack(URL root) { return new WriteBackTransaction(root); }
@Override protected void commit() throws IOException { LOG.log(Level.FINE, "Committed:{0}", getRootDir()); try { for (File f : deleted) { f.delete(); } flushFiles(true); } finally { clean(); } }
private void maybeFlush() throws IOException { if (disableCache || memExhausted) { LOG.log(Level.FINE, "Memory exhausted:{0}", getRootDir()); flushFiles(false); memExhausted = false; createCacheRef(); } }
private void flushFiles(boolean inCommit) throws IOException { LOG.log(Level.FINE, "Flushing:{0}", getRootDir()); doFlushFiles(contentCache.first(), inCommit); doFlushFiles(contentCache.second(), inCommit); }
private void addFile( @NonNull final Location location, @NonNull final String packageName, @NonNull final CachedFileObject fo) { LOG.log(Level.FINE, "File added to cache:{0}:{1}", new Object[] { fo.getFile(), root }); // check whether the softref has been freed: final Map<String, Map<File, CachedFileObject>> cache = getCacheLine(location, false); Map<File, CachedFileObject> dirContent = cache.get(packageName); if (dirContent == null) { dirContent = new HashMap<File, CachedFileObject>(); cache.put(packageName, dirContent); } dirContent.put(toFile(fo), fo); }
@NonNull final String packageName, @NonNull final Iterable<JavaFileObject> files) { final Collection<File> added = listDir(location, packageName); Iterable<JavaFileObject> res = files; if (deleted.isEmpty() && added.isEmpty()) { Collection<JavaFileObject> toAdd = getFileObjects(location, packageName); Collection<Iterable<JavaFileObject>> chain = new ArrayList<Iterable<JavaFileObject>>(2); chain.add(toAdd);
@Override @NonNull JavaFileObject createFileObject( @NonNull final Location location, @NonNull final File file, @NonNull final File root, @NullAllowed final JavaFileFilterImplementation filter, @NullAllowed final Charset encoding) { final String[] pkgNamePair = FileObjects.getFolderAndBaseName(FileObjects.getRelativePath(root, file), File.separatorChar); String pname = FileObjects.convertFolder2Package(pkgNamePair[0], File.separatorChar); CachedFileObject cfo = getFileObject(location, pname, pkgNamePair[1], false); if (cfo != null) { return cfo; } String relPath = FileObjects.getRelativePath(root, file); File shadowRoot = new File(root.getParent(), root.getName() + WORK_SUFFIX); File shadowFile = new File(shadowRoot, relPath); workDirs.add(shadowRoot); cfo = new CachedFileObject(this, file, pname, pkgNamePair[1], filter, encoding); cfo.setShadowFile(shadowFile); addFile(location, pname, cfo); if (!shadowRoot.mkdirs() && !shadowRoot.exists() && !shadowRoot.isDirectory()) { throw new IllegalStateException(); } return cfo; }
@NonNull private Collection<File> listDir( @NonNull final Location location, @NonNull final String dir) { final Map<String, Map<File, CachedFileObject>> cache = getCacheLine(location, true); Map<File, CachedFileObject> content = cache.get(dir); return content == null ? Collections.<File>emptyList() : content.keySet(); }
@Override protected void rollBack() throws IOException { clean(); }
WriteBackTransaction(URL root) { super(true); this.root = root; deleted = new HashSet<File>(); createCacheRef(); }
@Override JavaFileObject readFileObject( @NonNull final Location location, @NonNull final String dirName, @NonNull final String relativeName) { return getFileObject(location, dirName, relativeName, true); }
/** * This method makes a copy of the storage * @param dir * @return */ @NonNull private Collection<JavaFileObject> getFileObjects( @NonNull final Location location, @NonNull final String dir) { final Map<String, Map<File, CachedFileObject>> cache = getCacheLine(location, true); Map<File, CachedFileObject> content = cache.get(dir); return new ArrayList<JavaFileObject>(content.values()); }
@CheckForNull private CachedFileObject getFileObject( @NonNull final Location location, @NonNull final String dir, @NonNull final String file, @NonNull final boolean readOnly) { final Map<String, Map<File, CachedFileObject>> cache = getCacheLine(location, readOnly); final Map<File, CachedFileObject> content = cache.get(dir); if (content != null) { for (Map.Entry<File, CachedFileObject> en : content.entrySet()) { if (file.equals(en.getKey().getName())) { return en.getValue(); } } } return null; }