@Override public void insertAnnotation(MailboxId mailboxId, MailboxAnnotation mailboxAnnotation) { Preconditions.checkArgument(!mailboxAnnotation.isNil()); lock.writeLock().lock(); try { mailboxesAnnotations.put((InMemoryId)mailboxId, mailboxAnnotation.getKey().asString(), mailboxAnnotation.getValue().get()); } finally { lock.writeLock().unlock(); } }
private Iterable<MailboxAnnotation> retrieveAllAnnotations(InMemoryId mailboxId) { lock.readLock().lock(); try { return Iterables.transform( mailboxesAnnotations.row(mailboxId).entrySet(), input -> MailboxAnnotation.newInstance(new MailboxAnnotationKey(input.getKey()), input.getValue())); } finally { lock.readLock().unlock(); } }
private List<MailboxAnnotation> filterItemsBySize(List<MailboxAnnotation> mailboxAnnotations, final Optional<Integer> maxsize) { Predicate<MailboxAnnotation> lowerPredicate = annotation -> maxsize .map(maxSizeInput -> (annotation.size() <= maxSizeInput)) .orElse(true); return mailboxAnnotations.stream() .filter(lowerPredicate) .collect(Guavate.toImmutableList()); }
private void deleteRelatedAnnotations(MailboxId mailboxId, AnnotationMapper annotationMapper) { List<MailboxAnnotation> annotations = annotationMapper.getAllAnnotations(mailboxId); for (MailboxAnnotation annotation : annotations) { try { annotationMapper.deleteAnnotation(mailboxId, annotation.getKey()); } catch (Exception e) { logger.error("Unable to delete annotation {} cause {}", annotation.getKey(), e.getMessage()); } } } }
@Test public void newInstanceMailboxAnnotationShouldCreateNewInstance() throws Exception { MailboxAnnotation annotation = MailboxAnnotation.newInstance(ANNOTATION_KEY, ANNOTATION_VALUE); assertThat(annotation.getKey()).isEqualTo(ANNOTATION_KEY); assertThat(annotation.getValue()).contains(ANNOTATION_VALUE); }
@Override public void updateAnnotations(MailboxPath mailboxPath, MailboxSession session, List<MailboxAnnotation> mailboxAnnotations) throws MailboxException { AnnotationMapper annotationMapper = mailboxSessionMapperFactory.getAnnotationMapper(session); MailboxId mailboxId = checkThenGetMailboxId(mailboxPath, session); annotationMapper.execute(Mapper.toTransaction(() -> { for (MailboxAnnotation annotation : mailboxAnnotations) { if (annotation.isNil()) { annotationMapper.deleteAnnotation(mailboxId, annotation.getKey()); } else if (canInsertOrUpdate(mailboxId, annotation, annotationMapper)) { annotationMapper.insertAnnotation(mailboxId, annotation); } } })); }
@Test public void isNilShouldReturnFalseForNotNilObject() throws Exception { MailboxAnnotation nilAnnotation = MailboxAnnotation.newInstance(ANNOTATION_KEY, ANNOTATION_VALUE); assertThat(nilAnnotation.isNil()).isFalse(); }
@Test public void decodeMessageShouldReturnRequestWhenCommandHasEmptyAnnotationValue() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment \" \") \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); SetAnnotationRequest request = (SetAnnotationRequest) parser.decode(command, lineReader, TAG, null); assertThat(request.getMailboxAnnotations().get(0).getValue()).isPresent(); }
public int size() { if (isNil()) { return 0; } return value.get().length(); }
@Test public void sizeOfAnnotationShouldBeReturnLengthOfValue() throws Exception { MailboxAnnotation mailboxAnnotation = MailboxAnnotation.newInstance(ANNOTATION_KEY, ANNOTATION_VALUE); assertThat(mailboxAnnotation.size()).isEqualTo(8); }
private MailboxAnnotation readNextAnnotation(ImapRequestLineReader request) throws DecodingException { try { String key = request.atom(); String value = request.nstring(); return Optional.ofNullable(value) .map(transforMailboxAnnotation(key)) .orElse(MailboxAnnotation.nil(createAnnotationKey(key))); } catch (IllegalArgumentException e) { throw new DecodingException(HumanReadableText.ILLEGAL_ARGUMENTS, "The key is not valid: " + e.getMessage()); } }
@Override public List<MailboxAnnotation> getAnnotationsByKeys(MailboxId mailboxId, final Set<MailboxAnnotationKey> keys) { return ImmutableList.copyOf( Iterables.filter(retrieveAllAnnotations((InMemoryId)mailboxId), input -> keys.contains(input.getKey()))); }
@Test public void decodeMessageShouldReturnRequestWhenCommandAnnotationValueIsNILString() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment \"NIL\") \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); SetAnnotationRequest request = (SetAnnotationRequest) parser.decode(command, lineReader, TAG, null); assertThat(request.getMailboxAnnotations().get(0).getValue()).isPresent(); }
@Test public void encodingShouldWellFormWhenNilReturnedAnnotation() throws Exception { AnnotationResponse response = new AnnotationResponse("INBOX", ImmutableList.of(MailboxAnnotation.nil(PRIVATE_KEY))); encoder.encode(response, composer, imapSession); assertThat(byteImapResponseWriter.getString()).isEqualTo("* METADATA \"INBOX\" ()\r\n"); } }
private void composeAnnotation(ImapResponseComposer composer, ImapSession session, MailboxAnnotation annotation) throws IOException { if (annotation.isNil()) { LOGGER.warn("There is nil data of key {} on store: ", annotation.getKey().asString()); } else { composer.message(annotation.getKey().asString()); composer.quote(annotation.getValue().orElse("")); } }
private Predicate<MailboxAnnotation> getPredicateFilterByOne(final Set<MailboxAnnotationKey> keys) { return input -> keys.stream().anyMatch(filterAnnotationsByParentKey(input.getKey())); }