auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
public byte[] getCacheKey() { START if (pattern == null) { END 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(); } can pattern be null ?

Target:
public byte[] getCacheKey() { byte[] patternBytes = StringUtils.toUtf8(pattern); return ByteBuffer.allocate(1 + patternBytes.length) .put(CACHE_TYPE_ID) .put(patternBytes) .array(); }
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 ) ); START checkBasicAPIs(strings, deserialized, deserialized.isAllowReverseLookup()); END } this should be checkBasicAPIs(strings, deserialized, false);

Target:
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, false); }
Source:
public String apply(String value) { START return Strings.emptyToNull(String.format(format, value)); END } 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)); ```

Target:
public String apply(String value) { return String.format(format, value); }
Source:
public String apply(String dimValue) { if (Strings.isNullOrEmpty(dimValue)) { return null; } int len = dimValue.length(); if (index < len) { START if (length > 0) { END return dimValue.substring(index, Math.min(end, len)); } else { return dimValue.substring(index); } } else { return null; } } 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)`

Target:
public String apply(String dimValue) { if (Strings.isNullOrEmpty(dimValue)) { return null; } int len = dimValue.length(); if (index < len) { if (end > 0) { return dimValue.substring(index, Math.min(end, len)); } else { return dimValue.substring(index); } } else { return null; } }
Source:
public boolean preservesOrdering() { START return false; END } this should be true when `index == 0` to allow optimizations for this particular case.

Target:
public boolean preservesOrdering() { return index == 0 ? true : false; }
Source:
public byte[] getCacheKey() { byte[] valueBytes = StringUtils.toUtf8(value); START return ByteBuffer.allocate(2 + valueBytes.length) END .put(caseSensitive ? (byte)1 : 0) .put(CACHE_TYPE_ID) .put(valueBytes) .array(); } Could you put the CACHE_TYPE_ID first?

Target:
public byte[] getCacheKey() { if (value == null) { return ByteBuffer.allocate(2) .put(CACHE_TYPE_ID) .put(caseSensitive ? (byte) 1 : 0).array(); } byte[] valueBytes = StringUtils.toUtf8(value); return ByteBuffer.allocate(2 + valueBytes.length) .put(CACHE_TYPE_ID) .put(caseSensitive ? (byte) 1 : 0) .put(valueBytes) .array(); }
Source:
public ContainsSearchQuerySpec( START @JsonProperty("value") String value, END @JsonProperty("caseSensitive") boolean caseSensitive ) { this.value = value; this.caseSensitive = caseSensitive; this.target = value == null || caseSensitive ? value : value.toLowerCase(); } Is `value == null` a valid case here?

Target:
public ContainsSearchQuerySpec( @JsonProperty("value") String value, @JsonProperty("caseSensitive") boolean caseSensitive ) { this.value = value; this.caseSensitive = caseSensitive; }
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 { START return fs.create(path); END } } ); } 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.

Target:
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); } } ); }
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; START this.level2 = level2; END } 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"); ```

Target:
public HybridCacheProvider( @JacksonInject @Named("l1") CacheProvider level1, @JacksonInject @Named("l2") CacheProvider level2 ) { 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) { START if (!requestURI.startsWith("/")) { END 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); } } is this even necessary given that we use new `URI(...)` ?

Target:
public URL getRedirectURL(String queryString, String requestURI) { 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); } }
Source:
public ResourceHolder<T> get() { CountingHolder holder; START size.decrementAndGet(); END // 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; } size looks like it is no longer needed

Target:
public ResourceHolder<T> get() { final CountingHolder holder; // 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; }
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 { START activeTasks.remove(task.getId()); END giant.unlock(); } } unlock should have a finally block of its own, even if this remove is in its own try{}finally{} block.

Target:
public void remove(final Task task) { giant.lock(); try { 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()); } } finally { giant.unlock(); } }
Source:
START private AggregatorFactory[] getCombiningAggregators(AggregatorFactory[] aggregators) END { AggregatorFactory[] combiningAggregators = new AggregatorFactory[aggregators.length]; for (int i = 0; i < aggregators.length; i++) { combiningAggregators[i] = aggregators[i].getCombiningFactory(); } return combiningAggregators; } `private static`?

Target:
private static 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; }
Source:
public AggregatorFactory getCombiningFactory() START { END return new SketchMergeAggregatorFactory(name, name, size, shouldFinalize, true); } This looks like it's fixing a bug in datasketches, is that right?

Target:
public AggregatorFactory getCombiningFactory() { return new SketchMergeAggregatorFactory(name, name, size, shouldFinalize, false); }
Source:
public SegmentMetadataQueryQueryToolChest( SegmentMetadataQueryConfig config ) { START this.config = config; END defaultHistory = this.config.getDefaultHistory(); } Storing either the config or the defaultHistory period should be enough- doesn't look like there's a need to store both

Target:
public SegmentMetadataQueryQueryToolChest( SegmentMetadataQueryConfig config ) { this.config = config; }
Source:
public void getSegmentFiles(final DataSegment segment, final File outDir) throws SegmentLoadingException { START Map<String, Object> loadSpec = segment.getLoadSpec(); END 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); } 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)

