Mercurial Hosting > luan
comparison src/goodjava/lucene/backup/Backup.java @ 1499:22e15cf73040
lucene.backup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 09 May 2020 23:14:13 -0600 |
parents | |
children | f01abd6d5858 |
comparison
equal
deleted
inserted
replaced
1498:1b809d2fdf03 | 1499:22e15cf73040 |
---|---|
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; | |
24 | |
25 Backup(File dir) { | |
26 this.dir = dir; | |
27 } | |
28 | |
29 synchronized void handle(RpcServer rpc,RpcCall call) { | |
30 try { | |
31 handle2(rpc,call); | |
32 } catch(IOException e) { | |
33 throw new RuntimeException(e); | |
34 } | |
35 } | |
36 | |
37 void handle2(RpcServer rpc,RpcCall call) throws IOException { | |
38 dir.mkdirs(); | |
39 //logger.info(call.cmd+" "+Arrays.asList(call.args)); | |
40 String fileName = null; | |
41 if( call.cmd.equals("check") ) { | |
42 // nothing | |
43 } else if( call.cmd.equals("add") || call.cmd.equals("append") ) { | |
44 fileName = (String)call.args[2]; | |
45 File f = new File(dir,fileName); | |
46 LogFile log = new LogFile(f); | |
47 LogOutputStream out = log.output(); | |
48 IoUtils.copyAll(call.in,out); | |
49 out.commit(); | |
50 out.close(); | |
51 logger.info(call.cmd+" "+fileName+" "+call.lenIn); | |
52 } else | |
53 throw new RuntimeException("cmd "+call.cmd); | |
54 List logInfo = (List)call.args[1]; | |
55 logger.info("check "+logInfo); | |
56 RpcResult result = new RpcResult("ok"); | |
57 for( Object obj : logInfo ) { | |
58 Map fileInfo = (Map)obj; | |
59 String name = (String)fileInfo.get("name"); | |
60 File f = new File(dir,name); | |
61 if( !f.exists() ) { | |
62 if( name.equals(fileName) ) logger.error("missing"); | |
63 result = new RpcResult("missing",name); | |
64 break; | |
65 } | |
66 long end = (Long)fileInfo.get("end"); | |
67 LogFile log = new LogFile(f); | |
68 long logEnd = log.end(); | |
69 if( logEnd > end ) { | |
70 logger.error("logEnd > end - shouldn't happen, file="+name+" logEnd="+logEnd+" end="+end); | |
71 result = new RpcResult("missing",name); | |
72 break; | |
73 } | |
74 if( logEnd < end ) { | |
75 if( name.equals(fileName) ) logger.error("incomplete"); | |
76 result = new RpcResult("incomplete",name,logEnd); | |
77 break; | |
78 } | |
79 } | |
80 if( call.cmd.equals("add") ) { | |
81 boolean complete = true; | |
82 List<LogFile> logs = new ArrayList<LogFile>(); | |
83 for( Object obj : logInfo ) { | |
84 Map fileInfo = (Map)obj; | |
85 String name = (String)fileInfo.get("name"); | |
86 File f = new File(dir,name); | |
87 if( !f.exists() ) { | |
88 complete = false; | |
89 break; | |
90 } | |
91 logs.add( new LogFile(f) ); | |
92 } | |
93 if( complete ) { | |
94 File index = new File(dir,"index"); | |
95 LoggingIndexWriter.writeIndex(logs,index); | |
96 logger.info("write index"); | |
97 } | |
98 } | |
99 rpc.write(result); | |
100 } | |
101 | |
102 | |
103 } |