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