Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 0181826

Browse files
author
Rustam Aliyev
committed
Refactor code to prepare for custom label attributes. #39
1 parent bab3420 commit 0181826

File tree

14 files changed

+390
-332
lines changed

14 files changed

+390
-332
lines changed

itests/src/test/java/com/elasticinbox/itests/RestV2IT.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
import com.elasticinbox.core.model.LabelConstants;
5353
import com.elasticinbox.core.model.LabelCounters;
54-
import com.elasticinbox.core.model.Labels;
54+
import com.elasticinbox.core.model.LabelMap;
5555
import com.elasticinbox.core.model.Marker;
5656
import com.elasticinbox.core.model.ReservedLabels;
5757
import com.google.common.io.ByteStreams;
@@ -745,8 +745,8 @@ public void mailboxScrubTest() throws IOException
745745
{
746746
initAccount();
747747

748-
Pair<Labels, Map<Integer, List<UUID>>> pair = populateMailbox();
749-
Labels labels = pair.getLeft();
748+
Pair<LabelMap, Map<Integer, List<UUID>>> pair = populateMailbox();
749+
LabelMap labels = pair.getLeft();
750750
Map<Integer, List<UUID>> messages = pair.getRight();
751751

752752
// TODO: wipe off counters and indexes here. need to communicate with metadata store directly
@@ -755,21 +755,21 @@ public void mailboxScrubTest() throws IOException
755755
expect().
756756
statusCode(200).and().
757757
body("'" + ReservedLabels.INBOX.getId() + "'.total",
758-
equalTo(labels.getLabelCounters(ReservedLabels.INBOX.getId()).getTotalMessages().intValue())).
758+
equalTo(labels.get(ReservedLabels.INBOX.getId()).getCounters().getTotalMessages().intValue())).
759759
body("'" + ReservedLabels.INBOX.getId() + "'.unread",
760-
equalTo(labels.getLabelCounters(ReservedLabels.INBOX.getId()).getUnreadMessages().intValue())).
760+
equalTo(labels.get(ReservedLabels.INBOX.getId()).getCounters().getUnreadMessages().intValue())).
761761
body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.total",
762-
equalTo(labels.getLabelCounters(ReservedLabels.NOTIFICATIONS.getId()).getTotalMessages().intValue())).
762+
equalTo(labels.get(ReservedLabels.NOTIFICATIONS.getId()).getCounters().getTotalMessages().intValue())).
763763
body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.unread",
764-
equalTo(labels.getLabelCounters(ReservedLabels.NOTIFICATIONS.getId()).getUnreadMessages().intValue())).
764+
equalTo(labels.get(ReservedLabels.NOTIFICATIONS.getId()).getCounters().getUnreadMessages().intValue())).
765765
body("'" + ReservedLabels.SPAM.getId() + "'.total",
766-
equalTo(labels.getLabelCounters(ReservedLabels.SPAM.getId()).getTotalMessages().intValue())).
766+
equalTo(labels.get(ReservedLabels.SPAM.getId()).getCounters().getTotalMessages().intValue())).
767767
body("'" + ReservedLabels.SPAM.getId() + "'.unread",
768-
equalTo(labels.getLabelCounters(ReservedLabels.SPAM.getId()).getUnreadMessages().intValue())).
768+
equalTo(labels.get(ReservedLabels.SPAM.getId()).getCounters().getUnreadMessages().intValue())).
769769
body("'" + ReservedLabels.TRASH.getId() + "'.total",
770-
equalTo(labels.getLabelCounters(ReservedLabels.TRASH.getId()).getTotalMessages().intValue())).
770+
equalTo(labels.get(ReservedLabels.TRASH.getId()).getCounters().getTotalMessages().intValue())).
771771
body("'" + ReservedLabels.TRASH.getId() + "'.unread",
772-
equalTo(labels.getLabelCounters(ReservedLabels.TRASH.getId()).getUnreadMessages().intValue())).
772+
equalTo(labels.get(ReservedLabels.TRASH.getId()).getCounters().getUnreadMessages().intValue())).
773773
when().
774774
get(REST_PATH + "/mailbox?metadata=true").asString();
775775

