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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import io.weaviate.client6.v1.api.collections.CollectionHandleDefaults;
import io.weaviate.client6.v1.api.collections.ObjectMetadata;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.internal.MapUtil;
import io.weaviate.client6.v1.internal.grpc.ByteStringUtil;
import io.weaviate.client6.v1.internal.grpc.Rpc;
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc.WeaviateBlockingStub;
Expand Down Expand Up @@ -65,8 +65,10 @@ public static <T> Rpc<InsertManyRequest<T>, WeaviateProtoBatch.BatchObjectsReque
var errors = new ArrayList<String>(insertErrors.size());
var uuids = new ArrayList<String>();

var failed = insertErrors.stream()
.collect(Collectors.toMap(err -> err.getIndex(), err -> err.getError()));
var failed = MapUtil.collect(
insertErrors.stream(),
err -> err.getIndex(),
err -> err.getError());

var iter = insertObjects.listIterator();
while (iter.hasNext()) {
Expand Down Expand Up @@ -137,6 +139,10 @@ public static <T> void buildObject(WeaviateProtoBatch.BatchObject.Builder object
var value = entry.getValue();
var protoValue = com.google.protobuf.Value.newBuilder();

if (value == null) {
return;
}

if (value instanceof String v) {
protoValue.setStringValue(v);
} else if (value instanceof UUID v) {
Expand All @@ -147,7 +153,7 @@ public static <T> void buildObject(WeaviateProtoBatch.BatchObject.Builder object
protoValue.setBoolValue(v.booleanValue());
} else if (value instanceof Number v) {
protoValue.setNumberValue(v.doubleValue());
} else if (value instanceof List v) {
} else if (value instanceof List<?> v) {
protoValue.setListValue(
com.google.protobuf.ListValue.newBuilder()
.addAllValues(v.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ static <T> Rpc<QueryRequest, WeaviateProtoSearchGet.SearchRequest, QueryResponse
group.getMaxDistance(),
group.getNumberOfObjects(),
objects);
}).collect(Collectors.toMap(QueryResponseGroup::name, Function.identity()));
})
// Collectors.toMap() throws an NPE if either key or value in the map are null.
// In this specific case it is safe to use it, as the function in the map above
// always returns a QueryResponseGroup.
// The name of the group should not be null either, that's something we assume
// about the server's response.
.collect(Collectors.toMap(QueryResponseGroup::name, Function.identity()));

return new QueryResponseGrouped<T>(allObjects, groups);
}, () -> rpc.method(), () -> rpc.methodAsync());
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/io/weaviate/client6/v1/internal/MapUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.weaviate.client6.v1.internal;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class MapUtil {
/** Prevent public initialization. */
private MapUtil() {
}

/**
* Collect stream entries into a map. Use this method whenever
* potential null keys or null values prohibit {@link Collectors#toMap}.
*
* <p>
* Example:
*
* <pre>{@code
* Map<Integer, Integer> = MapUtil.collect(
* Stream.of(1, 2, 3),
* Function.identity(), // use value as key
* el -> el.equals(3) ? null : el;
* );
*
* // Result: {1: 1, 2: 2, 3: null};
* }</pre>
*
* @param stream Stream of elements {@link T}.
* @param keyFn Transforms element {@link T} to key {@link K}.
* @param keyFn Transforms element {@link T} to value {@link V}.
* @return Map
*/
public static <K, V, T> Map<K, V> collect(Stream<T> stream, Function<T, K> keyFn, Function<T, V> valueFn) {
return stream.collect(
HashMap::new,
(m, el) -> m.put(keyFn.apply(el), valueFn.apply(el)),
HashMap::putAll);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package io.weaviate.client6.v1.internal.orm;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MapReader implements PropertiesReader<Map<String, Object>> {
private final Map<String, Object> properties;

public MapReader(Map<String, Object> properties) {
this.properties = properties;
// Defensive copy to ensure original properties are not modified
this.properties = Collections.unmodifiableMap(new HashMap<>(properties));
}

@Override
public Map<String, Object> readProperties() {
return Map.copyOf(properties); // ensure original properties immutable
return properties;
}
}