<commit_before>/**
 * Copyright 2015 Evernote Corporation. All rights reserved.
 */
package com.evernote.clients;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.evernote.edam.error.EDAMNotFoundException;
import com.evernote.edam.error.EDAMSystemException;
import com.evernote.edam.error.EDAMUserException;
import com.evernote.edam.notestore.NoteFilter;
import com.evernote.edam.notestore.NotesMetadataList;
import com.evernote.edam.notestore.NotesMetadataResultSpec;
import com.evernote.edam.type.LinkedNotebook;
import com.evernote.edam.type.NoteSortOrder;
import com.evernote.thrift.TException;

/**
 * Provides helper methods to search notes in different note stores.
 * <p>
 * It's NOT thread safe.
 *
 * @author alexchenzl
 */
public class ENSearchHelper {

  private final ENClientFactory clientFactory;
  private final NoteStoreClient personalClient;
  private NoteStoreClient businessClient;
  // cache ENLinkedNotebookHelper objects for re-use
  private final Map<String, ENLinkedNotebookHelper> linkedNotebookHelpers =
      new HashMap<String, ENLinkedNotebookHelper>();

  public ENSearchHelper(ENClientFactory factory, NoteStoreClient client) {
    if (factory == null || client == null) {
      throw new IllegalArgumentException("All arguments must not be null!");
    }
    this.clientFactory = factory;
    this.personalClient = client;
  }

  /**
   * Searches personal notes in the user's account. It will not search shared notebook or
   * business notebook that the user has joined
   * 
   * @param search A {@link SearchParam} object that wraps all parameters for this search
   * @return A list of NotesMetadataList
   * @throws EDAMUserException
   * @throws EDAMSystemException
   * @throws EDAMNotFoundException
   * @throws TException
   */
  public List<NotesMetadataList> findPersonalNotes(final SearchParam search)
      throws EDAMUserException, EDAMSystemException, EDAMNotFoundException, TException {
    if (search == null) {
      return null;
    }
    return findNotesMetadata(search, personalClient, search.getNoteFilter());
  }

  /**
   * Searches notes in a specified LinkedNotebook. The specified LinkedNotebook is
   * corresponding to either a normal shared notebook or a business notebook the user has
   * joined.
   * 
   * @param search A {@link SearchParam} object that wraps all parameters for this search
   * @param linkedNotebook The specified LinkedNotebook
   * @return A list of NotesMetadataList
   * @throws EDAMUserException
   * @throws EDAMSystemException
   * @throws EDAMNotFoundException
   * @throws TException
   */
  public List<NotesMetadataList> findNotesInLinkedNotebook(final SearchParam search,
      final LinkedNotebook linkedNotebook) throws EDAMUserException, EDAMSystemException,
          EDAMNotFoundException, TException {

    if (search == null || linkedNotebook == null) {
      return null;
    }
    ENLinkedNotebookHelper linkedNotebookHelper = getLinkedNotebookHelper(linkedNotebook);
    if (linkedNotebookHelper == null) {
      return null;
    }
    String notebookGuid = linkedNotebookHelper.getCorrespondingNotebookGuid();

    // create a deep copy so that we don't touch the initial search request values
    NoteFilter noteFilter = new NoteFilter(search.getNoteFilter());
    noteFilter.setNotebookGuid(notebookGuid);

    return findNotesMetadata(search, linkedNotebookHelper.getSharedClient(), noteFilter);
  }

  /**
   * Searches notes within those shared notebooks that the user has joined that are
   * business notebooks in the business that the user is currently a member of.
   * 
   * @param search A {@link SearchParam} object that wraps all parameters for this search
   * @return A list of NotesMetadataList
   * @throws EDAMUserException
   * @throws EDAMSystemException
   * @throws EDAMNotFoundException
   * @throws TException
   */
  public List<NotesMetadataList> findBusinessNotes(final SearchParam search)
      throws EDAMUserException, EDAMSystemException, EDAMNotFoundException, TException {
    if (search == null) {
      return null;
    }
    getBusinessClient();
    return findNotesMetadata(search, businessClient, search.getNoteFilter());
  }

  private List<NotesMetadataList> findNotesMetadata(SearchParam search,
      NoteStoreClient client, NoteFilter filter) throws EDAMUserException,
          EDAMSystemException, EDAMNotFoundException, TException {

    List<NotesMetadataList> result = new ArrayList<NotesMetadataList>();
    final int maxNotes = search.getMaxNotes();
    int offset = search.getOffset();

    int remaining = maxNotes - offset;
    while (remaining > 0) {
      NotesMetadataList notesMetadata = client.findNotesMetadata(filter, offset, maxNotes,
          search.getResultSpec());
      offset = notesMetadata.getStartIndex() + notesMetadata.getNotesSize();
      remaining = notesMetadata.getTotalNotes() - offset;
      result.add(notesMetadata);
    }
    return result;

  }

