forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add query building API for the
$fill
aggregation pipeline stage (mo…
…ngodb#965) JAVA-4394
- Loading branch information
Showing
21 changed files
with
1,155 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
driver-core/src/main/com/mongodb/client/model/fill/FillComputation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mongodb.client.model.fill; | ||
|
||
import com.mongodb.annotations.Evolving; | ||
import com.mongodb.client.model.Aggregates; | ||
import com.mongodb.client.model.WindowedComputations; | ||
import org.bson.Document; | ||
import org.bson.conversions.Bson; | ||
|
||
import static com.mongodb.assertions.Assertions.notNull; | ||
|
||
/** | ||
* The core part of the {@code $fill} pipeline stage of an aggregation pipeline. | ||
* A pair of an expression/method and a path to a field to be filled with evaluation results of the expression/method. | ||
* | ||
* @see Aggregates#fill(FillOptions, Iterable) | ||
* @see Aggregates#fill(FillOptions, FillComputation, FillComputation...) | ||
* @mongodb.server.release 5.3 | ||
* @since 4.7 | ||
*/ | ||
@Evolving | ||
public interface FillComputation extends Bson { | ||
/** | ||
* Returns a {@link FillComputation} that uses the specified {@code expression}. | ||
* | ||
* @param field The field to fill. | ||
* @param expression The expression. | ||
* @param <TExpression> The {@code expression} type. | ||
* @return The requested {@link FillComputation}. | ||
* @mongodb.driver.manual core/document/#dot-notation Dot notation | ||
*/ | ||
static <TExpression> ValueFillComputation value(final String field, TExpression expression) { | ||
return new FillConstructibleBsonElement(notNull("field", field), | ||
new Document("value", (notNull("expression", expression)))); | ||
} | ||
|
||
/** | ||
* Returns a {@link FillComputation} that uses the {@link WindowedComputations#locf(String, Object) locf} method. | ||
* | ||
* @param field The field to fill. | ||
* @return The requested {@link FillComputation}. | ||
* @mongodb.driver.manual core/document/#dot-notation Dot notation | ||
*/ | ||
static LocfFillComputation locf(final String field) { | ||
return new FillConstructibleBsonElement(notNull("field", field), | ||
new Document("method", "locf")); | ||
} | ||
|
||
/** | ||
* Returns a {@link FillComputation} that uses the {@link WindowedComputations#linearFill(String, Object) linear} method. | ||
* <p> | ||
* {@linkplain FillOptions#sortBy(Bson) Sorting} is required.</p> | ||
* | ||
* @param field The field to fill. | ||
* @return The requested {@link FillComputation}. | ||
* @mongodb.driver.manual core/document/#dot-notation Dot notation | ||
*/ | ||
static LinearFillComputation linear(final String field) { | ||
return new FillConstructibleBsonElement(notNull("field", field), | ||
new Document("method", "linear")); | ||
} | ||
|
||
/** | ||
* Creates a {@link FillComputation} from a {@link Bson} in situations when there is no builder method | ||
* that better satisfies your needs. | ||
* This method cannot be used to validate the syntax. | ||
* <p> | ||
* <i>Example</i><br> | ||
* The following code creates two functionally equivalent {@link FillComputation}s, | ||
* though they may not be {@linkplain Object#equals(Object) equal}. | ||
* <pre>{@code | ||
* FillComputation field1 = FillComputation.locf("fieldName"); | ||
* FillComputation field2 = FillComputation.of(new Document("fieldName", new Document("method", "locf"))); | ||
* }</pre> | ||
* | ||
* @param fill A {@link Bson} representing the required {@link FillComputation}. | ||
* @return The requested {@link FillComputation}. | ||
*/ | ||
static FillComputation of(final Bson fill) { | ||
return new FillConstructibleBsonElement(notNull("fill", fill)); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
driver-core/src/main/com/mongodb/client/model/fill/FillConstructibleBson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mongodb.client.model.fill; | ||
|
||
import com.mongodb.internal.client.model.AbstractConstructibleBson; | ||
import org.bson.Document; | ||
import org.bson.conversions.Bson; | ||
|
||
import static com.mongodb.assertions.Assertions.notNull; | ||
import static com.mongodb.internal.client.model.Util.sizeAtLeast; | ||
|
||
final class FillConstructibleBson extends AbstractConstructibleBson<FillConstructibleBson> implements FillOptions { | ||
static final FillConstructibleBson EMPTY_IMMUTABLE = new FillConstructibleBson(AbstractConstructibleBson.EMPTY_IMMUTABLE); | ||
|
||
FillConstructibleBson(final Bson base) { | ||
super(base); | ||
} | ||
|
||
private FillConstructibleBson(final Bson base, final Document appended) { | ||
super(base, appended); | ||
} | ||
|
||
@Override | ||
protected FillConstructibleBson newSelf(final Bson base, final Document appended) { | ||
return new FillConstructibleBson(base, appended); | ||
} | ||
|
||
@Override | ||
public <TExpression> FillOptions partitionBy(final TExpression expression) { | ||
notNull("expression", expression); | ||
return newMutated(doc -> { | ||
doc.remove("partitionByFields"); | ||
doc.append("partitionBy", expression); | ||
}); | ||
} | ||
|
||
@Override | ||
public FillOptions partitionByFields(final Iterable<String> fields) { | ||
notNull("fields", fields); | ||
return newMutated(doc -> { | ||
doc.remove("partitionBy"); | ||
if (sizeAtLeast(fields, 1)) { | ||
doc.append("partitionByFields", fields); | ||
} else { | ||
doc.remove("partitionByFields"); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public FillOptions sortBy(final Bson sortBy) { | ||
return newAppended("sortBy", notNull("sortBy", sortBy)); | ||
} | ||
|
||
@Override | ||
public FillOptions option(final String name, final Object value) { | ||
return newAppended(notNull("name", name), notNull("value", value)); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
driver-core/src/main/com/mongodb/client/model/fill/FillConstructibleBsonElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mongodb.client.model.fill; | ||
|
||
import com.mongodb.internal.client.model.AbstractConstructibleBsonElement; | ||
import org.bson.conversions.Bson; | ||
|
||
final class FillConstructibleBsonElement extends AbstractConstructibleBsonElement<FillConstructibleBsonElement> implements | ||
ValueFillComputation, LocfFillComputation, LinearFillComputation { | ||
FillConstructibleBsonElement(final String name, final Bson value) { | ||
super(name, value); | ||
} | ||
|
||
FillConstructibleBsonElement(final Bson baseElement) { | ||
super(baseElement); | ||
} | ||
|
||
private FillConstructibleBsonElement(final Bson baseElement, final Bson appendedElementValue) { | ||
super(baseElement, appendedElementValue); | ||
} | ||
|
||
@Override | ||
protected FillConstructibleBsonElement newSelf(final Bson baseElement, final Bson appendedElementValue) { | ||
return new FillConstructibleBsonElement(baseElement, appendedElementValue); | ||
} | ||
} |
Oops, something went wrong.