从 getAllEntries 限制 NotesViewEntryCollection 的最佳实践

Best Practice to limit NotesViewEntryCollection from getAllEntries

我想创建更好的 Web 服务,以分页显示 NotesView 中的集合。

而且我发现 View.getAllEntries 从更大的角度来看存在一些性能问题。

在 MongoDB,我可以将 findAll() 与 skip() 和 limit() 一起使用。

我怎样才能在 Domino 上做到这一点?

使用ViewNavigator class。如果是翻阅大视图,比view.getAllEntries()快多了。

您可以使用 view.createViewNav() 或类似方法获取 ViewNavigator 的实例。为获得最佳性能,请在获取导航器之前调用 view.setAutoUpdate(false)

您可以通过搜索网络找到更多信息。 This article 看起来是个不错的起点。

下面这个函数从视图中获取所有 AllEntries,输出结果为 JSON 对象。请尝试以下操作,如果有效请告诉我。

private String consultView(View view, int counter,int position) throws Exception{
    String strValue = "";
    ViewNavigator nav;
    int count = 0;
    view.setAutoUpdate(false);
    nav = view.createViewNav();
    nav.setEntryOptions(ViewNavigator.VN_ENTRYOPT_NOCOUNTDATA);
    nav.setBufferMaxEntries(400);
    int limit = counter;
    int skippedEntries = nav.skip(position);
    String number = "";
    int inde = 111;
    if (skippedEntries == position) {
        Map<Integer, String> columnNameMap = new HashMap<Integer, String>();
        for (ViewColumn col : (List<ViewColumn>) view.getColumns()) {
            if (col.getColumnValuesIndex() < 65535 && Utilisties.containsVar(viewObject.getRetCols(), col.getItemName())) {
                columnNameMap.put(col.getColumnValuesIndex(), col.getItemName());
            }
        }
        List nodeData = new ArrayList();
        ViewEntry entry = nav.getCurrent();
        while (entry != null && count <= (limit - 1)) {
            if (!entry.isCategory()) {
                try {
                    HashMap<String, Object> entryMap = new HashMap<String, Object>();
                    count++;
                    List<Object> columnValues = entry.getColumnValues();
                    entryMap.put("unid", entry.getUniversalID());
                    entryMap.put("position", entry.getPosition('.'));
                    entryMap.put("pos",  entry.getPosition('.'));
                    entryMap.put("userpos", count);
                    for (Integer index : columnNameMap.keySet())
                        entryMap.put(columnNameMap.get(index).toString(),columnValues.get(index));

                    nodeData.add(entryMap);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            ViewEntry tmpentry = nav.getNext(entry);
            entry.recycle();
            entry = tmpentry;
        }
        JsonJavaObject returnJSON = new JsonJavaObject();
        returnJSON.put("errorcode", 0);
        returnJSON.put("errormessage", "");
        if(viewObject.getGetCount())
                returnJSON.put("total",getViewCount(view));

        returnJSON.put("data", nodeData);
        strValue = returnJSON.toString();
    }
    nav.recycle();
    view.recycle();

    return strValue;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import com.ibm.commons.util.io.json.JsonJavaObject;
    import lotus.domino.NotesException;
    import lotus.domino.View;
    import lotus.domino.ViewColumn;
    import lotus.domino.ViewEntryCollection;
    import lotus.domino.ViewNavigator;
    import lotus.domino.ViewEntry;

     private String consultView(View view, int counter,int position) throws Exception{
        String strValue = "";
        ViewNavigator nav;
        int count = 0;
        view.setAutoUpdate(false);
        nav = view.createViewNav();
        nav.setEntryOptions(ViewNavigator.VN_ENTRYOPT_NOCOUNTDATA);
        nav.setBufferMaxEntries(400);
        int limit = counter;
        int skippedEntries = nav.skip(position);
        String number = "";

        if (skippedEntries == position) {
            Map<Integer, String> columnNameMap = new HashMap<Integer, String>();
            for (ViewColumn col : (List<ViewColumn>) view.getColumns()) {
                if (col.getColumnValuesIndex() < 65535) {
                    columnNameMap.put(col.getColumnValuesIndex(), col.getItemName());
                }
            }
            List nodeData = new ArrayList();
            ViewEntry entry = nav.getCurrent();
            while (entry != null && count <= (limit - 1)) {
                if (!entry.isCategory()) {
                    try {
                        HashMap<String, Object> entryMap = new HashMap<String, Object>();
                        count++;
                        List<Object> columnValues = entry.getColumnValues();
                        entryMap.put("unid", entry.getUniversalID());
                        entryMap.put("position", entry.getPosition('.'));
                        entryMap.put("pos",  entry.getPosition('.'));
                        entryMap.put("userpos", count);
                        for (Integer index : columnNameMap.keySet())
                            entryMap.put(columnNameMap.get(index).toString(),columnValues.get(index));

                        nodeData.add(entryMap);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                ViewEntry tmpentry = nav.getNext(entry);
                entry.recycle();
                entry = tmpentry;
            }
            JsonJavaObject returnJSON = new JsonJavaObject();
            returnJSON.put("errorcode", 0);
            returnJSON.put("errormessage", "");

                    returnJSON.put("total",getViewCount(view));

            returnJSON.put("data", nodeData);
            strValue = returnJSON.toString();
        }
        nav.recycle();
        view.recycle();

        return strValue;
}

private int getViewCount(View view) throws NotesException {
    int count = 0;
    ViewEntryCollection entryCollection = view.getAllEntries();
    count = entryCollection.getCount();
    entryCollection.recycle();
    return count;
}
}