changeset 1367:836e00bf7ce2

add Lucene backup_to
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 18 Jun 2019 16:27:03 -0600
parents ae2321a09723
children 5225cd6ed478
files src/luan/modules/Boot.luan src/luan/modules/IoLuan.java src/luan/modules/Utils.java src/luan/modules/lucene/Lucene.luan
diffstat 4 files changed, 40 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/Boot.luan	Mon Jun 17 21:50:40 2019 -0600
+++ b/src/luan/modules/Boot.luan	Tue Jun 18 16:27:03 2019 -0600
@@ -173,6 +173,8 @@
 	this.set_last_modified = io.set_last_modified
 	this.length = io.file.length
 	this.rename_to = io.rename_to
+	this.link_to = io.link_to
+	this.symlink_to = io.symlink_to
 	this.is_symbolic_link = io.is_symbolic_link
 
 	function this.child(name)
--- a/src/luan/modules/IoLuan.java	Mon Jun 17 21:50:40 2019 -0600
+++ b/src/luan/modules/IoLuan.java	Tue Jun 18 16:27:03 2019 -0600
@@ -134,9 +134,11 @@
 	}
 
 
-	private static File objToFile(Object obj) {
+	private static File objToFile(Luan luan,Object obj) throws LuanException {
 		if( obj instanceof String ) {
-			return new File((String)obj);
+			String fileName = (String)obj;
+			check(luan,"file:"+fileName);
+			return new File(fileName);
 		}
 		if( obj instanceof LuanTable ) {
 			LuanTable t = (LuanTable)obj;
@@ -444,14 +446,28 @@
 			return file.exists();
 		}
 
-		public void rename_to(Object destObj) throws LuanException {
-			File dest = objToFile(destObj);
+		public void rename_to(Luan luan,Object destObj) throws LuanException {
+			File dest = objToFile(luan,destObj);
 			if( dest==null )
-				throw new LuanException( "bad argument #1 to 'objToFile' (string or file table expected)" );
+				throw new LuanException( "bad argument #1 to 'rename_to' (string or file table expected)" );
 			if( !file.renameTo(dest) )
 				throw new LuanException("couldn't rename file "+file+" to "+dest);
 		}
 
+		public void link_to(Luan luan,Object destObj) throws LuanException, IOException {
+			File dest = objToFile(luan,destObj);
+			if( dest==null )
+				throw new LuanException( "bad argument #1 to 'link_to' (string or file table expected)" );
+			Files.createLink( file.toPath(), dest.toPath() );
+		}
+
+		public void symlink_to(Luan luan,Object destObj) throws LuanException, IOException {
+			File dest = objToFile(luan,destObj);
+			if( dest==null )
+				throw new LuanException( "bad argument #1 to 'symlink_to' (string or file table expected)" );
+			Files.createSymbolicLink( file.toPath(), dest.toPath() );
+		}
+
 		public LuanFile canonical(Luan luan) throws LuanException, IOException {
 			return new LuanFile(luan,file.getCanonicalFile());
 		}
@@ -535,13 +551,13 @@
 		final File dir;
 		Process proc;
 
-		private BaseOs(String cmd,LuanTable options) throws IOException, LuanException {
+		private BaseOs(Luan luan,String cmd,LuanTable options) throws IOException, LuanException {
 			this.cmd = cmd;
 			File dir = null;
 			if( options != null ) {
 				Map map = options.asMap();
 				Object obj = map.remove("dir");
-				dir = objToFile(obj);
+				dir = objToFile(luan,obj);
 				if( dir==null )
 					throw new LuanException( "bad option 'dir' (string or file table expected)" );
 				if( !map.isEmpty() )
@@ -596,7 +612,7 @@
 
 	public static final class LuanOs extends BaseOs {
 		public LuanOs(Luan luan,String cmd,LuanTable options) throws IOException, LuanException {
-			super(cmd,options);
+			super(luan,cmd,options);
 			check(luan,"os:"+cmd);
 			this.proc = Runtime.getRuntime().exec(cmd,null,dir);
 		}
@@ -604,7 +620,7 @@
 
 	public static final class LuanBash extends BaseOs {
 		public LuanBash(Luan luan,String cmd,LuanTable options) throws IOException, LuanException {
-			super(cmd,options);
+			super(luan,cmd,options);
 			check(luan,"bash:"+cmd);
 			this.proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir);
 		}
--- a/src/luan/modules/Utils.java	Mon Jun 17 21:50:40 2019 -0600
+++ b/src/luan/modules/Utils.java	Tue Jun 18 16:27:03 2019 -0600
@@ -93,108 +93,5 @@
 		copyAll(in,out);
 		return out.toByteArray();
 	}
-/*
-	public static boolean exists(File file) {
-		try {
-			return file.exists() && file.getName().equals(file.getCanonicalFile().getName());
-		} catch(IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
-*/
-/*
-	private static File toFile(String path) {
-		if( path.contains("//") )
-			return null;
-		File file = new File(path);
-		return file.exists() ? file : null;
-	}
 
-	private static URL toUrl(String path) {
-		if( path.indexOf(':') == -1 )
-			return null;
-		if( path.startsWith("classpath:") ) {
-			path = path.substring(10);
-			if( path.contains("//") )
-				return null;
-			URL url;
-			if( !path.contains("#") ) {
-				url = ClassLoader.getSystemResource(path);
-			} else {
-				String[] a = path.split("#");
-				url = ClassLoader.getSystemResource(a[0]);
-				if( url==null ) {
-					for( int i=1; i<a.length; i++ ) {
-						url = ClassLoader.getSystemResource(a[0]+"/"+a[i]);
-						if( url != null ) {
-							try {
-								url = new URL(url,".");
-							} catch(MalformedURLException e) {
-								throw new RuntimeException(e);
-							}
-							break;
-						}
-					}
-				}
-			}
-			return url==null ? null : url;
-		}
-		try {
-			return new URL(path);
-		} catch(MalformedURLException e) {}
-		return null;
-	}
-
-	static boolean exists(String path) {
-		return toFile(path)!=null || toUrl(path)!=null;
-	}
-*/
-
-
-
-/*	replace by uri"os:..."
-
-	// process
-
-	public static class ProcessException extends IOException {
-		private ProcessException(String msg) {
-			super(msg);
-		}
-	}
-
-	public static void checkProcess(Process proc)
-		throws IOException
-	{
-		try {
-			proc.waitFor();
-		} catch(InterruptedException e) {
-			throw new RuntimeException(e);
-		}
-		int exitVal = proc.exitValue();
-		if( exitVal != 0 ) {
-			Reader err = new InputStreamReader(proc.getErrorStream());
-			String error = readAll(err);
-			err.close();
-			throw new ProcessException(error);
-		}
-	}
-
-	public static String getOutput(Process proc)
-		throws IOException
-	{
-		Reader in = new InputStreamReader(proc.getInputStream());
-		String s = readAll(in);
-		in.close();
-		return s;
-	}
-
-	public static String execProcess(String... cmd)
-		throws IOException
-	{
-		Process proc = Runtime.getRuntime().exec(cmd);
-		String s = getOutput(proc);
-		checkProcess(proc);
-		return s;
-	}
-*/
 }
--- a/src/luan/modules/lucene/Lucene.luan	Mon Jun 17 21:50:40 2019 -0600
+++ b/src/luan/modules/lucene/Lucene.luan	Tue Jun 18 16:27:03 2019 -0600
@@ -48,7 +48,6 @@
 	end
 
 	index.to_string = java_index.to_string
---	index.backup = java_index.backup
 	index.snapshot = java_index.snapshot
 	index.advanced_search = java_index.advanced_search
 	index.search_in_transaction = java_index.search_in_transaction
@@ -126,6 +125,19 @@
 		end
 	end
 
+	function index.backup_to(backup_dir)
+		backup_dir.delete()
+		backup_dir.mkdir()
+		index.snapshot( function(dir_path,file_names)
+			local lucene_dir = uri("file:"..dir_path)
+			for _, file_name in ipairs(file_names) do
+				local lucene_file = lucene_dir.child(file_name)
+				local backup_file = backup_dir.child(file_name)
+				backup_file.link_to(lucene_file)
+			end
+		end )
+	end
+
 	function index.zip(zip_file)
 		index.snapshot( function(dir_path,file_names)
 			zip_file.delete()