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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Weaviate Java client <img alt='Weaviate logo' src='https://github.com/weaviate/java-client/blob/v6/assets/duke-client6.png' width='200' align='right' />
# Weaviate Java client <img alt='Weaviate logo' src='https://github.com/weaviate/java-client/blob/v6/logo.png' width='200' align='right' />

[![Build Status](https://github.com/weaviate/java-client/actions/workflows/.github/workflows/test.yaml/badge.svg?branch=main)](https://github.com/weaviate/java-client/actions/workflows/.github/workflows/test.yaml)

Expand Down Expand Up @@ -183,7 +183,7 @@ WeaviateClient wcd = WeaviateClient.connectToWeaviateCloud("my-cluster-url.io",
> [!TIP]
> The client holds a number of resources (HTTP connection pools, gRPC channel) which must be disposed of correclty then they are no longer needed.
> If the client's lifecycle is tied to that of your app, closing the client via `client.close()` is a good way to do that.
>
>
> Otherwise, use the client inside a [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) statement:
>
>```java
Expand Down Expand Up @@ -684,7 +684,7 @@ var song1 = songs.query.byId(
song -> song.returnReferences(QueryReference.single("artist"))
);
System.out.println(
"Artist's last name is: " +
"Artist's last name is: " +
song1.properties().artist().lastName()
);
```
Expand Down
File renamed without changes
47 changes: 45 additions & 2 deletions src/it/java/io/weaviate/integration/ORMITest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.weaviate.integration;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
Expand All @@ -14,6 +15,7 @@
import io.weaviate.ConcurrentTest;
import io.weaviate.client6.v1.api.WeaviateClient;
import io.weaviate.client6.v1.api.collections.CollectionConfig;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.api.collections.annotations.Collection;
import io.weaviate.client6.v1.api.collections.annotations.Property;
import io.weaviate.client6.v1.api.collections.data.InsertManyResponse.InsertObject;
Expand All @@ -23,7 +25,7 @@
public class ORMITest extends ConcurrentTest {
private static WeaviateClient client = Container.WEAVIATE.getClient();

@Collection("ORMITest")
@Collection("ORMITestThings")
static record Thing(
// text / text[]
String text,
Expand Down Expand Up @@ -95,7 +97,7 @@ public void test_createCollection() throws Exception {

// Assert
Assertions.assertThat(config).get()
.returns("ORMITest", CollectionConfig::collectionName)
.returns("ORMITestThings", CollectionConfig::collectionName)
.extracting(CollectionConfig::properties,
InstanceOfAssertFactories.list(io.weaviate.client6.v1.api.collections.Property.class))
.extracting(p -> Map.entry(
Expand Down Expand Up @@ -307,4 +309,45 @@ public void test_insertManyAndQuery() throws Exception {
.usingRecursiveComparison(COMPARISON_CONFIG)
.asInstanceOf(InstanceOfAssertFactories.list(Thing.class));
}

@Collection("ORMITestSongs")
record Song(
String title,
String album,
int year,
boolean hasAward,
Long monthlyListeners) {
}

/**
* Test that serialization works correctly when some fields are null and
* deserialization works correctly when some properties are not returned.
*/
@Test
public void test_partialScan() throws IOException {
client.collections.create(Song.class);

var songs = client.collections.use(Song.class);

// Act: insert with nulls
var dystopia = songs.data.insert(new Song(
"Dystopia",
null,
2016,
true,
null));

// Act: return subset of the properties
var got = songs.query.byId(dystopia.uuid(),
q -> q.returnProperties("title", "hasAward"));

// Assert
Assertions.assertThat(got).get()
.extracting(WeaviateObject::properties)
.returns("Dystopia", Song::title)
.returns(null, Song::album)
.returns(0, Song::year)
.returns(true, Song::hasAward)
.returns(null, Song::monthlyListeners);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private static <T> void setProperty(String property, WeaviateProtoProperties.Val
} else if (value.hasBoolValue()) {
builder.setBoolean(property, value.getBoolValue());
} else if (value.hasIntValue()) {
builder.setInteger(property, value.getIntValue());
builder.setLong(property, value.getIntValue());
} else if (value.hasNumberValue()) {
builder.setDouble(property, value.getNumberValue());
} else if (value.hasBlobValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void setBoolean(String property, Boolean value) {
}

@Override
public void setInteger(String property, Long value) {
public void setLong(String property, Long value) {
properties.put(property, value);
}

Expand Down
28 changes: 25 additions & 3 deletions src/main/java/io/weaviate/client6/v1/internal/orm/PojoBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,34 @@
import org.apache.commons.lang3.ArrayUtils;

final class PojoBuilder<PropertiesT extends Record> implements PropertiesBuilder<PropertiesT> {
private static final Map<Class<?>, Object> PRIMITIVE_DEFAULTS;

static {
PRIMITIVE_DEFAULTS = Map.of(
boolean.class, false,
short.class, (short) 0,
int.class, 0,
long.class, 0L,
float.class, 0f,
double.class, 0d);
}

private final PojoDescriptor<PropertiesT> descriptor;
private final Constructor<PropertiesT> ctor;
private final Map<String, Arg> ctorArgs;

static record Arg(Class<?> type, Object value) {
/**
* Create a new Arg, replacing a null value with
* default if the type is a known primitive class.
*/
static Arg withPrimitiveDefault(Class<?> type, Object value) {
if (PRIMITIVE_DEFAULTS.containsKey(type)) {
return new Arg(type, PRIMITIVE_DEFAULTS.get(type));
}
return new Arg(type, value);
}

Arg withValue(Object value) {
return new Arg(this.type, value);
}
Expand All @@ -31,7 +54,7 @@ Arg withValue(Object value) {
.map(arg -> {
// LinkedHashMap allows null values.
var type = arg.getType();
ctorArgs.put(arg.getName(), new Arg(type, null));
ctorArgs.put(arg.getName(), Arg.withPrimitiveDefault(type, null));
return type;
})
.toArray(Class<?>[]::new);
Expand Down Expand Up @@ -103,8 +126,7 @@ public void setBoolean(String propertyName, Boolean value) {
}

@Override
// TODO: rename to setLong
public void setInteger(String propertyName, Long value) {
public void setLong(String propertyName, Long value) {
if (isType(propertyName, short.class, Short.class)) {
setValue(propertyName, value.shortValue());
} else if (isType(propertyName, int.class, Integer.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface PropertiesBuilder<T> {

void setBoolean(String property, Boolean value);

void setInteger(String property, Long value);
void setLong(String property, Long value);

void setDouble(String property, Double value);

Expand Down