com.yimidida.shards.select.impl.ShardSelectImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.yimidida.shards.select.impl.ShardSelectImpl.java

Source

/*
 * @(#)ShardSelectImpl.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 com.yimidida.shards.select.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.yimidida.shards.Shard;
import com.yimidida.shards.ShardId;
import com.yimidida.shards.ShardOperation;
import com.yimidida.shards.select.SelectFactory;
import com.yimidida.shards.select.ShardSelect;
import com.yimidida.shards.strategy.access.ShardAccessStrategy;
import com.yimidida.shards.strategy.exit.impl.ConcatenateListsExitStrategy;
import com.yimidida.shards.strategy.exit.impl.ExitOperationsSelectCollector;
import com.yimidida.shards.strategy.exit.impl.SelectOneExitStrategy;
import com.yimidida.shards.strategy.reduce.ShardReduceStrategy;
import com.yimidida.shards.utils.ParameterUtil;

/**
 * 
 */
public class ShardSelectImpl implements ShardSelect {

    private final List<Shard> shards;

    private final SelectFactory selectFactory;

    private final ShardAccessStrategy shardAccessStrategy;

    /**
     * The queryCollector is not used in ShardedQueryImpl as it would require
     * this implementation to parse the query string and extract which exit
     * operations would be appropriate. This member is a place holder for future
     * development.
     */
    private final ExitOperationsSelectCollector selectCollector;

    public ShardSelectImpl(List<Shard> shards, SelectFactory selectFactory, ShardAccessStrategy shardAccessStrategy,
            ShardReduceStrategy shardReduceStrategy) {
        this.shards = shards;
        this.selectFactory = selectFactory;
        this.shardAccessStrategy = shardAccessStrategy;
        this.selectCollector = new ExitOperationsSelectCollector(selectFactory, shardReduceStrategy);
    }

    @Override
    @SuppressWarnings("unchecked")
    public <E> List<E> getResultList() {
        ShardOperation<List<Object>> shardOp = new ShardOperation<List<Object>>() {
            public List<Object> execute(SqlSession session, ShardId shardId) {

                return session.selectList(selectFactory.getStatement(),
                        ParameterUtil.resolve(selectFactory.getParameter(), shardId), selectFactory.getRowBounds());
            }

            public String getOperationName() {
                return "getResultList()";
            }
        };

        return (List<E>) shardAccessStrategy.apply(shards, shardOp, new ConcatenateListsExitStrategy(),
                selectCollector);
    }

    @Override
    public <K, V> Map<K, V> getResultMap() {
        //      ShardOperation<Map<K, V>> shardOp = new ShardOperation<Map<K, V>>() {
        //         public Map<K, V> execute(SqlSession session, ShardId shardId) {
        //
        //            return session.selectMap(selectFactory.getStatement(), 
        //                  ParameterUtil.resolve(selectFactory.getParameter(), shardId), 
        //                  selectFactory.getMapKey());
        //         }
        //
        //         public String getOperationName() {
        //            return "getResultMap()";
        //         }
        //      };

        throw new UnsupportedOperationException();

        //      return (T) shardAccessStrategy.apply(
        //            shards,
        //            shardOp,
        //            new SelectOneExitStrategy(),
        //            new ExitOperationsSelectMapCollector(selectFactory
        //                  .getStatement()));
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T getSingleResult() {

        ShardOperation<Object> shardOp = new ShardOperation<Object>() {
            public Object execute(SqlSession session, ShardId shardId) {

                return session.selectOne(selectFactory.getStatement(),
                        ParameterUtil.resolve(selectFactory.getParameter(), shardId));
            }

            public String getOperationName() {
                return "getSingleResult()";
            }
        };

        return (T) shardAccessStrategy.apply(shards, shardOp, new SelectOneExitStrategy(), selectCollector);
    }

}