Skip to content

Commit

Permalink
RUM-4098: Remove update date from tlv type
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmos committed Jun 2, 2024
1 parent f0ceab6 commit 459dd96
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 75 deletions.
2 changes: 1 addition & 1 deletion dd-sdk-android-core/api/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ interface com.datadog.android.core.persistence.datastore.DataStoreCallback<T: An
fun onFailure()
fun onNoData()
data class com.datadog.android.core.persistence.datastore.DataStoreContent<T: Any>
constructor(Long, Int, T?)
constructor(Int, T?)
interface com.datadog.android.core.persistence.datastore.DataStoreHandler
fun <T: Any> setValue(String, T, Int = 0, com.datadog.android.core.persistence.Serializer<T>)
fun <T: Any> value(String, Int = 0, DataStoreCallback<T>, com.datadog.android.core.internal.persistence.Deserializer<String, T>)
Expand Down
12 changes: 5 additions & 7 deletions dd-sdk-android-core/api/dd-sdk-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -790,15 +790,13 @@ public abstract interface class com/datadog/android/core/persistence/datastore/D
}

public final class com/datadog/android/core/persistence/datastore/DataStoreContent {
public fun <init> (JILjava/lang/Object;)V
public final fun component1 ()J
public final fun component2 ()I
public final fun component3 ()Ljava/lang/Object;
public final fun copy (JILjava/lang/Object;)Lcom/datadog/android/core/persistence/datastore/DataStoreContent;
public static synthetic fun copy$default (Lcom/datadog/android/core/persistence/datastore/DataStoreContent;JILjava/lang/Object;ILjava/lang/Object;)Lcom/datadog/android/core/persistence/datastore/DataStoreContent;
public fun <init> (ILjava/lang/Object;)V
public final fun component1 ()I
public final fun component2 ()Ljava/lang/Object;
public final fun copy (ILjava/lang/Object;)Lcom/datadog/android/core/persistence/datastore/DataStoreContent;
public static synthetic fun copy$default (Lcom/datadog/android/core/persistence/datastore/DataStoreContent;ILjava/lang/Object;ILjava/lang/Object;)Lcom/datadog/android/core/persistence/datastore/DataStoreContent;
public fun equals (Ljava/lang/Object;)Z
public final fun getData ()Ljava/lang/Object;
public final fun getLastUpdateDate ()J
public final fun getVersionCode ()I
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.annotation.WorkerThread
import com.datadog.android.api.InternalLogger
import com.datadog.android.core.internal.persistence.Deserializer
import com.datadog.android.core.internal.persistence.datastore.ext.toInt
import com.datadog.android.core.internal.persistence.datastore.ext.toLong
import com.datadog.android.core.internal.persistence.file.existsSafe
import com.datadog.android.core.internal.persistence.tlvformat.TLVBlock
import com.datadog.android.core.internal.persistence.tlvformat.TLVBlockFileReader
Expand Down Expand Up @@ -89,19 +88,17 @@ internal class DatastoreFileReader(
deserializer: Deserializer<String, T>,
tlvBlocks: List<TLVBlock>
): DataStoreContent<T>? {
if (tlvBlocks[0].type != TLVBlockType.LAST_UPDATE_DATE &&
tlvBlocks[1].type != TLVBlockType.VERSION_CODE
if (tlvBlocks[0].type != TLVBlockType.VERSION_CODE &&
tlvBlocks[1].type != TLVBlockType.DATA
) {
logBlocksInUnexpectedBlocksOrderError()
return null
}

val lastUpdateBlock = tlvBlocks[0]
val versionCodeBlock = tlvBlocks[1]
val dataBlock = tlvBlocks[2]
val versionCodeBlock = tlvBlocks[0]
val dataBlock = tlvBlocks[1]

return DataStoreContent(
lastUpdateDate = lastUpdateBlock.data.toLong(),
versionCode = versionCodeBlock.data.toInt(),
data = deserializer.deserialize(String(dataBlock.data))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ internal class DatastoreFileWriter(
key = key
)

val lastUpdateBlock = getLastUpdateDateBlock()
val versionCodeBlock = getVersionCodeBlock(version)
val dataBlock = getDataBlock(data, serializer)

// failed to serialize one or more blocks
if (lastUpdateBlock == null || versionCodeBlock == null || dataBlock == null) {
if (versionCodeBlock == null || dataBlock == null) {
return
}

val dataToWrite = listOf(lastUpdateBlock, versionCodeBlock, dataBlock).join(
val dataToWrite = listOf(versionCodeBlock, dataBlock).join(
separator = byteArrayOf(),
internalLogger = internalLogger
)
Expand Down Expand Up @@ -102,18 +101,6 @@ internal class DatastoreFileWriter(
return dataBlock.serialize()
}

private fun getLastUpdateDateBlock(): ByteArray? {
val now = System.currentTimeMillis()
val lastUpdateDateByteArray = now.toByteArray()
val lastUpdateDateBlock = TLVBlock(
type = TLVBlockType.LAST_UPDATE_DATE,
data = lastUpdateDateByteArray,
internalLogger = internalLogger
)

return lastUpdateDateBlock.serialize()
}

private fun getVersionCodeBlock(version: Int): ByteArray? {
val versionCodeByteArray = version.toByteArray()
val versionBlock = TLVBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
package com.datadog.android.core.internal.persistence.tlvformat

internal enum class TLVBlockType(val rawValue: UShort) {
LAST_UPDATE_DATE(0x00u),
VERSION_CODE(0x01u),
DATA(0x02u);
VERSION_CODE(0x00u),
DATA(0x01u);

companion object {
private val map = values().associateBy { it.rawValue }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface DataStoreCallback<T : Any> {
/**
* Called on successfully fetching data from the datastore.
*
* @param dataStoreContent contains the datastore data, version and lastUpdateDate.
* @param dataStoreContent contains the datastore data and version.
*/
fun onSuccess(dataStoreContent: DataStoreContent<T>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ package com.datadog.android.core.persistence.datastore
* Datastore entry contents and metadata.
*
* @param T type of data used by this entry in the datastore.
* @property lastUpdateDate date when the entry was written.
* @property versionCode version used by the entry.
* @property data content of the entry.
*/
data class DataStoreContent<T : Any>(
val lastUpdateDate: Long,
val versionCode: Int,
val data: T?
)
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ internal class DataStoreFileReaderTest {

private lateinit var fakeDataBytes: ByteArray
private lateinit var versionBlock: TLVBlock
private lateinit var lastUpdateDateBlock: TLVBlock
private lateinit var dataBlock: TLVBlock
private lateinit var blocksReturned: ArrayList<TLVBlock>

Expand All @@ -102,10 +101,9 @@ internal class DataStoreFileReaderTest {
whenever(mockDataStoreFile.existsSafe(mockInternalLogger)).thenReturn(true)
whenever(mockDeserializer.deserialize(fakeDataString)).thenReturn(fakeDataBytes)

lastUpdateDateBlock = createLastUpdateDateBlock()
versionBlock = createVersionBlock(true)
dataBlock = createDataBlock()
blocksReturned = arrayListOf(lastUpdateDateBlock, versionBlock, dataBlock)
blocksReturned = arrayListOf(versionBlock, dataBlock)
whenever(mockTLVBlockFileReader.read(mockDataStoreFile)).thenReturn(blocksReturned)

testedDatastoreFileReader = DatastoreFileReader(
Expand Down Expand Up @@ -218,7 +216,6 @@ internal class DataStoreFileReaderTest {
fun `M return deserialized data W read()`() {
// Given
blocksReturned.clear()
blocksReturned.add(createLastUpdateDateBlock())
blocksReturned.add(createVersionBlock(true))
blocksReturned.add(createDataBlock())

Expand Down Expand Up @@ -281,7 +278,7 @@ internal class DataStoreFileReaderTest {
@Test
fun `M log unexpectedBlocksOrder error W read() { unexpected block order }`() {
// Given
blocksReturned = arrayListOf(versionBlock, lastUpdateDateBlock, dataBlock)
blocksReturned = arrayListOf(dataBlock, versionBlock)
whenever(mockTLVBlockFileReader.read(mockDataStoreFile)).thenReturn(blocksReturned)

// When
Expand Down Expand Up @@ -347,15 +344,6 @@ internal class DataStoreFileReaderTest {
}
}

private fun createLastUpdateDateBlock(): TLVBlock =
TLVBlock(
type = TLVBlockType.LAST_UPDATE_DATE,
data = ByteBuffer.allocate(Long.SIZE_BYTES)
.putLong(System.currentTimeMillis())
.array(),
internalLogger = mockInternalLogger
)

private fun createDataBlock(dataBytes: ByteArray = fakeDataBytes): TLVBlock =
TLVBlock(
type = TLVBlockType.DATA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package com.datadog.android.core.internal.persistence.tlvformat

import com.datadog.android.api.InternalLogger
import com.datadog.android.core.internal.persistence.datastore.ext.toByteArray
import com.datadog.android.core.internal.persistence.file.FileReaderWriter
import com.datadog.android.core.internal.persistence.tlvformat.TLVBlockFileReader.Companion.FAILED_TO_DESERIALIZE_ERROR
import com.datadog.android.utils.forge.Configurator
Expand Down Expand Up @@ -54,17 +53,15 @@ internal class TLVBlockFileReaderTest {
@StringForgery(regex = "^(\\w{3})\$") // a minimal number of chars to avoid flakiness
private lateinit var fakeDataString: String

private lateinit var fakeUpdateBytes: ByteArray
private lateinit var fakeVersionBytes: ByteArray
private lateinit var fakeDataBytes: ByteArray
private lateinit var fakeBufferBytes: ByteArray

@BeforeEach
fun setup(@IntForgery(min = 0, max = 10) fakeVersion: Int) {
val lastUpdateBytes = createLastUpdateBytes()
val versionBytes = createVersionBytes(fakeVersion)
val dataBytes = createDataBytes()
val dataToWrite = lastUpdateBytes + versionBytes + dataBytes
val dataToWrite = versionBytes + dataBytes

whenever(mockFileReaderWriter.readData(mockFile)).thenReturn(dataToWrite)

Expand Down Expand Up @@ -118,13 +115,10 @@ internal class TLVBlockFileReaderTest {
val tlvArray = testedReader.read(file = mockFile)

// Then
assertThat(tlvArray.size).isEqualTo(3)
val lastUpdateObject = tlvArray[0]
val versionObject = tlvArray[1]
val dataObject = tlvArray[2]
assertThat(tlvArray.size).isEqualTo(2)
val versionObject = tlvArray[0]
val dataObject = tlvArray[1]

assertThat(lastUpdateObject.type).isEqualTo(TLVBlockType.LAST_UPDATE_DATE)
assertThat(lastUpdateObject.data).isEqualTo(fakeUpdateBytes)
assertThat(versionObject.type).isEqualTo(TLVBlockType.VERSION_CODE)
assertThat(versionObject.data).isEqualTo(fakeVersionBytes)
assertThat(dataObject.type).isEqualTo(TLVBlockType.DATA)
Expand Down Expand Up @@ -168,19 +162,6 @@ internal class TLVBlockFileReaderTest {
)
}

private fun createLastUpdateBytes(): ByteArray {
val now = System.currentTimeMillis()
fakeUpdateBytes = now.toByteArray()
val lastUpdateType = TLVBlockType.LAST_UPDATE_DATE.rawValue.toShort()

return ByteBuffer
.allocate(fakeUpdateBytes.size + Int.SIZE_BYTES + Short.SIZE_BYTES)
.putShort(lastUpdateType)
.putInt(fakeUpdateBytes.size)
.put(fakeUpdateBytes)
.array()
}

private fun createVersionBytes(fakeVersion: Int): ByteArray {
val versionType = TLVBlockType.VERSION_CODE.rawValue.toShort()
fakeVersionBytes = ByteBuffer.allocate(Int.SIZE_BYTES).putInt(fakeVersion).array()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ internal class TLVBlockTypeTest {
@Test
fun `M return type value W fromValue() { existing value }`() {
// When
val shortValue = TLVBlockType.fromValue(TLVBlockType.LAST_UPDATE_DATE.rawValue)
val shortValue = TLVBlockType.fromValue(TLVBlockType.VERSION_CODE.rawValue)

// Then
assertThat(shortValue).isEqualTo(TLVBlockType.LAST_UPDATE_DATE)
assertThat(shortValue).isEqualTo(TLVBlockType.VERSION_CODE)
}

@Test
Expand Down

0 comments on commit 459dd96

Please sign in to comment.