All Downloads are FREE. Search and download functionalities are using the official Maven repository.

proj.zoie.store.AbstractZoieStore Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
package proj.zoie.store;

import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Comparator;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.log4j.Logger;

import proj.zoie.impl.indexing.ZoieConfig;

public abstract class AbstractZoieStore implements ZoieStore {

	private static Logger logger = Logger.getLogger(AbstractZoieStore.class);
	private boolean _dataCompressed = true;
	private Comparator _versionComparator = ZoieConfig.DEFAULT_VERSION_COMPARATOR;

	private volatile String _version = null;
	private final Long2ObjectLinkedOpenHashMap _dataMap = new Long2ObjectLinkedOpenHashMap();

	private final ReentrantReadWriteLock _dataMapLock = new ReentrantReadWriteLock();
	private final WriteLock _writeLock = _dataMapLock.writeLock();
	private final ReadLock _readLock = _dataMapLock.readLock();

	public void setVersionComparator(Comparator versionComparator) {
		_versionComparator = versionComparator;
	}

	public Comparator getVersionComparator() {
		return _versionComparator;
	}

	public void setDataCompressed(boolean dataCompressed) {
		_dataCompressed = dataCompressed;
	}

	public boolean isDataCompressed() {
		return _dataCompressed;
	}
	
	protected abstract void persist(long uid, byte[] data) throws IOException;

	protected abstract void persistDelete(long uid) throws IOException;

	protected abstract byte[] getFromStore(long uid) throws IOException;

	protected abstract void commitVersion(String version) throws IOException;

	public abstract String getPersistedVersion() throws IOException;

	public abstract void close() throws IOException;
	public abstract void open() throws IOException;
	
	public static byte[] compress(byte[] src) throws IOException{
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GZIPOutputStream gzipStream = new GZIPOutputStream(bout);

        gzipStream.write(src);
        gzipStream.flush();
        gzipStream.close();
        bout.flush();

        return bout.toByteArray();
	}

	public static byte[] uncompress(byte[] src) throws IOException{
		byte[] buffer = new byte[1024];  // 1k buffer
		ByteArrayInputStream bin = new ByteArrayInputStream(src);
		GZIPInputStream gzipIn = new GZIPInputStream(bin);
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		while(gzipIn.available()>0){
			int len = gzipIn.read(buffer);
			if (len<=0) break;
			if (len




© 2015 - 2024 Weber Informatics LLC | Privacy Policy