Package com.google.common.io

Examples of com.google.common.io.Closer


        }

        private Object readCompressedObject(File cacheFile, Class<?> type) {
            // The file is there, load it
            try {
                Closer closer = Closer.create();
                try {
                    InputStream in = closer.register(new FileInputStream(cacheFile));
                    InputStream gzin = closer.register(new GZIPInputStream(in));
                    ObjectInputStream oin = closer.register(new CustomClassLoaderObjectInputStream(gzin, classLoader));
                    return type.cast(oin.readObject());
                } catch (Throwable th) {
                    throw closer.rethrow(th);
                } finally {
                    closer.close();
                }
            } catch (IOException ex) {
                logger.warn("ignoring cache file {} due to read error: {}",
                            cacheFile.getName(), ex.toString());
                logger.info("This error can be caused by a corrupted cache file.");
View Full Code Here


            return makeDataSource();
        }
        try {
            logger.info("sampling {} of {}",
                        subsampleFraction, source.getName());
            Closer closer = Closer.create();
            RatingWriter subsampleWriter = closer.register(RatingWriters.csv(subsampleFile));
            try {
                mode.doSample(source, subsampleWriter, subsampleFraction, getProject().getRandom());
            } catch (Throwable th) {
                throw closer.rethrow(th);
            } finally {
                closer.close();
            }
        } catch (IOException e) {
            throw new TaskExecutionException("Error writing output file", e);
        }
        return makeDataSource();
View Full Code Here

            logger.info("Starting evaluation of {} algorithms ({} from LensKit) on {} data sets",
                        Iterables.size(experiments.getAllAlgorithms()),
                        experiments.getAlgorithms().size(),
                        experiments.getDataSets().size());
            Closer closer = Closer.create();

            try {
                outputs = openExperimentOutputs(layout, measurements, resultsBuilder, closer);
                DAGNode<JobGraph.Node,JobGraph.Edge> jobGraph;
                try {
                    jobGraph = makeJobGraph(experiments);
                } catch (RecommenderConfigurationException ex) {
                    throw new TaskExecutionException("Recommender configuration error", ex);
                }
                if (taskGraphFile != null) {
                    logger.info("writing task graph to {}", taskGraphFile);
                    JobGraph.writeGraphDescription(jobGraph, taskGraphFile);
                }
                registerTaskListener(jobGraph);

                // tell all metrics to get started
                runEvaluations(jobGraph);
            } catch (Throwable th) {
                throw closer.rethrow(th, TaskExecutionException.class, InterruptedException.class);
            } finally {
                closer.close();
            }

            logger.info("evaluation {} completed", getName());

            return resultsBuilder.build();
View Full Code Here

        timer.stop();
        logger.info("built model in {}", timer);
        File output = getOutputFile();
        CompressionMode comp = CompressionMode.autodetect(output);
        logger.info("writing model to {}", output);
        Closer closer = Closer.create();
        try {
            OutputStream stream = closer.register(new FileOutputStream(output));
            stream = closer.register(comp.wrapOutput(stream));
            engine.write(stream);
        } catch (Throwable th) {
            throw closer.rethrow(th);
        } finally {
            closer.close();
        }
    }
View Full Code Here

        EnumSet<BinaryFormatFlag> flags = EnumSet.noneOf(BinaryFormatFlag.class);
        if (useTimestamps()) {
            flags.add(BinaryFormatFlag.TIMESTAMPS);
        }
        logger.info("packing to {} with flags {}", getOutputFile(), flags);
        Closer closer = Closer.create();
        try {
            BinaryRatingPacker packer = closer.register(BinaryRatingPacker.open(getOutputFile(), flags));
            Cursor<Rating> ratings = closer.register(dao.streamEvents(Rating.class));
            packer.writeRatings(ratings);
            logger.info("packed {} ratings", packer.getRatingCount());
        } catch (Throwable th) {
            throw closer.rethrow(th);
        } finally {
            closer.close();
        }
    }
