@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); // add new label AccountPersistence.putLabel(mutator, mailbox.getId(), label); // commit batch operation mutator.execute(); return label.getId(); }
@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); // set new name AccountPersistence.putLabel(mutator, mailbox.getId(), label); // commit batch operation mutator.execute(); }