Mercurial Hosting > luan
annotate src/goodjava/lucene/backup/Backup.java @ 1508:86c5e7000ecf
lucene.backup checksum
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 16 May 2020 17:56:02 -0600 |
parents | e66e3d50b289 |
children | 0ba144491a42 |
rev | line source |
---|---|
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); | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
48 if( call.cmd.equals("add") ) |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
49 IoUtils.delete(f); |
1499 | 50 LogFile log = new LogFile(f); |
51 LogOutputStream out = log.output(); | |
52 IoUtils.copyAll(call.in,out); | |
53 out.commit(); | |
54 out.close(); | |
55 logger.info(call.cmd+" "+fileName+" "+call.lenIn); | |
56 } else | |
57 throw new RuntimeException("cmd "+call.cmd); | |
58 List logInfo = (List)call.args[1]; | |
59 logger.info("check "+logInfo); | |
60 RpcResult result = new RpcResult("ok"); | |
61 for( Object obj : logInfo ) { | |
62 Map fileInfo = (Map)obj; | |
63 String name = (String)fileInfo.get("name"); | |
64 File f = new File(dir,name); | |
65 if( !f.exists() ) { | |
66 if( name.equals(fileName) ) logger.error("missing"); | |
67 result = new RpcResult("missing",name); | |
68 break; | |
69 } | |
70 long end = (Long)fileInfo.get("end"); | |
71 LogFile log = new LogFile(f); | |
72 long logEnd = log.end(); | |
73 if( logEnd > end ) { | |
74 logger.error("logEnd > end - shouldn't happen, file="+name+" logEnd="+logEnd+" end="+end); | |
75 result = new RpcResult("missing",name); | |
76 break; | |
77 } | |
78 if( logEnd < end ) { | |
79 if( name.equals(fileName) ) logger.error("incomplete"); | |
80 result = new RpcResult("incomplete",name,logEnd); | |
81 break; | |
82 } | |
1508
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
83 Object checksumObj = fileInfo.get("checksum"); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
84 if( checksumObj != null ) { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
85 long checksum = (Long)checksumObj; |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
86 if( log.checksum() != checksum ) { |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
87 index.delete(); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
88 result = new RpcResult("bad_checksum",name); |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
89 break; |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
90 } |
86c5e7000ecf
lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents:
1501
diff
changeset
|
91 } |
1499 | 92 } |
93 if( call.cmd.equals("add") ) { | |
94 boolean complete = true; | |
95 List<LogFile> logs = new ArrayList<LogFile>(); | |
96 for( Object obj : logInfo ) { | |
97 Map fileInfo = (Map)obj; | |
98 String name = (String)fileInfo.get("name"); | |
99 File f = new File(dir,name); | |
100 if( !f.exists() ) { | |
101 complete = false; | |
102 break; | |
103 } | |
104 logs.add( new LogFile(f) ); | |
105 } | |
106 if( complete ) { | |
107 LoggingIndexWriter.writeIndex(logs,index); | |
108 logger.info("write index"); | |
109 } | |
110 } | |
111 rpc.write(result); | |
112 } | |
113 | |
114 | |
115 } |