source: java/main/src/main/java/com/framsticks/dumping/LoadStream.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 2.6 KB
RevLine 
[77]1package com.framsticks.dumping;
2
3import com.framsticks.communication.File;
4import com.framsticks.params.ListSource;
[105]5import com.framsticks.structure.Path;
6import com.framsticks.structure.Tree;
7import com.framsticks.structure.TreeOperations;
[77]8import com.framsticks.util.*;
[105]9import com.framsticks.util.dispatching.FutureHandler;
[84]10import com.framsticks.util.lang.Pair;
11import com.framsticks.util.lang.Strings;
[100]12import org.apache.logging.log4j.Logger;
13import org.apache.logging.log4j.LogManager;
[77]14
15import java.io.BufferedReader;
16import java.io.IOException;
17import java.util.LinkedList;
18import java.util.List;
19
20/**
21 * @author Piotr Sniegowski
22 */
[96]23public class LoadStream extends Stream {
[77]24
[100]25        private final static Logger log = LogManager.getLogger(LoadStream.class.getName());
[77]26
[97]27        protected final Tree tree;
[84]28        protected final Path mountPath;
29        protected final BufferedReader stream;
[105]30        protected final FutureHandler<Path> future;
[84]31        protected final Stopwatch stopwatch = new Stopwatch();
[77]32
33
[105]34        public LoadStream(Path mountPath, BufferedReader stream, Tree tree, FutureHandler<Path> future) {
[97]35                this.tree = tree;
[84]36                this.mountPath = mountPath;
37                this.stream = stream;
38                this.future = future;
39        }
[77]40
[84]41        public void load() {
42                try {
43                        String line;
44                        Pair<String, String> query = null;
45                        List<File> files = null;
46                        List<String> content = null;
47                        //File file;
48                        while ((line = stream.readLine()) != null) {
49                                if (query == null) {
[96]50                                        query = Strings.splitIntoPair(line, ' ', "");
[84]51                                        files = new LinkedList<File>();
[100]52                                        log.trace("loading {}", line);
[84]53                                        continue;
54                                }
55                                if (content == null) {
56                                        if (line.equals("file")) {
57                                                content = new LinkedList<String>();
58                                                continue;
59                                        }
60                                        if (line.equals("ok")) {
61                                                if (query.first.equals("get")) {
[98]62                                                        // Path path = null;
63                                                        throw new UnimplementedException().msg("rework load stream");
64                                                        // Path path = TreeOperations.createIfNeeded(tree, query.second);
65                                                        // TreeOperations.processFetchedValues(path, files);
[84]66                                                } else if (query.first.equals("info")) {
67                                                        assert files.size() == 1;
[97]68                                                        TreeOperations.processFetchedInfo(tree, files.get(0));
[84]69                                                } else {
70                                                        assert false;
71                                                }
72                                                query = null;
73                                                files = null;
74                                                continue;
75                                        }
76                                        assert false;
77                                        continue;
78                                }
79                                if (line.equals("eof")) {
80                                        files.add(new File(query.second, new ListSource(content)));
81                                        content = null;
82                                        continue;
83                                }
84                                content.add(line);
85                        }
86                } catch (IOException e) {
[100]87                        log.error("failed to load: {}", e);
[96]88                        future.handle(new FramsticksException().msg("failed to load stream").cause(e));
[84]89                        return;
90                }
[100]91                log.info("loaded in: {}", stopwatch);
[97]92                future.pass(Path.to(tree, mountPath.getTextual()));
[84]93        }
[77]94
95
96}
Note: See TracBrowser for help on using the repository browser.