@@ -783,21 +783,21 @@ public void mailboxScrubTest() throws IOException
783783
expect().
784784
statusCode(200).and().
785785
body("'" + ReservedLabels.INBOX.getId() + "'.total",
786-
equalTo(labels.getLabelCounters(ReservedLabels.INBOX.getId()).getTotalMessages().intValue())).
786+
equalTo(labels.get(ReservedLabels.INBOX.getId()).getCounters().getTotalMessages().intValue())).
787787
body("'" + ReservedLabels.INBOX.getId() + "'.unread",
788-
equalTo(labels.getLabelCounters(ReservedLabels.INBOX.getId()).getUnreadMessages().intValue())).
788+
equalTo(labels.get(ReservedLabels.INBOX.getId()).getCounters().getUnreadMessages().intValue())).
789789
body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.total",
790-
equalTo(labels.getLabelCounters(ReservedLabels.NOTIFICATIONS.getId()).getTotalMessages().intValue())).
790+
equalTo(labels.get(ReservedLabels.NOTIFICATIONS.getId()).getCounters().getTotalMessages().intValue())).
791791
body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.unread",
792-
equalTo(labels.getLabelCounters(ReservedLabels.NOTIFICATIONS.getId()).getUnreadMessages().intValue())).
792+
equalTo(labels.get(ReservedLabels.NOTIFICATIONS.getId()).getCounters().getUnreadMessages().intValue())).
793793
body("'" + ReservedLabels.SPAM.getId() + "'.total",
794-
equalTo(labels.getLabelCounters(ReservedLabels.SPAM.getId()).getTotalMessages().intValue())).
794+
equalTo(labels.get(ReservedLabels.SPAM.getId()).getCounters().getTotalMessages().intValue())).
795795
body("'" + ReservedLabels.SPAM.getId() + "'.unread",
796-
equalTo(labels.getLabelCounters(ReservedLabels.SPAM.getId()).getUnreadMessages().intValue())).
796+
equalTo(labels.get(ReservedLabels.SPAM.getId()).getCounters().getUnreadMessages().intValue())).
797797
body("'" + ReservedLabels.TRASH.getId() + "'.total",
798-
equalTo(labels.getLabelCounters(ReservedLabels.TRASH.getId()).getTotalMessages().intValue())).
798+
equalTo(labels.get(ReservedLabels.TRASH.getId()).getCounters().getTotalMessages().intValue())).
799799
body("'" + ReservedLabels.TRASH.getId() + "'.unread",
800-
equalTo(labels.getLabelCounters(ReservedLabels.TRASH.getId()).getUnreadMessages().intValue())).
800+
equalTo(labels.get(ReservedLabels.TRASH.getId()).getCounters().getUnreadMessages().intValue())).
801801
when().
802802
get(REST_PATH + "/mailbox?metadata=true").asString();
803803

