@@ -0,0 +1,3752 @@
+package org.opentripplanner.transit;%0A%0Aimport com.amazonaws.services.s3.AmazonS3Client;%0Aimport com.amazonaws.services.s3.model.S3Object;%0Aimport org.apache.commons.io.IOUtils;%0Aimport org.opentripplanner.standalone.CommandLineParameters;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.OutputStream;%0Aimport java.util.zip.ZipEntry;%0Aimport java.util.zip.ZipInputStream;%0A%0A/**%0A * This holds one or more TransportNetworks keyed on unique strings.%0A * This is a replacement for ClusterGraphBuilder.%0A * TODO this should serialize any networks it builds, attempt to reload from disk, and copy serialized networks to S3.%0A */%0Apublic class TransportNetworkCache %7B%0A%0A    private static final Logger LOG = LoggerFactory.getLogger(TransportNetworkCache.class);%0A%0A    private AmazonS3Client s3 = new AmazonS3Client();%0A%0A    private static final String CACHE_DIR = %22network-cache%22;%0A%0A    private final String sourceBucket;%0A%0A    String currentNetworkId = null;%0A%0A    TransportNetwork currentNetwork = null;%0A%0A    public TransportNetworkCache(String sourceBucket) %7B%0A        this.sourceBucket = sourceBucket;%0A    %7D%0A%0A    /**%0A     * Return the graph for the given unique identifier for graph builder inputs on S3.%0A     * If this is the same as the last graph built, just return the pre-built graph.%0A     * If not, build the graph from the inputs, fetching them from S3 to the local cache as needed.%0A     */%0A    public synchronized TransportNetwork getNetwork (String networkId) %7B%0A%0A        LOG.info(%22Finding or building a TransportNetwork for ID %7B%7D%22, networkId);%0A%0A        if (networkId.equals(currentNetworkId)) %7B%0A            LOG.info(%22Network ID has not changed. Reusing the last one that was built.%22);%0A            return currentNetwork;%0A        %7D%0A%0A        // The location of the inputs that will be used to build this graph%0A        File dataDirectory = new File(CACHE_DIR, networkId);%0A%0A        // If we don't have a local copy of the inputs, fetch graph data as a ZIP from S3 and unzip it.%0A        if( ! dataDirectory.exists() %7C%7C dataDirectory.list().length == 0) %7B%0A            LOG.info(%22Downloading graph input files from S3.%22);%0A            dataDirectory.mkdirs();%0A            S3Object graphDataZipObject = s3.getObject(sourceBucket, networkId + %22.zip%22);%0A            ZipInputStream zis = new ZipInputStream(graphDataZipObject.getObjectContent());%0A            try %7B%0A                ZipEntry entry;%0A                while ((entry = zis.getNextEntry()) != null) %7B%0A                    File entryDestination = new File(dataDirectory, entry.getName());%0A                    // Are both these mkdirs calls necessary?%0A                    entryDestination.getParentFile().mkdirs();%0A                    if (entry.isDirectory())%0A                        entryDestination.mkdirs();%0A                    else %7B%0A                        OutputStream entryFileOut = new FileOutputStream(entryDestination);%0A                        IOUtils.copy(zis, entryFileOut);%0A                        entryFileOut.close();%0A                    %7D%0A                %7D%0A                zis.close();%0A            %7D catch (Exception e) %7B%0A                // TODO delete cache dir which is probably corrupted.%0A                LOG.info(%22Error retrieving transportation network input files%22, e);%0A            %7D%0A        %7D else %7B%0A            LOG.info(%22Input files were found locally. Using these files from the cache.%22);%0A        %7D%0A%0A        // Now we have a local copy of these graph inputs. Make a graph out of them.%0A        CommandLineParameters params = new CommandLineParameters();%0A        currentNetwork = TransportNetwork.fromDirectory(new File(CACHE_DIR, networkId));%0A        currentNetworkId = networkId;%0A        return currentNetwork;%0A%0A    %7D%0A%0A%7D%0A

Add TransportNetworkBuilder, a replacement for ClusterGraphBuilder