Friday, January 21, 2011

Lesson #2: When using ViewNavigator, disable autoUpdate

If you cannot follow the advice of Lesson #1 and get the following error:

Notes error: Entry not found in index

 

You are in luck, for here is the answer to your woes.

If you are navigating through a Notes View using a ViewNavigator on a remote database and get that error there is one usual culprit: the view has auto-update turned on. There is a simple fix luckily. Simply disable autoupdate on that view before you loop through the navigator, and then re-enable it when you are done. Here is an example:

View view = database.getView("yourview");
ViewNavigator nav = view.createViewNavigator();
ViewEntry entry = nav.getFirst();

view.setAutoUpdate(false);

while(entry!=null){
 //do stuff with the entry
 ViewEntry temp = entry;
 entry = nav.getNext(entry);
 temp.recycle();
}

view.setAutoUpdate(true);

Voila! No more error.

Lesson #1: DocumentCollections all the time

An important lesson I have recently learned is that if you are processing a lot of documents with a java agent, do not use a View or ViewNavigator. For some reason which I do not understand, getting all of the documents in a view by iterating through a ViewNavigator requires a large amount of memory or there is some sort of leak. Therefore, just do:
View view = database.getView("SomeView");
DocumentCollection docs = database.search(view.getSelectionFormula());
Document doc;
int n = 0;
while((doc=docs.getNthDocument(++n))!=null){
 //Do stuff with document
 doc.save(true,false);
 doc.recycle();
}
and voila you are rolling through your (massive) amount of documents, no warnings coming from the server about using too much memory.