Skip to content

Commit

Permalink
MAILBOX-270: MailboxMapper has to be unique on mapper factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Quynh Nguyen committed Sep 8, 2016
1 parent baae24e commit f88464d
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 153 deletions.
Expand Up @@ -56,7 +56,7 @@ public AttachmentMapper createAttachmentMapper(MailboxSession session) throws Ma
}

@Override
public AnnotationMapper createAnnotationMapper(MailboxId mailboxId, MailboxSession session)
public AnnotationMapper createAnnotationMapper(MailboxSession session)
throws MailboxException {
throw new NotImplementedException();
}
Expand Down
Expand Up @@ -99,8 +99,8 @@ Session getSession() {
}

@Override
public AnnotationMapper createAnnotationMapper(MailboxId mailboxId, MailboxSession mailboxSession)
public AnnotationMapper createAnnotationMapper(MailboxSession mailboxSession)
throws MailboxException {
return new CassandraAnnotationMapper((CassandraId)mailboxId, session);
return new CassandraAnnotationMapper(session);
}
}
Expand Up @@ -39,6 +39,7 @@
import org.apache.james.mailbox.cassandra.table.CassandraAnnotationTable;
import org.apache.james.mailbox.model.MailboxAnnotation;
import org.apache.james.mailbox.model.MailboxAnnotationKey;
import org.apache.james.mailbox.model.MailboxId;
import org.apache.james.mailbox.store.mail.AnnotationMapper;
import org.apache.james.mailbox.store.transaction.NonTransactionalMapper;

Expand All @@ -49,48 +50,50 @@

