Gmail IMAP, Searching for optimal method for X most recently active Gmail threads
I am looking for the optimal way to acquire a list of the top X most recently active Gmail threads.  

**Background:**

I am using Java accessing [IMAP with OAuth][1] in a Google Apps for Education domain.  A [Gmail Atom inbox feed][2] is available which can list the last 20 threads containing unread messages.  Access to this feed seems to be very fast much faster than anything I having managed to produce using OAuth/IMAP.

The advantage of using the IMAP approach over the Gmail Atom inbox feed is with IMAP I can access an arbitrary number of messages (not just 20), see read messages, get thread size, get any associated google labels, fetch quota details and check for flags.  Essentially this will give my users a much more Gmail like experience (I only need a read only experience for our portal).  My problem is access is IMAP access is significantly slower than the Atom feed.  Comparison wise the IMAP method takes around 10 seconds whilst the Atom feed is usually returned within 2 seconds.

I am aware and have been working with the [Gmail IMAP Extensions][3] and [Gmail Advanced Search syntax][4] .

**Current Method:**

Imagine I want the top 40 threads from my IMAP inbox.  Currently I download some arbitrary number of messages say (40 * 4), fetching only the *X-GM-THRID*.  I iterate through these messages storing the thread id as I go (fetching more messages if required) until I either exhaust the list of inbox messages or I reach my target number of threads.

I then have a list of Gmail thread ids which I can use perform an IMAP search (with an appropriate FetchProfile.Item's, depending on what I require).

I iterate through the search results producing something like (using the wonderful [Google Guava/Google Collections][5] Multimap):

    Multimap<Long, Message> threadMultiMap = LinkedListMultimap.create();

and this is easily massaged into:

    LinkedHashMap<Long, Message[]> threadMap; 

**Is there a better way than iterating through the INBOX until X distinct message threads have been identified?**

  [1]: http://code.google.com/apis/gmail/oauth/
  [2]: http://code.google.com/apis/gmail/docs/inbox_feed.html
  [3]: http://code.google.com/apis/gmail/imap/
  [4]: http://mail.google.com/support/bin/answer.py?answer=7190
  [5]: http://code.google.com/p/guava-libraries/
Gmail IMAP, Searching for optimal method for X most recently active Gmail threads
===

I am looking for the optimal way to acquire a list of the top X most recently active Gmail threads.  

**Background:**

I am using Java accessing [IMAP with OAuth][1] in a Google Apps for Education domain.  A [Gmail Atom inbox feed][2] is available which can list the last 20 threads containing unread messages.  Access to this feed seems to be very fast much faster than anything I having managed to produce using OAuth/IMAP.

The advantage of using the IMAP approach over the Gmail Atom inbox feed is with IMAP I can access an arbitrary number of messages (not just 20), see read messages, get thread size, get any associated google labels, fetch quota details and check for flags.  Essentially this will give my users a much more Gmail like experience (I only need a read only experience for our portal).  My problem is access is IMAP access is significantly slower than the Atom feed.  Comparison wise the IMAP method takes around 10 seconds whilst the Atom feed is usually returned within 2 seconds.

I am aware and have been working with the [Gmail IMAP Extensions][3] and [Gmail Advanced Search syntax][4] .

**Current Method:**

Imagine I want the top 40 threads from my IMAP inbox.  Currently I download some arbitrary number of messages say (40 * 4), fetching only the *X-GM-THRID*.  I iterate through these messages storing the thread id as I go (fetching more messages if required) until I either exhaust the list of inbox messages or I reach my target number of threads.

I then have a list of Gmail thread ids which I can use perform an IMAP search (with an appropriate FetchProfile.Item's, depending on what I require).

I iterate through the search results producing something like (using the wonderful [Google Guava/Google Collections][5] Multimap):

    Multimap<Long, Message> threadMultiMap = LinkedListMultimap.create();

and this is easily massaged into:

    LinkedHashMap<Long, Message[]> threadMap; 

**Is there a better way than iterating through the INBOX until X distinct message threads have been identified?**

  [1]: http://code.google.com/apis/gmail/oauth/
  [2]: http://code.google.com/apis/gmail/docs/inbox_feed.html
  [3]: http://code.google.com/apis/gmail/imap/
  [4]: http://mail.google.com/support/bin/answer.py?answer=7190
  [5]: http://code.google.com/p/guava-libraries/