1 | package com.framsticks.dumping; |
---|
2 | |
---|
3 | import com.framsticks.communication.File; |
---|
4 | import com.framsticks.params.ListSource; |
---|
5 | import com.framsticks.structure.Path; |
---|
6 | import com.framsticks.structure.Tree; |
---|
7 | import com.framsticks.structure.TreeOperations; |
---|
8 | import com.framsticks.util.*; |
---|
9 | import com.framsticks.util.dispatching.FutureHandler; |
---|
10 | import com.framsticks.util.lang.Pair; |
---|
11 | import com.framsticks.util.lang.Strings; |
---|
12 | import org.apache.logging.log4j.Logger; |
---|
13 | import org.apache.logging.log4j.LogManager; |
---|
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 | */ |
---|
23 | public class LoadStream extends Stream { |
---|
24 | |
---|
25 | private final static Logger log = LogManager.getLogger(LoadStream.class.getName()); |
---|
26 | |
---|
27 | protected final Tree tree; |
---|
28 | protected final Path mountPath; |
---|
29 | protected final BufferedReader stream; |
---|
30 | protected final FutureHandler<Path> future; |
---|
31 | protected final Stopwatch stopwatch = new Stopwatch(); |
---|
32 | |
---|
33 | |
---|
34 | public LoadStream(Path mountPath, BufferedReader stream, Tree tree, FutureHandler<Path> future) { |
---|
35 | this.tree = tree; |
---|
36 | this.mountPath = mountPath; |
---|
37 | this.stream = stream; |
---|
38 | this.future = future; |
---|
39 | } |
---|
40 | |
---|
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) { |
---|
50 | query = Strings.splitIntoPair(line, ' ', ""); |
---|
51 | files = new LinkedList<File>(); |
---|
52 | log.trace("loading {}", line); |
---|
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")) { |
---|
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); |
---|
66 | } else if (query.first.equals("info")) { |
---|
67 | assert files.size() == 1; |
---|
68 | TreeOperations.processFetchedInfo(tree, files.get(0)); |
---|
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) { |
---|
87 | log.error("failed to load: {}", e); |
---|
88 | future.handle(new FramsticksException().msg("failed to load stream").cause(e)); |
---|
89 | return; |
---|
90 | } |
---|
91 | log.info("loaded in: {}", stopwatch); |
---|
92 | future.pass(Path.to(tree, mountPath.getTextual())); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | } |
---|