@Override public boolean equals(Object obj) { if (obj instanceof MailboxAnnotation) { MailboxAnnotation o = (MailboxAnnotation) obj; return Objects.equal(key, o.getKey()) && Objects.equal(value, o.getValue()); } else { return false; } }
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()); } } } }
@Override public List<MailboxAnnotation> getAnnotationsByKeys(MailboxId mailboxId, final Set<MailboxAnnotationKey> keys) { return ImmutableList.copyOf( Iterables.filter(retrieveAllAnnotations((InMemoryId)mailboxId), input -> keys.contains(input.getKey()))); }
private Predicate<MailboxAnnotation> getPredicateFilterByOne(final Set<MailboxAnnotationKey> keys) { return input -> keys.stream().anyMatch(filterAnnotationsByParentKey(input.getKey())); }
@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(); } }
@Override public boolean exist(MailboxId mailboxId, MailboxAnnotation mailboxAnnotation) { CassandraId cassandraId = (CassandraId)mailboxId; Optional<Row> row = Optional.ofNullable( session.execute( getStoredAnnotationsQueryByKey(cassandraId, mailboxAnnotation.getKey().asString())) .one()); return row.isPresent(); }
@Override public List<MailboxAnnotation> getAnnotationsByKeysWithOneDepth(MailboxId mailboxId, Set<MailboxAnnotationKey> keys) { return getFilteredLikes((JPAId) mailboxId, keys, key -> annotation -> key.isParentOrIsEqual(annotation.getKey())); }
private Stream<MailboxAnnotation> getAnnotationsByKeyWithOneDepth(CassandraId mailboxId, MailboxAnnotationKey key) { return cassandraUtils.convertToStream(session.execute(getStoredAnnotationsQueryLikeKey(mailboxId, key.asString()))) .map(this::toAnnotation) .filter(annotation -> key.isParentOrIsEqual(annotation.getKey())); }
@Override public List<MailboxAnnotation> getAnnotationsByKeysWithAllDepth(MailboxId mailboxId, Set<MailboxAnnotationKey> keys) { return getFilteredLikes((JPAId) mailboxId, keys, key -> annotation -> key.isAncestorOrIsEqual(annotation.getKey())); }
@Override public boolean exist(MailboxId mailboxId, MailboxAnnotation mailboxAnnotation) { return mailboxesAnnotations.contains((InMemoryId)mailboxId, mailboxAnnotation.getKey().asString()); }
private Stream<MailboxAnnotation> getAnnotationsByKeyWithAllDepth(CassandraId mailboxId, MailboxAnnotationKey key) { return cassandraUtils.convertToStream(session.execute(getStoredAnnotationsQueryLikeKey(mailboxId, key.asString()))) .map(this::toAnnotation) .filter(annotation -> key.isAncestorOrIsEqual(annotation.getKey())); }
@Override public boolean exist(MailboxId mailboxId, MailboxAnnotation mailboxAnnotation) { JPAId jpaId = (JPAId) mailboxId; Optional<JPAMailboxAnnotation> row = Optional.ofNullable(getEntityManager().find(JPAMailboxAnnotation.class, new JPAMailboxAnnotationId(jpaId.getRawId(), mailboxAnnotation.getKey().asString()))); return row.isPresent(); }
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("")); } }
@Override public void insertAnnotation(MailboxId mailboxId, MailboxAnnotation mailboxAnnotation) { Preconditions.checkArgument(!mailboxAnnotation.isNil()); JPAId jpaId = (JPAId) mailboxId; if (getAnnotationsByKeys(mailboxId, ImmutableSet.of(mailboxAnnotation.getKey())).isEmpty()) { getEntityManager().persist( new JPAMailboxAnnotation(jpaId.getRawId(), mailboxAnnotation.getKey().asString(), mailboxAnnotation.getValue().orElse(null))); } else { getEntityManager().find(JPAMailboxAnnotation.class, new JPAMailboxAnnotationId(jpaId.getRawId(), mailboxAnnotation.getKey().asString())) .setValue(mailboxAnnotation.getValue().orElse(null)); } }
@Override public void insertAnnotation(MailboxId mailboxId, MailboxAnnotation mailboxAnnotation) { Preconditions.checkArgument(!mailboxAnnotation.isNil()); session.execute(insertInto(CassandraAnnotationTable.TABLE_NAME) .value(CassandraAnnotationTable.MAILBOX_ID, ((CassandraId) mailboxId).asUuid()) .value(CassandraAnnotationTable.KEY, mailboxAnnotation.getKey().asString()) .value(CassandraAnnotationTable.VALUE, mailboxAnnotation.getValue().get())); }
@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); } } })); }