Package com.elasticinbox.core.model

Examples of com.elasticinbox.core.model.LabelMap


  @Override
  public LabelMap getAllWithMetadata(final Mailbox mailbox)
      throws IOException
  {
    // get labels
    LabelMap labels = AccountPersistence.getLabels(mailbox.getId());

    // set labels' counters
    Map<Integer, LabelCounters> counters = LabelCounterPersistence.getAll(mailbox.getId());

    for (int labelId : counters.keySet())
    {
      if (labels.containsId(labelId) && counters.containsKey(labelId)) {
        labels.get(labelId).setCounters(counters.get(labelId));
      } else if (labels.containsId(labelId) && !counters.containsKey(labelId)) {
        // assume zeros for all counters if not yet initialised
        labels.get(labelId).setCounters(new LabelCounters());
      } else if (!labels.containsId(labelId) && counters.containsKey(labelId)) {
        logger.warn("Found counters for label {}/{}, but label does not exist.", mailbox.getId(), labelId);
      }
    }

    return labels;
View Full Code Here


  @Override
  public int add(Mailbox mailbox, Label label)
  {
    // get all existing labels
    LabelMap existingLabels = AccountPersistence.getLabels(mailbox.getId());

    LabelUtils.validateLabelName(label.getName(), existingLabels);

    try {
      // generate new label id
      int labelId = LabelUtils.getNewLabelId(existingLabels.getIds());
      label.setId(labelId);
    } catch (IllegalLabelException ile) {
      // log and rethrow
      logger.warn("{} reached max random label id attempts with {} labels",
            mailbox, existingLabels.size());
      throw ile;
    }

    // begin batch operation
    Mutator<String> mutator = createMutator(keyspace, strSe);
View Full Code Here

 
  @Override
  public void update(Mailbox mailbox, Label label) throws IOException
  {
    // get all existing labels
    LabelMap existingLabels = AccountPersistence.getLabels(mailbox.getId());

    // validate only if name is changed (skips letter case changes)
    if (label.getName() != null && !existingLabels.containsName(label.getName())) {
      LabelUtils.validateLabelName(label.getName(), existingLabels);
    }

    // check if label id reserved
    if (ReservedLabels.contains(label.getId())) {
      throw new ExistingLabelException("This is reserved label and can't be modified");
    }

    // check if label id exists
    if (!existingLabels.containsId(label.getId())) {
      throw new IllegalLabelException("Label does not exist");
    }

    // begin batch operation
    Mutator<String> mutator = createMutator(keyspace, strSe);
View Full Code Here

    for (List<UUID> idSubList : Lists.partition(messageIds, readBatchSize))
    {
      // get label stats
      MessageAggregator ma = new MessageAggregator(mailbox, idSubList);
      LabelMap labels = ma.aggregateCountersByLabel();

      // validate message ids
      List<UUID> validMessageIds = new ArrayList<UUID>(ma.getValidMessageIds());
      List<UUID> invalidMessageIds = new ArrayList<UUID>(ma.getInvalidMessageIds());

      // add only valid messages to purge index
      PurgeIndexPersistence.add(mutator, mailbox.getId(), validMessageIds);

      // remove valid message ids from label indexes, including "all"
      LabelIndexPersistence.remove(mutator, mailbox.getId(), validMessageIds, labels.getIds());

      // decrement label counters (add negative value)
      for (Integer labelId : labels.getIds()) {
        LabelCounterPersistence.subtract(mutator, mailbox.getId(), labelId, labels.get(labelId).getCounters());
      }

      // remove invalid message ids from all known labels
      LabelMap allLabels = AccountPersistence.getLabels(mailbox.getId());
      LabelIndexPersistence.remove(mutator, mailbox.getId(), invalidMessageIds, allLabels.getIds());

      // signal end of batch
      mutator.executeIfFull();
    }
View Full Code Here

  }

  @Override
  public LabelMap scrub(final Mailbox mailbox, final boolean rebuildIndex)
  {
    LabelMap labels = new LabelMap();
    Map<UUID, Message> messages;
    Set<UUID> purgePendingMessages = new HashSet<UUID>();
   
    // initiate throttling mutator
    ThrottlingMutator<String> mutator = new ThrottlingMutator<String>(keyspace, strSe,
        BatchConstants.BATCH_WRITES, BatchConstants.BATCH_WRITE_INTERVAL);
   
    logger.debug("Recalculating counters for {}", mailbox);

    // Get message IDs pending purge. Such messages should be excluded during calculation.
    purgePendingMessages = PurgeIndexPersistence.getAll(mailbox.getId());

    logger.debug("Found {} messages pending purge. Will exclude them from calculations.", purgePendingMessages.size());

    UUID start = TimeUUIDUtils.getUniqueTimeUUIDinMillis();
    do {
      // reset start, read messages and calculate label counters
      messages = MessagePersistence.getRange(
          mailbox.getId(), start, BatchConstants.BATCH_READS);

      for (UUID messageId : messages.keySet())
      {
        start = messageId; // shift next query start

        // skip messages from purge queue
        if (purgePendingMessages.contains(messageId)) continue;

        Message message = messages.get(messageId);

        // add counters for each of the labels
        for (int labelId : message.getLabels())
        {
          if (!labels.containsId(labelId)) {
            Label label = new Label(labelId).setCounters(message.getLabelCounters());
            labels.put(label);
          } else {
            labels.get(labelId).incrementCounters(message.getLabelCounters());
          }

          if (rebuildIndex)
          {
            // add message ID to the label index
            LabelIndexPersistence.add(mutator, mailbox.getId(), messageId, labelId);
            mutator.executeIfFull();
          }
        }

        logger.debug("Counters state after message {} is {}", messageId, labels.toString());
      }
     
    }
    while (messages.size() >= BatchConstants.BATCH_READS);
View Full Code Here

     *
     * @return
     */
    public LabelMap aggregateCountersByLabel()
    {
      LabelMap labels = new LabelMap();

      // get all labels of all messages, including label "all"
      for (UUID messageId : this.messages.keySet())
      {
        Set<Integer> messageLabels = this.messages.get(messageId).getLabels();
 
        for (int labelId : messageLabels)
        {
          if (!labels.containsId(labelId)) {
            Label label = new Label(labelId).
                setCounters(this.messages.get(messageId).getLabelCounters());
            labels.put(label);
          } else {
            labels.get(labelId).getCounters().add(
                this.messages.get(messageId).getLabelCounters());
          }
        }
      }

View Full Code Here

  public void mailboxScrubTest() throws IOException
  {
    initAccount();
   
    Pair<LabelMap, Map<Integer, List<UUID>>> pair = populateMailbox();
    LabelMap labels = pair.getLeft();
    Map<Integer, List<UUID>> messages = pair.getRight();
   
    // TODO: wipe off counters and indexes here. need to communicate with metadata store directly

    // check label counters before scrub
    expect().
      statusCode(200).and().
      body("'" + ReservedLabels.INBOX.getId() + "'.total",
          equalTo(labels.get(ReservedLabels.INBOX.getId()).getCounters().getTotalMessages().intValue())).
      body("'" + ReservedLabels.INBOX.getId() + "'.unread",
          equalTo(labels.get(ReservedLabels.INBOX.getId()).getCounters().getUnreadMessages().intValue())).
      body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.total",
          equalTo(labels.get(ReservedLabels.NOTIFICATIONS.getId()).getCounters().getTotalMessages().intValue())).
      body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.unread",
          equalTo(labels.get(ReservedLabels.NOTIFICATIONS.getId()).getCounters().getUnreadMessages().intValue())).
      body("'" + ReservedLabels.SPAM.getId() + "'.total",
          equalTo(labels.get(ReservedLabels.SPAM.getId()).getCounters().getTotalMessages().intValue())).
      body("'" + ReservedLabels.SPAM.getId() + "'.unread",
          equalTo(labels.get(ReservedLabels.SPAM.getId()).getCounters().getUnreadMessages().intValue())).
      body("'" + ReservedLabels.TRASH.getId() + "'.total",
          equalTo(labels.get(ReservedLabels.TRASH.getId()).getCounters().getTotalMessages().intValue())).
      body("'" + ReservedLabels.TRASH.getId() + "'.unread",
          equalTo(labels.get(ReservedLabels.TRASH.getId()).getCounters().getUnreadMessages().intValue())).
    when().
      get(REST_PATH + "/mailbox?metadata=true").asString();

    // scrub label counters
    expect().
      statusCode(204).
    when().
      post(REST_PATH + "/mailbox/scrub/counters");

    // check label counters after scrub
    expect().
      statusCode(200).and().
      body("'" + ReservedLabels.INBOX.getId() + "'.total",
          equalTo(labels.get(ReservedLabels.INBOX.getId()).getCounters().getTotalMessages().intValue())).
      body("'" + ReservedLabels.INBOX.getId() + "'.unread",
          equalTo(labels.get(ReservedLabels.INBOX.getId()).getCounters().getUnreadMessages().intValue())).
      body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.total",
          equalTo(labels.get(ReservedLabels.NOTIFICATIONS.getId()).getCounters().getTotalMessages().intValue())).
      body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.unread",
          equalTo(labels.get(ReservedLabels.NOTIFICATIONS.getId()).getCounters().getUnreadMessages().intValue())).
      body("'" + ReservedLabels.SPAM.getId() + "'.total",
          equalTo(labels.get(ReservedLabels.SPAM.getId()).getCounters().getTotalMessages().intValue())).
      body("'" + ReservedLabels.SPAM.getId() + "'.unread",
          equalTo(labels.get(ReservedLabels.SPAM.getId()).getCounters().getUnreadMessages().intValue())).
      body("'" + ReservedLabels.TRASH.getId() + "'.total",
          equalTo(labels.get(ReservedLabels.TRASH.getId()).getCounters().getTotalMessages().intValue())).
      body("'" + ReservedLabels.TRASH.getId() + "'.unread",
          equalTo(labels.get(ReservedLabels.TRASH.getId()).getCounters().getUnreadMessages().intValue())).
    when().
      get(REST_PATH + "/mailbox?metadata=true").asString();
   
    // scrub label indexes
    expect().
View Full Code Here

   * @throws IOException
   */
  protected static Pair<LabelMap, Map<Integer, List<UUID>>> populateMailbox() throws IOException
  {
    Map<Integer, List<UUID>> messages = new HashMap<Integer, List<UUID>>();
    LabelMap labels = new LabelMap();
    LabelCounters inboxCounters = new LabelCounters();
    LabelCounters notifCounters = new LabelCounters();
    LabelCounters trashCounters = new LabelCounters();
    LabelCounters spamCounters = new LabelCounters();

    // INBOX: add 5 messages, mark 2 as unread
    messages.put(ReservedLabels.INBOX.getId(), new ArrayList<UUID>());
    List<UUID> inboxMessages = messages.get(ReservedLabels.INBOX.getId());
    inboxMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
    inboxMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
    inboxMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
    inboxMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
    inboxMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
    markAsRead(inboxMessages.get(3));
    markAsRead(inboxMessages.get(4));
    inboxCounters.setTotalMessages(5L);
    inboxCounters.setUnreadMessages(3L);
    labels.put(ReservedLabels.INBOX);
    labels.get(ReservedLabels.INBOX.getId()).setCounters(inboxCounters);

    // NOTIFICATIONS: add 3 messages, mark 1 as read
    messages.put(ReservedLabels.NOTIFICATIONS.getId(), new ArrayList<UUID>());
    List<UUID> notifMessages = messages.get(ReservedLabels.NOTIFICATIONS.getId());
    notifMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.NOTIFICATIONS.getId()));
    notifMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.NOTIFICATIONS.getId()));
    notifMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.NOTIFICATIONS.getId()));
    markAsRead(notifMessages.get(1));
    notifCounters.setTotalMessages(3L);
    notifCounters.setUnreadMessages(2L);
    labels.put(ReservedLabels.NOTIFICATIONS);
    labels.get(ReservedLabels.NOTIFICATIONS.getId()).setCounters(notifCounters);

    // SPAM: add 5 messages, keep all unread
    messages.put(ReservedLabels.SPAM.getId(), new ArrayList<UUID>());
    List<UUID> spamMessages = messages.get(ReservedLabels.SPAM.getId());
    spamMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
    spamMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
    spamMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
    spamMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
    spamMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
    spamCounters.setTotalMessages(5L);
    spamCounters.setUnreadMessages(5L);
    labels.put(ReservedLabels.SPAM);
    labels.get(ReservedLabels.SPAM.getId()).setCounters(spamCounters);

    // TRASH: add 4 messages, mark 2 as read
    messages.put(ReservedLabels.TRASH.getId(), new ArrayList<UUID>());
    List<UUID> trashMessages = messages.get(ReservedLabels.TRASH.getId());
    trashMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.TRASH.getId()));
    trashMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.TRASH.getId()));
    trashMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.TRASH.getId()));
    trashMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.TRASH.getId()));
    markAsRead(trashMessages.get(0));
    markAsRead(trashMessages.get(2));
    trashCounters.setTotalMessages(4L);
    trashCounters.setUnreadMessages(2L);
    labels.put(ReservedLabels.TRASH);
    labels.get(ReservedLabels.TRASH.getId()).setCounters(trashCounters);

    return Pair.of(labels, messages);
  }
