@@ -62,11 +62,11 @@ public final class LabelCounterPersistence
62
62
public final static String CN_TYPE_LABEL = "l" ;
63
63
64
64
/** Label counter subtype for total bytes */
65
- public final static String CN_SUBTYPE_BYTES = "b" ;
65
+ public final static char CN_SUBTYPE_BYTES = 'b' ;
66
66
/** Label counter subtype for total messages */
67
- public final static String CN_SUBTYPE_MESSAGES = "m" ;
67
+ public final static char CN_SUBTYPE_MESSAGES = 'm' ;
68
68
/** Label counter subtype for unread messages */
69
- public final static String CN_SUBTYPE_UNREAD = "u" ;
69
+ public final static char CN_SUBTYPE_UNREAD = 'u' ;
70
70
71
71
private final static Keyspace keyspace = CassandraDAOFactory .getKeyspace ();
72
72
private final static StringSerializer strSe = StringSerializer .get ();
@@ -96,7 +96,7 @@ public static Map<Integer, LabelCounters> getAll(final String mailbox)
96
96
97
97
QueryResult <CounterSlice <Composite >> r = sliceQuery .execute ();
98
98
99
- return compositeColumnsToCounters (r .get ().getColumns ());
99
+ return compositeColumnsToCounters (mailbox , r .get ().getColumns ());
100
100
}
101
101
102
102
/**
@@ -124,7 +124,7 @@ public static LabelCounters get(final String mailbox, final Integer labelId)
124
124
125
125
QueryResult <CounterSlice <Composite >> r = sliceQuery .execute ();
126
126
127
- Map <Integer , LabelCounters > counters = compositeColumnsToCounters (r .get ().getColumns ());
127
+ Map <Integer , LabelCounters > counters = compositeColumnsToCounters (mailbox , r .get ().getColumns ());
128
128
LabelCounters labelCounters = counters .containsKey (labelId ) ? counters .get (labelId ) : new LabelCounters ();
129
129
130
130
logger .debug ("Fetched counters for single label {} with {}" , labelId , labelCounters );
@@ -256,12 +256,12 @@ public static void deleteAll(Mutator<String> mutator, final String mailbox)
256
256
* @return
257
257
*/
258
258
private static HCounterColumn <Composite > countersToCompositeColumn (
259
- final Integer labelId , final String subtype , final Long count )
259
+ final Integer labelId , final char subtype , final Long count )
260
260
{
261
261
Composite composite = new Composite ();
262
262
composite .addComponent (CN_TYPE_LABEL , strSe );
263
263
composite .addComponent (labelId .toString (), strSe );
264
- composite .addComponent (subtype , strSe );
264
+ composite .addComponent (Character . toString ( subtype ) , strSe );
265
265
return createCounterColumn (composite , count , new CompositeSerializer ());
266
266
}
267
267
@@ -272,7 +272,7 @@ private static HCounterColumn<Composite> countersToCompositeColumn(
272
272
* @return
273
273
*/
274
274
private static Map <Integer , LabelCounters > compositeColumnsToCounters (
275
- final List <HCounterColumn <Composite >> columnList )
275
+ final String mailbox , final List <HCounterColumn <Composite >> columnList )
276
276
{
277
277
Map <Integer , LabelCounters > result =
278
278
new HashMap <Integer , LabelCounters >(LabelConstants .MAX_RESERVED_LABEL_ID );
@@ -283,6 +283,7 @@ private static Map<Integer, LabelCounters> compositeColumnsToCounters(
283
283
for (HCounterColumn <Composite > c : columnList )
284
284
{
285
285
int labelId = Integer .parseInt (c .getName ().get (1 , strSe ));
286
+ char subtype = c .getName ().get (2 , strSe ).charAt (0 );
286
287
287
288
// since columns are ordered by labels, we can
288
289
// flush label counters to result map as we traverse
@@ -293,14 +294,25 @@ private static Map<Integer, LabelCounters> compositeColumnsToCounters(
293
294
prevLabelId = labelId ;
294
295
}
295
296
296
- String subtype = c .getName ().get (2 , strSe );
297
+ long cValue = 0 ;
298
+
299
+ // never return negative counter value
300
+ if (c .getValue () >= 0 ) {
301
+ cValue = c .getValue ();
302
+ } else {
303
+ logger .warn ("Negative counter value found {}/{}: " , labelId );
304
+ }
297
305
298
- if (subtype .equals (CN_SUBTYPE_BYTES )) {
299
- labelCounters .setTotalBytes (c .getValue ());
300
- } else if (subtype .equals (CN_SUBTYPE_MESSAGES )) {
301
- labelCounters .setTotalMessages (c .getValue ());
302
- } else if (subtype .equals (CN_SUBTYPE_UNREAD )) {
303
- labelCounters .setUnreadMessages (c .getValue ());
306
+ switch (subtype ) {
307
+ case CN_SUBTYPE_BYTES :
308
+ labelCounters .setTotalBytes (cValue );
309
+ break ;
310
+ case CN_SUBTYPE_MESSAGES :
311
+ labelCounters .setTotalMessages (cValue );
312
+ break ;
313
+ case CN_SUBTYPE_UNREAD :
314
+ labelCounters .setUnreadMessages (cValue );
315
+ break ;
304
316
}
305
317
}
306
318
0 commit comments