Add ability to store and retrieve a list of MapBuffer

Summary:
Add ability to store and retrieve a list of MapBuffer as an entry of MapBuffer.

```
Example:

MapBuffer1
{
 key1: "test string",
 key2: MapBuffer2,
 key3: [MapBuffer3, MapBuffer4]
}
```

Changelog:
[General][Added] Add ability to store and retrieve a list of MapBuffer

Reviewed By: JoshuaGross

Differential Revision: D38460204

fbshipit-source-id: 3e721418be2dca6d5f15f665753844d6f531e31c
This commit is contained in:
Lulu Wu
2022-08-11 10:14:38 -07:00
committed by Facebook GitHub Bot
parent 7e580b97bf
commit fc065151ce
8 changed files with 123 additions and 0 deletions
@@ -126,6 +126,16 @@ interface MapBuffer : Iterable<MapBuffer.Entry> {
*/
fun getMapBuffer(key: Int): MapBuffer
/**
* Provides parsed [List<MapBuffer>] value if the entry for given key exists with [DataType.MAP]
* type
* @param key key to lookup [List<MapBuffer>] value for
* @return value associated with the requested key
* @throws IllegalArgumentException if the key doesn't exist
* @throws IllegalStateException if the data type doesn't match
*/
fun getMapBufferList(key: Int): List<MapBuffer>
/** Iterable entry representing parsed MapBuffer values */
interface Entry {
/**
@@ -137,6 +137,24 @@ class ReadableMapBuffer : MapBuffer {
return ReadableMapBuffer(ByteBuffer.wrap(newBuffer))
}
private fun readMapBufferListValue(position: Int): List<ReadableMapBuffer> {
val readMapBufferList = arrayListOf<ReadableMapBuffer>()
var offset = offsetForDynamicData + buffer.getInt(position)
val sizeMapBufferList = buffer.getInt(offset)
offset += Int.SIZE_BYTES
var curLen = 0
while (curLen < sizeMapBufferList) {
val sizeMapBuffer = buffer.getInt(offset + curLen)
val newMapBuffer = ByteArray(sizeMapBuffer)
curLen = curLen + Int.SIZE_BYTES
buffer.position(offset + curLen)
buffer[newMapBuffer, 0, sizeMapBuffer]
readMapBufferList.add(ReadableMapBuffer(ByteBuffer.wrap(newMapBuffer)))
curLen = curLen + sizeMapBuffer
}
return readMapBufferList
}
private fun getKeyOffsetForBucketIndex(bucketIndex: Int): Int {
return HEADER_SIZE + BUCKET_SIZE * bucketIndex
}
@@ -172,6 +190,9 @@ class ReadableMapBuffer : MapBuffer {
override fun getMapBuffer(key: Int): ReadableMapBuffer =
readMapBufferValue(getTypedValueOffsetForKey(key, MapBuffer.DataType.MAP))
override fun getMapBufferList(key: Int): List<ReadableMapBuffer> =
readMapBufferListValue(getTypedValueOffsetForKey(key, MapBuffer.DataType.MAP))
override fun hashCode(): Int {
buffer.rewind()
return buffer.hashCode()
@@ -106,6 +106,8 @@ class WritableMapBuffer : MapBuffer {
override fun getMapBuffer(key: Int): MapBuffer = verifyValue(key, values.get(key))
override fun getMapBufferList(key: Int): List<MapBuffer> = verifyValue(key, values.get(key))
/** Generalizes verification of the value types based on the requested type. */
private inline fun <reified T> verifyValue(key: Int, value: Any?): T {
require(value != null) { "Key not found: $key" }