wvlet.airframe.tablet.obj.ObjectTabletReader.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of airframe-tablet_2.13.0-RC2 Show documentation
Show all versions of airframe-tablet_2.13.0-RC2 Show documentation
Data format conversion library
The newest version!
package wvlet.airframe.tablet.obj
import wvlet.airframe.codec.{MessageCodec, MessageCodecFactory}
import wvlet.airframe.msgpack.spi.MessagePack
import wvlet.airframe.surface.Surface
import wvlet.airframe.tablet.{MessagePackRecord, Record, TabletReader}
import wvlet.log.LogSupport
import scala.reflect.runtime.{universe => ru}
/**
*
*/
class ObjectTabletReader[A](elementCodec: MessageCodec[A], input: Seq[A]) extends TabletReader with LogSupport {
private val cursor = input.iterator
def read: Option[Record] = {
if (!cursor.hasNext) {
None
} else {
val elem = cursor.next()
val packer = MessagePack.newBufferPacker
if (elem == null) {
packer.packNil
} else {
elementCodec.pack(packer, elem)
}
Some(MessagePackRecord(packer.toByteArray))
}
}
}
object ObjectTabletReader {
def newTabletReader[A](seq: Seq[A], surface: Surface, codec: Map[Surface, MessageCodec[_]] = Map.empty) = {
val elementCodec = MessageCodecFactory.defaultFactory.withCodecs(codec).of(surface)
new ObjectTabletReader[A](elementCodec.asInstanceOf[MessageCodec[A]], seq)
}
def newTabletReaderOf[A: ru.TypeTag](seq: Seq[A], codec: Map[Surface, MessageCodec[_]] = Map.empty) = {
val elementCodec = MessageCodecFactory.defaultFactory.withCodecs(codec).of[A]
new ObjectTabletReader[A](elementCodec, seq)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy