1 | package com.framsticks.dumping; |
---|
2 | |
---|
3 | import static com.framsticks.core.TreeOperations.*; |
---|
4 | import static com.framsticks.params.AccessOperations.*; |
---|
5 | import com.framsticks.core.Node; |
---|
6 | import com.framsticks.core.Path; |
---|
7 | import com.framsticks.params.Access; |
---|
8 | import com.framsticks.params.CompositeParam; |
---|
9 | import com.framsticks.params.FramsClass; |
---|
10 | import com.framsticks.params.ListAccess; |
---|
11 | import com.framsticks.params.Sink; |
---|
12 | import com.framsticks.parsers.Savers; |
---|
13 | import com.framsticks.core.Tree; |
---|
14 | import com.framsticks.util.*; |
---|
15 | import com.framsticks.util.dispatching.Dispatching; |
---|
16 | import com.framsticks.util.dispatching.Future; |
---|
17 | |
---|
18 | import org.apache.logging.log4j.Logger; |
---|
19 | import org.apache.logging.log4j.LogManager; |
---|
20 | import com.framsticks.util.dispatching.RunAt; |
---|
21 | |
---|
22 | import java.util.HashSet; |
---|
23 | import java.util.Set; |
---|
24 | import static com.framsticks.util.lang.Containers.filterInstanceof; |
---|
25 | |
---|
26 | /** |
---|
27 | * @author Piotr Sniegowski |
---|
28 | */ |
---|
29 | public class SaveStream extends Stream { |
---|
30 | |
---|
31 | private final static Logger log = LogManager.getLogger(SaveStream.class.getName()); |
---|
32 | |
---|
33 | protected final Sink sink; |
---|
34 | protected final Tree tree; |
---|
35 | protected final Future<Void> future; |
---|
36 | protected final Stopwatch stopwatch = new Stopwatch(); |
---|
37 | protected final Set<FramsClass> storedInfo = new HashSet<FramsClass>(); |
---|
38 | |
---|
39 | private int dispatched = 0; |
---|
40 | |
---|
41 | public SaveStream(Sink sink, Tree tree, Path root, Future<Void> future) { |
---|
42 | assert Dispatching.isThreadSafe(); |
---|
43 | this.sink = sink; |
---|
44 | this.tree = tree; |
---|
45 | this.future = future; |
---|
46 | dispatchWrite(root); |
---|
47 | } |
---|
48 | |
---|
49 | protected void dispatchWrite(final Path path) { |
---|
50 | ++dispatched; |
---|
51 | tree.dispatch(new RunAt<Tree>(tree) { |
---|
52 | @Override |
---|
53 | protected void runAt() { |
---|
54 | write(path); |
---|
55 | } |
---|
56 | }); |
---|
57 | } |
---|
58 | |
---|
59 | protected void finished() { |
---|
60 | assert tree.isActive(); |
---|
61 | log.info("stored in {}", stopwatch); |
---|
62 | future.pass(null); |
---|
63 | } |
---|
64 | |
---|
65 | public void write(final Path path) { |
---|
66 | assert tree.isActive(); |
---|
67 | if (!path.isResolved()) { |
---|
68 | log.debug("path {} is not resolved - skipping", path); |
---|
69 | } else { |
---|
70 | Access access = bindAccess(path); |
---|
71 | assert access != null; |
---|
72 | FramsClass framsClass = access.getFramsClass(); |
---|
73 | assert framsClass != null; |
---|
74 | if (!storedInfo.contains(framsClass)) { |
---|
75 | storedInfo.add(framsClass); |
---|
76 | sink.print("info ").print(path.getTextual()).breakLine(); |
---|
77 | sink.print("file").breakLine(); |
---|
78 | Savers.saveFramsClass(sink, framsClass); |
---|
79 | sink.print("eof").breakLine(); |
---|
80 | sink.print("ok").breakLine(); |
---|
81 | } |
---|
82 | if (!(access instanceof ListAccess)) { |
---|
83 | sink.print("get ").print(path.getTextual()).breakLine(); |
---|
84 | sink.print("file").breakLine(); |
---|
85 | //stream.print("#" + access.getSelected().getClass().getCanonicalName() + "\n"); |
---|
86 | save(access, sink); |
---|
87 | sink.print("eof").breakLine(); |
---|
88 | sink.print("ok").breakLine(); |
---|
89 | } |
---|
90 | for (CompositeParam p : filterInstanceof(access.getParams(), CompositeParam.class)) { |
---|
91 | final Path childPath = path.appendNode(new Node(path.getTree(), p, access.get(p, Object.class))); |
---|
92 | if (childPath.isResolved() && getInfoFromCache(childPath) != null) { |
---|
93 | dispatchWrite(childPath); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | --dispatched; |
---|
98 | if (dispatched == 0) { |
---|
99 | finished(); |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|