  private NoteStoreClient getBusinessClient() throws TException, EDAMUserException,
      EDAMSystemException {
    if (businessClient == null) {
      ENBusinessNotebookHelper businessNotebookHelper = clientFactory
          .createBusinessNotebookHelper();
      businessClient = businessNotebookHelper.getBusinessClient();
    }
    return businessClient;
  }
<commit_msg>fix search offset bug<commit_after>/**
 * Copyright 2015 Evernote Corporation. All rights reserved.
 */
package com.evernote.clients;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.evernote.edam.error.EDAMNotFoundException;
import com.evernote.edam.error.EDAMSystemException;
import com.evernote.edam.error.EDAMUserException;
import com.evernote.edam.notestore.NoteFilter;
import com.evernote.edam.notestore.NotesMetadataList;
import com.evernote.edam.notestore.NotesMetadataResultSpec;
import com.evernote.edam.type.LinkedNotebook;
import com.evernote.edam.type.NoteSortOrder;
import com.evernote.thrift.TException;

/**
 * Provides helper methods to search notes in different note stores.
 * <p>
 * It's NOT thread safe.
 *
 * @author alexchenzl
 */
public class ENSearchHelper {

  private final ENClientFactory clientFactory;
  private final NoteStoreClient personalClient;
  private NoteStoreClient businessClient;
  // cache ENLinkedNotebookHelper objects for re-use
  private final Map<String, ENLinkedNotebookHelper> linkedNotebookHelpers =
      new HashMap<String, ENLinkedNotebookHelper>();

  public ENSearchHelper(ENClientFactory factory, NoteStoreClient client) {
    if (factory == null || client == null) {
      throw new IllegalArgumentException("All arguments must not be null!");
    }
    this.clientFactory = factory;
    this.personalClient = client;
  }

  /**
   * Searches personal notes in the user's account. It will not search shared notebook or
   * business notebook that the user has joined
   * 
   * @param search A {@link SearchParam} object that wraps all parameters for this search
   * @return A list of NotesMetadataList
   * @throws EDAMUserException
   * @throws EDAMSystemException
   * @throws EDAMNotFoundException
   * @throws TException
   */
  public List<NotesMetadataList> findPersonalNotes(final SearchParam search)
      throws EDAMUserException, EDAMSystemException, EDAMNotFoundException, TException {
    if (search == null) {
      return null;
    }
    return findNotesMetadata(search, personalClient, search.getNoteFilter());
  }

  /**
   * Searches notes in a specified LinkedNotebook. The specified LinkedNotebook is
   * corresponding to either a normal shared notebook or a business notebook the user has
   * joined.
   * 
   * @param search A {@link SearchParam} object that wraps all parameters for this search
   * @param linkedNotebook The specified LinkedNotebook
   * @return A list of NotesMetadataList
   * @throws EDAMUserException
   * @throws EDAMSystemException
   * @throws EDAMNotFoundException
   * @throws TException
   */
  public List<NotesMetadataList> findNotesInLinkedNotebook(final SearchParam search,
      final LinkedNotebook linkedNotebook) throws EDAMUserException, EDAMSystemException,
      EDAMNotFoundException, TException {

    if (search == null || linkedNotebook == null) {
      return null;
    }
    ENLinkedNotebookHelper linkedNotebookHelper = getLinkedNotebookHelper(linkedNotebook);
    if (linkedNotebookHelper == null) {
      return null;
    }
    String notebookGuid = linkedNotebookHelper.getCorrespondingNotebookGuid();

    // create a deep copy so that we don't touch the initial search request values
    NoteFilter noteFilter = new NoteFilter(search.getNoteFilter());
    noteFilter.setNotebookGuid(notebookGuid);

    return findNotesMetadata(search, linkedNotebookHelper.getSharedClient(), noteFilter);
  }

  /**
   * Searches notes within those shared notebooks that the user has joined that are
   * business notebooks in the business that the user is currently a member of.
   * 
   * @param search A {@link SearchParam} object that wraps all parameters for this search
   * @return A list of NotesMetadataList
   * @throws EDAMUserException
   * @throws EDAMSystemException
   * @throws EDAMNotFoundException
   * @throws TException
   */
  public List<NotesMetadataList> findBusinessNotes(final SearchParam search)
      throws EDAMUserException, EDAMSystemException, EDAMNotFoundException, TException {
    if (search == null) {
      return null;
    }
    getBusinessClient();
    return findNotesMetadata(search, businessClient, search.getNoteFilter());
  }

  private List<NotesMetadataList> findNotesMetadata(SearchParam search,
      NoteStoreClient client, NoteFilter filter) throws EDAMUserException,
      EDAMSystemException, EDAMNotFoundException, TException {

    List<NotesMetadataList> result = new ArrayList<NotesMetadataList>();
    final int maxNotes = search.getMaxNotes();
    int offset = search.getOffset();

    int remaining = maxNotes - offset;
    while (remaining > 0) {
      NotesMetadataList notesMetadata = client.findNotesMetadata(filter, offset, maxNotes,
          search.getResultSpec());
      result.add(notesMetadata);

      offset = notesMetadata.getStartIndex() + notesMetadata.getNotesSize();
      if (notesMetadata.getTotalNotes() <= offset) {
        remaining = 0;
      } else {
        remaining = maxNotes - offset;
      }
    }
    return result;

  }

  private NoteStoreClient getBusinessClient() throws TException, EDAMUserException,
      EDAMSystemException {
    if (businessClient == null) {
      ENBusinessNotebookHelper businessNotebookHelper = clientFactory
          .createBusinessNotebookHelper();
      businessClient = businessNotebookHelper.getBusinessClient();
    }
    return businessClient;
  }

{'fraction_non_alphanumeric': 0.07623442547300416, 'fraction_numerical': 0.0010152284263959391, 'mean_word_length': 4.152639087018545, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4149524', 'n_tokens_mistral': 2945, 'n_tokens_neox': 2834, 'n_words': 837}