couchbase lite - 重新打开数据库后检索文档属性会产生不同的结果

时间:2017-04-10 05:26:42

标签: scala couchbase-lite

重新打开数据库后检索文档属性会产生不同的结果。请参阅下面三次打印的相同文档作为示例。

关键代码段(完整代码here) - 您可能希望首先看到主要方法:

class QuoteInfoDb(language: Language) {
  val log = LoggerFactory.getLogger(getClass.getName)
  var quoteDb: Database = null
  var annotationDb: Database = null
  var dbManager: Manager = null
  def openDatabases() = {
    dbManager =  new Manager(new JavaContext("data") {
      override def getRootDirectory: File = {
        val rootDirectoryPath = "/home/vvasuki/subhAShita-db-sanskrit"
        new File(rootDirectoryPath)
      }
    }, Manager.DEFAULT_OPTIONS)
    dbManager.setStorageType("ForestDB")
    quoteDb = dbManager.getDatabase(s"quote_db__${language.code}")
    annotationDb = dbManager.getDatabase(s"annotation_db__${language.code}")
  }

  def closeDatabases = {
    quoteDb.close()
    annotationDb.close()
  }


  def updateDocument(document: Document, jsonMap: Map[String,Object]) = {
    document.update(new Document.DocumentUpdater() {
      override def update(newRevision: UnsavedRevision): Boolean = {
        val properties = newRevision.getUserProperties
        properties.putAll(jsonMap.asJava)
        newRevision.setUserProperties(properties)
        true
      }
    })
  }
  def testQuoteWrite() = {
    val jsonMap =Map("scriptRenderings"-> List(Map("text"-> "दण्डः शास्ति प्रजाः सर्वाः दण्ड एवाभिरक्षति। दण्डः सुप्तेषु जागर्ति दण्डं धर्मं विदुर्बुधाः।।", "scheme" -> "dev", "startLetter" -> "द")),
      "jsonClass"->"QuoteText",
      "language"->Map("code" -> "sa"),
      "key"->"damDaHshaastiprajaaHsarvaaHdamDaevaabhiraxatidamDaHsupteShujaagartidamDamdharmamvidurbudhaaH"
    )
    val document = quoteDb.getDocument(jsonMap("key").toString)
    updateDocument(document, jsonMap)
  }

  def testQuoteRetrieval() = {
    val id = "damDaHshaastiprajaaHsarvaaHdamDaevaabhiraxatidamDaHsupteShujaagartidamDamdharmamvidurbudhaaH"
    val doc = quoteDb.getDocument(id)
    val jsonMap = doc.getUserProperties
    log debug jsonMap.toString
  }
}

object dbMakerSanskrit {
  val log = LoggerFactory.getLogger(getClass.getName)
  val quoteInfoDb = new QuoteInfoDb(language = Language("sa"))

  def main(args: Array[String]): Unit = {
    quoteInfoDb.openDatabases()
    quoteInfoDb.testQuoteWrite()
    quoteInfoDb.testQuoteRetrieval()
    quoteInfoDb.closeDatabases
    quoteInfoDb.openDatabases()
    quoteInfoDb.testQuoteRetrieval()
    quoteInfoDb.testQuoteRetrieval()
    // quoteInfoDb.checkConflicts
//    quoteInfoDb.exportToTsv
//    log info s"Updated records ${vishvasaPriyaSamskritaPadyani.take(1).map(quoteInfoDb.addQuoteWithInfo(_)).sum} from vishvasaPriyaSamskritaPadyani"
  }
}

产生以下输出:

22:29:25 subhAShitaDb.QuoteInfoDb 130 --- {metre={defined=false, empty=true}, scriptRenderings=List(Map(text -> दण्डः शास्ति प्रजाः सर्वाः दण्ड एवाभिरक्षति। दण्डः सुप्तेषु जागर्ति दण्डं धर्मं विदुर्बुधाः।।, scheme -> dev, startLetter -> द)), jsonClass=QuoteText, language=Map(code -> sa), key=damDaHshaastiprajaaHsarvaaHdamDaevaabhiraxatidamDaHsupteShujaagartidamDamdharmamvidurbudhaaH}
22:29:25 subhAShitaDb.QuoteInfoDb 130 --- {metre={defined=false, empty=true}, scriptRenderings={empty=false, traversableAgain=true}, jsonClass=QuoteText, language={traversableAgain=true, empty=false}, key=damDaHshaastiprajaaHsarvaaHdamDaevaabhiraxatidamDaHsupteShujaagartidamDamdharmamvidurbudhaaH}
22:29:25 subhAShitaDb.QuoteInfoDb 130 --- {metre={defined=false, empty=true}, scriptRenderings={empty=false, traversableAgain=true}, jsonClass=QuoteText, language={traversableAgain=true, empty=false}, key=damDaHshaastiprajaaHsarvaaHdamDaevaabhiraxatidamDaHsupteShujaagartidamDamdharmamvidurbudhaaH}

如何确保每次都得到与第一次相似的结果?

1 个答案:

答案 0 :(得分:0)

我得到了帮助here。在写入数据库时​​,我能够通过递归将scala映射转换为java映射(使用此tip)来修复代码。