View Full Code Here

            if (useTimestamps) {
                flags.add(BinaryFormatFlag.TIMESTAMPS);
                order = SortOrder.TIMESTAMP;
            }

            Closer closer = Closer.create();
            try {
                try {
                    BinaryRatingPacker packer = closer.register(BinaryRatingPacker.open(file, flags));
                    Cursor<Rating> ratings = closer.register(dao.streamEvents(Rating.class, order));
                    packer.writeRatings(ratings);
                } catch (Throwable th) {
                    throw closer.rethrow(th);
                } finally {
                    closer.close();
                }
                BinaryRatingDAO result = BinaryRatingDAO.open(file);
                // try to delete the file early, helps keep things clean on Unix
                if (file.delete()) {
                    logger.debug("unlinked {}, will be deleted when freed", file);
View Full Code Here

     * @param file The file to read.
     * @return A list of longs.
     */
    public static LongList readIdList(File file) throws IOException {
        LongList items = new LongArrayList();
        Closer closer = Closer.create();
        try {
            FileReader fread = closer.register(new FileReader(file));
            BufferedReader buf = closer.register(new BufferedReader(fread));
            String line;
            int lno = 0;
            while ((line = buf.readLine()) != null) {
                lno += 1;
                if (line.trim().isEmpty()) {
                    continue; // skip blank lines
                }
                long item;
                try {
                    item = Long.parseLong(line.trim());
                } catch (IllegalArgumentException ex) {
                    throw new IOException("invalid ID on " + file + " line " + lno + ": " + line);
                }
                items.add(item);
            }
        } catch (Throwable th) {
            throw closer.rethrow(th);
        } finally {
            closer.close();
        }
        return items;
    }
View Full Code Here

        ImmutableSetMultimap.Builder<String,String> mapping = ImmutableSetMultimap.builder();
        try {
            Enumeration<URL> urls = loader.getResources("META-INF/classes.lst");
            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                Closer closer = Closer.create();
                try {
                    InputStream stream = closer.register(url.openStream());
                    Reader rdr = closer.register(new InputStreamReader(stream, "UTF-8"));
                    BufferedReader buf = closer.register(new BufferedReader(rdr));
                    String line = buf.readLine();
                    while (line != null) {
                        int idx = line.lastIndexOf('.');
                        if (idx >= 0) {
                            String name = line.substring(idx + 1);
                            String pkg = line.substring(0, idx);
                            mapping.put(name, pkg);
                        }
                        line = buf.readLine();
                    }
                } catch (Throwable th) {
                    throw closer.rethrow(th);
                } finally {
                    closer.close();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Error loading class lists", e);
        }
View Full Code Here

    @SuppressWarnings("PMD.AvoidCatchingThrowable")
    private void runEvaluation() throws IOException, RecommenderBuildException {
        EventBus bus = task.getProject().getEventBus();
        bus.post(JobEvents.started(this));
        Closer closer = Closer.create();
        try {
            outputs = task.getOutputs().getPrefixed(algorithmInfo, dataSet);
            TableWriter userResults = outputs.getUserWriter();
            List<Object> outputRow = Lists.newArrayList();

            logger.info("Building {} on {}", algorithmInfo, dataSet);
            StopWatch buildTimer = new StopWatch();
            buildTimer.start();
            buildRecommender();
            buildTimer.stop();
            logger.info("Built {} in {}", algorithmInfo.getName(), buildTimer);

            logger.info("Measuring {} on {}", algorithmInfo.getName(), dataSet.getName());

            StopWatch testTimer = new StopWatch();
            testTimer.start();
            List<Object> userRow = Lists.newArrayList();

            List<MetricWithAccumulator<?>> accumulators = Lists.newArrayList();

            for (Metric<?> eval: outputs.getMetrics()) {
                accumulators.add(makeMetricAccumulator(eval));
            }

            LongSet testUsers = dataSet.getTestData().getUserDAO().getUserIds();
            final NumberFormat pctFormat = NumberFormat.getPercentInstance();
            pctFormat.setMaximumFractionDigits(2);
            pctFormat.setMinimumFractionDigits(2);
            final int nusers = testUsers.size();
            logger.info("Testing {} on {} ({} users)", algorithmInfo, dataSet, nusers);
            int ndone = 0;
            for (LongIterator iter = testUsers.iterator(); iter.hasNext();) {
                if (Thread.interrupted()) {
                    throw new InterruptedException("eval job interrupted");
                }
                long uid = iter.nextLong();
                userRow.add(uid);
                userRow.add(null); // placeholder for the per-user time
                assert userRow.size() == 2;

                Stopwatch userTimer = Stopwatch.createStarted();
                TestUser test = getUserResults(uid);

                userRow.add(test.getTrainHistory().size());
                userRow.add(test.getTestHistory().size());

                for (MetricWithAccumulator<?> accum : accumulators) {
                    List<Object> ures = accum.measureUser(test);
                    if (ures != null) {
                        userRow.addAll(ures);
                    }
                }
                userTimer.stop();
                userRow.set(1, userTimer.elapsed(TimeUnit.MILLISECONDS) * 0.001);
                if (userResults != null) {
                    try {
                        userResults.writeRow(userRow);
                    } catch (IOException e) {
                        throw new RuntimeException("error writing user row", e);
                    }
                }
                userRow.clear();

                ndone += 1;
                if (ndone % 100 == 0) {
                    testTimer.split();
                    double time = testTimer.getSplitTime();
                    double tpu = time / ndone;
                    double tleft = (nusers - ndone) * tpu;
                    logger.info("tested {} of {} users ({}), ETA {}",
                                ndone, nusers, pctFormat.format(((double) ndone) / nusers),
                                DurationFormatUtils.formatDurationHMS((long) tleft));
                }
            }
            testTimer.stop();
            logger.info("Tested {} in {}", algorithmInfo.getName(), testTimer);

            writeMetricValues(buildTimer, testTimer, outputRow, accumulators);
            bus.post(JobEvents.finished(this));
        } catch (Throwable th) {
            bus.post(JobEvents.failed(this, th));
            throw closer.rethrow(th, RecommenderBuildException.class);
        } finally {
            try {
                cleanup();
            } finally {
                outputs = null;
                closer.close();
            }
        }
    }
View Full Code Here

        logger.debug("graph has {} nodes", graph.getReachableNodes().size());
        logger.debug("simulating instantiation");
        RecommenderInstantiator instantiator = RecommenderInstantiator.create(graph);
        DAGNode<Component, Dependency> unshared = instantiator.simulate();
        logger.debug("unshared graph has {} nodes", unshared.getReachableNodes().size());
        Closer close = Closer.create();
        try {
            Writer writer = close.register(new FileWriter(graphvizFile));
            GraphWriter gw = close.register(new GraphWriter(writer));
            GraphDumper dumper = new GraphDumper(graph, unshared.getReachableNodes(), gw);
            logger.debug("writing root node");
            String rid = dumper.setRoot(graph);
            // process each other node & add an edge
            for (DAGEdge<Component, Dependency> e: graph.getOutgoingEdges()) {
                DAGNode<Component, Dependency> target = e.getTail();
                Component csat = target.getLabel();
                if (!satIsNull(csat.getSatisfaction())) {
                    logger.debug("processing node {}", csat.getSatisfaction());
                    String id = dumper.process(target);
                    gw.putEdge(EdgeBuilder.create(rid, id)
                                          .set("arrowhead", "vee")
                                          .build());
                }
            }
            // and we're done
            dumper.finish();
        } catch (Throwable th) {
            throw close.rethrow(th);
        } finally {
            close.close();
        }
    }
View Full Code Here

TOP

Related Classes of com.google.common.io.Closer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.