代码之家  ›  专栏  ›  技术社区  ›  Ondra Žižka David Lilljegren

OrientDB:将什么合并到什么?

  •  0
  • Ondra Žižka David Lilljegren  · 技术社区  · 6 年前

    我对OrientDB的API文档有点困惑。 气味# merge() 方法是:

    参数:
    -要合并的其他ODocument实例
    iUpdateOnlyMode 多值字段的iMergeSingleItemsOfMultiValueFields -如果为true,则合并多字段字段的单个项(集合、映射、数组等)

    由此,我不知道什么时候会合并成什么。这是一种改善的状态,它 used to be worse

    有人能正常解释什么会合并成什么吗?也许罗恩·基特尔能知道?

    1 回复  |  直到 5 年前
        1
  •  0
  •   Ondra Žižka David Lilljegren    6 年前

    我做了一些测试,似乎这个API的名字有点奇怪或者文档有误。

    我会料到的 doc1.merge(doc2) 合并 doc2 doc1 . 因为您通常在编辑doc1,并且可以连续合并多个内容,例如:

    doc1.merge(partialDocA);
    doc1.merge(partialDocB);
    doc1.merge(partialDocC);
    

    根据 merge()

    partialDocA.merge(doc1, true, false);
    partialDocB.merge(doc1, true, false);
    partialDocC.merge(doc1, true, false);
    

    在哪里 true merge 不删除其余部分 文件1 . 与 false partialDocC . 好吧,那是什么合并?这叫做替换,而不是合并。

    如果是真的,应该命名 mergeFrom(targetDoc, replaceInsteadOfMerge, somethingWithMultiFields)


    所以除非我忽略了什么,从 Map 看起来像:

    public ODocument mergeDocument(Map<String, Object> incomingDocMap)
    {
        String sourceUri = (String) incomingDocMap.get(DocumentAttributes.SOURCE_URI.toString());
        if (null == sourceUri)
            throw new IllegalArgumentException("Document sourceUri is null.");
        String docType = (String) incomingDocMap.get(Crawler.Attributes.TYPE);
        if (null == docType)
            throw new IllegalArgumentException("Document docType is null.");
    
        // Get a document by sourceUri
        String sql = "SELECT * FROM " + docType + " WHERE sourceuri=?";
        activateOnCurrentThread();
        List<ODocument> results = db.command(new OSQLSynchQuery<ODocument>(sql)).execute(sourceUri);
        if (results.size() == 0)
            throw new RuntimeException("No document with sourceUri '"+sourceUri+"'.");
    
        // Update it from the given map.
        ODocument incomingDoc = new ODocument(docType);
        incomingDoc.fromMap(incomingDocMap);
        //ODocument merged = incomingDoc.merge(results.get(0), true, false);
        ODocument merged = results.get(0).merge(incomingDoc, true, false);
        //merged.save(); /// Duplicated key
        return merged;
    }
    

    @return 所以我不知道我是否可以继续使用原来的 ODocument