auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
private int getCurrValidWorkers(Collection<ImmutableWorkerInfo> workers) { final Predicate<ImmutableWorkerInfo> isValidWorker = ProvisioningUtil.createValidWorkerPredicate(config); final int currValidWorkers = Collections2.filter(workers, isValidWorker).size(); log.info("Current valid workers: %d", currValidWorkers); return currValidWorkers; }

Target:
suggest `debug` level
Source:
public boolean didMove() { return moved || (copied && deletedOld); }

Target:
```moved``` and ```moveObject()``` are not needed anymore.
Source:
public void reset() { if (!initialized) { throw new ISE("Grouper is not initialized"); } if (closed) { throw new ISE("Grouper is closed"); } for (Grouper<KeyType> grouper : groupers) { grouper.reset(); } }

Target:
May replace with forEach
Source:
public void close() { try { closer.close(); } catch (IOException e) { throw Throwables.propagate(e); } }

Target:
Could be simply `new RuntimeException(e)`
Source:
private String buildLookupQuery(String table, String filter, String keyColumn, String valueColumn) { String query; if (Strings.isNullOrEmpty(filter)) { query = StringUtils.format( "SELECT %s, %s FROM %s", keyColumn, valueColumn, table ); } else { query = StringUtils.format( "SELECT %s, %s FROM %s WHERE %s", keyColumn, valueColumn, table, filter ); } return query; }

Target:
This variable is not needed, there could be two return statements in `if` and `else` blocks
Source:
public String toString() { return StringUtils.nonStrictFormat( "QueryInterruptedException{msg=%s, code=%s, class=%s, host=%s}", getMessage(), errorCode, errorClass, host ); }

Target:
nonStrictFormat() is designed mostly to be called indirectly, e. g. in a method or constructor which accepts `Object... args` itself. In this case I see no point to use it instead of `StringUtils.format()`.
Source:
public void addJobProperties(Job job) { Configuration conf = job.getConfiguration(); for (final Map.Entry<String, String> entry : schema.getTuningConfig().getJobProperties().entrySet()) { conf.set(entry.getKey(), entry.getValue()); } }

Target:
can you change this impl to be addJobProperties(job.getConfiguration()) ?
Source:
public boolean isSegmentLoadedByServer(String serverKey, DataSegment segment) { DruidServerHolder holder = servers.get(serverKey); if (holder != null) { return holder.druidServer.getSegment(segment.getIdentifier()) != null; } else { return false; } }

Target:
This is a nit, but I find inverted logic like this to be hard to following when reading code. I think it would be better as just ``` if (holder == null) { return false; } return holder.druidServer.getSegment(segment.getIdentifier()) != null; ```
Source:
public String apply(@Nullable Object value) { if (Objects.isNull(value)) { return null; } if (value instanceof Number) { return bucket(((Number) value).doubleValue()); } else if (value instanceof String) { return apply((String) value); } return null; }

Target:
Objects.isNull method exists for being used as lambda, direct use is just an obscure way to write `value == null`
Source:
public String apply(@Nullable String value) { try { return bucket(Double.parseDouble(value)); } catch (NumberFormatException | NullPointerException ex) { return null; } }

