tachyon.master.Image.java Source code

Java tutorial

Introduction

Here is the source code for tachyon.master.Image.java

Source

/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.master;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectWriter;

import tachyon.Constants;
import tachyon.TachyonURI;
import tachyon.UnderFileSystem;

/**
 * Master data image.
 */
public class Image {
    private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);

    /**
     * Write a new image to path. This method assumes having a lock on the master info.
     * 
     * @param info the master info to generate the image
     * @param path the new image path
     * @throws IOException
     */
    public static void create(MasterInfo info, String path) throws IOException {
        String tPath = path + ".tmp";
        String parentFolder = path.substring(0, path.lastIndexOf(TachyonURI.SEPARATOR));
        LOG.info("Creating the image file: " + tPath);
        UnderFileSystem ufs = UnderFileSystem.get(path, info.getTachyonConf());
        if (!ufs.exists(parentFolder)) {
            LOG.info("Creating parent folder " + parentFolder);
            ufs.mkdirs(parentFolder, true);
        }
        OutputStream os = ufs.create(tPath);
        DataOutputStream imageOs = new DataOutputStream(os);
        ObjectWriter writer = JsonObject.createObjectMapper().writer();

        info.writeImage(writer, imageOs);
        imageOs.flush();
        imageOs.close();

        LOG.info("Succefully created the image file: " + tPath);
        ufs.delete(path, false);
        ufs.rename(tPath, path);
        ufs.delete(tPath, false);
        LOG.info("Renamed " + tPath + " to " + path);
        // safe to close, nothing created here with scope outside function
        ufs.close();
    }

    /**
     * Load an image into the masterinfo.
     * 
     * @param info the masterinfo to fill.
     * @param path the data to load
     * @throws IOException
     */
    public static void load(MasterInfo info, String path) throws IOException {
        UnderFileSystem ufs = UnderFileSystem.get(path, info.getTachyonConf());
        if (!ufs.exists(path)) {
            LOG.info("Image " + path + " does not exist.");
            return;
        }
        LOG.info("Loading image " + path);
        DataInputStream imageIs = new DataInputStream(ufs.open(path));
        JsonParser parser = JsonObject.createObjectMapper().getFactory().createParser(imageIs);

        info.loadImage(parser, new TachyonURI(path));
        imageIs.close();
        ufs.close();
    }

    /**
     * Rename the src to the dst. Only used to rename the Image.
     * 
     * @param src The src image path
     * @param dst The dst image path
     * @param info The MasterInfo used to rename the image
     * @throws IOException
     */
    public static void rename(String src, String dst, MasterInfo info) throws IOException {
        UnderFileSystem ufs = UnderFileSystem.get(src, info.getTachyonConf());
        ufs.rename(src, dst);
        LOG.info("Renamed " + src + " to " + dst);
    }
}