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