use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.
the class ZMailbox method internalSearch.
// ------------------------
private synchronized ZSearchResult internalSearch(String convId, ZSearchParams params, boolean nest) throws ServiceException {
QName name;
if (convId != null) {
name = MailConstants.SEARCH_CONV_REQUEST;
} else if (params.getTypes().equals(ZSearchParams.TYPE_VOICE_MAIL) || params.getTypes().equals(ZSearchParams.TYPE_CALL)) {
name = VoiceConstants.SEARCH_VOICE_REQUEST;
} else if (params.getTypes().equals(ZSearchParams.TYPE_GAL)) {
name = AccountConstants.SEARCH_GAL_REQUEST;
} else {
name = MailConstants.SEARCH_REQUEST;
}
Element req = newRequestElement(name);
if (params.getTypes().equals(ZSearchParams.TYPE_GAL)) {
req.addAttribute(AccountConstants.A_TYPE, GalEntryType.account.name());
req.addElement(AccountConstants.E_NAME).setText(params.getQuery());
//req.addAttribute(MailConstants.A_SORTBY, SearchSortBy.nameAsc.name());
}
req.addAttribute(MailConstants.A_CONV_ID, convId);
if (nest) {
req.addAttribute(MailConstants.A_NEST_MESSAGES, true);
}
if (params.getLimit() != 0) {
req.addAttribute(MailConstants.A_QUERY_LIMIT, params.getLimit());
}
if (params.getOffset() != 0) {
req.addAttribute(MailConstants.A_QUERY_OFFSET, params.getOffset());
}
if (params.getSortBy() != null) {
req.addAttribute(MailConstants.A_SORTBY, params.getSortBy().name());
}
if (params.getTypes() != null) {
req.addAttribute(MailConstants.A_SEARCH_TYPES, params.getTypes());
}
if (params.getFetch() != null && params.getFetch() != Fetch.none) {
// use "1" for "first" for backward compat until DF is updated
req.addAttribute(MailConstants.A_FETCH, params.getFetch() == Fetch.first ? "1" : params.getFetch().name());
}
if (params.getCalExpandInstStart() != 0) {
req.addAttribute(MailConstants.A_CAL_EXPAND_INST_START, params.getCalExpandInstStart());
}
if (params.getCalExpandInstEnd() != 0) {
req.addAttribute(MailConstants.A_CAL_EXPAND_INST_END, params.getCalExpandInstEnd());
}
if (params.isPreferHtml()) {
req.addAttribute(MailConstants.A_WANT_HTML, params.isPreferHtml());
}
if (params.isMarkAsRead()) {
req.addAttribute(MailConstants.A_MARK_READ, params.isMarkAsRead());
}
if (params.isRecipientMode()) {
req.addAttribute(MailConstants.A_RECIPIENTS, params.isRecipientMode());
}
if (params.getField() != null) {
req.addAttribute(MailConstants.A_FIELD, params.getField());
}
if (params.getInDumpster()) {
req.addAttribute(MailConstants.A_IN_DUMPSTER, true);
}
req.addAttribute(MailConstants.E_QUERY, params.getQuery(), Element.Disposition.CONTENT);
if (params.getCursor() != null) {
Cursor cursor = params.getCursor();
Element cursorEl = req.addElement(MailConstants.E_CURSOR);
if (cursor.getPreviousId() != null) {
cursorEl.addAttribute(MailConstants.A_ID, cursor.getPreviousId());
}
if (cursor.getPreviousSortValue() != null) {
cursorEl.addAttribute(MailConstants.A_SORTVAL, cursor.getPreviousSortValue());
}
}
if (params.getTypes().equals(ZSearchParams.TYPE_VOICE_MAIL) || params.getTypes().equals(ZSearchParams.TYPE_CALL)) {
getAllPhoneAccounts();
setVoiceStorePrincipal(req);
}
Element resp = invoke(req);
if (params.getTypes().equals(ZSearchParams.TYPE_GAL)) {
try {
resp.getAttribute(MailConstants.A_SORTBY);
} catch (Exception e) {
resp.addAttribute(MailConstants.A_SORTBY, params.getSortBy().name());
}
try {
resp.getAttribute(MailConstants.A_QUERY_OFFSET);
} catch (Exception e) {
resp.addAttribute(MailConstants.A_QUERY_OFFSET, params.getOffset());
}
}
return new ZSearchResult(resp, nest, params.getTimeZone() != null ? params.getTimeZone() : getPrefs().getTimeZone());
}
use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.
the class ZSearchContext method getNextHit.
/**
*
* @return next search hit in the results, or null.
* @throws com.zimbra.common.service.ServiceException
*/
public ZSearchHit getNextHit() throws ServiceException {
if (mCount < mMaxHits) {
if (mResult == null || (mIndex + 1 >= mResult.getHits().size() && mResult.hasMore())) {
if (mResult != null) {
// get next page
if (mByOffset) {
mParams.setOffset(mParams.getOffset() + mResult.getHits().size());
} else {
List<ZSearchHit> hits = mResult.getHits();
ZSearchHit lastHit = hits.get(hits.size() - 1);
mParams.setCursor(new Cursor(lastHit.getId(), lastHit.getSortField()));
}
}
// search it
if (mParams.getConvId() != null)
mResult = mMailbox.searchConversation(mParams.getConvId(), mParams);
else
mResult = mMailbox.search(mParams);
mIndex = 0;
} else {
++mIndex;
}
}
if (mCount >= mMaxHits || mIndex >= mResult.getHits().size()) {
mHasMore = false;
return null;
} else {
++mCount;
mHasMore = mCount < mMaxHits && ((mIndex + 1 < mResult.getHits().size() || mResult.hasMore()));
return mResult.getHits().get(mIndex);
}
}
use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.
the class ZSearchPager method search.
ZSearchResult search(ZMailbox mailbox, int page, boolean useCursor) throws ServiceException {
while (mResults.size() <= page) {
if (mResults.size() == 0) {
mParams.setCursor(null);
mParams.setOffset(0);
} else {
ZSearchResult lastResult = mResults.get(mResults.size() - 1);
if (!lastResult.hasMore())
break;
if (useCursor) {
List<ZSearchHit> lastHits = lastResult.getHits();
ZSearchHit lastHit = lastHits.get(lastHits.size() - 1);
mParams.setCursor(new Cursor(lastHit.getId(), lastHit.getSortField()));
} else {
mParams.setCursor(null);
mParams.setOffset(page * mParams.getLimit());
}
}
ZSearchResult result = mParams.getConvId() == null ? mailbox.search(mParams) : mailbox.searchConversation(mParams.getConvId(), mParams);
mResults.add(result);
if (result.getConversationSummary() != null) {
mConvId = result.getConversationSummary().getId();
}
for (ZSearchHit hit : result.getHits()) mHitMap.put(hit.getId(), hit);
if (!result.hasMore())
break;
}
if (page < mResults.size())
return mResults.get(page);
else
return mResults.get(mResults.size() - 1);
}
use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.
the class ZSearchCache method fetchMoreHits.
private void fetchMoreHits(ZMailbox mailbox) throws ServiceException {
if (!mHasMore)
return;
if (mLastHit == null) {
mParams.setCursor(null);
mParams.setOffset(0);
} else {
mParams.setCursor(new Cursor(mLastHit.getId(), mLastHit.getSortField()));
}
ZSearchResult result = mParams.getConvId() == null ? mailbox.search(mParams) : mailbox.searchConversation(mParams.getConvId(), mParams);
int i = mHits.size();
for (ZSearchHit hit : result.getHits()) {
ZSearchCacheHit ch = new ZSearchCacheHit();
ch.hit = hit;
ch.index = i++;
mHits.add(ch);
mHitMap.put(hit.getId(), ch);
mLastHit = hit;
}
mConvSummary = result.getConversationSummary();
if (mConvSummary != null)
mConvId = mConvSummary.getId();
mHasMore = result.hasMore();
}
use of com.zimbra.client.ZSearchParams.Cursor in project zm-mailbox by Zimbra.
the class TestSearchSortByDate method testMessagesCloseTimestamps.
@Test
public void testMessagesCloseTimestamps() {
try {
Mailbox recieverMbox = TestUtil.getMailbox(RECIPIENT);
// - 3600*60*1000;
long timestamp = System.currentTimeMillis();
int numMessages = 2207;
int firstLimit = 100;
int incLimit = 50;
int offset = 0;
ArrayList<String> expectedIds = new ArrayList<String>();
for (int i = 0; i < numMessages; i++) {
Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress(RECIPIENT), TestUtil.getAddress(SENDER), NAME_PREFIX + " testing bug " + i, String.format("this message contains a search string %s which we are searching for and a number %d and timestamp %d", SEARCH_STRING, i, timestamp), timestamp);
expectedIds.add(Integer.toString(msg.getId()));
}
for (int i = 0; i < numMessages; i++) {
Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress(RECIPIENT), TestUtil.getAddress(SENDER), NAME_PREFIX + " testing bug " + i, "this message does not contains a search string which we are searching for", timestamp);
}
Collections.reverse(expectedIds);
Thread.sleep(100);
ZMailbox zmbx = TestUtil.getZMailbox(RECIPIENT);
ZSearchParams searchParams = new ZSearchParams("in:inbox " + SEARCH_STRING);
searchParams.setSortBy(SearchSortBy.dateDesc);
searchParams.setLimit(firstLimit);
searchParams.setTypes(ZSearchParams.TYPE_MESSAGE);
List<ZMessage> resultSet = TestUtil.search(zmbx, searchParams);
assertEquals(firstLimit, resultSet.size());
int gotMessages = resultSet.size();
List<String> seenIds = new ArrayList<String>();
ZMessage lastHit = null;
for (ZMessage m : resultSet) {
seenIds.add(m.getId());
lastHit = m;
}
int recCount = 1;
offset += firstLimit;
while (gotMessages > 0) {
searchParams.setCursor((new Cursor(lastHit.getId(), Long.toString(lastHit.getReceivedDate()))));
searchParams = new ZSearchParams("in:inbox " + SEARCH_STRING);
searchParams.setSortBy(SearchSortBy.dateDesc);
searchParams.setLimit(incLimit);
searchParams.setTypes(ZSearchParams.TYPE_MESSAGE);
searchParams.setOffset(offset);
resultSet = TestUtil.search(zmbx, searchParams);
recCount++;
gotMessages = resultSet.size();
int resCount = 0;
for (ZMessage msg : resultSet) {
assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", recCount, resCount, msg.getId(), seenIds.indexOf(msg.getId())), seenIds.contains(msg.getId()));
seenIds.add(msg.getId());
resCount++;
lastHit = msg;
}
// Thread.sleep(100); //jetty sometimes crashes on Mac when bombarded with request without a timeout
offset += incLimit;
}
assertEquals("Returned incorrect number of messages", numMessages, seenIds.size());
} catch (ServiceException e) {
e.printStackTrace();
fail(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations