/*
 * Copyright 2012 Athens Team
 * 
 * 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 com.eincs.athens.db.leveldb;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import org.iq80.leveldb.DBException;
import org.iq80.leveldb.WriteBatch;
import org.iq80.leveldb.WriteOptions;

import com.eincs.athens.db.StatisticsDB;
import com.eincs.athens.db.data.Statistics;
import com.eincs.athens.db.data.StatisticsKey;
import com.eincs.athens.db.leveldb.AthensDBFactory.AthensDB;

/**
 * @author Jung-Haeng Lee
 */
public class LevelDBStatisticsDB implements StatisticsDB {

	private AthensDB athensDB;

	public LevelDBStatisticsDB(AthensDB athensDB) {
		this.athensDB = athensDB;
	}

	@Override
	public Statistics getStatistics(StatisticsKey key) throws DBException,
			IOException, ClassNotFoundException {
		byte[] result = athensDB.get(key.toBytes());
		if (result != null) {
			ByteArrayInputStream bais = new ByteArrayInputStream(result);
			ObjectInputStream ois = new ObjectInputStream(bais);
			Statistics obj = (Statistics) ois.readObject();
			return obj;
		}
		return null;
	}

	@Override
	public void putStatistics(StatisticsKey key, Statistics statistics)
			throws DBException, IOException {
		WriteBatch batch = athensDB.createWriteBatch();
		batch.put(key.toBytes(), statistics.toBytes());
		athensDB.write(batch, new WriteOptions());
		batch.close();
	}

	@Override
	public void removeStatistics(StatisticsKey key) throws DBException,
			IOException {
		athensDB.delete(key.toBytes());
	}

	@Override
	public void clear() throws DBException {
		// TODO Auto-generated method stub
	}
}

@@ -1421,235 +1421,47 @@
 ;%0A%09%09
-if (result != null) %7B%0A%09%09%09ByteArrayInputStream bais = new ByteArrayInputStream(result);%0A%09%09%09ObjectInputStream ois = new ObjectInputStream(bais);%0A%09%09%09Statistics obj = (Statistics) ois.readObject();%0A%09%09%09return obj;%0A%09%09%7D%0A%09%09return null;
+return Statistics.fromBytes(result);%0A%09%09
 %0A%09%7D%0A

create Statistics by fromBytes