Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/goodjava/lucene/backup/Backup.java Sat May 09 23:14:13 2020 -0600 @@ -0,0 +1,103 @@ +package goodjava.lucene.backup; + +import java.io.File; +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.rpc.RpcServer; +import goodjava.rpc.RpcCall; +import goodjava.rpc.RpcResult; +import goodjava.logging.Logger; +import goodjava.logging.LoggerFactory; +import goodjava.lucene.logging.LogFile; +import goodjava.lucene.logging.LoggingIndexWriter; +import goodjava.lucene.logging.LogOutputStream; + + +class Backup { + private static final Logger logger = LoggerFactory.getLogger(Backup.class); + + private final File dir; + + Backup(File dir) { + this.dir = dir; + } + + synchronized void handle(RpcServer rpc,RpcCall call) { + try { + handle2(rpc,call); + } catch(IOException e) { + throw new RuntimeException(e); + } + } + + void handle2(RpcServer rpc,RpcCall call) throws IOException { + dir.mkdirs(); + //logger.info(call.cmd+" "+Arrays.asList(call.args)); + String fileName = null; + if( call.cmd.equals("check") ) { + // nothing + } else if( call.cmd.equals("add") || call.cmd.equals("append") ) { + fileName = (String)call.args[2]; + File f = new File(dir,fileName); + LogFile log = new LogFile(f); + LogOutputStream out = log.output(); + IoUtils.copyAll(call.in,out); + out.commit(); + out.close(); + logger.info(call.cmd+" "+fileName+" "+call.lenIn); + } else + throw new RuntimeException("cmd "+call.cmd); + List logInfo = (List)call.args[1]; + logger.info("check "+logInfo); + RpcResult result = new RpcResult("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); + break; + } + long end = (Long)fileInfo.get("end"); + LogFile log = new LogFile(f); + 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); + break; + } + if( logEnd < end ) { + if( name.equals(fileName) ) logger.error("incomplete"); + result = new RpcResult("incomplete",name,logEnd); + break; + } + } + if( call.cmd.equals("add") ) { + boolean complete = true; + List<LogFile> logs = new ArrayList<LogFile>(); + for( Object obj : logInfo ) { + Map fileInfo = (Map)obj; + String name = (String)fileInfo.get("name"); + File f = new File(dir,name); + if( !f.exists() ) { + complete = false; + break; + } + logs.add( new LogFile(f) ); + } + if( complete ) { + File index = new File(dir,"index"); + LoggingIndexWriter.writeIndex(logs,index); + logger.info("write index"); + } + } + rpc.write(result); + } + + +}