View Full Code Here

   * @param mailbox
   */
  public static void deleteIndexes(Mutator<String> mutator, final String mailbox)
  {
    // get all labels
    LabelMap labels = AccountPersistence.getLabels(mailbox);

    for (Integer labelId : labels.getIds()) {
      deleteIndex(mutator, mailbox, labelId);
    }

    // delete purge index
    String key = getLabelKey(mailbox, PurgeIndexPersistence.PURGE_LABEL_ID);
View Full Code Here

   * @param mailbox
   * @return
   */
  public static LabelMap getLabels(final String mailbox)
  {
    LabelMap labels = new LabelMap();

    // get list of user specific labels from Cassandra
    Map<String, Object> attributes = getAll(mailbox);

    // add user specific labels
    for (Map.Entry<String, Object> a : attributes.entrySet())
    {
      if (a.getKey().startsWith(CN_LABEL_NAME_PREFIX))
      {
        // set label name
        Integer labelId = Integer.parseInt(a.getKey().split(CN_SEPARATOR)[1]);
        String labelName = (String) a.getValue();
       
        if (labels.containsId(labelId)) {
          labels.get(labelId).setName(labelName);
        } else {
          Label label = new Label(labelId, labelName);
          labels.put(label);
        }
      }
      else if (a.getKey().startsWith(CN_LABEL_ATTRIBUTE_PREFIX))
      {
        // set label custom attribute
        String[] attrKeys = a.getKey().split(CN_SEPARATOR);
        Integer labelId = Integer.parseInt(attrKeys[1]);
        String attrName = attrKeys[2];
        String attrValue = (String) a.getValue();
       
        if (labels.containsId(labelId)) {
          labels.get(labelId).addAttribute(attrName, attrValue);
        } else {
          Label label = new Label(labelId);
          label.addAttribute(attrName, attrValue);
          labels.put(label);
        }
      }
    }

    // add default reserved labels
    for (Label l : ReservedLabels.getAll())
    {
      Label label = new Label(l.getId(), l.getName());
      labels.put(label);
    }

    return labels;
  }
View Full Code Here

TOP

Related Classes of com.elasticinbox.core.model.LabelMap

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.