Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.skife.jdbi.v2.sqlobject;
  
  import  com.fasterxml.classmate.members.ResolvedMethod;
  import  net.sf.cglib.proxy.MethodProxy;
 
 import java.util.List;
 
 {
     private final String  sql;
     private final boolean transactional;
     private final ChunkSizeFunction batchChunkSize;
 
     public BatchHandler(Class<?> sqlObjectType, ResolvedMethod method)
     {
         super(sqlObjectTypemethod);
         Method raw_method = method.getRawMember();
         SqlBatch anno = raw_method.getAnnotation(SqlBatch.class);
         this. = SqlObject.getSql(annoraw_method);
         this. = anno.transactional();
         this. = determineBatchChunkSize(sqlObjectTyperaw_method);
     }
 
     private ChunkSizeFunction determineBatchChunkSize(Class<?> sqlObjectTypeMethod raw_method)
     {
         // this next big if chain determines the batch chunk size. It looks from most specific
         // scope to least, that is: as an argument, then on the method, then on the class,
         // then default to Integer.MAX_VALUE
 
         int index_of_batch_chunk_size_annotation_on_parameter;
         if ((index_of_batch_chunk_size_annotation_on_parameter = findBatchChunkSizeFromParam(raw_method)) >= 0) {
             return new ParamBasedChunkSizeFunction(index_of_batch_chunk_size_annotation_on_parameter);
         }
         else if (raw_method.isAnnotationPresent(BatchChunkSize.class)) {
             final int size = raw_method.getAnnotation(BatchChunkSize.class).value();
             if (size <= 0) {
                 throw new IllegalArgumentException("Batch chunk size must be >= 0");
             }
             return new ConstantChunkSizeFunction(size);
         }
         else if (sqlObjectType.isAnnotationPresent(BatchChunkSize.class)) {
             final int size = BatchChunkSize.class.cast(sqlObjectType.getAnnotation(BatchChunkSize.class)).value();
             return new ConstantChunkSizeFunction(size);
         }
         else {
             return new ConstantChunkSizeFunction(.);
         }
     }
 
     private int findBatchChunkSizeFromParam(Method raw_method)
     {
         Annotation[][] param_annos = raw_method.getParameterAnnotations();
         for (int i = 0; i < param_annos.lengthi++) {
             Annotation[] annos = param_annos[i];
             for (Annotation anno : annos) {
                 if (anno.annotationType().isAssignableFrom(BatchChunkSize.class)) {
                     return i;
                 }
             }
         }
         return -1;
     }
 
     public Object invoke(HandleDing hObject targetObject[] args, MethodProxy mp)
     {
         Handle handle = h.getHandle();
 
         List<Iteratorextras = new ArrayList<Iterator>();
         for (final Object arg : args) {
             if (arg instanceof Iterable) {
                 extras.add(((Iterablearg).iterator());
             }
             else if (arg instanceof Iterator) {
                 extras.add((Iteratorarg);
             }
             else if (arg.getClass().isArray()) {
                 extras.add(Arrays.asList((Object[])arg).iterator());
             }
             else {
                 extras.add(new Iterator()
                 {
                     public boolean hasNext()
                     {
                         return true;
                     }
 
                     public Object next()
                    {
                        return arg;
                    }
                    public void remove()
                    {
                        // NOOP
                    }
                }
                );
            }
        }
        int processed = 0;
        List<int[]> rs_parts = new ArrayList<int[]>();
        PreparedBatch batch = handle.prepareBatch();
        applyCustomizers(batchargs);
        Object[] _args;
        int chunk_size = .call(args);
        while ((_args = next(extras)) != null) {
            PreparedBatchPart part = batch.add();
            applyBinders(part_args);
            if (++processed == chunk_size) {
                // execute this chunk
                processed = 0;
                rs_parts.add(executeBatch(handlebatch));
                batch = handle.prepareBatch();
                populateSqlObjectData((ConcreteStatementContextbatch.getContext());
                applyCustomizers(batchargs);
            }
        }
        //execute the rest
        rs_parts.add(executeBatch(handlebatch));
        // combine results
        int end_size = 0;
        for (int[] rs_part : rs_parts) {
            end_size += rs_part.length;
        }
        int[] rs = new int[end_size];
        int offset = 0;
        for (int[] rs_part : rs_parts) {
            System.arraycopy(rs_part, 0, rsoffsetrs_part.length);
            offset += rs_part.length;
        }
        return rs;
    }
    private int[] executeBatch(final Handle handlefinal PreparedBatch batch)
    {
        if ((!handle.isInTransaction()) && ) {
            // it is safe to use same prepared batch as the inTransaction passes in the same
            // Handle instance.
            return handle.inTransaction(new TransactionCallback<int[]>()
            {
                public int[] inTransaction(Handle connTransactionStatus statusthrows Exception
                {
                    return batch.execute();
                }
            });
        }
        else {
            return batch.execute();
        }
    }
    private static Object[] next(List<Iteratorargs)
    {
        List<Objectrs = new ArrayList<Object>();
        for (Iterator arg : args) {
            if (arg.hasNext()) {
                rs.add(arg.next());
            }
            else {
                return null;
            }
        }
        return rs.toArray();
    }
    private static interface ChunkSizeFunction
    {
        int call(Object[] args);
    }
    private static class ConstantChunkSizeFunction implements ChunkSizeFunction
    {
        private final int value;
        ConstantChunkSizeFunction(int value) {
            this. = value;
        }
        public int call(Object[] args)
        {
            return ;
        }
    }
    private static class ParamBasedChunkSizeFunction implements ChunkSizeFunction
    {
        private final int index;
        ParamBasedChunkSizeFunction(int index) {
            this. = index;
        }
        public int call(Object[] args)
        {
            return (Integer)args[];
        }
    }
New to GrepCode? Check out our FAQ X