Permalink
Browse files

Preserve label case when storing. Change nested label separator.

Cleanup.
  • Loading branch information...
1 parent 34d3529 commit 8dad10cb96816f9c3c1d5d01d2881acebc2f95db @rstml rstml committed Jan 31, 2012
@@ -240,7 +240,7 @@ public void reservedLabelsTest() throws IOException
// check latest message
String jsonResponse = given().
- pathParam("labelId", ReservedLabels.INBOX.getLabelId()).
+ pathParam("labelId", ReservedLabels.INBOX.getId()).
expect().
statusCode(200).
when().

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -79,8 +79,8 @@ public void add(final Mailbox mailbox) throws IOException, IllegalArgumentExcept
// Add predefined labels
for (Label l : ReservedLabels.getAll()) {
String labelKey = new StringBuilder(AccountPersistence.CN_LABEL_PREFIX).
- append(l.getLabelId()).toString();
- attributes.put(labelKey, l.getLabelName());
+ append(l.getId()).toString();
+ attributes.put(labelKey, l.getName());
}
AccountPersistence.set(mailbox.getId(), attributes);
@@ -103,7 +103,7 @@ public void delete(final Mailbox mailbox) throws IOException
do {
// get all message ids
messageIds = LabelIndexPersistence.get(mailbox.getId(),
- ReservedLabels.ALL_MAILS.getLabelId(), start,
+ ReservedLabels.ALL_MAILS.getId(), start,
CassandraDAOFactory.MAX_COLUMNS_PER_REQUEST, true);
// get all message headers
@@ -88,9 +88,6 @@ public Labels getAllWithMetadata(final Mailbox mailbox)
@Override
public int add(final Mailbox mailbox, String label)
{
- // enforce lowercase
- label = label.toLowerCase();
-
// get all existing labels
Labels existingLabels = new Labels();
existingLabels.add(AccountPersistence.getLabels(mailbox.getId()));
@@ -125,14 +122,14 @@ public int add(final Mailbox mailbox, String label)
public void rename(final Mailbox mailbox, final Integer labelId, String label)
throws IOException
{
- // enforce lowercase
- label = label.toLowerCase();
-
// get all existing labels
Labels existingLabels = new Labels();
existingLabels.add(AccountPersistence.getLabels(mailbox.getId()));
- LabelUtils.validateLabelName(label, existingLabels);
+ // validate only if name is changed (skip for letter case changes)
+ if (!existingLabels.getName(labelId).equalsIgnoreCase(label)) {
+ LabelUtils.validateLabelName(label, existingLabels);
+ }
// check if label id reserved
if(ReservedLabels.contains(labelId)) {
@@ -112,7 +112,7 @@ public void put(final Mailbox mailbox, UUID messageId, Message message, InputStr
// Check quota
LabelCounters mailboxCounters = LabelCounterPersistence.get(
- mailbox.getId(), ReservedLabels.ALL_MAILS.getLabelId());
+ mailbox.getId(), ReservedLabels.ALL_MAILS.getId());
long requiredBytes = mailboxCounters.getTotalBytes() + message.getSize();
long requiredCount = mailboxCounters.getTotalMessages() + 1;
@@ -152,7 +152,7 @@ public void put(final Mailbox mailbox, UUID messageId, Message message, InputStr
}
// automatically add "all" label to all new messages
- message.addLabel(ReservedLabels.ALL_MAILS.getLabelId());
+ message.addLabel(ReservedLabels.ALL_MAILS.getId());
try {
// begin batch operation
@@ -308,7 +308,7 @@ public void removeLabel(final Mailbox mailbox, final Set<Integer> labelIds,
return;
// label "all" cannot be removed from message
- if (labelIds.contains(ReservedLabels.ALL_MAILS.getLabelId()))
+ if (labelIds.contains(ReservedLabels.ALL_MAILS.getId()))
throw new IllegalLabelException("This label cannot be removed");
// build list of attributes
@@ -54,9 +54,8 @@
import me.prettyprint.hector.api.query.QueryResult;
import com.elasticinbox.core.cassandra.CassandraDAOFactory;
+import com.elasticinbox.core.model.LabelConstants;
import com.elasticinbox.core.model.LabelCounters;
-import com.elasticinbox.core.model.Labels;
-import com.elasticinbox.core.model.ReservedLabels;
public final class LabelCounterPersistence
{
@@ -77,13 +76,13 @@
new StringBuilder(CN_LABEL_PREFIX).append(0).toString();
Map<Integer, LabelCounters> result =
- new HashMap<Integer, LabelCounters>(ReservedLabels.MAX_RESERVED_LABEL_ID);
+ new HashMap<Integer, LabelCounters>(LabelConstants.MAX_RESERVED_LABEL_ID);
MultigetSuperSliceCounterQuery<String, String, String> q =
createMultigetSuperSliceCounterQuery(keyspace, strSe, strSe, strSe);
q.setColumnFamily(CF_COUNTERS);
- q.setRange(startColumnName, null, false, Labels.MAX_LABEL_ID);
+ q.setRange(startColumnName, null, false, LabelConstants.MAX_LABEL_ID);
q.setKeys(mailbox);
QueryResult<CounterSuperRows<String, String, String>> r = q.execute();
@@ -53,11 +53,11 @@ public int hashCode() {
return labelId.hashCode();
}
- public String getLabelName() {
+ public String getName() {
return labelName;
}
- public Integer getLabelId() {
+ public Integer getId() {
return labelId;
}
@@ -0,0 +1,19 @@
+package com.elasticinbox.core.model;
+
+public class LabelConstants
+{
+ /** Maximum label count per mailbox (including reserved labels) */
+ public final static int MAX_LABEL_ID = 9999;
+
+ /** Maximum label name length */
+ public final static int MAX_LABEL_NAME_LENGTH = 250;
+
+ /** Character used as nested label separator */
+ public final static Character NESTED_LABEL_SEPARATOR = '^';
+
+ /**
+ * Maximum reserved label ID. I.e. reserved label ID can be between
+ * 0..MAX_RESERVED_LABEL_ID
+ */
+ public final static int MAX_RESERVED_LABEL_ID = 20;
+}
@@ -44,13 +44,13 @@
*/
public class Labels
{
- /** Maximum label count per mailbox (including reserved labels) */
- public final static int MAX_LABEL_ID = 9999;
- public final static int MAX_LABEL_NAME_LENGTH = 250;
- public final static String NESTED_LABEL_SEPARATOR = "/";
-
private Map<Integer, String> labels;
private Map<Integer, LabelCounters> counters;
+
+ private final static String JSON_NAME = "name";
+ private final static String JSON_SIZE = "size";
+ private final static String JSON_MESSAGES_TOTAL = "total";
+ private final static String JSON_MESSAGES_UNREAD = "new";
public Labels() {
labels = new HashMap<Integer, String>(1);
@@ -73,7 +73,7 @@ public void add(int labelId, String labelName) {
* @param label
*/
public void add(Label label) {
- labels.put(label.getLabelId(), label.getLabelName());
+ labels.put(label.getId(), label.getName());
}
/**
@@ -84,6 +84,17 @@ public void add(Label label) {
public void add(Map<Integer, String> labels) {
this.labels.putAll(labels);
}
+
+ /**
+ * Get label name by ID
+ *
+ * @param labelId
+ * @return
+ */
+ public String getName(int labelId) {
+ String name = labels.get(labelId);
+ return name;
+ }
/**
* Add counters to a single label
@@ -138,13 +149,18 @@ public boolean containsId(Integer labelId) {
}
/**
- * Checks whether label with given name exists
- *
+ * Checks whether label with given name exists. Case insensitive.
+ *
* @param labelName
* @return
*/
- public boolean containsName(String labelName) {
- return labels.containsValue(labelName);
+ public boolean containsName(String labelName)
+ {
+ for (String v : labels.values()) {
+ if (v.equalsIgnoreCase(labelName))
+ return true;
+ }
+ return false;
}
/**
@@ -167,16 +183,16 @@ public boolean containsName(String labelName) {
{
Integer labelId = label.getKey();
metadata.put(labelId, new HashMap<String, Object>(4));
- metadata.get(labelId).put("name", label.getValue());
+ metadata.get(labelId).put(JSON_NAME, label.getValue());
if (counters.containsKey(labelId)) {
- metadata.get(labelId).put("size", counters.get(labelId).getTotalBytes());
- metadata.get(labelId).put("total",counters.get(labelId).getTotalMessages());
- metadata.get(labelId).put("new", counters.get(labelId).getNewMessages());
+ metadata.get(labelId).put(JSON_SIZE, counters.get(labelId).getTotalBytes());
+ metadata.get(labelId).put(JSON_MESSAGES_TOTAL, counters.get(labelId).getTotalMessages());
+ metadata.get(labelId).put(JSON_MESSAGES_UNREAD,counters.get(labelId).getNewMessages());
} else {
- metadata.get(labelId).put("size", 0);
- metadata.get(labelId).put("total", 0);
- metadata.get(labelId).put("new", 0);
+ metadata.get(labelId).put(JSON_SIZE, 0);
+ metadata.get(labelId).put(JSON_MESSAGES_TOTAL, 0);
+ metadata.get(labelId).put(JSON_MESSAGES_UNREAD, 0);
}
}
@@ -50,20 +50,19 @@
public final static Label IMPORTANT = new Label(7, "important");
public final static Label NOTIFICATIONS = new Label(8, "notifications");
public final static Label ATTACHMENTS = new Label(9, "attachments");
- public final static int MAX_RESERVED_LABEL_ID = 20;
private final static ImmutableMap<Integer, Label> labels =
new ImmutableMap.Builder<Integer, Label>()
- .put(ALL_MAILS.getLabelId(), ALL_MAILS)
- .put(INBOX.getLabelId(), INBOX)
- .put(DRAFTS.getLabelId(), DRAFTS)
- .put(SENT.getLabelId(), SENT)
- .put(TRASH.getLabelId(), TRASH)
- .put(SPAM.getLabelId(), SPAM)
- .put(STARRED.getLabelId(), STARRED)
- .put(IMPORTANT.getLabelId(), IMPORTANT)
- .put(NOTIFICATIONS.getLabelId(), NOTIFICATIONS)
- .put(ATTACHMENTS.getLabelId(), ATTACHMENTS)
+ .put(ALL_MAILS.getId(), ALL_MAILS)
+ .put(INBOX.getId(), INBOX)
+ .put(DRAFTS.getId(), DRAFTS)
+ .put(SENT.getId(), SENT)
+ .put(TRASH.getId(), TRASH)
+ .put(SPAM.getId(), SPAM)
+ .put(STARRED.getId(), STARRED)
+ .put(IMPORTANT.getId(), IMPORTANT)
+ .put(NOTIFICATIONS.getId(), NOTIFICATIONS)
+ .put(ATTACHMENTS.getId(), ATTACHMENTS)
.build();
public static Label get(final int labelId) {
@@ -33,6 +33,7 @@
import com.elasticinbox.core.ExistingLabelException;
import com.elasticinbox.core.IllegalLabelException;
import com.elasticinbox.core.model.Label;
+import com.elasticinbox.core.model.LabelConstants;
import com.elasticinbox.core.model.Labels;
import com.elasticinbox.core.model.ReservedLabels;
@@ -49,8 +50,8 @@ public static Integer getNewLabelId()
{
// New label ID whould be greater than reserved label IDs and within
// allowed range (less than MAX_LABEL_ID).
- int labelId = ReservedLabels.MAX_RESERVED_LABEL_ID
- + random.nextInt(Labels.MAX_LABEL_ID - ReservedLabels.MAX_RESERVED_LABEL_ID);
+ int labelId = LabelConstants.MAX_RESERVED_LABEL_ID
+ + random.nextInt(LabelConstants.MAX_LABEL_ID - LabelConstants.MAX_RESERVED_LABEL_ID);
return labelId;
}
@@ -62,7 +63,7 @@ public static Integer getNewLabelId()
public static void validateLabelName(final String label, final Labels existingLabels)
{
// check total length of label
- if (label.length() > Labels.MAX_LABEL_NAME_LENGTH)
+ if (label.length() > LabelConstants.MAX_LABEL_NAME_LENGTH)
throw new IllegalLabelException("Label name exceeds maximum allowed length");
// check if label already exists
@@ -71,11 +72,11 @@ public static void validateLabelName(final String label, final Labels existingLa
// check if starts with reserved label
for (Label l : ReservedLabels.getAll()) {
- if (label.startsWith(l.getLabelName() + Labels.NESTED_LABEL_SEPARATOR))
+ if (label.startsWith(l.getName() + LabelConstants.NESTED_LABEL_SEPARATOR.toString()))
throw new IllegalLabelException("Netsted labels are not allowed under reserved labels");
}
- if (label.contains(Labels.NESTED_LABEL_SEPARATOR + Labels.NESTED_LABEL_SEPARATOR))
+ if (label.contains(LabelConstants.NESTED_LABEL_SEPARATOR.toString() + LabelConstants.NESTED_LABEL_SEPARATOR.toString()))
throw new IllegalLabelException("Illegal use of nested label separator");
// check special symbols? for now we allow any symbol
@@ -88,7 +88,7 @@ public ElasticInboxDeliveryAgent(MessageDAO messageDAO)
}
message.setSize((long) env.getSize()); // update message size
- message.addLabel(ReservedLabels.INBOX.getLabelId()); // default location
+ message.addLabel(ReservedLabels.INBOX.getId()); // default location
logEnvelope(env, message, deliveryId);

0 comments on commit 8dad10c

Please sign in to comment.