Target:
Null is "expected" here, so suggested to check for it explicitly, rather than operate via exception handling, that is an antipattern in java
Source:
static int requiredBufferCapacity( int cardinality, AggregatorFactory[] aggregatorFactories ) { final int cardinalityWithMissingValue = cardinality + 1; final int recordSize = Arrays.stream(aggregatorFactories) .mapToInt(AggregatorFactory::getMaxIntermediateSize) .sum(); return Integer.BYTES + // grouping key size getUsedFlagBufferCapacity(cardinalityWithMissingValue) + // total used flags size cardinalityWithMissingValue * recordSize; // total values size }

Target:
I don't understand where it is actually used, I see only where `getUsedFlagBufferCapacity` (for bitset) and `cardinalityWithMissingValue * recordSize` (for records) space is used
Source:
private static ImmutableWorkerInfo selectFromEligibleWorkers(final Map<String, ImmutableWorkerInfo> eligibleWorkers) { return eligibleWorkers.values().stream().min( Comparator.comparing(ImmutableWorkerInfo::getAvailableCapacity).reversed() ).orElse(null); }

Target:
It could be simple `.max(Comparator.comparing(ImmutableWorkerInfo::getAvailableCapacity))`
Source:
public int getGroupingKeySize() { return Doubles.BYTES; }

Target:
Could use Double.BYTES
Source:
public boolean checkUnsortedEncodedKeyComponentsEqual(@Nullable Float lhs, @Nullable Float rhs) { return Objects.equals(DimensionHandlerUtils.nullToZero(lhs), DimensionHandlerUtils.nullToZero(rhs)); }

Target:
`DimensionHandlerUtils.nullToZero(lhs)` is non-null, `Objects.equals()` is designed to accepts nullable arguments, so it's less confusing to use `DimensionHandlerUtils.nullToZero(lhs).equals(DimensionHandlerUtils.nullToZero(rhs))` here. + Same for Double, Long.
Source:
public int getUnsortedEncodedKeyComponentHashCode(@Nullable Float key) { return Objects.hashCode(DimensionHandlerUtils.nullToZero(key)); }

Target:
Similar to above, suggested `DimensionHandlerUtils.nullToZero(key).hashCode()`. + Same for Double, Long.
Source:
public void queryNative(Blackhole blackhole) throws Exception { HashMap<String, Object> context = Maps.newHashMap(); final Sequence<Row> resultSequence = QueryPlus.wrap(groupByQuery).run(walker, context); final ArrayList<Row> resultList = Sequences.toList(resultSequence, Lists.<Row>newArrayList()); for (Row row : resultList) { blackhole.consume(row); } }

Target:
Could we remove this local var?
Source:
protected ExprEval eval(final double x, final double y) { return ExprEval.of((long) x / (long) y); }

Target:
Maybe divide in double?
Source:
private LogicalType determineTimestampSpecLogicalType(Schema schema, String timestampSpecField) { for (Schema.Field field : schema.getFields()) { if (field.name().equals(timestampSpecField)) { this.timestampSpecLogicalType = field.schema().getLogicalType(); return this.timestampSpecLogicalType; } } // The timestamp field does not exist in the provided schema return null; }

Target:
Please annotate `@Nullable` as well.
Source:
public void open() throws IOException { writer = CompressionFactory.getFloatSerializer( ioPeon, StringUtils.safeFormat("%s.float_column", filenameBase), byteOrder, compression ); writer.open(); }

Target:
Probably should crash if bad format string
Source:
public static File makeDimFile(File dir, String dimension) { return new File(dir, StringUtils.safeFormat("dim_%s.drd", dimension)); }

Target:
Probably should crash if bad format string
Source:
public static File makeTimeFile(File dir, ByteOrder order) { return new File(dir, StringUtils.safeFormat("time_%s.drd", order)); }

Target:
Probably should crash if bad format string
Source:
public void open() throws IOException { writer = CompressionFactory.getLongSerializer( ioPeon, StringUtils.safeFormat("%s.long_column", filenameBase), byteOrder, encoding, compression ); writer.open(); }

Target:
Probably should crash if bad format string
Source:
private String makeFilename(String suffix) { return StringUtils.safeFormat("%s.%s", filenameBase, suffix); }

Target:
Probably should crash if bad format string
Source:
public void fill(int index, long[] toFill) { if (totalSize - index < toFill.length) { throw new IndexOutOfBoundsException( StringUtils.safeFormat( "Cannot fill array of size[%,d] at index[%,d]. Max size[%,d]", toFill.length, index, totalSize ) ); } for (int i = 0; i < toFill.length; i++) { toFill[i] = get(index + i); } }

Target:
do you want to just add an `IOBE`?
Source:
public static String generateValueFileName(String fileNameBase, int fileNum) { return StringUtils.safeFormat("%s_value_%d", fileNameBase, fileNum); }

Target:
Probably should crash if bad format string
Source:
public static String generateHeaderFileName(String fileNameBase) { return StringUtils.safeFormat("%s_header", fileNameBase); }

Target:
Probably should crash if bad format string
Source:
public VSizeIndexedIntsWriter( final IOPeon ioPeon, final String filenameBase, final int maxValue ) { this.ioPeon = ioPeon; this.valueFileName = StringUtils.safeFormat("%s.values", filenameBase); this.numBytes = VSizeIndexedInts.getNumBytesForMax(maxValue); }

Target:
Probably should crash if bad format string
Source:
public VSizeIndexedWriter( IOPeon ioPeon, String filenameBase, int maxId ) { this.ioPeon = ioPeon; this.metaFileName = StringUtils.safeFormat("%s.meta", filenameBase); this.headerFileName = StringUtils.safeFormat("%s.header", filenameBase); this.valuesFileName = StringUtils.safeFormat("%s.values", filenameBase); this.maxId = maxId; }

Target:
Probably should crash if bad format string
Source:
Plumber initPlumber() { log.info("Someone get us a plumber!"); plumber = fireDepartment.findPlumber(); return plumber; }

Target:
Seems that this method could be private, and don't need to return Plumber.
Source:
public int compare(String s, String s2) { // Avoid conversion to bytes for equal references if(Objects.equals(s, s2)){ return 0; } return ORDERING.compare(s, s2); }

Target:
Is this really better? For strings that _are_ equal this is faster but for strings that aren't, it should be slower. I'd guess that most comparisons are between strings that are not equal, so the old code should perform better.
Source:
public Object2LongMap<String> getSegmentAvailability() { final Object2LongOpenHashMap<String> retVal = new Object2LongOpenHashMap<>(); if (segmentReplicantLookup == null) { return retVal; } for (DataSegment segment : getAvailableDataSegments()) { if (segmentReplicantLookup.getTotalReplicants(segment.getIdentifier()) == 0) { retVal.addTo(segment.getDataSource(), 1); } } return retVal; }

Target:
This change omits segments whose total replicants is not 0. I think it shouldn't change the behavior.
Source:
public Status getStatusHTTP(@Context final HttpServletRequest req) { Access access = authorizationCheck(req, Action.READ); if (!access.isAllowed()) { log.warn("Authorization failure."); return null; } return status; }

Target:
Why is this doing `log.warn` and returning null instead of a code 403? Code 403 would make more sense to me. If it's because you just don't have anything you can return to do that, then you could try either, - switch the return type to Response - or, define a `ForbiddenException` and an `ExceptionMapper<ForbiddenException>` in the manner of `CustomExceptionMapper`; and throw the ForbiddenException.
Source:
public void destroy() { keytab = null; serverSubject = null; for (LoginContext loginContext : loginContexts) { try { loginContext.logout(); } catch (LoginException ex) { log.warn(ex.getMessage(), ex); } } loginContexts.clear(); }

Target:
This is backwards. It should be `log.warn(ex, ex.getMessage())`.
Source:
public SegmentMetadataQuery withFinalizedAnalysisTypes(SegmentMetadataQueryConfig config) { return Druids.SegmentMetadataQueryBuilder .copy(this) .analysisTypes(com.google.common.base.Objects.firstNonNull(analysisTypes, config.getDefaultAnalysisTypes())) .build(); }

Target:
Don't create a new query object if analysisTypes are already non-null, just return `this`
Source:
public IndexIngestionSpec( @JsonProperty("dataSchema") DataSchema dataSchema, @JsonProperty("ioConfig") IndexIOConfig ioConfig, @JsonProperty("tuningConfig") IndexTuningConfig tuningConfig ) { super(dataSchema, ioConfig, tuningConfig); this.dataSchema = dataSchema; this.ioConfig = ioConfig; this.tuningConfig = tuningConfig == null ? new IndexTuningConfig() : tuningConfig; if (this.ioConfig.isAppendToExisting() && this.tuningConfig.isForceGuaranteedRollup()) { log.warn("Perfect rollup is not guaranteed when appending to existing data sources." + " forceGuaranteedRollup flag is ignored."); } }

Target:
Avoid warnings on construction, since constructors get called on deserialization in many contexts. Also, is this useful, given there is another check is the task itself?
Source:
public void reset() { hasParsedHeader = !firstRowIsHeader; }

Target:
This could just be `hasParsedHeader = false` right? Seems simpler
Source:
public TaskAnnouncementTest() { TestUtils testUtils = new TestUtils(); jsonMapper = testUtils.getTestObjectMapper(); final File baseDir = new File("lol"); baseDir.mkdir(); baseDir.deleteOnExit(); }

Target:
What's this for? Could it be a `@Rule TemporaryFolder` instead? That's preferred for any temporary directories or files needed by tests.
Source:
public LocalFirehoseFactory( @JsonProperty("baseDir") File baseDir, @JsonProperty("filter") String filter, // Backwards compatible @JsonProperty("parser") StringInputRowParser parser ) { super( FileUtils.listFiles( Preconditions.checkNotNull(baseDir).getAbsoluteFile(), new WildcardFileFilter(filter), TrueFileFilter.INSTANCE ) ); this.baseDir = baseDir; this.filter = filter; this.parser = parser; log.info("Initialized with " + getObjects() + " files"); }

Target:
This shouldn't happen in construction. Firehose factories get constructed semi-frequently (anytime Task objects are deserialized) and that doesn't necessarily mean they will really be used. Anything that hits the disk or network should be deferred until the firehose is connected.
Source:
public TaskSerdeTest() { TestUtils testUtils = new TestUtils(); jsonMapper = testUtils.getTestObjectMapper(); for (final Module jacksonModule : new FirehoseModule().getJacksonModules()) { jsonMapper.registerModule(jacksonModule); } final File baseDir = new File("lol"); baseDir.mkdir(); baseDir.deleteOnExit(); }

Target:
Could this be a `@Rule TemporaryFolder` instead?
Source:
public Function<Result<TimeseriesResultValue>, Result<TimeseriesResultValue>> makePostComputeManipulatorFn( TimeseriesQuery query, MetricManipulationFn fn ) { boolean shouldFinalize = MetricManipulatorFns.finalizing().equals(fn); return makeComputeManipulatorFn(query, fn, shouldFinalize); }

Target:
The equals check is brittle, I think it'd be better to do a check against a query context key. Or alternatively, do something like groupBy does, and use `mergeResults` to modify the query to remove the post-aggregators. Then, everything downstream of mergeResults on the outermost node (typically the broker) will ignore the post-aggregators. That could be done by overriding `doRun` in the ResultMergeQueryRunner.
Source:
public List<String> getNames() { final Iterable<DataSourceWithSegmentSpec> sourceSpecs = query.getDataSources(); return StreamSupport.stream(sourceSpecs.spliterator(), false) .flatMap(spec -> spec.getDataSource().getNames().stream()) .collect(Collectors.toList()); }

Target:
What if there are duplicates in this stream?
Source:
public ValueMatcher makeValueMatcher(final LongColumnSelector selector, final String value) { Long matchVal = DimensionHandlerUtils.convertObjectToLong(value); if (matchVal == null) { matchVal = DimensionHandlerUtils.getIntegralFromFloatString(value); } if (matchVal == null) { return BooleanValueMatcher.of(false); } final long matchValLong = matchVal; return new ValueMatcher() { @Override public boolean matches() { return selector.get() == matchValLong; } }; }

Target:
Could this method call and check be done inside `DimensionHandlerUtils.convertObjectToLong()`?
Source:
private BigDecimal getBigDecimalLowerBoundFromFloatString(String floatStr) { BigDecimal convertedBD; try { convertedBD = new BigDecimal(floatStr); } catch (NumberFormatException nfe) { return null; } if (lowerStrict) { convertedBD = convertedBD.setScale(0, RoundingMode.FLOOR); } else { convertedBD = convertedBD.setScale(0, RoundingMode.CEILING); } return convertedBD; }

Target:
Need not complicate control flow, suggested `return convertedBD.setScale(0, RoundingMode.FLOOR);` (and below)
Source:
public ValueMatcher makeValueMatcher(final LongColumnSelector selector, final String value) { Long matchVal = DimensionHandlerUtils.convertObjectToLong(value); if (matchVal == null) { return BooleanValueMatcher.of(false); } final long matchValLong = matchVal; return new ValueMatcher() { @Override public boolean matches() { return selector.get() == matchValLong; } }; }

Target:
Could have stayed final.
Source:
public Object convertUnsortedEncodedKeyComponentToActualArrayOrList(Long key, boolean asList) { return Lists.newArrayList(key); }

Target:
ImmutableList.of may be cheaper. It just stores the `key` as a field.
Source:
protected int tryAcquireShared(int ignore) { return getState() >= STARTED ? 1 : -1; }

Target:
Should this be `getState() > STARTED`? It looks like with `>=`, awaitStarted would be able to proceed to the state check when the state is not yet some kind of `START_EXITED_*`, and so it might fail when it should have succeeded.
Source:
public String toString() { return "TimeseriesQuery{" + "dataSource='" + getDataSource() + '\'' + ", querySegmentSpec=" + getQuerySegmentSpec() + ", virtualColumns=" + virtualColumns + ", descending=" + isDescending() + ", dimFilter=" + dimFilter + ", granularity='" + granularity + '\'' + ", aggregatorSpecs=" + aggregatorSpecs + ", postAggregatorSpecs=" + postAggregatorSpecs + ", context=" + getContext() + '}'; }

Target:
virtualColumns and descending go in different order elsewhere in this class
Source:
public ImmutableBitmap union(Iterable<ImmutableBitmap> b) throws ClassCastException { if (b instanceof List) { final List<ImmutableBitmap> bitmapList = (List<ImmutableBitmap>) b; if (bitmapList.isEmpty()) { return makeEmptyImmutableBitmap(); } else if (bitmapList.size() == 1) { return Iterables.getOnlyElement(b); } } return new WrappedImmutableConciseBitmap(ImmutableConciseSet.union(unwrap(b))); }

Target:
Can check for `Collection` here
Source:
byte[] getPrototypeClassBytecode() throws IOException { if (prototypeClassBytecode == null) { ClassLoader cl = prototypeClass.getClassLoader(); InputStream prototypeClassBytecodeStream = cl.getResourceAsStream(prototypeClassBytecodeName + ".class"); prototypeClassBytecode = ByteStreams.toByteArray(prototypeClassBytecodeStream); } return prototypeClassBytecode; }

Target:
Need to close the stream.
Source:
} catch (Exception e) { throw new RuntimeException("Cannot access Unsafe methods", e); }

Target:
The catch block needs to be moved to one line below.
Source:
public boolean determineApplyLimitPushDown() { final boolean forceLimitPushDown = validateAndGetForceLimitPushDown(); if (limitSpec instanceof DefaultLimitSpec) { DefaultLimitSpec defaultLimitSpec = (DefaultLimitSpec) limitSpec; // If only applying an orderby without a limit, don't try to push down if (defaultLimitSpec.getLimit() == Integer.MAX_VALUE) { return false; } if (forceLimitPushDown) { return true; } // If the sorting order only uses columns in the grouping key, we can always push the limit down // to the buffer grouper without affecting result accuracy boolean sortHasNonGroupingFields = DefaultLimitSpec.sortingOrderHasNonGroupingFields( limitSpec, getDimensions() ); return !sortHasNonGroupingFields; } return false; }

Target:
i think this can be implemented in one line `return validateAndGetForceLimitPushDown() || DefaultLimitSpec.sortingOrderHasNonGroupingFields(..);` you might have to add the `defaultLimitSpec.getLimit() == Integer.MAX_VALUE` check inside `DefaultLimitSpec.sortingOrderHasNonGroupingFields(..)`
Source:
public void reset() { size = 0; growthCount = 0; // clear the used bits of both buffers for (int i = 0; i < maxBuckets; i++) { subHashTableBuffers[0].put(i * bucketSizeWithHash, (byte) 0); subHashTableBuffers[1].put(i * bucketSizeWithHash, (byte) 0); } tableBuffer = subHashTableBuffers[0]; }

Target:
this one is not needed and will automatically be zeroed out in adjustTableWhenFull(..) when needed.
Source:
private void sendToKafka(final String topic, MemoryBoundLinkedBlockingQueue<String> recordQueue) { ObjectContainer<String> objectToSend; try { while((objectToSend = recordQueue.take()) != null) { producer.send(new ProducerRecord<String, String>(topic, objectToSend.getData()), producerCallback); } } catch (InterruptedException e) { log.warn(e, "Failed to take record from queue!"); } }

Target:
`take` would never be null, right? Did you mean poll?
Source:
public int hashCode() { return 1; }

Target:
Suggested `getClass().hashCode()` as something "more random"
Source:
public static ValueType fromString(String name) { if (name == null) { return ValueType.STRING; } return valueOf(name.toUpperCase()); }

Target:
Better to return `null` here and let callers deal with `null` however they want. This will default to STRING in _all_ cases of deserializing ValueType, even unforeseen ones where that might not be ideal behavior.
Source:
Outer(int fileNum, File outFile, int maxLength) throws FileNotFoundException { this.fileNum = fileNum; this.outFile = outFile; this.maxLength = maxLength; FileOutputStream outStream = new FileOutputStream(outFile); this.channel = outStream.getChannel(); closer.register(outStream); closer.register(channel); }

Target:
can you move the closer to the same place the file output stream is created? ```java final FileOutputStream outStream = closer.register(new FileOutputStream(outFile)); ``` should work
Source:
public int get(int index) { return 0; }

Target:
Have you intentionally omitted index check?
Source:
public static int findOpenPort(int basePort) { final int startPort = basePort == -1 ? -1 : rnd.nextInt(0x7fff) + basePort; return findOpenPortFrom(startPort); }

Target:
let's make this check for all negatives instead of just -1
Source:
public void close() throws IOException { flush(); started.set(false); exec.shutdown(); }

Target:
need to make sure that only one call to flush is in the queue maybe synchronize around start ?
Source:
public Response getVersion() { CacheScheduler.CacheState cacheState = factory.entry.getCacheState(); if (cacheState instanceof CacheScheduler.NoCache) { return Response.status(Response.Status.NOT_FOUND).build(); } else { String version = ((CacheScheduler.VersionedCache) cacheState).getVersion(); return Response.ok(ImmutableMap.of("version", version)).build(); } }

Target:
suggest `final`
Source:
private void createDanglingObjectHandler() { poolOfString.take(); }

Target:
can we remove this method and simply have the poolsOfString.take() where this method is called?
Source:
private static String makeId(String id, IndexIngestionSpec ingestionSchema) { return id != null ? id : String.format("index_%s_%s", makeDataSource(ingestionSchema), new DateTime().toString()); }

Target:
Don't need `toString()`
Source:
public void close() throws IOException { CloseQuietly.close(jsonParser); it = Iterators.emptyIterator(); }

Target:
If `close()` throws IOException, why consume possible IOException in `jsonParser`?
Source:
public Callable<String> getCachePopulator( final String id, final StaticMapExtractionNamespace extractionNamespace, final String lastVersion, final Map<String, String> swap ) { return new Callable<String>() { @Override public String call() throws Exception { if (lastVersion != null) { // Throwing AssertionError, because NamespaceExtractionCacheManager doesn't suppress Errors and will stop // trying to update the cache periodically. throw new AssertionError( "StaticMapExtractionNamespaceCacheFactory could only be configured for a namespace which is scheduled " + "to be updated once, not periodically. Last version: `" + lastVersion + "`"); } else { swap.putAll(extractionNamespace.getMap()); return version; } } }; }

Target:
Can this throw in `getCachePopulator` instead of in the callable? that way there's a failure immediately.
Source:
public Aggregator factorize(ColumnSelectorFactory metricFactory) { return new DoubleFirstAggregator( name, metricFactory.makeFloatColumnSelector(fieldName), metricFactory.makeLongColumnSelector(Column.TIME_COLUMN_NAME) ); }

Target:
minor nit: can we pass time column selector before field selector to be consistent with buffered aggregator ?
Source:
public PrefetchedLoadingLookupFactory( @JsonProperty("dataFetcher") PrefetchableFetcher prefetchableFetcher, @JsonProperty("loadingCacheSpec") LoadingCache<String, String> loadingCache, @JsonProperty("reverseLoadingCacheSpec") LoadingCache<String, List<String>> reverseLoadingCache ) { super(prefetchableFetcher, loadingCache, reverseLoadingCache, new PrefetchedLoadingLookup(prefetchableFetcher,loadingCache,reverseLoadingCache)); }

Target:
this instead of super?
Source:
public Map<String, String> parse(String input) { final Map<String, Object> inner = delegate.parse(input); final String k = Preconditions.checkNotNull( inner.get(key), "Key column [%s] missing data in line [%s]", key, input ).toString(); // Just in case is long final Object val = inner.get(value); if (val == null) { // Skip nulls return null; } return ImmutableMap.of(k, val.toString()); }

Target:
Since `Parser.parse()` is not marked as Nullable, maybe saner to return `emptyMap()` here and remove the check in `MapPopulator`?
Source:
public void testThrowParseExceptions(){ InputRow in = new MapBasedInputRow( timestamp, dims, event ); AggregatorFactory[] aggregatorFactories = new AggregatorFactory[] { new DoubleSumAggregatorFactory("agg_non_existing", "agg_non_existing_in"), new DoubleSumAggregatorFactory("m1out", "m1"), new LongSumAggregatorFactory("m2out", "m2"), new HyperUniquesAggregatorFactory("m3out", "m3"), new LongSumAggregatorFactory("unparseable", "m3") // Unparseable from String to Long }; try { InputRowSerde.toBytes(in, aggregatorFactories, true); Assert.fail("Should have thrown parseException"); } catch (ParseException e){ // expected } }

Target:
Better to use the `org.junit.rules.ExpectedException` `@Rule` or use `@Test(expected = ParseException.class)`
Source:
public void read1() { for (int i = 0; i < 500000; i++) { sum += d1.get(i); } }

Target:
should this count be parameterized?
Source:
public static Expr binaryOp(BinaryOpExprBase binary, Expr left, Expr right) { try { return binary.getClass() .getDeclaredConstructor(String.class, Expr.class, Expr.class) .newInstance(binary.op, left, right); } catch (Exception e) { log.info(e, "failed to rewrite expression " + binary); return binary; // best effort.. keep it working } }

Target:
Do we expect this to happen "normally" or is this really a problem indicating a bug? If it's going to happen "normally" it shouldn't be `log.info` as that will be too noisy in the logs. If it indicates a bug, please bump it up to `log.warn`.
Source:
private boolean possiblyStopAndRemoveSupervisorInternal(String id, boolean writeTombstone) { Pair<Supervisor, SupervisorSpec> pair = supervisors.get(id); if (pair != null) { if (writeTombstone) { metadataSupervisorManager.insert(id, new NoopSupervisorSpec()); // where NoopSupervisorSpec is a tombstone } pair.lhs.stop(true); supervisors.remove(id); return true; } return false; }

Target:
I think you can move this to the top instead of the get.
Source:
public boolean createOrUpdateAndStartSupervisor(SupervisorSpec spec) { Preconditions.checkState(started, "SupervisorManager not started"); Preconditions.checkNotNull(spec, "spec"); Preconditions.checkNotNull(spec.getId(), "spec.getId()"); synchronized (lock) { possiblyStopAndRemoveSupervisorInternal(spec.getId(), false); return createAndStartSupervisorInternal(spec, true); } }

Target:
You'll want this inside the lock
Source:
public boolean stopAndRemoveSupervisor(String id) { Preconditions.checkState(started, "SupervisorManager not started"); Preconditions.checkNotNull(id, "id"); synchronized (lock) { return possiblyStopAndRemoveSupervisorInternal(id, true); } }

Target:
Same here, should be inside the lock
Source:
public CaffeineCache(final Cache<NamedKey, byte[]> cache, CaffeineCacheConfig config) { this.cache = cache; this.config = config; }

Target:
Looks like this could be private
Source:
public OffheapIncrementalIndex( long minTimestamp, QueryGranularity gran, final AggregatorFactory[] metrics, boolean deserializeComplexMetrics, boolean reportParseExceptions, boolean sortFacts, int maxRowCount, StupidPool<ByteBuffer> bufferPool ) { this( new IncrementalIndexSchema.Builder().withMinTimestamp(minTimestamp) .withQueryGranularity(gran) .withMetrics(metrics) .withRollup(DEFAULT_ROLLUP) .build(), deserializeComplexMetrics, reportParseExceptions, sortFacts, maxRowCount, bufferPool ); }

Target:
can this use incrementalIndexSchema.isRollup?
Source:
public boolean supportsBitmapIndex(BitmapIndexSelector selector) { if(selector.getBitmapIndex(boundDimFilter.getDimension()) == null) { return false; } else { return true; } }

Target:
formatting
Source:
public boolean supportsBitmapIndex(BitmapIndexSelector selector) { if(selector.getBitmapIndex(dimension) == null) { return false; } else { return true; } }

Target:
formatting
Source:
RetryPolicyFactory createRetryPolicyFactory() { return new RetryPolicyFactory(new RetryPolicyConfig() .setMinWait(Period.seconds(2)) .setMaxWait(Period.seconds(8)) .setMaxRetryCount(5) ); }

Target:
Since @dclim is also the original author if these numbers, that would be nice.
Source:
public boolean aggregate(KeyType key) { final int keyHash = Groupers.hash(key); final int grouperNumber = grouperNumberForKeyHash(keyHash); final Grouper<KeyType> grouper = groupers.get(grouperNumber); synchronized (grouper) { return grouper.aggregate(key, keyHash); } }

Target:
can do return aggregate(key, keyHash)
Source:
private LookupExtractionFn ensureDelegate() { LookupExtractionFn delegate = this.delegate; if (null == delegate) { // http://www.javamex.com/tutorials/double_checked_locking.shtml synchronized (delegateLock) { delegate = this.delegate; if (null == delegate) { delegate = new LookupExtractionFn( Preconditions.checkNotNull(manager.get(getLookup()), "Lookup [%s] not found", getLookup()).get(), isRetainMissingValue(), getReplaceMissingValueWith(), isInjective(), isOptimize() ); this.delegate = delegate; } } } return delegate; }

Target:
why not always use this.delegate directly and avoid the extra local variable? it would make things shorter and less confusing.
Source:
public int hashCode() { int result = container != null ? container.hashCode() : 0; result = 31 * result + (aggregators != null ? Arrays.hashCode(aggregators) : 0); result = 31 * result + (queryGranularity != null ? queryGranularity.hashCode() : 0); return result; }

Target:
`container` is non-null
Source:
public ByteBuffer makeNewBuffer(long offset) { if (sizeAvailable.addAndGet(-1 * chunkSize) > 0) { return delegate.makeNewBuffer(offset); } else { throw new RuntimeException("No more space left for allocating buffers."); } }

Target:
this will decrease the availableSize and throw exception. this can possibly cause availableSize to get out of sync from the actual allocated buffer.
Source:
public V getIfPresent(K key) { V value = cache.get(key); if (value == null) { missCount.incrementAndGet(); } else { hitCount.getAndIncrement(); } return value; }

Target:
For readability sake, can these two increments use the same atomic method call?
Source:
public V get(K key, final Callable<? extends V> valueLoader) throws ExecutionException { V value = cache.get(key); if (value != null) return value; try { value = valueLoader.call(); cache.put(key, value); return value; } catch (Exception e) { throw new ISE(e, "got an exception while loading key [%s]", key); } }

Target:
not thread safe
Source:
public List<? extends Module> getJacksonModules() { return ImmutableList.<Module>of( new SimpleModule("DruidNamespacedExtractionModule") .registerSubtypes(NamespacedExtractor.class) .registerSubtypes(ExtractionNamespace.class) .registerSubtypes(NamespaceLookupExtractorFactory.class) ); }

Target:
these can all be on the same method call
Source:
public static List<Filter> toFilters(List<DimFilter> dimFilters) { return ImmutableList.copyOf( FunctionalIterable .create(dimFilters) .filter( new Predicate<DimFilter>() { @Override public boolean apply(DimFilter input) { return input != null; } } ).transform( new Function<DimFilter, Filter>() { @Override public Filter apply(DimFilter input) { return input.toFilter(); } } ) ); }

Target:
Should we throw an exception instead if a DimFilter was null?
Source:
public SelectorFilter( String dimension, String value, ExtractionFn extractionFn ) { this.dimension = dimension; this.value = Strings.nullToEmpty(value);; this.extractionFn = extractionFn; }

Target:
double `;;`
Source:
public ValueMatcher makeMatcher(ValueMatcherFactory factory) { if (extractionFn == null) { return factory.makeValueMatcher(dimension, value); } else { return factory.makeValueMatcher( dimension, new Predicate<String>() { @Override public boolean apply(String input) { // Assuming that a null/absent/empty dimension are equivalent from the druid perspective return value.equals(Strings.nullToEmpty(extractionFn.apply(Strings.emptyToNull(input)))); } } ); } }

Target:
I don't think the `Strings.emptyToNull` is needed on the input to the extractionFn; `input` shouldn't be empty anyway (ValueMatcherFactories aren't passing in empty strings to the predicate) and also most other callers of extractionFns don't do this.
Source:
public AsyncQueryForwardingServlet( @Json ObjectMapper jsonMapper, @Smile ObjectMapper smileMapper, QueryHostFinder hostFinder, @Router Provider<HttpClient> httpClientProvider, DruidHttpClientConfig httpClientConfig, ServiceEmitter emitter, RequestLogger requestLogger ) { this.jsonMapper = jsonMapper; this.smileMapper = smileMapper; this.hostFinder = hostFinder; this.httpClientProvider = httpClientProvider; this.httpClientConfig = httpClientConfig; this.emitter = emitter; this.requestLogger = requestLogger; // separate client with more aggressive connection timeouts // to prevent cancellations requests from blocking queries this.broadcastClient = httpClientProvider.get(); broadcastClient.setConnectTimeout(CANCELLATION_TIMEOUT); broadcastClient.setMaxRequestsQueuedPerDestination(MAX_QUEUED_CANCELLATIONS); }

Target:
should we also lower down maxConnectionsPerDestination ?
Source:
private DatasourceInputSplit toDataSourceSplit( List<WindowedDataSegment> segments, org.apache.hadoop.mapred.InputFormat fio, JobConf conf ) { String[] locations = null; try { locations = getFrequentLocations(segments, fio, conf); } catch (Exception e) { logger.error(e, "Exception thrown finding location of splits"); throw Throwables.propagate(e); } return new DatasourceInputSplit(segments, locations); }

Target:
i don't this needs to be propagated. finding locations should only be best effort.
Source:
public Set<String> getRunningTaskIds() { return getRunningTasks().keySet(); }

Target:
Does this need to be Set?
Source:
public List<String> getPartitionDimensions() { if (partitionDimension != null) { return ImmutableList.of(partitionDimension); } else { return null; } }

Target:
can you add `@Nullable` as method signature
Source:
public void setUp() throws Exception { amazonEC2Client = EasyMock.createMock(AmazonEC2Client.class); mockAmazonEC2Client = new EC2MockClient(); describeInstancesResult = EasyMock.createMock(DescribeInstancesResult.class); reservation = EasyMock.createMock(Reservation.class); instance = new Instance() .withInstanceId(INSTANCE_ID) .withLaunchTime(new Date()) .withImageId(AMI_ID) .withPrivateIpAddress(IP); managementConfig = new SimpleResourceManagementConfig().setWorkerPort(8080).setWorkerVersion(""); }

Target:
can you use EasyMock instead of creating a new class ? It doesn't seem to do anything custom else except get/set region.
Source:
public Iterator<String> iterator() { LOG.debug("Not json, hoping it is a good string : %s", string); return Splitter.on( new CharMatcher() { private boolean inQuotes = false; @Override public boolean matches(char c) { if ('"' == c) { inQuotes = !inQuotes; } if (inQuotes) { return false; } return CharMatcher.BREAKING_WHITESPACE.matches(c); } } ).omitEmptyStrings().split(string).iterator(); }

Target:
this debug msg is not valid anymore.
Source:
public void testBootstrapFromFile() throws IOException { LookupExtractorFactory lookupExtractorFactory = new LookupExtractorFactoryMock("data"); mapper.registerSubtypes(LookupExtractorFactoryMock.class); lookupReferencesManager.put("testMockForBootstrap",lookupExtractorFactory); lookupReferencesManager.stop(); lookupReferencesManager.start(); Assert.assertEquals(lookupExtractorFactory, lookupReferencesManager.get("testMockForBootstrap")); }

Target:
Can this simply be an EasyMock?
Source:
public FireHydrant getCurrHydrant() { synchronized (hydrantLock) { return currHydrant; } }

Target:
`@VisibleForTesting`, could also be package-private.
Source:
public void testSerde() throws JsonParseException, JsonMappingException, JsonProcessingException, IOException { ObjectMapper mapper = new DefaultObjectMapper(); SketchEstimateWithErrorBounds est = new SketchEstimateWithErrorBounds(100.0,101.5,98.5,2); Assert.assertEquals(est, mapper.readValue( mapper.writeValueAsString(est), SketchEstimateWithErrorBounds.class)); }

Target:
need only `IOException`
Source:
private ValueType getTypeFromDimVal(Object dimVal) { Object singleVal; if (dimVal instanceof List) { List dimValList = (List) dimVal; singleVal = dimValList.size() == 0 ? null : dimValList.get(0); } else { singleVal = dimVal; } if (singleVal == null) { return null; } ValueType valType = TYPE_MAP.get(singleVal.getClass().getSimpleName()); return valType; }

Target:
can directly return without storing in `valType` local variable
Source:
protected Collection<Worker> getWorkerFromZK(Collection<ZkWorker> workers) { return Collections2.transform( workers, new Function<ZkWorker, Worker>() { @Override public Worker apply(ZkWorker input) { return input.getWorker(); } } ); }

Target:
could this be `private`? RTR doesn't have subclasses as far as I know
Source:
public ExtractionType getExtractionType(){ return (child != null) ? ((child.getExtractionType() == ExtractionType.MANY_TO_ONE) ? ExtractionType.MANY_TO_ONE : fn.getExtractionType() ) : fn.getExtractionType(); }

Target:
I think this set of conditions is a bit hard to read, can you change it to something like: if (child != null && child.getExtractionType() == ExtractionType.MANY_TO_ONE) { return ExtractionType.MANY_TO_ONE; } else { return fn.getExtractionType(); }
Source:
public String toString() { StringBuilder sb = new StringBuilder(); sb.append("DimensionSelectorHavingSpec"); sb.append("{dimension='").append(dimension).append('\''); sb.append(", value='").append(value); if (getExtractionFn() != null) { sb.append("', extractionFunction='").append(getExtractionFn()); } sb.append("'}"); return sb.toString(); }

Target:
extractionfn is never null
Source:
private static Set<String> getWorkerNodeIDs(Collection<Worker> workers, WorkerBehaviorConfig workerConfig) { return Sets.<String>newHashSet(workerConfig.getAutoScaler().ipToIdLookup( Lists.newArrayList( Iterables.transform( workers, new Function<Worker, String>() { @Override public String apply(Worker input) { return input.getIp(); } } ) ) )); }

Target:
creating a set might not always be necessary, a collection should be enough for all the cases where we use getWorkerNodeIDs
Source:
public static List<String> readStringArray(DataInput in) throws IOException { int count = WritableUtils.readVInt(in); if(count == 0) { return null; } List<String> values = Lists.newArrayListWithCapacity(count); for(int i = 0; i < count; i++) { values.add(readString(in)); } return values; }

Target:
all these util methods should be private.
Source:
public void testQuerySerialization() throws IOException { RegexSearchQuerySpec spec = new RegexSearchQuerySpec("(upfront|total_market)"); String json = jsonMapper.writeValueAsString(spec); RegexSearchQuerySpec serdeQuery = jsonMapper.readValue(json, RegexSearchQuerySpec.class); Assert.assertEquals(spec, serdeQuery); }

Target:
can u change to ``` (RegexSearchQuerySpec) jsonMapper.readValue(json, SearchQuerySpec.class); ```
Source:
public byte[] getCacheKey() { if (pattern == null) { return ByteBuffer.allocate(1) .put(CACHE_TYPE_ID).array(); } byte[] patternBytes = StringUtils.toUtf8(pattern); return ByteBuffer.allocate(1 + patternBytes.length) .put(CACHE_TYPE_ID) .put(patternBytes) .array(); }

Target:
can pattern be null ?
Source:
public void testNotSortedSerialization() throws Exception { final String[] strings = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "k", "j", "l"}; GenericIndexed<String> deserialized = serializeAndDeserialize( GenericIndexed.fromArray( strings, GenericIndexed.STRING_STRATEGY ) ); checkBasicAPIs(strings, deserialized, deserialized.isAllowReverseLookup()); }

Target:
this should be checkBasicAPIs(strings, deserialized, false);
Source:
public String apply(String value) { return Strings.emptyToNull(String.format(format, value)); }

Target:
if format string is non-empty then String.format(..) can not be empty, so Strings.emptyToNull(..) is probably not doing anything. did you want to do following instead? ``` String.format(format, Strings.emptyToNull(value)); ```
Source:
public String apply(String dimValue) { if (Strings.isNullOrEmpty(dimValue)) { return null; } int len = dimValue.length(); if (index < len) { if (length > 0) { return dimValue.substring(index, Math.min(end, len)); } else { return dimValue.substring(index); } } else { return null; } }

Target:
Is this check required ? At this point `length` will be greater than 0 as we will return `null` if the length is 0 at line 62 `Strings.isNullOrEmpty(dimValue)`
Source:
public boolean preservesOrdering() { return false; }

Target:
this should be true when `index == 0` to allow optimizations for this particular case.
Source:
public byte[] getCacheKey() { byte[] valueBytes = StringUtils.toUtf8(value); return ByteBuffer.allocate(2 + valueBytes.length) .put(caseSensitive ? (byte)1 : 0) .put(CACHE_TYPE_ID) .put(valueBytes) .array(); }

Target:
Could you put the CACHE_TYPE_ID first?
Source:
public ContainsSearchQuerySpec( @JsonProperty("value") String value, @JsonProperty("caseSensitive") boolean caseSensitive ) { this.value = value; this.caseSensitive = caseSensitive; this.target = value == null || caseSensitive ? value : value.toLowerCase(); }

Target:
Is `value == null` a valid case here?
Source:
private static void uploadJar(File jarFile, final Path path,final FileSystem fs) throws IOException { log.info("Uploading jar to path[%s]", path); ByteStreams.copy( Files.newInputStreamSupplier(jarFile), new OutputSupplier<OutputStream>() { @Override public OutputStream getOutput() throws IOException { return fs.create(path); } } ); }

Target:
Will this retry on transient errors? It seems like we would want a copy to be able to retry (and overwrite if prior was a partial success) when feasible.
Source:
public HybridCacheProvider( @JacksonInject @Named("l1") CacheProvider level1, @JacksonInject @Named("l2") CacheProvider level2 ) { Preconditions.checkNotNull(level1, "l1 cache not specified for hybrid cache"); Preconditions.checkNotNull(level2, "l2 cache not specified for hybrid cache"); this.level1 = level1; this.level2 = level2; }

Target:
nit: you could do ``` this.level1 = Preconditions.checkNotNull(level1, "l1 cache not specified for hybrid cache"); this.level2 = Preconditions.checkNotNull(level2, "l2 cache not specified for hybrid cache"); ```
Source:
public URL getRedirectURL(String queryString, String requestURI) { if (!requestURI.startsWith("/")) { throw new IAE("Expected requestURI starting with '/'"); } try { final String leader = taskMaster.getLeader(); if (leader == null || leader.isEmpty()) { return null; } else { return new URI("http", leader, requestURI, queryString, null).toURL(); } } catch (Exception e) { throw Throwables.propagate(e); } }

Target:
is this even necessary given that we use new `URI(...)` ?
Source:
public ResourceHolder<T> get() { CountingHolder holder; size.decrementAndGet(); // items never stay out of the queue for long, so we'll get one eventually try { holder = queue.take(); } catch(InterruptedException e) { Thread.currentThread().interrupt(); throw Throwables.propagate(e); } // synchronize on item to ensure count cannot get changed by // CountingHolder.close right after we put the item back in the queue synchronized (holder) { holder.count.incrementAndGet(); queue.offer(holder); } return holder; }

Target:
size looks like it is no longer needed
Source:
public void remove(final Task task) { giant.lock(); try { log.info("Removing task[%s] from activeTasks", task.getId()); for (final TaskLockPosse taskLockPosse : findLockPossesForTask(task)) { unlock(task, taskLockPosse.getTaskLock().getInterval()); } } finally { activeTasks.remove(task.getId()); giant.unlock(); } }

Target:
unlock should have a finally block of its own, even if this remove is in its own try{}finally{} block.
Source:
private AggregatorFactory[] getCombiningAggregators(AggregatorFactory[] aggregators) { AggregatorFactory[] combiningAggregators = new AggregatorFactory[aggregators.length]; for (int i = 0; i < aggregators.length; i++) { combiningAggregators[i] = aggregators[i].getCombiningFactory(); } return combiningAggregators; }

Target:
`private static`?
Source:
public AggregatorFactory getCombiningFactory() { return new SketchMergeAggregatorFactory(name, name, size, shouldFinalize, true); }

Target:
This looks like it's fixing a bug in datasketches, is that right?
Source:
public SegmentMetadataQueryQueryToolChest( SegmentMetadataQueryConfig config ) { this.config = config; defaultHistory = this.config.getDefaultHistory(); }

Target:
Storing either the config or the defaultHistory period should be enough- doesn't look like there's a need to store both
Source:
public void getSegmentFiles(final DataSegment segment, final File outDir) throws SegmentLoadingException { Map<String, Object> loadSpec = segment.getLoadSpec(); String region = MapUtils.getString(loadSpec, "region"); String container = MapUtils.getString(loadSpec, "container"); String path = MapUtils.getString(loadSpec, "path"); log.info("Pulling index at path[%s] to outDir[%s]", path, outDir); prepareOutDir(outDir); getSegmentFiles(region, container, path, outDir); }

Target:
this is typically done by having a LoadSpec implementation that wires up the json config to the puller (see `io.druid.storage.azure.AzureLoadSpec` for a good example)
Source:
public int getBatchSize() { return getContextValue("batchSize") == null ? DEFAULT_BATCH_SIZE : Integer.parseInt(getContextValue("batchSize").toString()); }

Target:
This should be greater than 0 or else your % operation is going to get interesting.
Source:
public int getLockPriority() { return getContextValue("lockPriority") == null ? DEFAULT_TASK_PRIORITY : Integer.parseInt(getContextValue("lockPriority").toString()); }

Target:
can these key strings be `public static final String` in this class?
Source:
public DerbyConnectorRule() { this(Suppliers.ofInstance(MetadataStorageTablesConfig.fromBase("druidTest" + dbSafeUUID()))); }

Target:
is it necessary to create unique table IDs, if we already create a unique database per test?
Source:
public int add(InputRow row, Supplier<Committer> committerSupplier) throws IndexSizeExceededException { if (row == null) { return -1; } Sink sink = getSink(row.getTimestampFromEpoch()); if (sink == null) { return -1; } final int numRows = sink.add(row); if (!sink.canAppendRow()) { persist(committerSupplier.get()); } return numRows; }

Target:
if it is not doing anything, then we can remove it and not complicate life for no reason :)
Source:
public IndexTask( @JsonProperty("id") String id, @JsonProperty("resource") TaskResource taskResource, @JsonProperty("spec") IndexIngestionSpec ingestionSchema, @JacksonInject ObjectMapper jsonMapper ) { super( // _not_ the version, just something uniqueish makeId(id, ingestionSchema), String.format("index_%s", makeDataSource(ingestionSchema)), taskResource == null ? new TaskResource(makeId(id, ingestionSchema), 1) : taskResource, makeDataSource(ingestionSchema), makeInterval(ingestionSchema) ); this.ingestionSchema = ingestionSchema; this.jsonMapper = jsonMapper; }

Target:
I think having IndexTask use the existing constructor with `makeId(id, ingestionSchema)` for both the id and groupId, or adding a new (id, taskResource, dataSource, interval) constructor, would both be fine.
Source:
protected void finalize() throws Throwable { if (!closed.get()) { log.warn("Not closed! Object was[%s]. Allowing gc to prevent leak.", object); } super.finalize(); }

Target:
I wonder why this wasn't there before?
Source:
public static @NotNull Map<String, String> sanitizeException(@Nullable Throwable t) { return ImmutableMap.of( "error", t == null ? "null" : (t.getMessage() == null ? t.toString() : t.getMessage()) ); }

Target:
what does `@NotNull` means ? thought it is by default.