@@ -829,10 +829,10 @@ public void mailboxScrubTest() throws IOException
829829
* @return
830830
* @throws IOException
831831
*/
832-
protected static Pair<Labels, Map<Integer, List<UUID>>> populateMailbox() throws IOException
832+
protected static Pair<LabelMap, Map<Integer, List<UUID>>> populateMailbox() throws IOException
833833
{
834834
Map<Integer, List<UUID>> messages = new HashMap<Integer, List<UUID>>();
835-
Labels labels = new Labels();
835+
LabelMap labels = new LabelMap();
836836
LabelCounters inboxCounters = new LabelCounters();
837837
LabelCounters notifCounters = new LabelCounters();
838838
LabelCounters trashCounters = new LabelCounters();
@@ -850,7 +850,8 @@ protected static Pair<Labels, Map<Integer, List<UUID>>> populateMailbox() throws
850850
markAsRead(inboxMessages.get(4));
851851
inboxCounters.setTotalMessages(5L);
852852
inboxCounters.setUnreadMessages(3L);
853-
labels.incrementCounters(ReservedLabels.INBOX.getId(), inboxCounters);
853+
labels.put(ReservedLabels.INBOX);
854+
labels.get(ReservedLabels.INBOX.getId()).setCounters(inboxCounters);
854855

855856
// NOTIFICATIONS: add 3 messages, mark 1 as read
856857
messages.put(ReservedLabels.NOTIFICATIONS.getId(), new ArrayList<UUID>());
@@ -861,7 +862,8 @@ protected static Pair<Labels, Map<Integer, List<UUID>>> populateMailbox() throws
861862
markAsRead(notifMessages.get(1));
862863
notifCounters.setTotalMessages(3L);
863864
notifCounters.setUnreadMessages(2L);
864-
labels.incrementCounters(ReservedLabels.NOTIFICATIONS.getId(), notifCounters);
865+
labels.put(ReservedLabels.NOTIFICATIONS);
866+
labels.get(ReservedLabels.NOTIFICATIONS.getId()).setCounters(notifCounters);
865867

866868
// SPAM: add 5 messages, keep all unread
867869
messages.put(ReservedLabels.SPAM.getId(), new ArrayList<UUID>());
@@ -873,7 +875,8 @@ protected static Pair<Labels, Map<Integer, List<UUID>>> populateMailbox() throws
873875
spamMessages.add(addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
874876
spamCounters.setTotalMessages(5L);
875877
spamCounters.setUnreadMessages(5L);
876-
labels.incrementCounters(ReservedLabels.SPAM.getId(), spamCounters);
878+
labels.put(ReservedLabels.SPAM);
879+
labels.get(ReservedLabels.SPAM.getId()).setCounters(spamCounters);
877880

878881
// TRASH: add 4 messages, mark 2 as read
879882
messages.put(ReservedLabels.TRASH.getId(), new ArrayList<UUID>());
@@ -886,7 +889,8 @@ protected static Pair<Labels, Map<Integer, List<UUID>>> populateMailbox() throws
886889
markAsRead(trashMessages.get(2));
887890
trashCounters.setTotalMessages(4L);
888891
trashCounters.setUnreadMessages(2L);
889-
labels.incrementCounters(ReservedLabels.TRASH.getId(), trashCounters);
892+
labels.put(ReservedLabels.TRASH);
893+
labels.get(ReservedLabels.TRASH.getId()).setCounters(trashCounters);
890894

891895
return Pair.of(labels, messages);
892896
}

modules/core/src/main/java/com/elasticinbox/core/LabelDAO.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.io.IOException;
3232
import java.util.Map;
3333

34-
import com.elasticinbox.core.model.Labels;
34+
import com.elasticinbox.core.model.LabelMap;
3535
import com.elasticinbox.core.model.Mailbox;
3636

3737
/**
@@ -58,7 +58,7 @@ public interface LabelDAO
5858
* @return
5959
* @throws IOException
6060
*/
61-
public Labels getAllWithMetadata(Mailbox mailbox) throws IOException;
61+
public LabelMap getAllWithMetadata(Mailbox mailbox) throws IOException;
6262

6363
/**
6464
* Add new label to mailbox
@@ -101,5 +101,5 @@ public interface LabelDAO
101101
* @param mailbox
102102
* @param labels Lables and their new counter values
103103
*/
104-
public void setCounters(Mailbox mailbox, Labels labels);
104+
public void setCounters(Mailbox mailbox, LabelMap labels);
105105
}

modules/core/src/main/java/com/elasticinbox/core/MessageDAO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import java.util.UUID;
3838

3939
import com.elasticinbox.core.blob.BlobDataSource;
40-
import com.elasticinbox.core.model.Labels;
40+
import com.elasticinbox.core.model.LabelMap;
4141
import com.elasticinbox.core.model.Mailbox;
4242
import com.elasticinbox.core.model.Marker;
4343
import com.elasticinbox.core.model.Message;
@@ -240,5 +240,5 @@ public void removeLabel(Mailbox mailbox, Set<Integer> labelIds, UUID messageId)
240240
* @param rebuildIndex
241241
* @return
242242
*/
243-
public Labels scrub(Mailbox mailbox, boolean rebuildIndex);
243+
public LabelMap scrub(Mailbox mailbox, boolean rebuildIndex);
244244
}

