diff src/goodjava/lucene/backup/Backup.java @ 1509:0ba144491a42

lucene.backup zip
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 17 May 2020 14:29:33 -0600
parents 86c5e7000ecf
children 31b543826ca9
line wrap: on
line diff
--- a/src/goodjava/lucene/backup/Backup.java	Sat May 16 17:56:02 2020 -0600
+++ b/src/goodjava/lucene/backup/Backup.java	Sun May 17 14:29:33 2020 -0600
@@ -1,12 +1,15 @@
 package goodjava.lucene.backup;
 
 import java.io.File;
+import java.io.InputStream;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Map;
 import java.util.Arrays;
 import goodjava.io.IoUtils;
+import goodjava.io.BufferedInputStream;
 import goodjava.rpc.RpcServer;
 import goodjava.rpc.RpcCall;
 import goodjava.rpc.RpcResult;
@@ -28,16 +31,22 @@
 		this.index = new File(dir,"index");
 	}
 
-	synchronized void handle(RpcServer rpc,RpcCall call) {
+	void handle(RpcServer rpc,RpcCall call) {
 		try {
-			handle2(rpc,call);
+			IoUtils.mkdirs(dir);
+			if( call.cmd.equals("zip") ) {
+				handleZip(rpc);
+			} else {
+				handle2(rpc,call);
+			}
 		} catch(IOException e) {
 			throw new RuntimeException(e);
 		}
 	}
 
-	void handle2(RpcServer rpc,RpcCall call) throws IOException {
-		IoUtils.mkdirs(dir);
+	private static final RpcResult OK = new RpcResult(new Object[]{"ok"});
+
+	synchronized void handle2(RpcServer rpc,RpcCall call) throws IOException {
 		//logger.info(call.cmd+" "+Arrays.asList(call.args));
 		String fileName = null;
 		if( call.cmd.equals("check") ) {
@@ -57,14 +66,14 @@
 			throw new RuntimeException("cmd "+call.cmd);
 		List logInfo = (List)call.args[1];
 		logger.info("check "+logInfo);
-		RpcResult result = new RpcResult("ok");
+		RpcResult result = OK;
 		for( Object obj : logInfo ) {
 			Map fileInfo = (Map)obj;
 			String name = (String)fileInfo.get("name");
 			File f = new File(dir,name);
 			if( !f.exists() ) {
 				if( name.equals(fileName) )  logger.error("missing");
-				result = new RpcResult("missing",name);
+				result = new RpcResult(new Object[]{"missing",name});
 				break;
 			}
 			long end = (Long)fileInfo.get("end");
@@ -72,12 +81,12 @@
 			long logEnd = log.end();
 			if( logEnd > end ) {
 				logger.error("logEnd > end - shouldn't happen, file="+name+" logEnd="+logEnd+" end="+end);
-				result = new RpcResult("missing",name);
+				result = new RpcResult(new Object[]{"missing",name});
 				break;
 			}
 			if( logEnd < end ) {
 				if( name.equals(fileName) )  logger.error("incomplete");
-				result = new RpcResult("incomplete",name,logEnd);
+				result = new RpcResult(new Object[]{"incomplete",name,logEnd});
 				break;
 			}
 			Object checksumObj = fileInfo.get("checksum");
@@ -85,7 +94,7 @@
 				long checksum = (Long)checksumObj;
 				if( log.checksum() != checksum ) {
 					index.delete();
-					result = new RpcResult("bad_checksum",name);
+					result = new RpcResult(new Object[]{"bad_checksum",name});
 					break;
 				}
 			}
@@ -111,5 +120,19 @@
 		rpc.write(result);
 	}
 
+	void handleZip(RpcServer rpc) throws IOException {
+		File zip = File.createTempFile("luan_",".zip");
+		IoUtils.delete(zip);
+		String cmd = "zip -r " + zip + " " + dir.getName();
+logger.info("cmd = "+cmd);
+		synchronized(this) {
+			Process proc = Runtime.getRuntime().exec(cmd,null,dir.getParentFile());
+			IoUtils.waitFor(proc);
+		}
+		InputStream in = new BufferedInputStream(new FileInputStream(zip));
+		RpcResult result = new RpcResult(in,zip.length(),new Object[0]);
+		rpc.write(result);
+		IoUtils.delete(zip);
+	}
 
 }