/*
 * @(#)SequentialShardAccessStrategy.java 2012-8-1 下午10:00:00
 *
 * Copyright (c) 2011-2012 Makersoft.org all rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 */
package org.makersoft.shards.strategy.access.impl;

import java.util.List;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.makersoft.shards.Shard;
import org.makersoft.shards.ShardId;
import org.makersoft.shards.ShardOperation;
import org.makersoft.shards.strategy.access.ShardAccessStrategy;
import org.makersoft.shards.strategy.exit.ExitOperationsCollector;
import org.makersoft.shards.strategy.exit.ExitStrategy;

/**
 * 顺序访问策略
 */
public class SequentialShardAccessStrategy implements ShardAccessStrategy {

	private final Log log = LogFactory.getLog(getClass());

	public <T> T apply(List<Shard> shards, ShardOperation<T> operation,
			ExitStrategy<T> exitStrategy,
			ExitOperationsCollector exitOperationsCollector) {
		
		execute: for (Shard shard : getNextOrderingOfShards(shards)) {
			for(ShardId shardId : shard.getShardIds()){
				if (exitStrategy.addResult(operation.execute(shard.establishSqlSession(), shardId), shard)) {
					log.debug(String.format("Short-circuiting operation %s after execution against shard %s", operation.getOperationName(), shard));
					break execute;
				}
			}
			
		}
		
		return exitStrategy.compileResults(exitOperationsCollector);
	}

	/**
	 * Override this method if you want to control the order in which the shards
	 * are operated on (this comes in handy when paired with exit strategies
	 * that allow early exit because it allows you to evenly distribute load).
	 * Deafult implementation is to just iterate in the same order every time.
	 * 
	 * @param shards
	 *            The shards we might want to reorder
	 * @return Reordered view of the shards.
	 */
	protected Iterable<Shard> getNextOrderingOfShards(List<Shard> shards) {
		return shards;
	}
}

@@ -1007,16 +1007,80 @@
  %7B%0D%0A%09%09%0D%0A
+%09%09/*%0D%0A%09%09 * TODO:%E5%B0%86%E5%8F%8C%E9%87%8D%E5%BE%AA%E7%8E%AF%E4%BC%98%E5%8C%96%E4%B8%BA%E4%B8%80%E6%AC%A1%E5%BE%AA%E7%8E%AFMap%3CShardId,Shard%3E%EF%BC%8C%E4%BE%BF%E4%BA%8E%E4%BB%A3%E7%A0%81%E5%8F%AF%E8%AF%BB%E6%80%A7%E3%80%82%0D%0A%09%09 */%0D%0A
 %09%09execut

add copy right