-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for jackson smile data format with blackbird module fo…
…r increased serlization/deserialization performance (#100)
- Loading branch information
Showing
9 changed files
with
205 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...va/com/box/l10n/mojito/service/tm/textunitdtocache/TextUnitDTOsSmileCacheBlobStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.box.l10n.mojito.service.tm.textunitdtocache; | ||
|
||
import static com.box.l10n.mojito.service.blobstorage.StructuredBlobStorage.Prefix.TEXT_UNIT_DTOS_CACHE; | ||
|
||
import com.box.l10n.mojito.json.ObjectMapper; | ||
import com.box.l10n.mojito.service.blobstorage.Retention; | ||
import com.box.l10n.mojito.service.tm.search.TextUnitDTO; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.Optional; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConditionalOnProperty(name = "l10n.cache.textunit.smile.enabled", havingValue = "true") | ||
public class TextUnitDTOsSmileCacheBlobStorage extends TextUnitDTOsCacheBlobStorage { | ||
|
||
@Autowired | ||
@Qualifier("smile_format_object_mapper") | ||
ObjectMapper objectMapper; | ||
|
||
String getName(Long assetId, Long localeId) { | ||
return "asset/" + assetId + "/locale/" + localeId + ".smile"; | ||
} | ||
|
||
Optional<ImmutableList<TextUnitDTO>> getTextUnitsFromCache(Long assetId, Long localeId) { | ||
Optional<byte[]> bytes = | ||
structuredBlobStorage.getBytes(TEXT_UNIT_DTOS_CACHE, getName(assetId, localeId)); | ||
return bytes.map(this::convertToListOrEmptyList); | ||
} | ||
|
||
void writeTextUnitDTOsToCache( | ||
Long assetId, | ||
Long localeId, | ||
TextUnitDTOsCacheBlobStorageJson textUnitDTOsCacheBlobStorageJson) { | ||
byte[] bytes = objectMapper.writeValueAsBytes(textUnitDTOsCacheBlobStorageJson); | ||
structuredBlobStorage.putBytes( | ||
TEXT_UNIT_DTOS_CACHE, getName(assetId, localeId), bytes, Retention.PERMANENT); | ||
} | ||
|
||
ImmutableList<TextUnitDTO> convertToListOrEmptyList(byte[] s) { | ||
try { | ||
return ImmutableList.copyOf( | ||
objectMapper.readValue(s, TextUnitDTOsCacheBlobStorageJson.class).getTextUnitDTOs()); | ||
} catch (Exception e) { | ||
logger.error( | ||
"Can't convert the content into TextUnitDTOsCacheBlobStorageJson, return an empty list instead", | ||
e); | ||
return ImmutableList.of(); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...om/box/l10n/mojito/service/tm/textunitdtocache/TextUnitDTOsSmileCacheBlobStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.box.l10n.mojito.service.tm.textunitdtocache; | ||
|
||
import static com.box.l10n.mojito.service.blobstorage.StructuredBlobStorage.Prefix.TEXT_UNIT_DTOS_CACHE; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.box.l10n.mojito.json.ObjectMapper; | ||
import com.box.l10n.mojito.service.blobstorage.Retention; | ||
import com.box.l10n.mojito.service.blobstorage.StructuredBlobStorage; | ||
import com.box.l10n.mojito.service.tm.search.TextUnitDTO; | ||
import com.google.common.collect.ImmutableList; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
public class TextUnitDTOsSmileCacheBlobStorageTest { | ||
|
||
TextUnitDTOsSmileCacheBlobStorage textUnitDTOsCacheBlobStorage = | ||
new TextUnitDTOsSmileCacheBlobStorage(); | ||
|
||
ObjectMapper objectMapper = ObjectMapper.withSmileEnabled(); | ||
|
||
@Before | ||
public void setUp() { | ||
textUnitDTOsCacheBlobStorage.objectMapper = objectMapper; | ||
} | ||
|
||
@Test | ||
public void testSuffixAppendedToName() { | ||
String blobName = textUnitDTOsCacheBlobStorage.getName(1234L, 56L); | ||
assertEquals("asset/1234/locale/56.smile", blobName); | ||
} | ||
|
||
@Test | ||
public void testBytesWrittenToCache() { | ||
StructuredBlobStorage structuredBlobStorageMock = Mockito.mock(StructuredBlobStorage.class); | ||
TextUnitDTOsCacheBlobStorageJson textUnitDTOsCacheBlobStorageJson = | ||
new TextUnitDTOsCacheBlobStorageJson(); | ||
textUnitDTOsCacheBlobStorage.structuredBlobStorage = structuredBlobStorageMock; | ||
byte[] expectedBytes = objectMapper.writeValueAsBytes(textUnitDTOsCacheBlobStorageJson); | ||
textUnitDTOsCacheBlobStorage.writeTextUnitDTOsToCache( | ||
1234L, 56L, textUnitDTOsCacheBlobStorageJson); | ||
Mockito.verify(structuredBlobStorageMock) | ||
.putBytes( | ||
TEXT_UNIT_DTOS_CACHE, "asset/1234/locale/56.smile", expectedBytes, Retention.PERMANENT); | ||
} | ||
|
||
@Test | ||
public void testGetTextUnitDTOS() throws IOException { | ||
StructuredBlobStorage structuredBlobStorageMock = Mockito.mock(StructuredBlobStorage.class); | ||
TextUnitDTOsCacheBlobStorageJson textUnitDTOsCacheBlobStorageJson = | ||
new TextUnitDTOsCacheBlobStorageJson(); | ||
TextUnitDTO textUnitDTO = new TextUnitDTO(); | ||
textUnitDTO.setComment("testComment"); | ||
textUnitDTO.setTmTextUnitId(1L); | ||
textUnitDTO.setName("testName"); | ||
textUnitDTO.setSource("testSource"); | ||
textUnitDTOsCacheBlobStorageJson.setTextUnitDTOs(ImmutableList.of(textUnitDTO)); | ||
byte[] expectedBytes = objectMapper.writeValueAsBytes(textUnitDTOsCacheBlobStorageJson); | ||
Mockito.when( | ||
structuredBlobStorageMock.getBytes(TEXT_UNIT_DTOS_CACHE, "asset/1234/locale/56.smile")) | ||
.thenReturn(Optional.of(expectedBytes)); | ||
textUnitDTOsCacheBlobStorage.structuredBlobStorage = structuredBlobStorageMock; | ||
Optional<ImmutableList<TextUnitDTO>> textUnitDTOS = | ||
textUnitDTOsCacheBlobStorage.getTextUnitsFromCache(1234L, 56L); | ||
Mockito.verify(structuredBlobStorageMock) | ||
.getBytes(TEXT_UNIT_DTOS_CACHE, "asset/1234/locale/56.smile"); | ||
assertEquals(1, textUnitDTOS.get().size()); | ||
assertEquals("testComment", textUnitDTOS.get().get(0).getComment()); | ||
assertEquals(1L, textUnitDTOS.get().get(0).getTmTextUnitId().longValue()); | ||
assertEquals("testName", textUnitDTOS.get().get(0).getName()); | ||
assertEquals("testSource", textUnitDTOS.get().get(0).getSource()); | ||
} | ||
} |