public class CassandraAnnotationMapper extends NonTransactionalMapper implements AnnotationMapper {

private final CassandraId mailboxId;
private final Session session;

public CassandraAnnotationMapper(CassandraId mailboxId, Session session) {
this.mailboxId = mailboxId;
public CassandraAnnotationMapper(Session session) {
this.session = session;
}

public List<MailboxAnnotation> getAllAnnotations() {
return CassandraUtils.convertToStream(session.execute(getStoredAnnotationsQuery()))
public List<MailboxAnnotation> getAllAnnotations(MailboxId mailboxId) {
CassandraId cassandraId = (CassandraId)mailboxId;
return CassandraUtils.convertToStream(session.execute(getStoredAnnotationsQuery(cassandraId)))
.map(this::toAnnotation)
.collect(Collectors.toList());
}

public List<MailboxAnnotation> getAnnotationsByKeys(Set<MailboxAnnotationKey> keys) {
return CassandraUtils.convertToStream(session.execute(getStoredAnnotationsQueryForKeys(keys)))
public List<MailboxAnnotation> getAnnotationsByKeys(MailboxId mailboxId, Set<MailboxAnnotationKey> keys) {
CassandraId cassandraId = (CassandraId)mailboxId;
return CassandraUtils.convertToStream(session.execute(getStoredAnnotationsQueryForKeys(cassandraId, keys)))
.map(this::toAnnotation)
.collect(Collectors.toList());
}

public List<MailboxAnnotation> getAnnotationsByKeysWithOneDepth(Set<MailboxAnnotationKey> keys) {
public List<MailboxAnnotation> getAnnotationsByKeysWithOneDepth(MailboxId mailboxId, Set<MailboxAnnotationKey> keys) {
CassandraId cassandraId = (CassandraId)mailboxId;
return keys.stream()
.flatMap(this::getAnnotationsByKeyWithOneDepth)
.flatMap(annotation -> getAnnotationsByKeyWithOneDepth(cassandraId, annotation))
.collect(Guavate.toImmutableList());
}

public List<MailboxAnnotation> getAnnotationsByKeysWithAllDepth(Set<MailboxAnnotationKey> keys) {
public List<MailboxAnnotation> getAnnotationsByKeysWithAllDepth(MailboxId mailboxId, Set<MailboxAnnotationKey> keys) {
CassandraId cassandraId = (CassandraId)mailboxId;
return keys.stream()
.flatMap(this::getAnnotationsByKeyWithAllDepth)
.flatMap(annotation -> getAnnotationsByKeyWithAllDepth(cassandraId, annotation))
.collect(Guavate.toImmutableList());
}

public void deleteAnnotation(MailboxAnnotationKey key) {
public void deleteAnnotation(MailboxId mailboxId, MailboxAnnotationKey key) {
session.execute(delete().from(CassandraAnnotationTable.TABLE_NAME)
.where(eq(CassandraAnnotationTable.MAILBOX_ID, mailboxId.asUuid()))
.where(eq(CassandraAnnotationTable.MAILBOX_ID, ((CassandraId) mailboxId).asUuid()))
.and(eq(CassandraAnnotationTable.KEY, key.asString())));
}

public void insertAnnotation(MailboxAnnotation mailboxAnnotation) {
public void insertAnnotation(MailboxId mailboxId, MailboxAnnotation mailboxAnnotation) {
Preconditions.checkArgument(!mailboxAnnotation.isNil());
session.execute(insertInto(CassandraAnnotationTable.TABLE_NAME)
.value(CassandraAnnotationTable.MAILBOX_ID, mailboxId.asUuid())
.value(CassandraAnnotationTable.MAILBOX_ID, ((CassandraId) mailboxId).asUuid())
.value(CassandraAnnotationTable.KEY, mailboxAnnotation.getKey().asString())
.value(CassandraAnnotationTable.VALUE, mailboxAnnotation.getValue().get()));
}
Expand All @@ -100,20 +103,20 @@ private MailboxAnnotation toAnnotation(Row row) {
row.getString(CassandraAnnotationTable.VALUE));
}

private Select.Where getStoredAnnotationsQuery() {
private Select.Where getStoredAnnotationsQuery(CassandraId mailboxId) {
return select(CassandraAnnotationTable.SELECT_FIELDS)
.from(CassandraAnnotationTable.TABLE_NAME)
.where(eq(CassandraAnnotationTable.MAILBOX_ID, mailboxId.asUuid()));
}

private Select.Where getStoredAnnotationsQueryForKeys(Set<MailboxAnnotationKey> keys) {
return getStoredAnnotationsQuery().and(in(CassandraAnnotationTable.KEY, keys.stream()
private Select.Where getStoredAnnotationsQueryForKeys(CassandraId mailboxId, Set<MailboxAnnotationKey> keys) {
return getStoredAnnotationsQuery(mailboxId).and(in(CassandraAnnotationTable.KEY, keys.stream()
.map(MailboxAnnotationKey::asString)
.collect(Guavate.toImmutableList())));
}

private Select.Where getStoredAnnotationsQueryLikeKey(String key) {
return getStoredAnnotationsQuery()
private Select.Where getStoredAnnotationsQueryLikeKey(CassandraId mailboxId, String key) {
return getStoredAnnotationsQuery(mailboxId)
.and(gte(CassandraAnnotationTable.KEY, key))
.and(lte(CassandraAnnotationTable.KEY, buildNextKey(key)));
}
Expand All @@ -122,13 +125,13 @@ private String buildNextKey(String key) {
return key + MailboxAnnotationKey.SLASH_CHARACTER + Ascii.MAX;
}

private Stream<MailboxAnnotation> getAnnotationsByKeyWithAllDepth(MailboxAnnotationKey key) {
return CassandraUtils.convertToStream(session.execute(getStoredAnnotationsQueryLikeKey(key.asString())))
private Stream<MailboxAnnotation> getAnnotationsByKeyWithAllDepth(CassandraId mailboxId, MailboxAnnotationKey key) {
return CassandraUtils.convertToStream(session.execute(getStoredAnnotationsQueryLikeKey(mailboxId, key.asString())))
.map(this::toAnnotation);
}

private Stream<MailboxAnnotation> getAnnotationsByKeyWithOneDepth(MailboxAnnotationKey key) {
return CassandraUtils.convertToStream(session.execute(getStoredAnnotationsQueryLikeKey(key.asString())))
private Stream<MailboxAnnotation> getAnnotationsByKeyWithOneDepth(CassandraId mailboxId, MailboxAnnotationKey key) {
return CassandraUtils.convertToStream(session.execute(getStoredAnnotationsQueryLikeKey(mailboxId, key.asString())))
.map(this::toAnnotation)
.filter(annotation -> isChild(key, annotation));
}
Expand Down
Expand Up @@ -107,6 +107,6 @@ public AnnotationMapper createAnnotationMapper() throws MailboxException {
new CassandraModSeqProvider(cassandra.getConf()),
cassandra.getConf(),
cassandra.getTypesProvider()
).getAnnotationMapper(CassandraId.timeBased(), new MockMailboxSession("benwa"));
).getAnnotationMapper(new MockMailboxSession("benwa"));
}
}
Expand Up @@ -43,7 +43,6 @@
import org.apache.james.mailbox.hbase.mail.HBaseMailboxMapper;
import org.apache.james.mailbox.hbase.mail.HBaseMessageMapper;
import org.apache.james.mailbox.hbase.user.HBaseSubscriptionMapper;
import org.apache.james.mailbox.model.MailboxId;
import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
import org.apache.james.mailbox.store.mail.AnnotationMapper;
import org.apache.james.mailbox.store.mail.AttachmentMapper;
Expand Down Expand Up @@ -175,7 +174,7 @@ public UidProvider getUidProvider() {
}

@Override
public AnnotationMapper createAnnotationMapper(MailboxId mailboxId, MailboxSession session)
public AnnotationMapper createAnnotationMapper(MailboxSession session)
throws MailboxException {
throw new NotImplementedException();
}
Expand Down
Expand Up @@ -87,7 +87,7 @@ public MailboxSessionJCRRepository getRepository() {
}

@Override
public AnnotationMapper createAnnotationMapper(MailboxId mailboxId, MailboxSession session)
public AnnotationMapper createAnnotationMapper(MailboxSession session)
throws MailboxException {
throw new NotImplementedException();
}
Expand Down
Expand Up @@ -85,7 +85,7 @@ private EntityManager createEntityManager() {
}

@Override
public AnnotationMapper createAnnotationMapper(MailboxId mailboxId, MailboxSession session)
public AnnotationMapper createAnnotationMapper(MailboxSession session)
throws MailboxException {
throw new NotImplementedException();
}
Expand Down
Expand Up @@ -71,7 +71,7 @@ public AttachmentMapper createAttachmentMapper(MailboxSession session) throws Ma


@Override
public AnnotationMapper createAnnotationMapper(MailboxId mailboxId, MailboxSession session)
public AnnotationMapper createAnnotationMapper(MailboxSession session)
throws MailboxException {
throw new NotImplementedException();
}
Expand Down
Expand Up @@ -42,12 +42,14 @@ public class InMemoryMailboxSessionMapperFactory extends MailboxSessionMapperFac
private final MessageMapper messageMapper;
private final SubscriptionMapper subscriptionMapper;
private final AttachmentMapper attachmentMapper;
private final AnnotationMapper annotationMapper;

public InMemoryMailboxSessionMapperFactory() {
mailboxMapper = new InMemoryMailboxMapper();
messageMapper = new InMemoryMessageMapper(null, new InMemoryUidProvider(), new InMemoryModSeqProvider());
subscriptionMapper = new InMemorySubscriptionMapper();
attachmentMapper = new InMemoryAttachmentMapper();
annotationMapper = new InMemoryAnnotationMapper();
}

@Override
Expand Down Expand Up @@ -77,9 +79,9 @@ public void deleteAll() throws MailboxException {
}

@Override
public AnnotationMapper createAnnotationMapper(MailboxId mailboxId, MailboxSession session)
public AnnotationMapper createAnnotationMapper(MailboxSession session)
throws MailboxException {
return new InMemoryAnnotationMapper((InMemoryId)mailboxId);
return annotationMapper;
}

}
Expand Up @@ -31,6 +31,7 @@
import org.apache.james.mailbox.inmemory.InMemoryId;
import org.apache.james.mailbox.model.MailboxAnnotation;
import org.apache.james.mailbox.model.MailboxAnnotationKey;
import org.apache.james.mailbox.model.MailboxId;
import org.apache.james.mailbox.store.mail.AnnotationMapper;

import com.google.common.base.Function;
Expand All @@ -42,12 +43,10 @@
import com.google.common.collect.Table;

public class InMemoryAnnotationMapper implements AnnotationMapper {
private final InMemoryId mailboxId;
private final Table<InMemoryId, String, String> mailboxesAnnotations;
private final ReadWriteLock lock = new ReentrantReadWriteLock();

public InMemoryAnnotationMapper(InMemoryId mailboxId) {
this.mailboxId = mailboxId;
public InMemoryAnnotationMapper() {
mailboxesAnnotations = HashBasedTable.create();
}

Expand Down Expand Up @@ -78,14 +77,14 @@ public MailboxAnnotation apply(Entry<String, String> input) {
}

@Override
public List<MailboxAnnotation> getAllAnnotations() {
return ImmutableList.copyOf(retrieveAllAnnotations(mailboxId));
public List<MailboxAnnotation> getAllAnnotations(MailboxId mailboxId) {
return ImmutableList.copyOf(retrieveAllAnnotations((InMemoryId)mailboxId));
}

@Override
public List<MailboxAnnotation> getAnnotationsByKeys(final Set<MailboxAnnotationKey> keys) {
public List<MailboxAnnotation> getAnnotationsByKeys(MailboxId mailboxId, final Set<MailboxAnnotationKey> keys) {
return ImmutableList.copyOf(
Iterables.filter(retrieveAllAnnotations(mailboxId),
Iterables.filter(retrieveAllAnnotations((InMemoryId)mailboxId),
new Predicate<MailboxAnnotation>() {
@Override
public boolean apply(MailboxAnnotation input) {
Expand All @@ -95,13 +94,13 @@ public boolean apply(MailboxAnnotation input) {
}

@Override
public List<MailboxAnnotation> getAnnotationsByKeysWithAllDepth(final Set<MailboxAnnotationKey> keys) {
return ImmutableList.copyOf(Iterables.filter(retrieveAllAnnotations(mailboxId), getPredicateFilterByAll(keys)));
public List<MailboxAnnotation> getAnnotationsByKeysWithAllDepth(MailboxId mailboxId, final Set<MailboxAnnotationKey> keys) {
return ImmutableList.copyOf(Iterables.filter(retrieveAllAnnotations((InMemoryId)mailboxId), getPredicateFilterByAll(keys)));
}

@Override
public List<MailboxAnnotation> getAnnotationsByKeysWithOneDepth(final Set<MailboxAnnotationKey> keys) {
return ImmutableList.copyOf(Iterables.filter(getAnnotationsByKeysWithAllDepth(keys), getPredicateFilterByOne(keys)));
public List<MailboxAnnotation> getAnnotationsByKeysWithOneDepth(MailboxId mailboxId, final Set<MailboxAnnotationKey> keys) {
return ImmutableList.copyOf(Iterables.filter(getAnnotationsByKeysWithAllDepth(mailboxId, keys), getPredicateFilterByOne(keys)));
}

private Predicate<MailboxAnnotation> getPredicateFilterByAll(final Set<MailboxAnnotationKey> keys) {
Expand Down Expand Up @@ -141,18 +140,18 @@ public boolean apply(MailboxAnnotationKey key) {
}

@Override
public void insertAnnotation(MailboxAnnotation mailboxAnnotation) {
public void insertAnnotation(MailboxId mailboxId, MailboxAnnotation mailboxAnnotation) {
Preconditions.checkArgument(!mailboxAnnotation.isNil());
lock.writeLock().lock();
try {
mailboxesAnnotations.put(mailboxId, mailboxAnnotation.getKey().asString(), mailboxAnnotation.getValue().get());
mailboxesAnnotations.put((InMemoryId)mailboxId, mailboxAnnotation.getKey().asString(), mailboxAnnotation.getValue().get());
} finally {
lock.writeLock().unlock();
}
}

@Override
public void deleteAnnotation(MailboxAnnotationKey key) {
public void deleteAnnotation(MailboxId mailboxId, MailboxAnnotationKey key) {
lock.writeLock().lock();
try {
mailboxesAnnotations.remove(mailboxId, key.asString());
Expand Down
Expand Up @@ -57,7 +57,6 @@ public boolean supportPartialAttachmentFetch() {

@Override
public AnnotationMapper createAnnotationMapper() throws MailboxException {
return new InMemoryMailboxSessionMapperFactory().createAnnotationMapper(InMemoryId.of(random.nextInt()),
new MockMailboxSession("user"));
return new InMemoryMailboxSessionMapperFactory().createAnnotationMapper(new MockMailboxSession("user"));
}
}
Expand Up @@ -68,16 +68,16 @@ public AttachmentMapper getAttachmentMapper(MailboxSession session) throws Mailb
return mapper;
}

public AnnotationMapper getAnnotationMapper(MailboxId mailboxId, MailboxSession session) throws MailboxException {
public AnnotationMapper getAnnotationMapper(MailboxSession session) throws MailboxException {
AnnotationMapper mapper = (AnnotationMapper)session.getAttributes().get(ANNOTATIONMAPPER);
if (mapper == null) {
mapper = createAnnotationMapper(mailboxId, session);
mapper = createAnnotationMapper(session);
session.getAttributes().put(ANNOTATIONMAPPER, mapper);
}
return mapper;
}

public abstract AnnotationMapper createAnnotationMapper(MailboxId mailboxId, MailboxSession session) throws MailboxException;
public abstract AnnotationMapper createAnnotationMapper(MailboxSession session) throws MailboxException;

/**
* Create a {@link MessageMapper} instance which will get reused during the whole {@link MailboxSession}
Expand Down

0 comments on commit f88464d

Please sign in to comment.