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
Line 
1package com.framsticks.dumping;
2
3import com.framsticks.communication.File;
4import com.framsticks.params.ListSource;
5import com.framsticks.structure.Path;
6import com.framsticks.structure.Tree;
7import com.framsticks.structure.TreeOperations;
8import com.framsticks.util.*;
9import com.framsticks.util.dispatching.FutureHandler;
10import com.framsticks.util.lang.Pair;
11import com.framsticks.util.lang.Strings;
12import org.apache.logging.log4j.Logger;
13import org.apache.logging.log4j.LogManager;
14
15import java.io.BufferedReader;
16import java.io.IOException;
17import java.util.LinkedList;
18import java.util.List;
19
20/**
21 * @author Piotr Sniegowski
22 */
23public class LoadStream extends Stream {
24
25        private final static Logger log = LogManager.getLogger(LoadStream.class.getName());
26
27        protected final Tree tree;
28        protected final Path mountPath;
29        protected final BufferedReader stream;
30        protected final FutureHandler<Path> future;
31        protected final Stopwatch stopwatch = new Stopwatch();
32
33
34        public LoadStream(Path mountPath, BufferedReader stream, Tree tree, FutureHandler<Path> future) {
35                this.tree = tree;
36                this.mountPath = mountPath;
37                this.stream = stream;
38                this.future = future;
39        }
40
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) {
50                                        query = Strings.splitIntoPair(line, ' ', "");
51                                        files = new LinkedList<File>();
52                                        log.trace("loading {}", line);
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")) {
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);
66                                                } else if (query.first.equals("info")) {
67                                                        assert files.size() == 1;
68                                                        TreeOperations.processFetchedInfo(tree, files.get(0));
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) {
87                        log.error("failed to load: {}", e);
88                        future.handle(new FramsticksException().msg("failed to load stream").cause(e));
89                        return;
90                }
91                log.info("loaded in: {}", stopwatch);
92                future.pass(Path.to(tree, mountPath.getTextual()));
93        }
94
95
96}
Note: See TracBrowser for help on using the repository browser.