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

net.spy.memcached.internal.BulkGetFuture Maven / Gradle / Ivy

The newest version!
package net.spy.memcached.internal;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.compat.log.LoggerFactory;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationState;

/**
 * Future for handling results from bulk gets.
 *
 * Not intended for general use.
 *
 * @param  types of objects returned from the GET
 */
public class BulkGetFuture implements BulkFuture> {
	private final Map> rvMap;
	private final Collection ops;
	private final CountDownLatch latch;
	private boolean cancelled=false;
	private boolean timeout = false;

	public BulkGetFuture(Map> m,
			Collection getOps, CountDownLatch l) {
		super();
		rvMap = m;
		ops = getOps;
		latch=l;
	}

	public boolean cancel(boolean ign) {
		boolean rv=false;
		for(Operation op : ops) {
			rv |= op.getState() == OperationState.WRITING;
			op.cancel();
		}
		for (Future v : rvMap.values()) {
			v.cancel(ign);
		}
		cancelled=true;
		return rv;
	}

	public Map get()
		throws InterruptedException, ExecutionException {
		try {
			return get(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
		} catch (TimeoutException e) {
			throw new RuntimeException("Timed out waiting forever", e);
		}
	}

    /* (non-Javadoc)
     * @see net.spy.memcached.internal.BulkFuture#getSome(long, java.util.concurrent.TimeUnit)
     */
    public Map getSome(long to, TimeUnit unit)
            throws InterruptedException, ExecutionException {
        Collection timedoutOps = new HashSet();
        Map ret = internalGet(to, unit, timedoutOps);
        if (timedoutOps.size() > 0) {
            timeout = true;
            LoggerFactory.getLogger(getClass()).warn(
                    new CheckedOperationTimeoutException(
                            "Operation timed out: ", timedoutOps).getMessage());
        }
        return ret;

    }

    /*
     * get all or nothing: timeout exception is thrown if
     * all the data could not be retrieved
     *
     * @see java.util.concurrent.Future#get(long,
     * java.util.concurrent.TimeUnit)
     */
    public Map get(long to, TimeUnit unit)
            throws InterruptedException, ExecutionException, TimeoutException {
        Collection timedoutOps = new HashSet();
        Map ret = internalGet(to, unit, timedoutOps);
        if (timedoutOps.size() > 0) {
            this.timeout = true;
            throw new CheckedOperationTimeoutException("Operation timed out.",
                    timedoutOps);
        }
        return ret;
    }

    /**
     * refactored code common to both get(long, TimeUnit)
     * and getSome(long, TimeUnit)
     *
     * @param to
     * @param unit
     * @param timedoutOps
     * @return
     * @throws InterruptedException
     * @throws ExecutionException
     */
    private Map internalGet(long to, TimeUnit unit,
            Collection timedoutOps) throws InterruptedException,
            ExecutionException {
        if (!latch.await(to, unit)) {
            for (Operation op : ops) {
                if (op.getState() != OperationState.COMPLETE) {
                    MemcachedConnection.opTimedOut(op);
                    timedoutOps.add(op);
                } else {
                    MemcachedConnection.opSucceeded(op);
                }
            }
        }
        for (Operation op : ops) {
            if (op.isCancelled()) {
                throw new ExecutionException(new RuntimeException("Cancelled"));
            }
            if (op.hasErrored()) {
                throw new ExecutionException(op.getException());
            }
        }
        Map m = new HashMap();
        for (Map.Entry> me : rvMap.entrySet()) {
            m.put(me.getKey(), me.getValue().get());
        }
        return m;
    }

	public boolean isCancelled() {
		return cancelled;
	}

	public boolean isDone() {
		return latch.getCount() == 0;
	}

	/* set to true if timeout was reached.
	 *
	 * @see net.spy.memcached.internal.BulkFuture#isTimeout()
	 */
	public boolean isTimeout() {
		return timeout;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy