1499
|
1 package goodjava.lucene.backup;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.IOException;
|
|
5 import java.util.List;
|
|
6 import java.util.ArrayList;
|
|
7 import java.util.Map;
|
|
8 import java.util.Arrays;
|
|
9 import goodjava.io.IoUtils;
|
|
10 import goodjava.rpc.RpcServer;
|
|
11 import goodjava.rpc.RpcCall;
|
|
12 import goodjava.rpc.RpcResult;
|
|
13 import goodjava.logging.Logger;
|
|
14 import goodjava.logging.LoggerFactory;
|
|
15 import goodjava.lucene.logging.LogFile;
|
|
16 import goodjava.lucene.logging.LoggingIndexWriter;
|
|
17 import goodjava.lucene.logging.LogOutputStream;
|
|
18
|
|
19
|
|
20 class Backup {
|
|
21 private static final Logger logger = LoggerFactory.getLogger(Backup.class);
|
|
22
|
|
23 private final File dir;
|
1500
|
24 private final File index;
|
1499
|
25
|
|
26 Backup(File dir) {
|
|
27 this.dir = dir;
|
1500
|
28 this.index = new File(dir,"index");
|
1499
|
29 }
|
|
30
|
|
31 synchronized void handle(RpcServer rpc,RpcCall call) {
|
|
32 try {
|
|
33 handle2(rpc,call);
|
|
34 } catch(IOException e) {
|
|
35 throw new RuntimeException(e);
|
|
36 }
|
|
37 }
|
|
38
|
|
39 void handle2(RpcServer rpc,RpcCall call) throws IOException {
|
1501
|
40 IoUtils.mkdirs(dir);
|
1499
|
41 //logger.info(call.cmd+" "+Arrays.asList(call.args));
|
|
42 String fileName = null;
|
|
43 if( call.cmd.equals("check") ) {
|
|
44 // nothing
|
|
45 } else if( call.cmd.equals("add") || call.cmd.equals("append") ) {
|
|
46 fileName = (String)call.args[2];
|
|
47 File f = new File(dir,fileName);
|
|
48 LogFile log = new LogFile(f);
|
|
49 LogOutputStream out = log.output();
|
|
50 IoUtils.copyAll(call.in,out);
|
|
51 out.commit();
|
|
52 out.close();
|
|
53 logger.info(call.cmd+" "+fileName+" "+call.lenIn);
|
|
54 } else
|
|
55 throw new RuntimeException("cmd "+call.cmd);
|
|
56 List logInfo = (List)call.args[1];
|
|
57 logger.info("check "+logInfo);
|
|
58 RpcResult result = new RpcResult("ok");
|
|
59 for( Object obj : logInfo ) {
|
|
60 Map fileInfo = (Map)obj;
|
|
61 String name = (String)fileInfo.get("name");
|
|
62 File f = new File(dir,name);
|
|
63 if( !f.exists() ) {
|
|
64 if( name.equals(fileName) ) logger.error("missing");
|
|
65 result = new RpcResult("missing",name);
|
|
66 break;
|
|
67 }
|
|
68 long end = (Long)fileInfo.get("end");
|
|
69 LogFile log = new LogFile(f);
|
|
70 long logEnd = log.end();
|
|
71 if( logEnd > end ) {
|
|
72 logger.error("logEnd > end - shouldn't happen, file="+name+" logEnd="+logEnd+" end="+end);
|
|
73 result = new RpcResult("missing",name);
|
|
74 break;
|
|
75 }
|
|
76 if( logEnd < end ) {
|
|
77 if( name.equals(fileName) ) logger.error("incomplete");
|
|
78 result = new RpcResult("incomplete",name,logEnd);
|
|
79 break;
|
|
80 }
|
|
81 }
|
|
82 if( call.cmd.equals("add") ) {
|
|
83 boolean complete = true;
|
|
84 List<LogFile> logs = new ArrayList<LogFile>();
|
|
85 for( Object obj : logInfo ) {
|
|
86 Map fileInfo = (Map)obj;
|
|
87 String name = (String)fileInfo.get("name");
|
|
88 File f = new File(dir,name);
|
|
89 if( !f.exists() ) {
|
|
90 complete = false;
|
|
91 break;
|
|
92 }
|
|
93 logs.add( new LogFile(f) );
|
|
94 }
|
|
95 if( complete ) {
|
|
96 LoggingIndexWriter.writeIndex(logs,index);
|
|
97 logger.info("write index");
|
|
98 }
|
|
99 }
|
|
100 rpc.write(result);
|
|
101 }
|
|
102
|
|
103
|
|
104 }
|