0
votes

From traditional Notes development we learned that retrieving Domino objects like databases and views in script was not effective, and should be avoided in loops.

In XPages we cannot serialize Domino objects and often we retrieve the same object many times. We have an example where we are retrieving project data, based on project number stored in project related documents. The bean is scoped to applicationScope and results are cached.

public class Projects{ 
      private TreeMap<String, Project> projectList; 

      public Projects() { 

      } 

      public Project getProjectInfo(String projNum) { 
              Project project = null; 
              if (projectList==null) { 
                      projectList = new TreeMap<String,Project>(); 
              } 

              if (projectList.containsKey(projNum)) { 
                      project = projectList.get(projNum); 
              } else { 
                      try { 
                              Database projDb = DominoAccess.getDatabase("projects"); 
                              View v = projDb.getView("(projLookup)"); 
                              ViewEntry ve = v.getEntryByKey(projNum); 
                              if (ve != null) { 
                                      project = new Project(ve); 

                                      projectList.put(projNum, project); 
                              } 
                      } catch (Exception e) { 
                      } 
              } 

              return project; 
      } 
}

When this is first used in for example a repeat, the database and view objects are created for each document. Is this best practice or are there better ways of solving this?

I know we can put all projects in the Map at first use, but also not sure if this is best practice regarding memory?

1
Just few breadcrumbs to look for: your bean can implement Map interface and most of the logic goes to get() method; use lazy initialization and/or WeakHashMap internaly to conserve memory.Frantisek Kossuth

1 Answers

0
votes

When you use that in a repeat control (e.g. 30 times), you use 30x a @DBLookup. That doesn't seem very efficient. What you could do: use a view navigator to rapidly read all projects with id and unid into a map. Then instead of doing a @DBLookup you get the UNID from the map and do a getDocumentByUNID to load it. That should be faster. Another variation - if your project data isn't too big: save a JSON representation into a field and put that into the view navigator. This way you only need to read a view (again you have the 2 variation of sequentially read it or search it through) and a field.

Let me know if I need to clarify further