Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ allprojects {
targetCompatibility = JavaVersion.VERSION_21
}

tasks.withType(JavaCompile).configureEach {
// TODO: Address constructor this-escape warnings and re-enable this check.
options.compilerArgs.addAll(['-Xlint:all,-this-escape', '-Werror'])
}

repositories {
mavenCentral()
}
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/java/de/bwaldvogel/mongo/MongoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.ssl.SslContext;
Expand Down Expand Up @@ -76,8 +77,10 @@ public void bind(SocketAddress socketAddress) {

public void bind(SocketAddress socketAddress, int numberOfBossThreads, int numberOfWorkerThreads) {

bossGroup = new NioEventLoopGroup(numberOfBossThreads, new MongoThreadFactory("mongo-server-boss"));
workerGroup = new NioEventLoopGroup(numberOfWorkerThreads, new MongoThreadFactory("mongo-server-worker"));
bossGroup = new MultiThreadIoEventLoopGroup(numberOfBossThreads,
new MongoThreadFactory("mongo-server-boss"), NioIoHandler.newFactory());
workerGroup = new MultiThreadIoEventLoopGroup(numberOfWorkerThreads,
new MongoThreadFactory("mongo-server-worker"), NioIoHandler.newFactory());
channelGroup = new DefaultChannelGroup("mongodb-channels", workerGroup.next());

try {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/de/bwaldvogel/mongo/ServerVersion.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.bwaldvogel.mongo;

import java.util.Collections;
import java.util.List;

public enum ServerVersion implements MongoVersion {
Expand Down Expand Up @@ -32,7 +31,7 @@ public enum ServerVersion implements MongoVersion {

@Override
public List<Integer> getVersionArray() {
return Collections.unmodifiableList(versionArray);
return versionArray;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,11 @@ private Document handleBuildInfo() {
}

protected Document handleKillCursors(Document query) {
List<Long> cursorIds = (List<Long>) query.get("cursors");
List<?> cursorIds = (List<?>) query.get("cursors");
List<Long> cursorsKilled = new ArrayList<>();
List<Long> cursorsNotFound = new ArrayList<>();
for (Long cursorId : cursorIds) {
for (Object cursorIdValue : cursorIds) {
Long cursorId = (Long) cursorIdValue;
if (cursorRegistry.remove(cursorId)) {
log.info("Killed cursor {}", cursorId);
cursorsKilled.add(cursorId);
Expand Down Expand Up @@ -472,9 +473,9 @@ public void enableOplog() {

protected Oplog createOplog() {
MongoDatabase localDatabase = resolveDatabase("local");
MongoCollection<Document> collection = (MongoCollection<Document>) localDatabase.resolveCollection(OPLOG_COLLECTION_NAME, false);
MongoCollection<?> collection = localDatabase.resolveCollection(OPLOG_COLLECTION_NAME, false);
if (collection == null) {
collection = (MongoCollection<Document>) localDatabase.createCollectionOrThrowIfExists(OPLOG_COLLECTION_NAME);
collection = localDatabase.createCollectionOrThrowIfExists(OPLOG_COLLECTION_NAME);
}
return new CollectionBackedOplog(this, collection, cursorRegistry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,15 +678,15 @@ private void convertSelectorToDocument(Document selector, Document document) {
Object value = entry.getValue();

if (key.equals("$and")) {
List<Document> andValues = (List<Document>) value;
for (Document andValue : andValues) {
convertSelectorToDocument(andValue, document);
List<?> andValues = (List<?>) value;
for (Object andValue : andValues) {
convertSelectorToDocument((Document) andValue, document);
}
continue;
} else if (key.equals("$or")) {
List<Document> orValues = (List<Document>) value;
List<?> orValues = (List<?>) value;
if (orValues.size() == 1) {
convertSelectorToDocument(orValues.get(0), document);
convertSelectorToDocument((Document) orValues.get(0), document);
}
continue;
} else if (key.startsWith("$")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ private Document commandGetLastError(Channel channel, String command, Document q

Document result;
if (results != null && !results.isEmpty()) {
result = results.get(results.size() - 1);
result = results.getLast();
if (result == null) {
result = new Document();
}
Expand Down Expand Up @@ -891,7 +891,7 @@ private Document toError(Channel channel, MongoServerException ex) {
protected void putLastResult(Channel channel, Document result) {
List<Document> results = lastResults.get(channel);
// list must not be empty
Document last = results.get(results.size() - 1);
Document last = results.getLast();
Assert.isNull(last, () -> "last result already set: " + last);
results.set(results.size() - 1, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static ArrayFilters parse(List<Document> arrayFilters, Document updateQu
List<String> topLevelFieldNames = arrayFilter.keySet().stream()
.map(Utils::firstFragment)
.distinct()
.collect(Collectors.toList());
.toList();

if (topLevelFieldNames.size() > 1) {
throw new FailedToParseException("Error parsing array filter :: caused by ::" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static <T> T getSingleElement(Iterable<T> iterable) {

public static <T> T getLastElement(List<T> list) {
Assert.notEmpty(list);
return list.get(list.size() - 1);
return list.getLast();
}

public static <T> T getSingleElement(Iterable<T> iterable, Supplier<? extends RuntimeException> exceptionSupplier) {
Expand All @@ -37,22 +37,22 @@ public static <T> T getSingleElement(Iterable<T> iterable, Supplier<? extends Ru
return value;
}

static <T> List<List<T>> multiplyWithOtherElements(Collection<T> allValues, Collection<? extends Collection<T>> collectionValues) {
static List<List<Object>> multiplyWithOtherElements(Collection<?> allValues, Collection<? extends Collection<?>> collectionValues) {
int collectionValueSize = CollectionUtils.getElementAtPosition(collectionValues, 0).size();
for (Collection<T> collectionValue : collectionValues) {
for (Collection<?> collectionValue : collectionValues) {
Assert.isTrue(allValues.contains(collectionValue), () -> "Expected " + collectionValue + " to be part of " + allValues);
Assert.isTrue(collectionValue.size() == collectionValueSize, () -> "Expected " + collectionValue + " to be size " + collectionValueSize);
}

List<List<T>> result = new ArrayList<>(collectionValueSize);
List<List<Object>> result = new ArrayList<>(collectionValueSize);
for (int i = 0; i < collectionValueSize; i++) {
List<T> newValues = new ArrayList();
List<Object> newValues = new ArrayList<>();

for (Object value : allValues) {
if (collectionValues.contains(value)) {
newValues.add(CollectionUtils.getElementAtPosition((Collection<T>) value, i));
newValues.add(CollectionUtils.getElementAtPosition((Collection<?>) value, i));
} else {
newValues.add((T) value);
newValues.add(value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ public boolean matches(Document document, Document query) {
}

private void validateQueryValue(Object queryValue, String key) {
if (!(queryValue instanceof Document)) {
if (!(queryValue instanceof Document queryObject)) {
return;
}
if (BsonRegularExpression.isRegularExpression(queryValue)) {
return;
}
Document queryObject = (Document) queryValue;
if (!queryObject.keySet().isEmpty() && queryObject.keySet().iterator().next().startsWith("$")) {
for (String operator : queryObject.keySet()) {
if (Constants.REFERENCE_KEYS.contains(operator)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ private void apply(Document change, String modifier, String key, Object value) {
// no upsert → ignore
return;
}
//$FALL-THROUGH$
handleSet(key, value);
break;
case SET:
handleSet(key, value);
break;
Expand Down Expand Up @@ -325,9 +326,9 @@ private void handlePop(String modifier, String key, Object popValue) {

if (!list.isEmpty()) {
if (Utils.nullAwareEquals(normalizedValue, -1)) {
list.remove(0);
list.removeFirst();
} else {
list.remove(list.size() - 1);
list.removeLast();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/de/bwaldvogel/mongo/backend/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ Set<KeyValue> getKeyValues(Document document, boolean normalize) {
valuesPerKey.replaceAll((key, value) -> Utils.normalizeValue(value));
}

List<Collection<Object>> collectionValues = valuesPerKey.values().stream()
List<Collection<?>> collectionValues = valuesPerKey.values().stream()
.filter(value -> value instanceof Collection)
.map(value -> (Collection<Object>) value)
.map(value -> (Collection<?>) value)
.collect(Collectors.toList());

if (collectionValues.size() > 0) {
Expand All @@ -89,7 +89,7 @@ private void validateHasNoParallelArrays(Document document) {
}
if (arrayPaths.size() > 1) {
List<String> parallelArraysPaths = arrayPaths.stream()
.map(path -> path.get(path.size() - 1))
.map(List::getLast)
.collect(Collectors.toList());
throw new CannotIndexParallelArraysError(parallelArraysPaths);
}
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/de/bwaldvogel/mongo/backend/KeyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public final class KeyValue implements Serializable, Iterable<Object> {

private static final long serialVersionUID = 1L;

// Every constructor stores a serializable ArrayList here.
@SuppressWarnings("serial")
private final List<Object> values;

public KeyValue(Object... values) {
Expand Down Expand Up @@ -71,7 +73,7 @@ public Stream<Object> stream() {
public KeyValue normalized() {
return new KeyValue(values.stream()
.map(Utils::normalizeValue)
.collect(Collectors.toList()));
.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,9 @@ public boolean equals(Object o) {
if (o == null) {
return false;
}
if (!(o instanceof LinkedTreeSet)) {
if (!(o instanceof LinkedTreeSet<?> otherSet)) {
return false;
}
LinkedTreeSet<?> otherSet = (LinkedTreeSet<?>) o;
return orderedElements.equals(otherSet.orderedElements);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ private static void projectField(Document document, Document newDocument, String
Document newsubDocument = (Document) newDocument.computeIfAbsent(mainKey, k -> new Document());
projectField((Document) object, newsubDocument, subKey, projectionValue);
} else if (object instanceof List<?> values) {
// This list is created and populated only by this projection.
@SuppressWarnings("unchecked")
List<Object> newprojectedValues = (List<Object>) newDocument.computeIfAbsent(mainKey, k -> new ArrayList<>());

if ("$".equals(subKey) && !values.isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ private static List<String> collectCommonPathFragments(List<String> fragments1,

static String getLastFragment(String path) {
List<String> fragments = splitPath(path);
return fragments.get(fragments.size() - 1);
return fragments.getLast();
}

public static String getHostName() {
Expand Down
41 changes: 18 additions & 23 deletions core/src/main/java/de/bwaldvogel/mongo/backend/ValueComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,22 @@

public class ValueComparator implements Comparator<Object> {

private static final List<Class<?>> SORT_PRIORITY = new ArrayList<>();
// https://docs.mongodb.com/manual/reference/bson-type-comparison-order/
private static final List<Class<?>> SORT_PRIORITY = List.of(
MinKey.class,
Number.class,
String.class,
Document.class,
List.class,
BinData.class,
LegacyUUID.class,
UUID.class,
ObjectId.class,
Boolean.class,
Instant.class,
BsonTimestamp.class,
BsonRegularExpression.class,
MaxKey.class);

private static final ValueComparator ASCENDING = new ValueComparator(true);
private static final ValueComparator DESCENDING = new ValueComparator(false);
Expand All @@ -40,26 +55,6 @@ public static ValueComparator desc() {
return DESCENDING;
}

static {
/*
* https://docs.mongodb.com/manual/reference/bson-type-comparison-order/
*/
SORT_PRIORITY.add(MinKey.class);
SORT_PRIORITY.add(Number.class);
SORT_PRIORITY.add(String.class);
SORT_PRIORITY.add(Document.class);
SORT_PRIORITY.add(List.class);
SORT_PRIORITY.add(BinData.class);
SORT_PRIORITY.add(LegacyUUID.class);
SORT_PRIORITY.add(UUID.class);
SORT_PRIORITY.add(ObjectId.class);
SORT_PRIORITY.add(Boolean.class);
SORT_PRIORITY.add(Instant.class);
SORT_PRIORITY.add(BsonTimestamp.class);
SORT_PRIORITY.add(BsonRegularExpression.class);
SORT_PRIORITY.add(MaxKey.class);
}

private ValueComparator(boolean ascending) {
this(ascending, true);
}
Expand Down Expand Up @@ -238,8 +233,8 @@ private int compareLists(Object value1, Object value2) {
}

private Object getListValueForComparison(Object value) {
if (value instanceof Collection) {
List<Object> values = new ArrayList<>((Collection<Object>) value);
if (value instanceof Collection<?> collection) {
List<?> values = new ArrayList<>(collection);
if (values.isEmpty()) {
return Missing.getInstance();
}
Expand Down
Loading
Loading