void loadFilesIntoCache(File dir, FileCache cache) { File[] files = dir.listFiles(); if ( files == null) return; for (File f : files) { if (f.isDirectory()) loadFilesIntoCache(f, cache); else if (f.getPath().endsWith(".nc") && f.length() > 0) { //System.out.println(" open "+f.getPath()); try { String want = StringUtil2.replace(f.getPath(), '\\', "/"); DatasetUrl durl = new DatasetUrl(null, want); cache.acquire(factory, durl); count++; } catch (IOException e) { // e.printStackTrace(); System.out.println(" *** failed on " + f.getPath()); } } } }
@Test public void testPeriodicClear() throws IOException { FileCache cache = new FileCache(0, 10, 60 * 60); testPeriodicCleanup(cache); Map<Object, FileCache.CacheElement> map = cache.getCache(); assert map.values().size() == 0 : map.values().size(); cache = new FileCache(5, 10, 60 * 60); testPeriodicCleanup(cache); map = cache.getCache(); assert map.values().size() == 5 : map.values().size(); }
System.out.printf("TestNetcdfFileCache%n"); loadFilesIntoCache(new File(TestDir.cdmLocalTestDataDir), cache); System.out.println(" loaded " + count); loadFilesIntoCache(new File(TestDir.cdmLocalTestDataDir), cache); map = cache.getCache(); cache.showCache(new Formatter(System.out)); checkAllSame(elem.list); loadFilesIntoCache(new File(TestDir.cdmLocalTestDataDir), cache); map = cache.getCache(); Assert.assertEquals(saveCount, map.values().size()); loadFilesIntoCache(new File(TestDir.cdmLocalTestDataDir), cache); loadFilesIntoCache(new File(TestDir.cdmLocalTestDataDir), cache); map = cache.getCache(); Assert.assertEquals(saveCount, map.values().size());
private void testPeriodicCleanup(FileCache cache) throws IOException { loadFilesIntoCache(new File(TestDir.cdmLocalTestDataDir), cache); System.out.println(" loaded " + count); // close all Map<Object, FileCache.CacheElement> map = cache.getCache(); List<FileCacheable> files = new ArrayList<>(); for (Object key : map.keySet()) { FileCache.CacheElement elem = map.get(key); assert elem.list.size() == 1; for (FileCache.CacheElement.CacheFile file : elem.list) { files.add(file.ncfile); } } System.out.println(" close " + files.size()); for (FileCacheable ncfile : files) { ncfile.close(); } cache.showCache(new Formatter(System.out)); cache.cleanup(10); }
@Test public void testConcurrentAccess() throws InterruptedException { loadFilesIntoCache(new File(TestDir.cdmLocalTestDataDir), cache); Map<Object, FileCache.CacheElement> map = cache.getCache(); List<String> files = new ArrayList<>();