Iterate over keys within the passed inclusive range.
(final byte[] a,
final byte[] b, final int num)
| 1213 | * Iterate over keys within the passed inclusive range. |
| 1214 | */ |
| 1215 | public static Iterable<byte[]> iterateOnSplits(final byte[] a, |
| 1216 | final byte[] b, final int num) { |
| 1217 | byte[] aPadded; |
| 1218 | byte[] bPadded; |
| 1219 | if (a.length < b.length) { |
| 1220 | aPadded = padTail(a, b.length - a.length); |
| 1221 | bPadded = b; |
| 1222 | } else if (b.length < a.length) { |
| 1223 | aPadded = a; |
| 1224 | bPadded = padTail(b, a.length - b.length); |
| 1225 | } else { |
| 1226 | aPadded = a; |
| 1227 | bPadded = b; |
| 1228 | } |
| 1229 | if (compareTo(aPadded, bPadded) >= 0) { |
| 1230 | throw new IllegalArgumentException("b <= a"); |
| 1231 | } |
| 1232 | if (num <= 0) { |
| 1233 | throw new IllegalArgumentException("num cannot be < 0"); |
| 1234 | } |
| 1235 | byte[] prependHeader = { 1, 0 }; |
| 1236 | final BigInteger startBI = new BigInteger(add(prependHeader, aPadded)); |
| 1237 | final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded)); |
| 1238 | final BigInteger diffBI = stopBI.subtract(startBI); |
| 1239 | final BigInteger splitsBI = BigInteger.valueOf(num + 1); |
| 1240 | if (diffBI.compareTo(splitsBI) < 0) { |
| 1241 | return null; |
| 1242 | } |
| 1243 | final BigInteger intervalBI; |
| 1244 | try { |
| 1245 | intervalBI = diffBI.divide(splitsBI); |
| 1246 | } catch (Exception e) { |
| 1247 | LOG.error("Exception caught during division", e); |
| 1248 | return null; |
| 1249 | } |
| 1250 | |
| 1251 | final Iterator<byte[]> iterator = new Iterator<byte[]>() { |
| 1252 | private int i = -1; |
| 1253 | |
| 1254 | @Override |
| 1255 | public boolean hasNext() { |
| 1256 | return i < num + 1; |
| 1257 | } |
| 1258 | |
| 1259 | @Override |
| 1260 | public byte[] next() { |
| 1261 | i++; |
| 1262 | if (i == 0) |
| 1263 | return a; |
| 1264 | if (i == num + 1) |
| 1265 | return b; |
| 1266 | |
| 1267 | BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger |
| 1268 | .valueOf(i))); |
| 1269 | byte[] padded = curBI.toByteArray(); |
| 1270 | if (padded[1] == 0) |
| 1271 | padded = tail(padded, padded.length - 2); |
| 1272 | else |