modules/core/src/main/java/com/elasticinbox/core/cassandra/CassandraLabelDAO.java

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@
4848
import com.elasticinbox.core.cassandra.persistence.LabelCounterPersistence;
4949
import com.elasticinbox.core.cassandra.persistence.LabelIndexPersistence;
5050
import com.elasticinbox.core.cassandra.utils.BatchConstants;
51+
import com.elasticinbox.core.model.Label;
5152
import com.elasticinbox.core.model.LabelCounters;
52-
import com.elasticinbox.core.model.Labels;
53+
import com.elasticinbox.core.model.LabelMap;
5354
import com.elasticinbox.core.model.Mailbox;
5455
import com.elasticinbox.core.model.ReservedLabels;
5556
import com.elasticinbox.core.utils.LabelUtils;
@@ -63,8 +64,6 @@ public final class CassandraLabelDAO implements LabelDAO
6364
private final Keyspace keyspace;
6465
private final static StringSerializer strSe = StringSerializer.get();
6566

66-
private final static int MAX_NEW_LABEL_ID_ATTEMPTS = 200;
67-
6867
private final static Logger logger =
6968
LoggerFactory.getLogger(CassandraLabelDAO.class);
7069

@@ -73,67 +72,68 @@ public CassandraLabelDAO(Keyspace keyspace) {
7372
}
7473

7574
@Override
76-
public Labels getAllWithMetadata(final Mailbox mailbox)
75+
public LabelMap getAllWithMetadata(final Mailbox mailbox)
7776
throws IOException
7877
{
7978
// get labels
80-
Labels labels = new Labels();
81-
labels.add(AccountPersistence.getLabels(mailbox.getId()));
79+
LabelMap labels = AccountPersistence.getLabels(mailbox.getId());
80+
81+
// set labels' counters
82+
Map<Integer, LabelCounters> counters = LabelCounterPersistence.getAll(mailbox.getId());
83+
84+
for (int labelId : counters.keySet())
85+
{
86+
labels.get(labelId).setCounters(counters.get(labelId));
87+
}
8288

83-
// get labels' counters
84-
labels.setCounters(LabelCounterPersistence.getAll(mailbox.getId()));
85-
8689
return labels;
8790
}
8891

8992
@Override
9093
public Map<Integer, String> getAll(final Mailbox mailbox) {
91-
return AccountPersistence.getLabels(mailbox.getId());
94+
return AccountPersistence.getLabels(mailbox.getId()).getNameMap();
9295
}
9396

