/** * @param fileName * @return true if there is such a named file in this index */ public boolean fileExists(final String fileName) { readLock.lock(); try { return getFileList().contains(fileName); } finally { readLock.unlock(); } }
/** * Load implementation for FileListCacheKey; must return a * ConcurrentHashSet containing the names of all files in this Directory. */ private Object loadIntern() throws IOException { final String[] listAll = directory.listAll(); return new FileListCacheValue(listAll); }
/** * @return an array containing all names of existing "files" */ public String[] listFilenames() { readLock.lock(); try { return getFileList().toArray(); } finally { readLock.unlock(); } }
/** * Adds a new fileName in the list of files making up this index * @param fileName */ void addFileName(final String fileName) { writeLock.lock(); try { final FileListCacheValue fileList = getFileList(); boolean done = fileList.add(fileName); if (done) { updateFileList(fileList); if (trace) log.trace("Updated file listing: added " + fileName); } } finally { writeLock.unlock(); } }
/** * Optimized implementation to perform both a remove and an add * @param toRemove * @param toAdd */ public void removeAndAdd(final String toRemove, final String toAdd) { writeLock.lock(); try { FileListCacheValue fileList = getFileList(); boolean done = fileList.addAndRemove(toAdd, toRemove); if (done) { updateFileList(fileList); if (trace) { log.trace("Updated file listing: added " + toAdd + " and removed " + toRemove); } } } finally { writeLock.unlock(); } }
/** * Deleted a file from the list of files actively part of the index * @param fileName */ public void deleteFileName(final String fileName) { writeLock.lock(); try { FileListCacheValue fileList = getFileList(); boolean done = fileList.remove(fileName); if (done) { updateFileList(fileList); if (trace) log.trace("Updated file listing: removed " + fileName); } } finally { writeLock.unlock(); } }
/** * Adds a new fileName in the list of files making up this index * @param fileName */ void addFileName(final String fileName) { writeLock.lock(); try { final FileListCacheValue fileList = getFileList(); boolean done = fileList.add(fileName); if (done) { updateFileList(fileList); if (trace) log.trace("Updated file listing: added " + fileName); } } finally { writeLock.unlock(); } }
/** * Optimized implementation to perform both a remove and an add * @param toRemove * @param toAdd */ public void removeAndAdd(final String toRemove, final String toAdd) { writeLock.lock(); try { FileListCacheValue fileList = getFileList(); boolean done = fileList.addAndRemove(toAdd, toRemove); if (done) { updateFileList(fileList); if (trace) { log.trace("Updated file listing: added " + toAdd + " and removed " + toRemove); } } } finally { writeLock.unlock(); } }
/** * Deleted a file from the list of files actively part of the index * @param fileName */ public void deleteFileName(final String fileName) { writeLock.lock(); try { FileListCacheValue fileList = getFileList(); boolean done = fileList.remove(fileName); if (done) { updateFileList(fileList); if (trace) log.trace("Updated file listing: removed " + fileName); } } finally { writeLock.unlock(); } }
/** * @param fileName * @return true if there is such a named file in this index */ public boolean fileExists(final String fileName) { readLock.lock(); try { return getFileList().contains(fileName); } finally { readLock.unlock(); } }
@Override public FileListCacheValue readObject(final ObjectInput input) throws IOException { int size = UnsignedNumeric.readUnsignedInt(input); String[] names = new String[size]; for (int i = 0; i < size; i++) { names[i] = input.readUTF(); } return new FileListCacheValue(names); }
/** * @return an array containing all names of existing "files" */ public String[] listFilenames() { readLock.lock(); try { return getFileList().toArray(); } finally { readLock.unlock(); } }
public static void assertFileNotExists(Cache cache, String indexName, String fileName, long maxWaitForCondition, int affinitySegmentId) throws InterruptedException { FileListCacheValue fileList = (FileListCacheValue) cache.get(new FileListCacheKey(indexName, affinitySegmentId)); AssertJUnit.assertNotNull(fileList); AssertJUnit.assertFalse(fileList.contains(fileName)); //check is in sync: no waiting allowed in this case boolean allok = false; while (maxWaitForCondition >= 0 && !allok) { Thread.sleep(10); maxWaitForCondition -= 10; allok=true; FileMetadata metadata = (FileMetadata) cache.get(new FileCacheKey(indexName, fileName, affinitySegmentId)); if (metadata!=null) allok=false; for (int i = 0; i < 100; i++) { //bufferSize set to 0 as metadata might not be available, and it's not part of equals/hashcode anyway. ChunkCacheKey key = new ChunkCacheKey(indexName, fileName, i, 0, affinitySegmentId); if (cache.get(key)!=null) allok=false; } } assertTrue(allok); }
@Override public FileListCacheValue readObject(final ObjectInput input) throws IOException { int size = UnsignedNumeric.readUnsignedInt(input); String[] names = new String[size]; for (int i = 0; i < size; i++) { names[i] = input.readUTF(); } return new FileListCacheValue(names); }
private void assertFileAfterDeletion(Cache cache) { FileListCacheValue fileList = (FileListCacheValue) cache.get(new FileListCacheKey(INDEX_NAME, -1)); AssertJUnit.assertNotNull(fileList); AssertJUnit.assertFalse(fileList.contains(filename)); FileMetadata metadata = (FileMetadata) cache.get(new FileCacheKey(INDEX_NAME, filename, -1)); AssertJUnit.assertNotNull(metadata); long totalFileSize = metadata.getSize(); int chunkNumbers = (int)(totalFileSize / CHUNK_SIZE); for(int i = 0; i < chunkNumbers; i++) { AssertJUnit.assertNotNull(cache.get(new ChunkCacheKey(INDEX_NAME, filename, i, CHUNK_SIZE, -1))); } boolean fileNameExistsInCache = false; for(Object key : cache.keySet()) { if(key instanceof FileCacheKey) { FileCacheKey keyObj = (FileCacheKey) key; if(keyObj.getFileName().contains(filename)) { fileNameExistsInCache = true; } } } AssertJUnit.assertTrue(fileNameExistsInCache); }
/** * Load implementation for FileListCacheKey; must return a * ConcurrentHashSet containing the names of all files in this Directory. */ private Object loadIntern() throws IOException { final String[] listAll = directory.listAll(); return new FileListCacheValue(listAll); }
/** * Verified the file exists and has a specified value for readLock; * Consider that null should be interpreted as value 1; */ public static void assertFileExistsHavingRLCount(Cache cache, String fileName, String indexName, int expectedReadcount, int chunkSize, boolean expectRegisteredInFat, int affinitySegmentId) { FileListCacheValue fileList = (FileListCacheValue) cache.get(new FileListCacheKey(indexName,affinitySegmentId)); assertNotNull(fileList); assertTrue(fileList.contains(fileName) == expectRegisteredInFat); FileMetadata metadata = (FileMetadata) cache.get(new FileCacheKey(indexName, fileName, affinitySegmentId)); assertNotNull(metadata); long totalFileSize = metadata.getSize(); int chunkNumbers = (int)(totalFileSize / chunkSize); for (int i = 0; i < chunkNumbers; i++) { assertNotNull(cache.get(new ChunkCacheKey(indexName, fileName, i, metadata.getBufferSize(), affinitySegmentId))); } FileReadLockKey readLockKey = new FileReadLockKey(indexName,fileName, affinitySegmentId); Object value = cache.get(readLockKey); if (expectedReadcount <= 1) { assertTrue("readlock value is " + value, value == null || Integer.valueOf(1).equals(value)); } else { assertNotNull(value); assertTrue(value instanceof Integer); int v = (Integer)value; assertEquals(v, expectedReadcount); } }
/** * @return the current list of files being part of the index */ @GuardedBy("writeLock") private FileListCacheValue getFileList() { FileListCacheValue fileList = (FileListCacheValue) cache.get(fileListCacheKey); if (fileList == null) { fileList = new FileListCacheValue(); FileListCacheValue prev = (FileListCacheValue) cache.putIfAbsent(fileListCacheKey, fileList); if (prev != null) { fileList = prev; } } if (trace) log.trace("Refreshed file listing view"); return fileList; }
assertEquals(actualFileSize, totalFileSize); if (!ignoreFiles.contains(fileCacheKey.getFileName())) { assertTrue(fileCacheKey + " should not have existed", fileList.contains(fileCacheKey.getFileName()));
/** * @return the current list of files being part of the index */ @GuardedBy("writeLock") private FileListCacheValue getFileList() { FileListCacheValue fileList = (FileListCacheValue) cache.get(fileListCacheKey); if (fileList == null) { fileList = new FileListCacheValue(); FileListCacheValue prev = (FileListCacheValue) cache.putIfAbsent(fileListCacheKey, fileList); if (prev != null) { fileList = prev; } } if (trace) log.trace("Refreshed file listing view"); return fileList; }