### Question:
TableSplitsCache extends BaseHdfsFileCacheUtil { public static List<Text> trimSplits(List<Text> tableSplits, int maxSplits) { if (tableSplits.size() <= maxSplits) { return tableSplits; } double r = (maxSplits + 1) / (double) (tableSplits.size()); double pos = 0; ArrayList<Text> subset = new ArrayList<>(maxSplits); int numberSplitsUsed = 0; for (int i = 0; i < tableSplits.size() && numberSplitsUsed < maxSplits; i++) { pos += r; while (pos > 1) { subset.add(tableSplits.get(i)); numberSplitsUsed++; pos -= 1; } } return subset; } TableSplitsCache(Configuration conf); static List<Text> trimSplits(List<Text> tableSplits, int maxSplits); @Override void setCacheFilePath(Configuration conf); static Path getSplitsPath(Configuration conf); static boolean shouldRefreshSplits(Configuration conf); FileStatus getFileStatus(); @Override void writeCacheFile(FileSystem fs, Path tmpSplitsFile); List<Text> getSplits(String table); List<Text> getSplits(String table, int maxSplits); Map<String,List<Text>> getSplits(); static final String REFRESH_SPLITS; static final String SPLITS_CACHE_DIR; static final String MAX_SPLIT_DECREASE; static final String MAX_SPLIT_PERCENTAGE_DECREASE; }### Answer:
@Test public void testTrimTableSplits() { setSplitsCacheDir(); int reducers = 487; int numsplits = 195365; List<Text> splits = new ArrayList<>(numsplits); for (int i = 0; i < numsplits; i++) { splits.add(new Text(Integer.toString(i))); } List<Text> newSplits = TableSplitsCache.trimSplits(splits, reducers - 1); Assert.assertEquals("split count", (reducers - 1), newSplits.size()); Assert.assertEquals("first split", 401, Integer.parseInt(newSplits.get(0).toString())); Assert.assertEquals("num splits - last split", 402, numsplits - Integer.parseInt(newSplits.get(newSplits.size() - 1).toString())); int lastsplit = 0; for (int i = 0; i < newSplits.size(); i++) { int split = Integer.parseInt(newSplits.get(i).toString()); Assert.assertTrue("split size", (split - lastsplit) >= 401); Assert.assertTrue("split size", (split - lastsplit) <= 402); lastsplit = split; } }