Target:
public void getSegmentFiles(final DataSegment segment, final File outDir) throws SegmentLoadingException { final Map<String, Object> loadSpec = segment.getLoadSpec(); final String region = MapUtils.getString(loadSpec, "region"); final String container = MapUtils.getString(loadSpec, "container"); final 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); }
Source:
public int getBatchSize() { START return getContextValue("batchSize") == null ? DEFAULT_BATCH_SIZE : Integer.parseInt(getContextValue("batchSize").toString()); END } This should be greater than 0 or else your % operation is going to get interesting.

Target:
public int getBatchSize() { return getContextValue("batchSize") == null ? DEFAULT_BATCH_SIZE : (Integer.parseInt(getContextValue("batchSize").toString()) > 0 ? Integer.parseInt(getContextValue("batchSize").toString()) : DEFAULT_BATCH_SIZE); }
Source:
public int getLockPriority() { START return getContextValue("lockPriority") == null ? DEFAULT_TASK_PRIORITY : Integer.parseInt(getContextValue("lockPriority").toString()); END } can these key strings be `public static final String` in this class?

Target:
public int getLockPriority() { return getLockPriority(DEFAULT_TASK_PRIORITY); }
Source:
public DerbyConnectorRule() { START this(Suppliers.ofInstance(MetadataStorageTablesConfig.fromBase("druidTest" + dbSafeUUID()))); END } is it necessary to create unique table IDs, if we already create a unique database per test?

Target:
public DerbyConnectorRule() { this(Suppliers.ofInstance(MetadataStorageTablesConfig.fromBase("druidTest"))); }
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); START if (!sink.canAppendRow()) { END persist(committerSupplier.get()); } return numRows; } if it is not doing anything, then we can remove it and not complicate life for no reason :)

Target:
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; } return sink.add(row); }
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), START String.format("index_%s", makeDataSource(ingestionSchema)), END taskResource == null ? new TaskResource(makeId(id, ingestionSchema), 1) : taskResource, makeDataSource(ingestionSchema), makeInterval(ingestionSchema) ); this.ingestionSchema = ingestionSchema; this.jsonMapper = jsonMapper; } 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.

Target:
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), taskResource, makeDataSource(ingestionSchema), makeInterval(ingestionSchema) ); this.ingestionSchema = ingestionSchema; this.jsonMapper = jsonMapper; }
Source:
protected void finalize() throws Throwable { if (!closed.get()) { log.warn("Not closed! Object was[%s]. Allowing gc to prevent leak.", object); } START super.finalize(); END } I wonder why this wasn't there before?

Target:
protected void finalize() throws Throwable { try { if (!closed.get()) { log.warn("Not closed! Object was[%s]. Allowing gc to prevent leak.", object); } } finally { super.finalize(); } }
Source:
START public static @NotNull Map<String, String> sanitizeException(@Nullable Throwable t) END { return ImmutableMap.of( "error", t == null ? "null" : (t.getMessage() == null ? t.toString() : t.getMessage()) ); } what does `@NotNull` means ? thought it is by default.

Target:
public static Map<String, String> sanitizeException(@Nullable Throwable t) { return ImmutableMap.of( "error", t == null ? "null" : (t.getMessage() == null ? t.toString() : t.getMessage()) ); }
Source:
public boolean replaces(@Nullable LookupExtractorFactory other) { if (this == other) { return false; } if (other == null) { return true; } if (!(other instanceof MapLookupExtractorFactory)) { return true; } final MapLookupExtractorFactory that = (MapLookupExtractorFactory) other; START return isOneToOne != that.isOneToOne || !map.equals(that.map); END } thought this `return isOneToOne != that.isOneToOne || !map.equals(that.map);` is equivalent to `!this.equals(that)`

Target:
public boolean replaces(@Nullable LookupExtractorFactory other) { return !equals(other); }
Source:
public void configure(Binder binder) { JsonConfigProvider.bind(binder, PROPERTY_BASE, LookupConfig.class); LifecycleModule.register(binder, LookupReferencesManager.class); JsonConfigProvider.bind(binder, PROPERTY_BASE, LookupListeningAnnouncerConfig.class); Jerseys.addResource(binder, LookupListeningResource.class); START LifecycleModule.register(binder, LookupReferencesManager.class); END LifecycleModule.register(binder, LookupResourceListenerAnnouncer.class); } duplicated `LifecycleModule.register(binder, LookupReferencesManager.class)`

Target:
public void configure(Binder binder) { JsonConfigProvider.bind(binder, PROPERTY_BASE, LookupConfig.class); LifecycleModule.register(binder, LookupReferencesManager.class); JsonConfigProvider.bind(binder, PROPERTY_BASE, LookupListeningAnnouncerConfig.class); Jerseys.addResource(binder, LookupListeningResource.class); LifecycleModule.register(binder, LookupResourceListenerAnnouncer.class); }
Source:
public Response shutDown() { log.info("Received shutdown request"); executorService.schedule( new Runnable() { @Override public void run() { START lifecycle.stop(); END System.exit(-1); } }, 1, TimeUnit.SECONDS ); executorService.shutdown(); return Response.status(Response.Status.ACCEPTED).build(); } Are you able to get thread dumps of the not-terminating processes?

