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

Add deep copy instead of shallow copy in mappers #360

Merged
merged 1 commit into from
Feb 20, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]
- Close public access to TarantoolResult*Impl ([#326](https://github.com/tarantool/cartridge-java/issues/326))
- Add deep copy instead of shallow copy in default message pack mapper

## [0.10.1] - 2023-01-13

### Internal and API changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import static io.tarantool.driver.mappers.MapperReflectionUtils.getInterfaceParameterClass;

Expand Down Expand Up @@ -54,9 +55,10 @@ public DefaultMessagePackMapper() {
* @param mapper another mapper instance
*/
public DefaultMessagePackMapper(DefaultMessagePackMapper mapper) {
this();
this.valueConverters.putAll(mapper.valueConverters);
this.objectConverters.putAll(mapper.objectConverters);
this.valueConverters = mapper.valueConverters.entrySet().stream()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's a good idea on its own, it will lead to large memory consumption growth. Remember that we are currently creating a mapper stack on each request.

Did this change come out of a recently encountered bug? I was worried about that after we made the last mappers refactoring, see my comment #300 (review). Maybe let's think about proper mapper caching this time?

Raw idea: we should define a "tagging" API for mappers (e.g. a hashcode for the whole stack) that we can use for caching the other mappers. Using such tag (hashcode) can allow caching mappers without space schema involved, but it will require each converter and mapper to provide a unique tag (hashcode).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this change come out of a recently encountered bug?

It's problem in springdata, because there we use registerValueConverter per request there. We should also think about how to separate custom mappers from default ones besides caching

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added your thoughts in #117

And also created a ticket about my separation idea #361

.collect(Collectors.toMap(Map.Entry::getKey, e -> new ArrayList<>(e.getValue())));
this.objectConverters = mapper.objectConverters.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> new ArrayList<>(e.getValue())));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.msgpack.value.impl.ImmutableLongValueImpl;
import org.msgpack.value.impl.ImmutableStringValueImpl;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -186,6 +187,31 @@ void registerObjectConverter() throws MessagePackObjectMapperException {
assertEquals(testValue, mapper.toValue(testTuple).asMapValue().map());
}

@Test
void test_defaultMessagePackMapperCopy_shouldWorkFine() throws MessagePackValueMapperException,
NoSuchFieldException, IllegalAccessException {
DefaultMessagePackMapper mapper = DefaultMessagePackMapperFactory.getInstance().defaultComplexTypesMapper();
MessagePackMapper copiedMapper = mapper.copy();
MessagePackMapper copiedOfMapper = DefaultMessagePackMapperFactory.getInstance().copyOf(mapper);

Field privateField
= DefaultMessagePackMapper.class.getDeclaredField("valueConverters");
// Set the accessibility as true
privateField.setAccessible(true);

assertEquals(1, ((List) ((Map) privateField.get(mapper)).get(ValueType.MAP)).size());
assertEquals(1, ((List) ((Map) privateField.get(copiedMapper)).get(ValueType.MAP)).size());

mapper.registerValueConverter(ValueType.MAP, CustomTuple.class,
(ValueConverter<MapValue, CustomTuple>) v -> null);

assertEquals(2, ((List) ((Map) privateField.get(mapper)).get(ValueType.MAP)).size());
assertEquals(1, ((List) ((Map) privateField.get(copiedMapper)).get(ValueType.MAP)).size());
assertEquals(1, ((List) ((Map) privateField.get(copiedOfMapper)).get(ValueType.MAP)).size());

mapper.copy();
}

//TODO: add this test when will it be resolved https://github.com/tarantool/cartridge-java/issues/118
// @Test
// void should_getObject_returnShort_ifParameterObjectClassIsShort() {
Expand Down