Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) 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 org.apache.flink.statistics.model; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Output; /** * A simple in-memory store for {@link StatisticsRecord}s * TODO: test me! */ @Deprecated public class SimpleStatisticsStore { private static final Log LOG = LogFactory.getLog(SimpleStatisticsStore.class); private HashMap<StatisticsStoreKey, ArrayList<StatisticsRecord>> statistics; private String path; public SimpleStatisticsStore() { LOG.debug("initialize StatisticsStore without path"); this.path = null; this.statistics = new HashMap<StatisticsStoreKey, ArrayList<StatisticsRecord>>(); } @SuppressWarnings("unchecked") public SimpleStatisticsStore(String path) throws IOException { LOG.debug("initialize StatisticsStore with path " + path); this.path = path; if (path != null) { File f = new File(path); if (!f.isFile() || !f.canRead() || f.length() == 0) { LOG.debug("Start new simple StatStore"); this.statistics = new HashMap<StatisticsStoreKey, ArrayList<StatisticsRecord>>(); } else { Input in = new Input(new FileInputStream(f)); Kryo kryo = new Kryo(); kryo.setRegistrationRequired(false); this.statistics = (HashMap<StatisticsStoreKey, ArrayList<StatisticsRecord>>) kryo .readClassAndObject(in); LOG.debug("Append to simple StatStore"); } } else { LOG.debug("Start new simple StatStore"); this.statistics = new HashMap<StatisticsStoreKey, ArrayList<StatisticsRecord>>(); } } /** * @return the path */ public String getPath() { return this.path; } /** * @param path * the path to set */ public void setPath(String path) { this.path = path; } public void clear() { this.statistics.clear(); } public int size() { return this.statistics.size(); } /** * @param key * @return a list of {@link StatisticsRecord}s * containing all statistics that have been mapped to the key. */ public synchronized List<StatisticsRecord> getRecords(StatisticsStoreKey key) { if (key == null) { throw new NullPointerException("A key must not be null."); } List<StatisticsRecord> result = this.statistics.get(key); if (result == null) { result = new ArrayList<StatisticsRecord>(0); } return result; } /** * Appends a statistic represented by a {@link StatisticsCommit} to this store. * * @param commit */ public void appendCommit(StatisticsCommit commit) { if (commit == null) { throw new NullPointerException("A commit must not be null."); } appendRecord(commit.getKey(), commit.getRecord()); if (LOG.isInfoEnabled()) { LOG.info("Stored statistic result: " + commit); } } /** * Appends a statistic represented by a {@link StatisticsRecord} to this store and maps it to a {@link StatisticsStoreKey}. * * @param key * @param record */ public synchronized void appendRecord(StatisticsStoreKey key, StatisticsRecord record) { if (key == null || record == null) { throw new NullPointerException("A key/JSON object must not be null."); } ArrayList<StatisticsRecord> records = this.statistics.get(key); if (records == null) { records = new ArrayList<StatisticsRecord>(); this.statistics.put(key, records); } records.add(record); } public void remove(StatisticsStoreKey key) { this.statistics.remove(key); } public void shutdown() throws IOException { if (path != null) { File f = new File(path); File parent_directory = f.getParentFile(); if (null != parent_directory) { parent_directory.mkdirs(); } if (f.isDirectory()) { if (path.endsWith("/")) { f = new File(path.substring(0, path.lastIndexOf("/") + 1) + "simpleStatStore.db"); } else { f = new File(path + "/simpleStatStore.db"); } } f.createNewFile(); if (!f.canWrite() && !f.setWritable(true)) { LOG.error("no permission to write index to file: " + path); throw new IOException(); } Output out = new Output(new FileOutputStream(f)); Kryo kryo = new Kryo(); kryo.setRegistrationRequired(false); kryo.writeClassAndObject(out, statistics); out.close(); } } }