Target:
public Response shutDown() { log.info("Received shutdown request"); executorService.schedule( new Runnable() { @Override public void run() { System.exit(0); } }, 1, TimeUnit.SECONDS ); executorService.shutdown(); return Response.status(Response.Status.ACCEPTED).build(); }
Source:
public IntegrationTestingConfig get() { START loadProperties(); END return new IntegrationTestingConfig() { @Override public String getCoordinatorHost() { return coordinatorHost; } @Override public String getIndexerHost() { return indexerHost; } @Override public String getRouterHost() { return routerHost; } @Override public String getBrokerHost() { return brokerHost; } @Override public String getMiddleManagerHost() { return middleManagerHost; } @Override public String getZookeeperHosts() { return zookeeperHosts; } }; } why would this be required again?

Target:
public IntegrationTestingConfig get() { return new IntegrationTestingConfig() { @Override public String getCoordinatorHost() { return coordinatorHost; } @Override public String getIndexerHost() { return indexerHost; } @Override public String getRouterHost() { return routerHost; } @Override public String getBrokerHost() { return brokerHost; } @Override public String getMiddleManagerHost() { return middleManagerHost; } @Override public String getZookeeperHosts() { return zookeeperHosts; } }; }
Source:
public AlphaNumericTopNMetricSpec( @JsonProperty("previousStop") String previousStop ) { // Set empty string as previousStop of super class otherwise we will get NPE super((previousStop == null) ? "" : previousStop); START this.previousStop = (previousStop == null) ? "" : previousStop; END } instead, can you put this.previousStop = (previousStop == null) ? "" : previousStop; in the constructor of LexicographicTopNMetricSpec and here , replace line#191 by this.previousStop = super.getPreviousStop() ?

Target:
public AlphaNumericTopNMetricSpec( @JsonProperty("previousStop") String previousStop ) { super(previousStop); }
Source:
public byte[] getCacheKey() { START byte[] previousStopBytes = StringUtils.toUtf8(getPreviousStop()); END return ByteBuffer.allocate(1 + previousStopBytes.length) .put(CACHE_TYPE_ID) .put(previousStopBytes) .array(); } i believe this will throw an NPE if previousStop is null

Target:
public byte[] getCacheKey() { byte[] previousStopBytes = getPreviousStop() == null ? new byte[]{} : StringUtils.toUtf8(getPreviousStop()); return ByteBuffer.allocate(1 + previousStopBytes.length) .put(CACHE_TYPE_ID) .put(previousStopBytes) .array(); }
Source:
public TopNMapFn( TopNQuery query, TopNAlgorithm topNAlgorithm ) { this.query = query; this.topNAlgorithm = topNAlgorithm; this.dimension = query.getDimensionSpec().getDimension(); START this.extractionFn = TopNQueryEngine.canApplyExtractionInPost(query) ? null : query.getDimensionSpec().getExtractionFn(); END } Given that we are using a rewrite strategy, at first glance, this seems weird to me. Is it a hold over from before and just not required, or is it still necessary for some reason?

Target:
public TopNMapFn( TopNQuery query, TopNAlgorithm topNAlgorithm ) { this.query = query; this.topNAlgorithm = topNAlgorithm; }
Source:
public TopNMapFn( TopNQuery query, TopNAlgorithm topNAlgorithm ) { this.query = query; this.topNAlgorithm = topNAlgorithm; this.dimension = query.getDimensionSpec().getDimension(); START this.extractionFn = query.getDimensionSpec().getExtractionFn(); END } this seems like an unnecessary optimization?

Target:
public TopNMapFn( TopNQuery query, TopNAlgorithm topNAlgorithm ) { this.query = query; this.topNAlgorithm = topNAlgorithm; }
Source:
public static Collection<DimensionSpec> extractionsToRewrite(GroupByQuery query) { return Collections2.filter( query.getDimensions(), new Predicate<DimensionSpec>() { @Override public boolean apply(@Nullable DimensionSpec input) { START return input != null END && input.getExtractionFn() != null && ExtractionFn.ExtractionType.ONE_TO_ONE.equals( input.getExtractionFn().getExtractionType() ); } } ); } I think we removed null dimensions specs, no?

Target:
public static Collection<DimensionSpec> extractionsToRewrite(GroupByQuery query) { return Collections2.filter( query.getDimensions(), new Predicate<DimensionSpec>() { @Override public boolean apply(DimensionSpec input) { return input.getExtractionFn() != null && ExtractionFn.ExtractionType.ONE_TO_ONE.equals( input.getExtractionFn().getExtractionType() ); } } ); }
Source:
public static boolean canApplyExtractionInPost(TopNQuery query) { START return query != null END && query.getDimensionSpec() != null && query.getDimensionSpec().getExtractionFn() != null && ExtractionFn.ExtractionType.ONE_TO_ONE.equals(query.getDimensionSpec().getExtractionFn().getExtractionType()) && query.getTopNMetricSpec().canBeOptimizedUnordered(); } I don't think we need to check for null here. We can just have Precondition.checkNotNull, since none of the instances where this is called would have a null query.

Target:
public static boolean canApplyExtractionInPost(TopNQuery query) { return query.getDimensionSpec() != null && query.getDimensionSpec().getExtractionFn() != null && ExtractionFn.ExtractionType.ONE_TO_ONE.equals(query.getDimensionSpec().getExtractionFn().getExtractionType()) && query.getTopNMetricSpec().canBeOptimizedUnordered(); }
Source:
public long getSerializedSize() { START return baseIntBuffers.getSerializedSize() + 1 + 4 + 4 + 1; END } Can we get some more information on the magic numbers here?

Target:
public long getSerializedSize() { return 1 + // version 4 + // totalSize 4 + // sizePer 1 + // compressionId baseIntBuffers.getSerializedSize(); // data }
Source:
private DataSegment createDescriptorFile(DataSegment segment, Path outDir, final FileSystem fs) throws IOException { final Path descriptorFile = new Path(outDir, "descriptor.json"); log.info("Creating descriptor file at[%s]", descriptorFile); START final ByteSource byteSource = ByteSource.wrap(jsonMapper.writeValueAsBytes(segment)); END byteSource.copyTo(new HdfsOutputStreamSupplier(fs, descriptorFile)); return segment; } This is not a requirement, but you can simply chain the methods here to make it more readable ``` java ByteSource .wrap(jsonMapper.writeValueAsBytes(segment)) .copyTo(new HdfsOutputStreamSupplier(fs, descriptorFile)); ```

Target:
private DataSegment createDescriptorFile(DataSegment segment, Path outDir, final FileSystem fs) throws IOException { final Path descriptorFile = new Path(outDir, "descriptor.json"); log.info("Creating descriptor file at[%s]", descriptorFile); ByteSource .wrap(jsonMapper.writeValueAsBytes(segment)) .copyTo(new HdfsOutputStreamSupplier(fs, descriptorFile)); return segment; }
Source:
public String apply(long value) { START return this.apply(Long.valueOf(value)); END } I wonder if there's not a reason to just let it auto-box?

Target:
public String apply(long value) { return this.apply((Long)value); }
Source:
private void createAuditTable(final IDBI dbi, final String tableName) { createTable( dbi, tableName, ImmutableList.of( String.format( START "CREATE TABLE %1$s (\n" END + " id %2$s NOT NULL,\n" + " audit_key VARCHAR(255) NOT NULL,\n" + " type VARCHAR(255) NOT NULL,\n" + " author VARCHAR(255) NOT NULL,\n" + " email_id VARCHAR(255) NOT NULL,\n" + " comment VARCHAR(2048) NOT NULL,\n" + " created_date VARCHAR(255) NOT NULL,\n" + " payload %3$s NOT NULL,\n" + " PRIMARY KEY(id)\n" + ")", tableName, getSerialType(), getPayloadType() ), String.format("CREATE INDEX idx_%1$s_key_time ON %1$s(audit_key, created_date)", tableName), String.format("CREATE INDEX idx_%1$s_type_time ON %1$s(audit_key, created_date)", tableName), String.format("CREATE INDEX idx_%1$s_audit_time ON %1$s(created_date)", tableName) ) ); } I think we need a blob in here about what the previous state or something was. E.g., if we modified a rule, what was the previous rule?

Target:
private void createAuditTable(final IDBI dbi, final String tableName) { createTable( dbi, tableName, ImmutableList.of( String.format( "CREATE TABLE %1$s (\n" + " id %2$s NOT NULL,\n" + " audit_key VARCHAR(255) NOT NULL,\n" + " type VARCHAR(255) NOT NULL,\n" + " author VARCHAR(255) NOT NULL,\n" + " comment VARCHAR(2048) NOT NULL,\n" + " created_date VARCHAR(255) NOT NULL,\n" + " payload %3$s NOT NULL,\n" + " PRIMARY KEY(id)\n" + ")", tableName, getSerialType(), getPayloadType() ), String.format("CREATE INDEX idx_%1$s_key_time ON %1$s(audit_key, created_date)", tableName), String.format("CREATE INDEX idx_%1$s_type_time ON %1$s(audit_key, created_date)", tableName), String.format("CREATE INDEX idx_%1$s_audit_time ON %1$s(created_date)", tableName) ) ); }
Source:
public OnheapIncrementalIndex(IncrementalIndexSchema incrementalIndexSchema, boolean deserializeComplexMetrics, int maxRowCount) { super(incrementalIndexSchema, deserializeComplexMetrics); this.facts = new ConcurrentSkipListMap<>(); this.maxRowCount = maxRowCount; START this.aggList = new Aggregator[maxRowCount][];// To handle concurrency, we allocate up to maxRowCount END for(int i = 0; i < this.aggList.length; ++i){ this.aggList[i] = null; } } I think this is overkill with memory allocation. We can revert back to the model we had in 0.6 and used a thread safe data structure here.

Target:
public OnheapIncrementalIndex( IncrementalIndexSchema incrementalIndexSchema, boolean deserializeComplexMetrics, int maxRowCount ) { super(incrementalIndexSchema, deserializeComplexMetrics); this.maxRowCount = maxRowCount; }
Source:
public int getId(String value) { START if (value == null) { END return -1; } final Integer id = falseIds.get(value); return id == null ? -1 : id; } null values should be treated like empty string.

Target:
public int getId(String value) { if (value == null) { value = ""; } final Integer id = falseIds.get(value); return id == null ? -1 : id; }
Source:
public ValueMatcher makeMatcher(ColumnSelectorFactory factory){ if (filters.size() == 0) { START return new BooleanValueMatcher(false); END } final ValueMatcher[] matchers = new ValueMatcher[filters.size()]; for (int i = 0; i < filters.size(); i++) { matchers[i] = filters.get(i).makeMatcher(factory); } return makeMatcher(matchers); } I think this changes behavior. Before an empty and filter would always return true, now it returns false.

Target:
public ValueMatcher makeMatcher(ColumnSelectorFactory factory) { final ValueMatcher[] matchers = new ValueMatcher[filters.size()]; for (int i = 0; i < filters.size(); i++) { matchers[i] = filters.get(i).makeMatcher(factory); } return makeMatcher(matchers); }
Source:
public byte[] compress(byte[] bytes) { LZFChunk chunk = null; try (final ResourceHolder<ChunkEncoder> encoder = CompressedPools.getChunkEncoder()) { START chunk = encoder.get().encodeChunk(bytes, 0, bytes.length); END } catch (IOException e) { log.error(e, "IOException thrown while closing ChunkEncoder."); } // IOException should be on ResourceHolder.close(), not encodeChunk, so this *should* never be null return null == chunk ? null : chunk.getData(); } let's move the return statement in here, so we don't need the extraneous check

Target:
public byte[] compress(byte[] bytes) { try (final ResourceHolder<ChunkEncoder> encoder = CompressedPools.getChunkEncoder()) { final LZFChunk chunk = encoder.get().encodeChunk(bytes, 0, bytes.length); return chunk.getData(); } catch (IOException e) { log.error(e, "IOException thrown while closing ChunkEncoder."); } // IOException should be on ResourceHolder.close(), not encodeChunk, so this *should* never happen return null; }
Source:
protected BasicDataSource getDatasource() { MetadataStorageConnectorConfig connectorConfig = getConfig(); BasicDataSource dataSource = new BasicDataSource(); dataSource.setUsername(connectorConfig.getUser()); dataSource.setPassword(connectorConfig.getPassword()); String uri = connectorConfig.getConnectURI(); dataSource.setUrl(uri); START if (connectorConfig.isUseValidationQuery()) { END dataSource.setValidationQuery(connectorConfig.getValidationQuery()); dataSource.setTestOnBorrow(true); } return dataSource; } i think we should just always use a validation query

Target:
protected BasicDataSource getDatasource() { MetadataStorageConnectorConfig connectorConfig = getConfig(); BasicDataSource dataSource = new BasicDataSource(); dataSource.setUsername(connectorConfig.getUser()); dataSource.setPassword(connectorConfig.getPassword()); String uri = connectorConfig.getConnectURI(); dataSource.setUrl(uri); dataSource.setValidationQuery(connectorConfig.getValidationQuery()); dataSource.setTestOnBorrow(true); return dataSource; }
Source:
public boolean isQueryCacheable(Query query) { START return !Sets.newHashSet(unCacheable).contains(query.getType()); END } no need to create a set here..

Target:
public boolean isQueryCacheable(Query query) { // O(n) impl, but I don't think we'll ever have a million query types here return !unCacheable.contains(query.getType()); }
Source:
START public List<String> getNames() END { return Lists.newArrayList(name); } does this have to be a list or can it be an iterable?

Target:
public List<String> getNames() { return Arrays.asList(name); }
Source:
public QueryRunner<Result<SelectResultValue>> preMergeQueryDecoration(QueryRunner<Result<SelectResultValue>> runner) { START return new UnionQueryRunner<Result<SelectResultValue>>( END new IntervalChunkingQueryRunner<Result<SelectResultValue>>( runner, config.getChunkPeriod() ) ); } is there no common place (at the broker level) we can add the unioning query runner?

Target:
public QueryRunner<Result<SelectResultValue>> preMergeQueryDecoration(QueryRunner<Result<SelectResultValue>> runner) { return new IntervalChunkingQueryRunner<Result<SelectResultValue>>( runner, config.getChunkPeriod() ); }
Source:
public Iterable<String> getNames() { START return Lists.newArrayList(name); END } Minor nitpick, but can we do Arrays.asList(name)?

Target:
public List<String> getNames() { return Arrays.asList(name); }
Source:
public CardinalityAggregator( String name, List<DimensionSelector> selectorList, boolean byRow ) { this.name = name; START this.selectorList = Lists.newArrayList(selectorList); END this.collector = HyperLogLogCollector.makeLatestCollector(); this.byRow = byRow; } why the copy?

Target:
public CardinalityAggregator( String name, List<DimensionSelector> selectorList, boolean byRow ) { this.name = name; this.selectorList = selectorList; this.collector = HyperLogLogCollector.makeLatestCollector(); this.byRow = byRow; }
Source:
public HashBasedNumberedShardSpec( @JsonProperty("partitionNum") int partitionNum, @JsonProperty("partitions") int partitions ) { super(partitionNum, partitions); START hashFunction = Hashing.murmur3_32(); END } can this be configurable? we make it configurable elsewhere

Target:
public HashBasedNumberedShardSpec( @JsonProperty("partitionNum") int partitionNum, @JsonProperty("partitions") int partitions ) { super(partitionNum, partitions); }
Source:
public Status doGet() { return new Status( START getVersion("/druid-server.version"), END getVersion("/druid-api.version"), getExtensionVersions(), new Memory(Runtime.getRuntime()) ); } why do we need a file to get this?

Target:
public Status doGet() { return getStatus(); }
Source:
public void run() { try { Injector injector = makeInjector(); START Lifecycle lifecycle = initLifecycle(injector); END try { injector.getInstance(ExecutorLifecycle.class).join(); lifecycle.stop(); } catch (Throwable t) { log.error(t, "Error when starting up. Failing."); System.exit(1); } } catch (Exception e) { throw Throwables.propagate(e); } } We are moving the exception catching out of the try{} down there and into this area. Is that intentional?

Target:
public void run() { try { Injector injector = makeInjector(); try { Lifecycle lifecycle = initLifecycle(injector); injector.getInstance(ExecutorLifecycle.class).join(); lifecycle.stop(); } catch (Throwable t) { log.error(t, "Error when starting up. Failing."); System.exit(1); } } catch (Exception e) { throw Throwables.propagate(e); } }
Source:
public void emitStats( String tier, MasterStats stats, List<ServerHolder> serverHolderList ) { START return; END } this isn't needed

Target:
public void emitStats( String tier, MasterStats stats, List<ServerHolder> serverHolderList ) { }
Source:
public ConjurerFirehoseFactory( @JsonProperty("startTime") Long startTime, @JsonProperty("stopTime") Long stopTime, @JsonProperty("linesPerSec") Integer linesPerSec, @JsonProperty("maxLines") Long maxLines, @JsonProperty("filePath") String filePath ) { START this(Conjurer.getBuilder().withStartTime(startTime).withStopTime(stopTime).withMaxLines(maxLines).withFilePath(filePath).withLinesPerSec(linesPerSec).withCustomSchema(true)); END } formatting

Target:
public ConjurerFirehoseFactory( @JsonProperty("startTime") Long startTime, @JsonProperty("stopTime") Long stopTime, @JsonProperty("linesPerSec") Integer linesPerSec, @JsonProperty("maxLines") Long maxLines, @JsonProperty("filePath") String filePath ) { this( Conjurer.getBuilder() .withStartTime(startTime) .withStopTime(stopTime) .withMaxLines(maxLines) .withFilePath(filePath) .withLinesPerSec(linesPerSec) ); }
Source:
public boolean hasNext() { boolean hasNext = delegate.hasNext() || !cursor.isDone(); START if(!hasNext) for(BufferAggregator agg : aggregators) agg.close(); END return hasNext; } squigglies ftw

Target:
public boolean hasNext() { return delegate.hasNext() || !cursor.isDone(); }
Source:
public boolean hasNext() { boolean hasNext = delegate.hasNext() || !cursor.isDone(); START if(!hasNext) { END // cleanup for(BufferAggregator agg : aggregators) { agg.close(); } } return hasNext; } It is not safe to clean things up here. If an exception is thrown while iterating, it will not complete the iteration and resources will never get cleaned up. The Sequence interface provides ways of ensuring that things get closed, look at GroupByQueryEngine.process(), there's a cleanup method that you can use there which will be guaranteed to be called.

Target:
public boolean hasNext() { return delegate.hasNext() || !cursor.isDone(); }
Source:
public Optional<Task> getTask(String taskid) { Preconditions.checkNotNull(taskid, "taskid"); if(tasks.containsKey(taskid)) { START return Optional.of(tasks.get(taskid).task); END } else { return Optional.absent(); } } Accessing fields is generally frowned upon in idiomatic Java. I know it's an annoying extra couple of keystrokes, but it appears like the Task object is not actually encapsulating any state.

Target:
public Optional<Task> getTask(String taskid) { Preconditions.checkNotNull(taskid, "taskid"); if(tasks.containsKey(taskid)) { return Optional.of(tasks.get(taskid).getTask()); } else { return Optional.absent(); } }
Source:
public static void injectSystemProperties(Job job) { for (String propName : System.getProperties().stringPropertyNames()) { START Configuration conf = job.getConfiguration(); END if (propName.startsWith("hadoop.")) { conf.set(propName.substring("hadoop.".length()), System.getProperty(propName)); } } } It's kinda weird to pull the configuration out for each and every property... Looks like that's how the code was, which was needless. Let's fix that up now and just get the configuration once.

Target:
public static void injectSystemProperties(Job job) { final Configuration conf = job.getConfiguration(); for (String propName : System.getProperties().stringPropertyNames()) { if (propName.startsWith("hadoop.")) { conf.set(propName.substring("hadoop.".length()), System.getProperty(propName)); } } }
Source:
public Object compute(Map<String, Object> combinedAggregators) { Object retVal = combinedAggregators.get(fieldName); START if (retVal == null) { END throw new ISE("Mismatch! Agg[%s] was not specified!", fieldName); } return retVal; } I'd rather figure this out earlier. Can we add a verifyFields(Set<String>) method to the postAggs which will verify that the fields it wants are there and recursively do the same for any other PostAggs they have?

Target:
public Object compute(Map<String, Object> combinedAggregators) { return combinedAggregators.get(fieldName); }
Source:
public void stop() { try { for (WorkerWrapper workerWrapper : zkWorkers.values()) { START workerWrapper.getWatcher().close(); END } } catch (Exception e) { throw Throwables.propagate(e); } finally { started = false; } } I wonder if the Wrapper shouldn't have a close() method if its own instead of making the people who use them know about all the things stored inside and what should and should not be closed.

Target:
public void stop() { try { for (WorkerWrapper workerWrapper : zkWorkers.values()) { workerWrapper.close(); } } catch (Exception e) { throw Throwables.propagate(e); } finally { started = false; } }
Source:
protected byte[] json2fleece(String json) throws LiteCoreException { boolean commit = false; db.beginTransaction(); FLSliceResult body = null; try { body = db.encodeJSON(json5(json).getBytes()); byte[] bytes = body.getBuf(); commit = true; return bytes; } finally { START if (body != null) body.free();; END db.endTransaction(commit); } } multiple semi colons;;

Target:
protected byte[] json2fleece(String json) throws LiteCoreException { boolean commit = false; db.beginTransaction(); FLSliceResult body = null; try { body = db.encodeJSON(json5(json).getBytes()); byte[] bytes = body.getBuf(); commit = true; return bytes; } finally { if (body != null) body.free(); db.endTransaction(commit); } }
Source:
private static String emptyDirectory(String path){ File f = new File(path); if(f.exists()) { for (File log : f.listFiles()) { log.delete(); } START f.delete(); END } return path; } It was on purpose (the directory is about to be used so there is no need to delete it), but I guess either way is fine

Target:
private static String emptyDirectory(String path){ File f = new File(path); if(f.exists()) { for (File log : f.listFiles()) { log.delete(); } } return path; }
Source:
ConsoleLogger() { START _domains.addAll(EnumSet.allOf(LogDomain.class)); END } As we have ALL, I think you can just add only ALL here.

Target:
ConsoleLogger() { }
Source:
Document(Database database, String id, FLDict body) { this(database, id, (C4Document) null); this._database = database; START this._id = id; END this._data = body; updateDictionary(); } I think we don't need to set _database and _id here as `this(database, id, (C4Document) null);` should already do that.

Target:
Document(Database database, String id, FLDict body) { this(database, id, (C4Document) null); this._data = body; updateDictionary(); }
Source:
public void testGetExistingDocWithIDInBatch() throws CouchbaseLiteException { final int NUM_DOCS = 10; START Database.getLog().getConsole().setLevel(LogLevel.VERBOSE); END // Save 10 docs: createDocs(NUM_DOCS); db.inBatch(new Runnable() { @Override public void run() { validateDocs(NUM_DOCS); } }); } Is this for testing if the console log works? Can we delete it if we don't need it?

Target:
public void testGetExistingDocWithIDInBatch() throws CouchbaseLiteException { final int NUM_DOCS = 10; // Save 10 docs: createDocs(NUM_DOCS); db.inBatch(new Runnable() { @Override public void run() { validateDocs(NUM_DOCS); } }); }
Source:
public String toString() { return "DocumentReplication {" + "replicator=" + replicator + ", is pushing =" + pushing + ", document id =" + docId + ", revision id =" + revId + ", error code =" + error.getCode()+ ", error domain=" + error.getDomain() + ", is transient=" + trans + ", flags =" + flags + ", doc is deleted =" + isDeleted + '}'; START } END Please remove info that is not in public API.

Target:
public String toString() { return "DocumentReplication {" + "replicator=" + replicator + ", is pushing =" + pushing + ", document id =" + docId + ", error code =" + error.getCode()+ ", error domain=" + error.getDomain() + ", doc is deleted =" + isDeleted + '}'; }
Source:
public List<Result> allResults() { START randomAccess = true; END return new ResultSetList(); } randomAccess is not thread safe.

Target:
public List<Result> allResults() { List<Result> results = new ArrayList<>(); Result result; while ((result = next()) != null) results.add(result); return results; }
Source:
public Result next() { if (query == null) throw new IllegalStateException("_query variable is null"); START if (randomAccess) END return null; synchronized (getDatabase().getLock()) { try { if (c4enum.next()) { return currentObject(); } else return null; } catch (LiteCoreException e) { Log.w(TAG, "Query enumeration error: %s", e); return null; } } } randomAccess is not thread safe.

Target:
public Result next() { if (query == null) throw new IllegalStateException("_query variable is null"); synchronized (getDatabase().getLock()) { try { if (c4enum.next()) { return currentObject(); } else return null; } catch (LiteCoreException e) { Log.w(TAG, "Query enumeration error: %s", e); return null; } } }
Source:
private DocumentChangeListenerToken addDocumentChangeListener(Executor executor, DocumentChangeListener listener, String docID) { // NOTE: caller method is synchronized. if (!docListenerTokens.containsKey(docID)) { START Set<DocumentChangeListenerToken> listeners = Collections.synchronizedSet(new HashSet<DocumentChangeListenerToken>()); END docListenerTokens.put(docID, listeners); registerC4DocObserver(docID); } DocumentChangeListenerToken token = new DocumentChangeListenerToken(executor, listener, docID); docListenerTokens.get(docID).add(token); return token; } Does it need to be a synchronized set?

Target:
private DocumentChangeListenerToken addDocumentChangeListener(Executor executor, DocumentChangeListener listener, String docID) { // NOTE: caller method is synchronized. if (!docListenerTokens.containsKey(docID)) { Set<DocumentChangeListenerToken> listeners = new HashSet<>(); docListenerTokens.put(docID, listeners); registerC4DocObserver(docID); } DocumentChangeListenerToken token = new DocumentChangeListenerToken(executor, listener, docID); docListenerTokens.get(docID).add(token); return token; }
Source:
private void postDocumentChanged(final String documentID) { synchronized (lock) { if (!c4DocObservers.containsKey(documentID) || c4db == null) return; // NOTE: docListenerTokens and its Set value are synchronized collections. // And DocumentChange is immutable Set<DocumentChangeListenerToken> tokens = docListenerTokens.get(documentID); if (tokens != null) { START synchronized (tokens) { END DocumentChange change = new DocumentChange(documentID); for (DocumentChangeListenerToken token : tokens) token.notify(change); } } } } As this is under the synchronized (lock), is this synchronized required?

Target:
private void postDocumentChanged(final String documentID) { synchronized (lock) { if (!c4DocObservers.containsKey(documentID) || c4db == null) return; Set<DocumentChangeListenerToken> tokens = docListenerTokens.get(documentID); if (tokens != null) { DocumentChange change = new DocumentChange(documentID); for (DocumentChangeListenerToken token : tokens) token.notify(change); } } }
Source:
START public Document getDocument() { END return rs.getQuery().getDatabase().getDocument(documentID); } Not in spec.

Target:
Document getDocument() { return rs.getQuery().getDatabase().getDocument(documentID); }
Source:
START public ArrayInterface setArray(int index, Array value) { END return setObject(index, value); } I think it should return Array here.

Target:
public Array setArray(int index, Array value) { return setObject(index, value); }
Source:
void mustBeOpen() { if (c4db == null) START throw new CouchbaseLiteRuntimeException("Database is not open."); END } I think we should throw IllegalStateException instead of CouchbaseLiteRuntimeException as it's self described that this is in an inconsistent or illegal state that users shouldn't do. CouchbaseLiteRuntimeException should be something that LC might thrown but not expected?

Target:
void mustBeOpen() { if (c4db == null) throw new IllegalStateException("Database is not open."); }
Source:
START boolean selectConflictingRevision() throws CouchbaseLiteException { END try { if (!c4doc.getRawDoc().selectNextLeaf(false, true)) return false; setC4Doc(c4doc); // self.c4Doc = _c4Doc; // This will update to the selected revision } catch (LiteCoreException e) { Log.e(TAG, "Failed to selectNextLeaf: doc -> " + c4doc, e); return false; } return true; } Is CouchbaseLiteException thrown from anything in this method? Or it should return void but throw CouchbaseLiteException instead?

Target:
boolean selectConflictingRevision() { try { if (!c4doc.getRawDoc().selectNextLeaf(false, true)) return false; setC4Doc(c4doc); // self.c4Doc = _c4Doc; // This will update to the selected revision } catch (LiteCoreException e) { Log.e(TAG, "Failed to selectNextLeaf: doc -> " + c4doc, e); return false; } return true; }
Source:
public static Node getNodeBySystemSession(Node node) throws RepositoryException { START node.getSession().save(); END SessionProvider systemSessionProvider = getSystemSessionProvider(); return (Node) systemSessionProvider.getSession(node.getSession().getWorkspace().getName(), getRepository()) .getItem(node.getPath()); } Why do you need to save the node before get it by system provider?

Target:
public static Node getNodeBySystemSession(Node node) throws RepositoryException { SessionProvider systemSessionProvider = getSystemSessionProvider(); return (Node) systemSessionProvider.getSession(node.getSession().getWorkspace().getName(), getRepository()) .getItem(node.getPath()); }
Source:
private static String getUnsupportedType(String component, String[] supportedTypes, Map<String, String> typeMapping) { String componentType = typeMapping.get(component); START if (component == null || componentType == null) { END return null; } for (String okayType: supportedTypes) { if (okayType.equalsIgnoreCase(componentType)) { return null; } } return componentType; } If component is null, this will have NPE'd by this point if typeMapping does not happen to be a type that allows null keys (most don't)

Target:
private static String getUnsupportedType(String component, String[] supportedTypes, Map<String, String> typeMapping) { if (component == null) { return null; } String componentType = typeMapping.get(component); if (componentType == null) { return null; } for (String okayType: supportedTypes) { if (okayType.equalsIgnoreCase(componentType)) { return null; } } return componentType; }
Source:
public AuthnRequest build(final SAML2MessageContext context) { final SingleSignOnService ssoService = context.getIDPSingleSignOnService(this.bindingType); final String idx = this.assertionConsumerServiceIndex > 0 ? String.valueOf(assertionConsumerServiceIndex) : null; final AssertionConsumerService assertionConsumerService = context.getSPAssertionConsumerService(idx); START assertionConsumerService.setLocation(this.urlResolver.compute(assertionConsumerService.getLocation(), context.getWebContext())); END return buildAuthnRequest(context, assertionConsumerService, ssoService); } Let's do that on this PR. You still have the issue of one metadata for N ACS URLs...

Target:
public AuthnRequest build(final SAML2MessageContext context) { final SingleSignOnService ssoService = context.getIDPSingleSignOnService(this.bindingType); final String idx = this.assertionConsumerServiceIndex > 0 ? String.valueOf(assertionConsumerServiceIndex) : null; final AssertionConsumerService assertionConsumerService = context.getSPAssertionConsumerService(idx); return buildAuthnRequest(context, assertionConsumerService, ssoService); }
Source:
protected boolean isTokenRequest(final WebContext context) { return ContextHelper.isGet(context) START && CommonHelper.isNotBlank(context.getRequestParameter(CasConfiguration.TICKET_PARAMETER).orElse(null)); END } isNotBlank should accept an optional and return false if it's not present

Target:
protected boolean isTokenRequest(final WebContext context) { return ContextHelper.isGet(context) && CommonHelper.isNotBlank(context.getRequestParameter(CasConfiguration.TICKET_PARAMETER)); }