/** returns true if all named files exist */ public static boolean allExists(String... files) { for (int i = 0; i < files.length; i++) { if (!Files.exists(files[i])) { logger.debug("Files " + files[i] + " doesn't exist"); return false; } } return true; }
/** returns true if all named files exist */ public static boolean allExists(String... files) { for (int i = 0; i < files.length; i++) { if (!Files.exists(files[i])) { logger.debug("Files " + files[i] + " doesn't exist"); return false; } } return true; }
/** Check that the a specified file exists as per Terrier's file system abstraction layer */ public static void main(String args[]) { System.out.println("Exists: " + args[0] + Files.exists(args[0])); }
/** Check that the a specified file exists as per Terrier's file system abstraction layer */ public static void main(String args[]) { System.out.println("Exists: " + args[0] + Files.exists(args[0])); }
@Override public boolean supports(IndexRef ref) { String l = ref.toString(); if (ref.size() > 1) return false; //this is a multi-index if (l.startsWith("http") || l.startsWith("https") || l.startsWith("concurrent")) return false; if (! l.endsWith(".properties")) return false; return Files.exists(l); }
@Override public boolean supports(IndexRef ref) { String l = ref.toString(); if (ref.size() > 1) return false; //this is a multi-index if (l.startsWith("http") || l.startsWith("https") || l.startsWith("concurrent")) return false; if (! l.endsWith(".properties")) return false; return Files.exists(l); }
/** Move an index from one location to another */ public static void renameIndex(String srcPath, String srcPrefix, String dstPath, String dstPrefix) throws IOException { final String actualPrefix = srcPrefix +'.'; for (String filename : Files.list(srcPath)) { if (filename.startsWith(actualPrefix)) { final String newFilename = filename.replaceFirst(srcPrefix, dstPrefix); if (! Files.rename(srcPath + "/" + filename, dstPath+"/"+ newFilename)) { final String srcExists = Files.exists(srcPath + "/" + filename) ? "exists" : "notexists"; final String destExists = Files.exists(dstPath+"/"+ newFilename) ? "exists" : "notexists"; throw new IOException("Rename of index structure file '"+srcPath + "/" + filename+"' ("+srcExists+") to " + "'"+ dstPath+"/"+ newFilename +"' ("+destExists+") failed - likely that source file is still open. " + "Possible indexing bug?"); } } } }
protected void loadBlacklist(String BlacklistSpecFilename) { //reads the trec_blacklist_docid file if (BlacklistSpecFilename != null && BlacklistSpecFilename.length() >0) { try { if (Files.exists(BlacklistSpecFilename)) { BufferedReader br =Files.openFileReader(BlacklistSpecFilename); String blackListedDocid = null; while ((blackListedDocid = br.readLine()) != null) { if (!blackListedDocid.startsWith("#") && !blackListedDocid.equals("")) DocIDBlacklist.add(blackListedDocid); } br.close(); } } catch (IOException ioe) { logger.error("Input/Output exception while reading the document black list." + "Stack trace follows", ioe); } } }
/** Move an index from one location to another */ public static void renameIndex(String srcPath, String srcPrefix, String dstPath, String dstPrefix) throws IOException { final String actualPrefix = srcPrefix +'.'; for (String filename : Files.list(srcPath)) { if (filename.startsWith(actualPrefix)) { final String newFilename = filename.replaceFirst(srcPrefix, dstPrefix); if (! Files.rename(srcPath + "/" + filename, dstPath+"/"+ newFilename)) { final String srcExists = Files.exists(srcPath + "/" + filename) ? "exists" : "notexists"; final String destExists = Files.exists(dstPath+"/"+ newFilename) ? "exists" : "notexists"; throw new IOException("Rename of index structure file '"+srcPath + "/" + filename+"' ("+srcExists+") to " + "'"+ dstPath+"/"+ newFilename +"' ("+destExists+") failed - likely that source file is still open. " + "Possible indexing bug?"); } } } }
protected void loadBlacklist(String BlacklistSpecFilename) { //reads the trec_blacklist_docid file if (BlacklistSpecFilename != null && BlacklistSpecFilename.length() >0) { try { if (Files.exists(BlacklistSpecFilename)) { BufferedReader br =Files.openFileReader(BlacklistSpecFilename); String blackListedDocid = null; while ((blackListedDocid = br.readLine()) != null) { if (!blackListedDocid.startsWith("#") && !blackListedDocid.equals("")) DocIDBlacklist.add(blackListedDocid); } br.close(); } } catch (IOException ioe) { logger.error("Input/Output exception while reading the document black list." + "Stack trace follows", ioe); } } }
protected void readDocumentBlacklist(String BlacklistSpecFilename) { //read the document blacklist if (BlacklistSpecFilename != null && BlacklistSpecFilename.length() >0) { try { DocIDBlacklist = new HashSet<String>(); if (Files.exists(BlacklistSpecFilename)) { BufferedReader br2 = Files.openFileReader(BlacklistSpecFilename); String blackListedDocid = null; while ((blackListedDocid = br2.readLine()) != null) { blackListedDocid = blackListedDocid.trim(); if (!blackListedDocid.startsWith("#") && !blackListedDocid.equals("")) DocIDBlacklist.add(blackListedDocid); } br2.close(); } } catch (IOException ioe) { logger.error("Input/Output exception while reading the document black list.", ioe); } } }
/** Write any dirty properties down to disk */ protected void flushProperties() throws IOException { if (dirtyProperties) { final String propertiesFilename = path + ApplicationSetup.FILE_SEPARATOR + prefix + PROPERTIES_SUFFIX; if ((Files.exists(propertiesFilename) && !Files .canWrite(propertiesFilename)) || (!Files.exists(propertiesFilename) && !Files .canWrite(path))) { logger.warn("Could not write to index properties at " + propertiesFilename + " because you do not have write permission on the index - some changes may be lost"); return; } final OutputStream outputStream = Files .writeFileStream(propertiesFilename); properties.store(outputStream, this.toString()); outputStream.close(); dirtyProperties = false; } }
/** Write any dirty properties down to disk */ protected void flushProperties() throws IOException { if (dirtyProperties) { final String propertiesFilename = path + ApplicationSetup.FILE_SEPARATOR + prefix + PROPERTIES_SUFFIX; if ((Files.exists(propertiesFilename) && !Files .canWrite(propertiesFilename)) || (!Files.exists(propertiesFilename) && !Files .canWrite(path))) { logger.warn("Could not write to index properties at " + propertiesFilename + " because you do not have write permission on the index - some changes may be lost"); return; } final OutputStream outputStream = Files .writeFileStream(propertiesFilename); properties.store(outputStream, this.toString()); outputStream.close(); dirtyProperties = false; } }
/** * Constructs an instance of the class for a given filename * Note that on a FileNotFoundException, this contructor will sleep for 2 seconds * before retrying to open the file. * @param filename String with the name of the underlying file * @throws java.io.IOException if an I/O error occurs */ public BitOutputStream(String filename) throws IOException { try{ dos = new DataOutputStream(Files.writeFileStream(filename)); } catch(FileNotFoundException fnfe) { final String dir = Files.getParent(filename); logger.warn("Could not open new BitOutputStream because it alleged file could not be found.", fnfe); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir) +" Dir.contentsSize="+Files.list(dir).length); sleep(1000); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir)+" Dir.contentsSize="+Files.list(dir).length); logger.warn("Retrying to write BitOutputStream."); dos = new DataOutputStream(Files.writeFileStream(filename)); logger.info("Previous warning can be ignored, BitOutputStream "+filename+" has opened successfully"); } init(); }
/** * Constructs an instance of the class for a given filename * Note that on a FileNotFoundException, this contructor will sleep for 2 seconds * before retrying to open the file. * @param filename String with the name of the underlying file * @throws java.io.IOException if an I/O error occurs */ public BitOutputStream(String filename) throws IOException { try{ dos = new DataOutputStream(Files.writeFileStream(filename)); } catch(FileNotFoundException fnfe) { final String dir = Files.getParent(filename); logger.warn("Could not open new BitOutputStream because it alleged file could not be found.", fnfe); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir) +" Dir.contentsSize="+Files.list(dir).length); sleep(1000); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir)+" Dir.contentsSize="+Files.list(dir).length); logger.warn("Retrying to write BitOutputStream."); dos = new DataOutputStream(Files.writeFileStream(filename)); logger.info("Previous warning can be ignored, BitOutputStream "+filename+" has opened successfully"); } init(); }
public ByteOutputStream(String filename) throws IOException { OutputStream file; try{ file = Files.writeFileStream(filename); init(file); } catch(FileNotFoundException fnfe) { final String dir = Files.getParent(filename); logger.warn("Could not open new ByteOutputStream because it alleged file could not be found.", fnfe); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir) +" Dir.contentsSize="+Files.list(dir).length); sleep(1000); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir)+" Dir.contentsSize="+Files.list(dir).length); logger.warn("Retrying to write BitOutputStream."); init(Files.writeFileStream(filename)); logger.info("Previous warning can be ignored, BitOutputStream "+filename+" has opened successfully"); } }
public ByteOutputStream(String filename) throws IOException { OutputStream file; try{ file = Files.writeFileStream(filename); init(file); } catch(FileNotFoundException fnfe) { final String dir = Files.getParent(filename); logger.warn("Could not open new ByteOutputStream because it alleged file could not be found.", fnfe); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir) +" Dir.contentsSize="+Files.list(dir).length); sleep(1000); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir)+" Dir.contentsSize="+Files.list(dir).length); logger.warn("Retrying to write BitOutputStream."); init(Files.writeFileStream(filename)); logger.info("Previous warning can be ignored, BitOutputStream "+filename+" has opened successfully"); } }
if (qrels == null) throw new IllegalArgumentException("Qrels folder not set"); if (! Files.exists(testData+this.corpusName+".runs")) throw new IllegalArgumentException("No corpus runs file found for a corpus named " + corpus + " at " + testData+this.corpusName+".runs");
@Test public void testExists() { for (String suffix : TESTCASE_EXTENSIONS) { assertTrue(Files.exists(ApplicationSetup.TERRIER_SHARE + "/tests/files/helloworld.txt" + suffix)); } }
@Test public void testWrite() throws Exception { for (String suffix : SUPPORTED_EXTENSIONS) { File f = super.tmpfolder.newFile("test.txt"+suffix); Writer w = Files.writeFileWriter(f.toString()); w.write("hello world\n"); w.close(); assertTrue(Files.exists(f.toString())); assertEquals("hello world", readFirstLineReader(f.toString())); assertEquals("hello world", readFirstLineStream(f.toString())); f.delete(); } }