Hi!

I'm trying to use MVStore to keep my custom object. It works fine for the 
first time, but when I try to reopen my database, I get error like:

Caused by: java.lang.IllegalArgumentException: Could not deserialize [-84, -
19, .. [1.4.191/0]
 at org.h2.mvstore.DataUtils.newIllegalArgumentException(DataUtils.java:736)
 at org.h2.mvstore.type.ObjectDataType.deserialize(ObjectDataType.java:372)
 at org.h2.mvstore.type.ObjectDataType$SerializedObjectType.read(
ObjectDataType.java:1547)
 at org.h2.mvstore.type.ObjectDataType.read(ObjectDataType.java:232)
 at org.h2.mvstore.type.ObjectDataType.read(ObjectDataType.java:115)
 at org.h2.mvstore.Page.read(Page.java:711)
 at org.h2.mvstore.Page.read(Page.java:195)
 at org.h2.mvstore.MVStore.readPage(MVStore.java:1939)
 at org.h2.mvstore.MVMap.readPage(MVMap.java:736)
 at org.h2.mvstore.Page.getChildPage(Page.java:217)
 at org.h2.mvstore.MVMap.put(MVMap.java:195)
 at org.h2.mvstore.MVMap.put(MVMap.java:121)


The simple test case, which works for the first time, but failes on the 
second one,  looks like:

val db = new MVStore.Builder().fileName("/tmp/mvstoretest.dat").open()

forAll(paymentGenerator, Gen.posNum[Long], Gen.posNum[Int]) { (tx: 
PaymentTransaction,
                                                               balance: Long,
                                                               lastRowHeight: 
Int) =>

  val map: MVMap[Int, Row] = db.openMap("map", new MVMap.Builder[Int, Row])
  val row = Row(AccState(balance), Seq(tx), lastRowHeight)
  map.put(lastRowHeight, row)
  map.get(lastRowHeight) shouldBe row
}
db.commit()



Object class is: 

case class Row(state: AccState, reason: Reason, lastRowHeight: Int) extends 
DataType {

 lazy val bytes: Array[Byte] = Ints.toByteArray(lastRowHeight) ++
 Longs.toByteArray(state.balance) ++
 Ints.toByteArray(reason.length) ++
 reason.foldLeft(Array.empty: Array[Byte]) { (b, scr) =>
 b ++ Ints.toByteArray(scr.bytes.length) ++ scr.bytes
 }

 override def compare(a: scala.Any, b: scala.Any): Int = (a, b) match {
 case (o1: Row, o2: Row) => BigInt(o1.bytes).compare(BigInt(o2.bytes))
 case _ => 1
 }

 override def write(buff: WriteBuffer, obj: scala.Any): Unit = {
 buff.put(obj.asInstanceOf[Row].bytes)
 }

 override def write(buff: WriteBuffer, obj: Array[AnyRef], len: Int, key: 
Boolean): Unit =
 obj.foreach(o => write(buff, o))

 override def read(buff: ByteBuffer): AnyRef = Row.deserialize(buff)

 override def read(buff: ByteBuffer, obj: Array[AnyRef], len: Int, key: 
Boolean): Unit = {
 (0 until len) foreach { i =>
 obj(i) = read(buff);
 }
 }

 override def getMemory(obj: scala.Any): Int = bytes.length
}

object Row {
 def deserialize(bytes: Array[Byte]): Row = {
 val b = ByteBuffer.allocate(bytes.length)
 b.put(bytes)
 b.flip()
 deserialize(b)
 }


 def deserialize(b: ByteBuffer): Row = {
 val lrh = b.getInt
 val accBalance = b.getLong
 val reasonLength = b.getInt()
 val reason: Seq[StateChangeReason] = (0 until reasonLength) map { i =>
 val txSize = b.getInt
 val tx = new Array[Byte](txSize)
 b.get(tx)
 if (txSize == 8) FeesStateChange(Longs.fromByteArray(tx))
 else LagonakiTransaction.parse(tx).get //todo: .get w/out catching
 }
 Row(AccState(accBalance), reason, lrh)
 }
}

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.

Reply via email to