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