[77] | 1 | package com.framsticks.dumping; |
---|
| 2 | |
---|
| 3 | import com.framsticks.communication.File; |
---|
| 4 | import com.framsticks.params.ListSource; |
---|
[105] | 5 | import com.framsticks.structure.Path; |
---|
| 6 | import com.framsticks.structure.Tree; |
---|
| 7 | import com.framsticks.structure.TreeOperations; |
---|
[77] | 8 | import com.framsticks.util.*; |
---|
[105] | 9 | import com.framsticks.util.dispatching.FutureHandler; |
---|
[84] | 10 | import com.framsticks.util.lang.Pair; |
---|
| 11 | import com.framsticks.util.lang.Strings; |
---|
[100] | 12 | import org.apache.logging.log4j.Logger; |
---|
| 13 | import org.apache.logging.log4j.LogManager; |
---|
[77] | 14 | |
---|
| 15 | import java.io.BufferedReader; |
---|
| 16 | import java.io.IOException; |
---|
| 17 | import java.util.LinkedList; |
---|
| 18 | import java.util.List; |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * @author Piotr Sniegowski |
---|
| 22 | */ |
---|
[96] | 23 | public class LoadStream extends Stream { |
---|
[77] | 24 | |
---|
[100] | 25 | private final static Logger log = LogManager.getLogger(LoadStream.class.getName()); |
---|
[77] | 26 | |
---|
[97] | 27 | protected final Tree tree; |
---|
[84] | 28 | protected final Path mountPath; |
---|
| 29 | protected final BufferedReader stream; |
---|
[105] | 30 | protected final FutureHandler<Path> future; |
---|
[84] | 31 | protected final Stopwatch stopwatch = new Stopwatch(); |
---|
[77] | 32 | |
---|
| 33 | |
---|
[105] | 34 | public LoadStream(Path mountPath, BufferedReader stream, Tree tree, FutureHandler<Path> future) { |
---|
[97] | 35 | this.tree = tree; |
---|
[84] | 36 | this.mountPath = mountPath; |
---|
| 37 | this.stream = stream; |
---|
| 38 | this.future = future; |
---|
| 39 | } |
---|
[77] | 40 | |
---|
[84] | 41 | public void load() { |
---|
| 42 | try { |
---|
| 43 | String line; |
---|
| 44 | Pair<String, String> query = null; |
---|
| 45 | List<File> files = null; |
---|
| 46 | List<String> content = null; |
---|
| 47 | //File file; |
---|
| 48 | while ((line = stream.readLine()) != null) { |
---|
| 49 | if (query == null) { |
---|
[96] | 50 | query = Strings.splitIntoPair(line, ' ', ""); |
---|
[84] | 51 | files = new LinkedList<File>(); |
---|
[100] | 52 | log.trace("loading {}", line); |
---|
[84] | 53 | continue; |
---|
| 54 | } |
---|
| 55 | if (content == null) { |
---|
| 56 | if (line.equals("file")) { |
---|
| 57 | content = new LinkedList<String>(); |
---|
| 58 | continue; |
---|
| 59 | } |
---|
| 60 | if (line.equals("ok")) { |
---|
| 61 | if (query.first.equals("get")) { |
---|
[98] | 62 | // Path path = null; |
---|
| 63 | throw new UnimplementedException().msg("rework load stream"); |
---|
| 64 | // Path path = TreeOperations.createIfNeeded(tree, query.second); |
---|
| 65 | // TreeOperations.processFetchedValues(path, files); |
---|
[84] | 66 | } else if (query.first.equals("info")) { |
---|
| 67 | assert files.size() == 1; |
---|
[97] | 68 | TreeOperations.processFetchedInfo(tree, files.get(0)); |
---|
[84] | 69 | } else { |
---|
| 70 | assert false; |
---|
| 71 | } |
---|
| 72 | query = null; |
---|
| 73 | files = null; |
---|
| 74 | continue; |
---|
| 75 | } |
---|
| 76 | assert false; |
---|
| 77 | continue; |
---|
| 78 | } |
---|
| 79 | if (line.equals("eof")) { |
---|
| 80 | files.add(new File(query.second, new ListSource(content))); |
---|
| 81 | content = null; |
---|
| 82 | continue; |
---|
| 83 | } |
---|
| 84 | content.add(line); |
---|
| 85 | } |
---|
| 86 | } catch (IOException e) { |
---|
[100] | 87 | log.error("failed to load: {}", e); |
---|
[96] | 88 | future.handle(new FramsticksException().msg("failed to load stream").cause(e)); |
---|
[84] | 89 | return; |
---|
| 90 | } |
---|
[100] | 91 | log.info("loaded in: {}", stopwatch); |
---|
[97] | 92 | future.pass(Path.to(tree, mountPath.getTextual())); |
---|
[84] | 93 | } |
---|
[77] | 94 | |
---|
| 95 | |
---|
| 96 | } |
---|