ViewResponse getMap()
You encouraged the community to provide feedback, so...here you go.
I am using the Couchbase Java SDK 1.1-dp. I wanted to get a list of keys back that match certain criteria, so I had to use a View. I issue the query, request no document be returned (just want the keys) and the ViewResponse returns with the expected values. I chose not to use an iterator, but instead chose to create a entry set and loop over that. My question is why the ViewResponse.getMap() throws an exception if there are no documents, but there are valid keys. Example:
// Returns all keys based on the view configuration
ViewResponse response= client.query( view, new Query().setIncludeDocs( false ) );
// error checking...
// Loop over the set
for( java.util.Map.Entry<String,Object> entry : results.getMap().entrySet() )
{
// do something with the keys...
}I would expect this to work, where String == key, and Object == null (no documents). Instead, an exception is thrown:
java.lang.UnsupportedOperationException: This view doesn't containdocuments
Which is not exactly true, it does contain documents, I chose not to return them. Plus there is a space missing in the error message :) Is there a reason to not allow the map to be used like this?
The value would be in being able to use the same processing code whether the query returned documents or not. Simply put, this API is forcing the use of iterators when it could and should return a map where the values might be null.
It's the same with the other "bugs" I have found in the client SDK. The implementation attempts to make decisions for the user, and sometimes those decisions are incorrect.
When I was writing this code, trying to get the the ViewResponse interface correct was quite difficult. There are three different types of ways a view can come back (reduced, with docs, and without docs). We decided that the best way to handle these three different result sets was to just throw an exception if you tried to access data that the view didn't return. I am open to alternatives if you have any suggestions. Below I will address some of your comments.
>> java.lang.UnsupportedOperationException: This view doesn't containdocuments
This error message could probably be a little bit more clear. Maybe "This view result doesn't contain documents"?
>> Is there a reason to not allow the map to be used like this?
What value would you gain from accessing the map if you are doing so only for it's keys? This operation can be done by simply by iterating the result. One reason I can think in favor of accessing the map when no documents exist in the result is consistency, but this comes at the cost of potential programming errors from assuming that a result came back as null.