9497
@Override
9598
public int add(final Mailbox mailbox, String label)
9699
{
100+
Integer labelId;
101+
97102
// get all existing labels
98-
Labels existingLabels = new Labels();
99-
existingLabels.add(AccountPersistence.getLabels(mailbox.getId()));
103+
LabelMap existingLabels = AccountPersistence.getLabels(mailbox.getId());
100104

101105
LabelUtils.validateLabelName(label, existingLabels);
102106

103-
// generate new unique label id
104-
int labelId = LabelUtils.getNewLabelId();
105-
int attempts = 1;
106-
while(existingLabels.containsId(labelId))
107-
{
108-
logger.debug("Generating new label ID");
109-
110-
if (attempts > MAX_NEW_LABEL_ID_ATTEMPTS) {
111-
// too many attempts to get new random id! too many labels?
112-
logger.info("{} reached max random label id attempts with {} labels",
113-
mailbox, existingLabels.getIds().size());
114-
throw new IllegalLabelException("Too many labels");
115-
}
116-
117-
labelId = LabelUtils.getNewLabelId();
118-
attempts++;
107+
try {
108+
labelId = LabelUtils.getNewLabelId(existingLabels.getIds());
109+
} catch (IllegalLabelException ile) {
110+
// log and rethrow
111+
logger.warn("{} reached max random label id attempts with {} labels",
112+
mailbox, existingLabels.size());
113+
throw ile;
119114
}
120115

116+
// begin batch operation
117+
Mutator<String> m = createMutator(keyspace, strSe);
118+
121119
// add new label
122-
AccountPersistence.setLabel(mailbox.getId(), labelId, label);
120+
AccountPersistence.setLabelName(m, mailbox.getId(), labelId, label);
121+
122+
// commit batch operation
123+
m.execute();
123124

124125
return labelId;
125126
}
126-
127+
127128
@Override
128129
public void rename(final Mailbox mailbox, final Integer labelId, String label)
129130
throws IOException
130131
{
131132
// get all existing labels
132-
Labels existingLabels = new Labels();
133-
existingLabels.add(AccountPersistence.getLabels(mailbox.getId()));
133+
LabelMap existingLabels = AccountPersistence.getLabels(mailbox.getId());
134134

135-
// validate only if name is changed (skip for letter case changes)
136-
if (!existingLabels.getName(labelId).equalsIgnoreCase(label)) {
135+
// validate only if name is changed (skips letter case changes)
136+
if (!existingLabels.containsName(label)) {
137137
LabelUtils.validateLabelName(label, existingLabels);
138138
}
139139

@@ -147,8 +147,14 @@ public void rename(final Mailbox mailbox, final Integer labelId, String label)
147147
throw new IllegalLabelException("Label does not exist");
148148
}
149149

150+
// begin batch operation
151+
Mutator<String> m = createMutator(keyspace, strSe);
152+
150153
// set new name
151-
AccountPersistence.setLabel(mailbox.getId(), labelId, label);
154+
AccountPersistence.setLabelName(m, mailbox.getId(), labelId, label);
155+
156+
// commit batch operation
157+
m.execute();
152158
}
153159

154160
@Override
@@ -194,34 +200,35 @@ public void delete(final Mailbox mailbox, final Integer labelId)
194200
}
195201

196202
@Override
197-
public void setCounters(Mailbox mailbox, Labels calculatedCounters)
203+
public void setCounters(Mailbox mailbox, LabelMap newCounters)
198204
{
199205
Map<Integer, LabelCounters> existingCounters =
200206
LabelCounterPersistence.getAll(mailbox.getId());
201207

202208
// begin batch operation
203209
Mutator<String> m = createMutator(keyspace, strSe);
204210

205-
// update calculated counters
206-
for (int labelId : calculatedCounters.getIds())
211+
// update with the new counter values
212+
for (Label label : newCounters.values())
207213
{
208-
LabelCounters diff = new LabelCounters(calculatedCounters.getLabelCounters(labelId));
214+
int labelId = label.getId();
215+
LabelCounters diff = new LabelCounters(label.getCounters());
209216

210217
if (existingCounters.containsKey(labelId)) {
211218
diff.add(existingCounters.get(labelId).getInverse());
212219
}
213220

214221
logger.debug(
215222
"Recalculated counters for label {}:\n\tCurrent: {}\n\tCalculated: {}\n\tDiff: {}",
216-
new Object[] { labelId, existingCounters.get(labelId),
217-
calculatedCounters.getLabelCounters(labelId), diff });
223+
new Object[] { labelId, existingCounters.get(labelId), label.getCounters(), diff });
218224

219225
LabelCounterPersistence.add(m, mailbox.getId(), labelId, diff);
220226
}
221-
227+
222228
// reset non-existing counters
223-
for (int labelId : existingCounters.keySet()) {
224-
if (!calculatedCounters.containsId(labelId)) {
229+
for (int labelId : existingCounters.keySet())
230+
{
231+
if (!newCounters.containsId(labelId)) {
225232
LabelCounterPersistence.subtract(
226233
m, mailbox.getId(), labelId, existingCounters.get(labelId));
227234
}

0 commit comments

Comments
 (0)