deephacks / lmdbjni

LMDB for Java
Apache License 2.0
204 stars 28 forks source link

Do I need to set comparator with each transaction of nested calls ? #88

Closed unoexperto closed 7 years ago

unoexperto commented 7 years ago

Let's say I have

1) merge method that schematically looks like below. 2) two databases. One with actual data and another with metadata.

Schematically code looks like this

def getObject(id: Int, txnParent: Transaction): ValueType = {
  val txn = env.createTransaction(txnParent, true)
  dbRaw.setComparator(txn, rawComparator)
  ....
}

def deleteObject(id: Int, txnParent: Transaction): Unit = {
  val txn = env.createTransaction(txnParent)
  dbRaw.setComparator(txn, rawComparator)
  ....
}

def saveObject(id: Int, value: ValueType, txnParent: Transaction): Unit = {
  val txn = env.createTransaction(txnParent)
  dbRaw.setComparator(txn, rawComparator)
  ....
}

def getMeta(id: Int, txnParent: Transaction): MetaType = {
  val txn = env.createTransaction(txnParent, true)
  dbMeta.setComparator(txn, metaComparator)
  ....
}

def deleteMeta(id: Int, txnParent: Transaction): Unit = {
  val txn = env.createTransaction(txnParent)
  dbMeta.setComparator(txn, metaComparator)
  ....
}

def saveMeta(id: Int, value: ValueType, txnParent: Transaction): Unit = {
  val txn = env.createTransaction(txnParent)
  dbMeta.setComparator(txn, metaComparator)
  ....
}

def mergeObjects(idOld: Int, idTarget: Int): Unit = {
 val txn = env.createWriteTransaction()
 dbMeta.setComparator(txn, metaComparator)
 dbRaw.setComparator(txn, rawComparator)

 val objOld = getObject(idOld, txn)
 val objTarget = getObject(idTarget, txn)
 val metaOld = getMeta(idOld, txn)
 val metaTarget = getMeta(idTarget, txn)

 deleteObject(idOld, txn)
 deleteMeta(idOld, txn)
 // do something with objects and save merged ones
 saveObject(idTarget, mergedObj, txn)
 saveMeta(idTarget, mergedMeta, txn)

 txn.close()
}

I have two questions

1) Do I need to set comparators in nested calls ? 2) Can I share parent transaction accross databases ?

Thanks!

krisskross commented 7 years ago

1) Have not tested but is suspect that the comparator is valid for a child tx also. Give it a try or read the LMDB docs. 2) Yes.