1 16 package com.jdon.model.query.block; 17 18 import java.util.Collection ; 19 import java.util.List ; 20 21 import org.apache.log4j.Logger; 22 23 import com.jdon.model.query.JdbcTemp; 24 import com.jdon.model.query.cache.BlockCacheManager; 25 import com.jdon.model.query.cache.QueryConditonDatakey; 26 27 36 public class BlockStrategy { 37 private final static Logger logger = Logger.getLogger(BlockStrategy.class); 38 39 private BlockQueryJDBC blockQueryJDBC; 40 41 private JdbcTemp jdbcTemp; 42 43 private BlockCacheManager blockCacheManager; 44 45 private int blockLength = 200; 46 47 48 53 public BlockStrategy(BlockQueryJDBC blockQueryJDBC, JdbcTemp jdbcTemp, BlockCacheManager blockCacheManager) { 54 super(); 55 this.blockQueryJDBC = blockQueryJDBC; 56 this.jdbcTemp = jdbcTemp; 57 this.blockCacheManager = blockCacheManager; 58 } 59 67 public Block locate(String sqlquery, Collection queryParams, Object locateId) { 68 int blockSize = getBlockLength(); 69 Block block = null; 70 int index = -1; 71 int prevBlockStart = Integer.MIN_VALUE; 72 int nextBlockStart = Integer.MIN_VALUE; 73 int start = 0; 74 75 logger.debug("[JdonFramework]try to locate a block locateId= " + locateId + " blockSize=" + blockSize); 76 77 try { 78 while (index == -1) { 79 block = getBlock(sqlquery, queryParams, start, blockSize); 80 if (block == null) 81 break; 82 83 List list = block.getList(); 84 index = list.indexOf(locateId); 85 if ((index >= 0) && (index < list.size())) { 86 logger.debug("[JdonFramework]found the locateId, index= " + index); 87 if ((index == 0) && (block.getStart() >= blockSize)) prevBlockStart = start - blockSize; 89 else if (index == blockSize - 1) nextBlockStart = start + blockSize; 91 break; 92 } else { 93 if (block.getCount() >= blockSize) start = start + blockSize; 95 else 96 break; 98 } 99 } 100 101 if (index == -1) { 102 logger.debug("[JdonFramework] not locate the block that have the locateId= " + locateId); 103 return null; } 105 106 if (prevBlockStart != Integer.MIN_VALUE) { 107 Block prevBlock = getBlock(sqlquery, queryParams, prevBlockStart, blockSize); 108 prevBlock.getList().addAll(block.getList()); 109 prevBlock.setStart(prevBlock.getStart() + prevBlock.getCount()); 110 prevBlock.setCount(prevBlock.getCount() + block.getCount()); 111 return prevBlock; 112 } else if (nextBlockStart != Integer.MIN_VALUE) { Block nextBlock = getBlock(sqlquery, queryParams, nextBlockStart, blockSize); 114 if (nextBlock != null) { 115 block.getList().addAll(nextBlock.getList()); 116 block.setCount(block.getCount() + nextBlock.getCount()); 117 } 118 return block; 119 } else 120 return block; 121 } catch (Exception e) { 122 logger.error(" locate Block error" + e); 123 } 124 return block; 125 } 126 127 133 public Block getBlock(String sqlquery, Collection queryParams, int startIndex, int count) { 134 logger.debug("[JdonFramework]enter getBlock .. "); 135 if ((count > this.blockLength) || (count <= 0)) { count = this.blockLength; 137 } 138 QueryConditonDatakey qcdk = new QueryConditonDatakey(sqlquery, queryParams, startIndex, count, this.blockLength); 139 Block block = getBlock(qcdk); 140 if (block.getCount() > 0) { 141 logger.debug("[JdonFramework]got a Block" + block.getCount()); 142 return block; 143 } else { 144 logger.debug("[JdonFramework]not found the block!"); 145 return null; 146 } 147 } 148 149 154 private Block getBlock(QueryConditonDatakey qcdk) { 155 Block clientBlock = new Block(qcdk.getStart(), qcdk.getCount()); 156 if (clientBlock.getCount() > this.blockLength) 157 clientBlock.setCount(this.blockLength); 158 159 List list = getBlockKeys(qcdk); 161 Block dataBlock = new Block(qcdk.getBlockStart(), list.size()); 162 163 int currentStart = clientBlock.getStart() - dataBlock.getStart(); 164 Block currentBlock = new Block(currentStart, clientBlock.getCount()); 165 currentBlock.setList(list); 166 167 try { 168 int lastCount = dataBlock.getCount() + dataBlock.getStart() - clientBlock.getStart(); 173 logger.debug("[JdonFramework] lastCount=" + lastCount); 174 if (lastCount < clientBlock.getCount()) { 176 if (dataBlock.getCount() == this.blockLength) { 179 int newStartIndex = dataBlock.getStart() + dataBlock.getCount(); 182 int newCount = clientBlock.getCount() - lastCount; 183 qcdk.setStart(newStartIndex); 184 qcdk.setCount(newCount); 185 logger.debug("[JdonFramework] newStartIndex=" + newStartIndex + " newCount=" + newCount); 186 Block nextBlock = getBlock(qcdk); 187 logger.debug("[JdonFramework] nextBlock.getCount()=" + nextBlock.getCount()); 188 currentBlock.setCount(currentBlock.getCount() + nextBlock.getCount()); 189 } else { 190 currentBlock.setCount(lastCount); 192 } 193 } 194 } catch (Exception e) { 195 logger.error(" getBlock error" + e); 196 } 197 return currentBlock; 198 } 199 200 205 private List getBlockKeys(QueryConditonDatakey qcdk) { 206 List keys = blockCacheManager.getBlockKeysFromCache(qcdk); 207 if ((keys == null)) { 208 keys = blockQueryJDBC.fetchDatas(qcdk); 209 blockCacheManager.saveBlockKeys(qcdk, keys); 210 } 211 logger.debug("[JdonFramework] getBlockKeys, size=" + keys.size()); 212 return keys; 213 } 214 215 216 219 public int getBlockLength() { 220 return blockLength; 221 } 222 223 226 public void setBlockLength(int blockLength) { 227 this.blockLength = blockLength; 228 } 229 230 } 231 | Popular Tags |