comparison 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
comparison
equal deleted inserted replaced
1508:86c5e7000ecf 1509:0ba144491a42
1 package goodjava.lucene.backup; 1 package goodjava.lucene.backup;
2 2
3 import java.io.File; 3 import java.io.File;
4 import java.io.InputStream;
5 import java.io.FileInputStream;
4 import java.io.IOException; 6 import java.io.IOException;
5 import java.util.List; 7 import java.util.List;
6 import java.util.ArrayList; 8 import java.util.ArrayList;
7 import java.util.Map; 9 import java.util.Map;
8 import java.util.Arrays; 10 import java.util.Arrays;
9 import goodjava.io.IoUtils; 11 import goodjava.io.IoUtils;
12 import goodjava.io.BufferedInputStream;
10 import goodjava.rpc.RpcServer; 13 import goodjava.rpc.RpcServer;
11 import goodjava.rpc.RpcCall; 14 import goodjava.rpc.RpcCall;
12 import goodjava.rpc.RpcResult; 15 import goodjava.rpc.RpcResult;
13 import goodjava.logging.Logger; 16 import goodjava.logging.Logger;
14 import goodjava.logging.LoggerFactory; 17 import goodjava.logging.LoggerFactory;
26 Backup(File dir) { 29 Backup(File dir) {
27 this.dir = dir; 30 this.dir = dir;
28 this.index = new File(dir,"index"); 31 this.index = new File(dir,"index");
29 } 32 }
30 33
31 synchronized void handle(RpcServer rpc,RpcCall call) { 34 void handle(RpcServer rpc,RpcCall call) {
32 try { 35 try {
33 handle2(rpc,call); 36 IoUtils.mkdirs(dir);
37 if( call.cmd.equals("zip") ) {
38 handleZip(rpc);
39 } else {
40 handle2(rpc,call);
41 }
34 } catch(IOException e) { 42 } catch(IOException e) {
35 throw new RuntimeException(e); 43 throw new RuntimeException(e);
36 } 44 }
37 } 45 }
38 46
39 void handle2(RpcServer rpc,RpcCall call) throws IOException { 47 private static final RpcResult OK = new RpcResult(new Object[]{"ok"});
40 IoUtils.mkdirs(dir); 48
49 synchronized void handle2(RpcServer rpc,RpcCall call) throws IOException {
41 //logger.info(call.cmd+" "+Arrays.asList(call.args)); 50 //logger.info(call.cmd+" "+Arrays.asList(call.args));
42 String fileName = null; 51 String fileName = null;
43 if( call.cmd.equals("check") ) { 52 if( call.cmd.equals("check") ) {
44 // nothing 53 // nothing
45 } else if( call.cmd.equals("add") || call.cmd.equals("append") ) { 54 } else if( call.cmd.equals("add") || call.cmd.equals("append") ) {
55 logger.info(call.cmd+" "+fileName+" "+call.lenIn); 64 logger.info(call.cmd+" "+fileName+" "+call.lenIn);
56 } else 65 } else
57 throw new RuntimeException("cmd "+call.cmd); 66 throw new RuntimeException("cmd "+call.cmd);
58 List logInfo = (List)call.args[1]; 67 List logInfo = (List)call.args[1];
59 logger.info("check "+logInfo); 68 logger.info("check "+logInfo);
60 RpcResult result = new RpcResult("ok"); 69 RpcResult result = OK;
61 for( Object obj : logInfo ) { 70 for( Object obj : logInfo ) {
62 Map fileInfo = (Map)obj; 71 Map fileInfo = (Map)obj;
63 String name = (String)fileInfo.get("name"); 72 String name = (String)fileInfo.get("name");
64 File f = new File(dir,name); 73 File f = new File(dir,name);
65 if( !f.exists() ) { 74 if( !f.exists() ) {
66 if( name.equals(fileName) ) logger.error("missing"); 75 if( name.equals(fileName) ) logger.error("missing");
67 result = new RpcResult("missing",name); 76 result = new RpcResult(new Object[]{"missing",name});
68 break; 77 break;
69 } 78 }
70 long end = (Long)fileInfo.get("end"); 79 long end = (Long)fileInfo.get("end");
71 LogFile log = new LogFile(f); 80 LogFile log = new LogFile(f);
72 long logEnd = log.end(); 81 long logEnd = log.end();
73 if( logEnd > end ) { 82 if( logEnd > end ) {
74 logger.error("logEnd > end - shouldn't happen, file="+name+" logEnd="+logEnd+" end="+end); 83 logger.error("logEnd > end - shouldn't happen, file="+name+" logEnd="+logEnd+" end="+end);
75 result = new RpcResult("missing",name); 84 result = new RpcResult(new Object[]{"missing",name});
76 break; 85 break;
77 } 86 }
78 if( logEnd < end ) { 87 if( logEnd < end ) {
79 if( name.equals(fileName) ) logger.error("incomplete"); 88 if( name.equals(fileName) ) logger.error("incomplete");
80 result = new RpcResult("incomplete",name,logEnd); 89 result = new RpcResult(new Object[]{"incomplete",name,logEnd});
81 break; 90 break;
82 } 91 }
83 Object checksumObj = fileInfo.get("checksum"); 92 Object checksumObj = fileInfo.get("checksum");
84 if( checksumObj != null ) { 93 if( checksumObj != null ) {
85 long checksum = (Long)checksumObj; 94 long checksum = (Long)checksumObj;
86 if( log.checksum() != checksum ) { 95 if( log.checksum() != checksum ) {
87 index.delete(); 96 index.delete();
88 result = new RpcResult("bad_checksum",name); 97 result = new RpcResult(new Object[]{"bad_checksum",name});
89 break; 98 break;
90 } 99 }
91 } 100 }
92 } 101 }
93 if( call.cmd.equals("add") ) { 102 if( call.cmd.equals("add") ) {
109 } 118 }
110 } 119 }
111 rpc.write(result); 120 rpc.write(result);
112 } 121 }
113 122
123 void handleZip(RpcServer rpc) throws IOException {
124 File zip = File.createTempFile("luan_",".zip");
125 IoUtils.delete(zip);
126 String cmd = "zip -r " + zip + " " + dir.getName();
127 logger.info("cmd = "+cmd);
128 synchronized(this) {
129 Process proc = Runtime.getRuntime().exec(cmd,null,dir.getParentFile());
130 IoUtils.waitFor(proc);
131 }
132 InputStream in = new BufferedInputStream(new FileInputStream(zip));
133 RpcResult result = new RpcResult(in,zip.length(),new Object[0]);
134 rpc.write(result);
135 IoUtils.delete(zip);
136 }
114 137
115 } 138 }