Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FMWK-256 Correctly convert array of primitives for writing #645

Merged
merged 2 commits into from
Oct 25, 2023
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 @@ -164,13 +164,17 @@ private boolean isNotWritable(AerospikePersistentProperty property) {
Object getValueToWrite(Object value, TypeInformation<?> type) {
if (value == null) {
return null;
} else if (type == null || conversions.isSimpleType(value.getClass())) {
} else if (type == null || isSimpleValue(value.getClass())) {
return getSimpleValueToWrite(value);
} else {
return getNonSimpleValueToWrite(value, type);
}
}

private boolean isSimpleValue(Class<?> clazz) {
return conversions.isSimpleType(clazz) && (!clazz.isArray() || clazz == byte[].class);
}

private Object getSimpleValueToWrite(Object value) {
Optional<Class<?>> customTarget = conversions.getCustomWriteTarget(value.getClass());
return customTarget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.ToString;
import lombok.Value;
import lombok.experimental.FieldDefaults;
Expand All @@ -43,6 +44,7 @@
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -533,7 +535,8 @@ public AerospikeWriteData convert(User user) {
Collection<Bin> bins = new ArrayList<>();
bins.add(new Bin("fs", user.name.firstName));
bins.add(new Bin("ls", user.name.lastName));
return new AerospikeWriteData(new Key("custom-namespace", "custom-set", Long.toString(user.id)), bins, 0, null);
return new AerospikeWriteData(new Key("custom-namespace", "custom-set", Long.toString(user.id)), bins, 0,
null);
}
}

Expand Down Expand Up @@ -738,6 +741,42 @@ public static class DocumentWithByteArray {
private byte[] array;
}

@Data
@AllArgsConstructor
@Document
public static class DocumentWithArray {

@Id
private String id;
@Field
private int[] array;
}

@Data
@AllArgsConstructor
@Document
public static class DocumentWithBigIntegerAndNestedArray {

@Id
@NonNull
private String id;
@Field
@NonNull
private BigInteger bigInteger;
private ObjectWithArray objectWithArray;
}

public static class ObjectWithArray {

@Version
public Long version;
Integer[] array;

public ObjectWithArray(Integer[] array) {
this.array = array;
}
}

@Data
@AllArgsConstructor
@Document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.policy.Policy;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.aerospike.BaseBlockingIntegrationTests;
import org.springframework.data.aerospike.SampleClasses;
import org.springframework.data.aerospike.sample.Person;
import org.springframework.data.aerospike.utility.AsyncUtils;
import org.springframework.data.aerospike.utility.ServerVersionUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -39,8 +43,19 @@
import static org.springframework.data.aerospike.SampleClasses.DocumentWithTouchOnRead;
import static org.springframework.data.aerospike.SampleClasses.VersionedClass;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class AerospikeTemplateSaveTests extends BaseBlockingIntegrationTests {

@AfterAll
public void afterAll() {
template.delete(VersionedClass.class);
template.delete(SampleClasses.DocumentWithArray.class);
template.delete(SampleClasses.DocumentWithBigIntegerAndNestedArray.class);
template.delete(Person.class);
template.delete(CustomCollectionClass.class);
template.delete(DocumentWithTouchOnRead.class);
}

// test for RecordExistsAction.REPLACE_ONLY policy
@Test
public void shouldReplaceAllBinsPresentInAerospikeWhenSavingDocument() {
Expand All @@ -56,6 +71,31 @@ public void shouldReplaceAllBinsPresentInAerospikeWhenSavingDocument() {
assertThat(aeroRecord.bins.get("field")).isEqualTo("foo2");
}

@Test
public void shouldSaveDocumentWithArray() {
SampleClasses.DocumentWithArray doc = new SampleClasses.DocumentWithArray(id, new int[]{0, 1, 2, 3, 4, 5});
template.save(doc);

Key key = new Key(getNameSpace(), "DocumentWithArray", id);
Record aeroRecord = client.get(new Policy(), key);
assertThat(aeroRecord.bins.get("array")).isNotNull();
}

@Test
public void shouldSaveDocumentWithoutCustomConverter() {
SampleClasses.ObjectWithArray objectWithArray = new SampleClasses.ObjectWithArray(new Integer[]{0, 1, 2,
3, 4});
SampleClasses.DocumentWithBigIntegerAndNestedArray doc =
new SampleClasses.DocumentWithBigIntegerAndNestedArray(id,
new BigInteger("100"), objectWithArray);
template.save(doc);

Key key = new Key(getNameSpace(),
template.getSetName(SampleClasses.DocumentWithBigIntegerAndNestedArray.class), id);
Record aeroRecord = client.get(new Policy(), key);
assertThat(aeroRecord.bins).isNotEmpty();
}

@Test
public void shouldSaveAndSetVersion() {
VersionedClass first = new VersionedClass(id, "foo");
Expand Down
Loading