Friday, August 7, 2009

Test if -specific- content has changed since last FTIndex

In the previous version of this test, I tested against all documents in the database.

The app I made the function for has now started using caching through documents, so the requirements for the test has changed. I now need to know if a subset of the documents have changed. The FTIndex doesn't need to be updated if the cache changes..

The updated version takes a second parameter, a Search-query. This lets you filter out only the content you're interested in.

Function contentChangedSinceLastFTIndex( db As NotesDatabase, Byval query As String ) As Boolean 
Dim session As New NotesSession

Dim lastFTIndexed As Variant
lastFTIndexed = db.LastFTIndexed

'// First test (least resource-hungry)
If Not ( db.LastModified > lastFTIndexed ) Then
contentChangedSinceLastFTIndex = False
Exit Function
End If

'// Conclusive test - test if actual documents (not design elements) have
'// been changed since last FTIndex
Dim dateFTIndexed As New NotesDateTime ( lastFTIndexed )
Dim col As NotesDocumentCollection
If query = "" Then
'// Has any document changed?
Set col = db.GetModifiedDocuments( dateFTIndexed )
Else '// Has documents fitting [query] changed since last FTIndex?

'// Adjust the FTIndexed-date by a second, to avoid getting the
'// last saved document (if the FT-index is up to date)
Call dateFTIndexed.AdjustSecond( 1 )
Set col = db.Search( query, dateFTIndexed, 0 )
End If
If col.Count > 0 Then contentChangedSinceLastFTIndex = True

End Function


Don't worry about speed. In my test-app, the search takes less than a millisecond with a close cutoff date.

Update: Leus' Mobiele wrote a Java version of the above code:
private boolean contentChangedSinceLastFTIndex( Database db, String query) throws NotesException {
DateTime dateFTIndexed = db.getLastFTIndexed();

// First test (least resource-hungry)
if ( db.getLastModified().timeDifference( dateFTIndexed ) <= 0) {
return false;
}

// Conclusive test - test if actual documents (not design elements) have
// been changed since last FTIndex

DocumentCollection col;

if (query.length() == 0) { // Has any document changed?
col = db.getModifiedDocuments( dateFTIndexed );
} else {
// Has documents fitting [query] changed since last FTIndex?
// Adjust the FTIndexed-date by a second, to avoid getting the
// last saved document (if the FT-index is up to date)

dateFTIndexed.adjustSecond(1);
col = db.search( query, dateFTIndexed, 0);
}

if (col.getCount() > 0) {
return true;
} else {
return false;
}
}

2 comments:

Mark Leusink said...

Great tip!

The only problem was I needed it in a Java agent, so here's the Java version:

private boolean contentChangedSinceLastFTIndex( Database db, String query) throws NotesException {

DateTime dateFTIndexed = db.getLastFTIndexed();

// First test (least resource-hungry)
if ( db.getLastModified().timeDifference( dateFTIndexed ) <= 0) {
return false;
}

// Conclusive test - test if actual documents (not design elements) have
// been changed since last FTIndex

DocumentCollection col;

if (query.length() == 0) {
// Has any document changed?
col = db.getModifiedDocuments( dateFTIndexed );

} else {

// Has documents fitting [query] changed since last FTIndex?
// Adjust the FTIndexed-date by a second, to avoid getting the
// last saved document (if the FT-index is up to date)

dateFTIndexed.adjustSecond(1);
col = db.search( query, dateFTIndexed, 0);

}

if (col.getCount() > 0) {
return true;
} else {
return false;
}
}

Tommy Valand said...

Thanks. I'll add the java code to the post..