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