Skip to content

Commit

Permalink
JAMES-4103 Allow customizing only the attachment id assignation
Browse files Browse the repository at this point in the history
Ease writing custom logic for this a LOT
  • Loading branch information
chibenwa committed Jan 21, 2025
1 parent f44c531 commit 6b2b119
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ public CassandraMailboxSessionMapperFactory(UidProvider uidProvider, ModSeqProvi
deletedMessageDAO);
this.cassandraMailboxMapper = new CassandraMailboxMapper(mailboxDAO, mailboxPathV3DAO, userMailboxRightsDAO, aclMapper, cassandraConfiguration);
this.cassandraSubscriptionMapper = new CassandraSubscriptionMapper(session);
this.cassandraAttachmentMapper = new CassandraAttachmentMapper(attachmentDAOV2, blobStore, new StringBackedAttachmentIdFactory());
this.cassandraAttachmentMapper = new CassandraAttachmentMapper(attachmentDAOV2, blobStore,
new CassandraAttachmentMapper.AttachmentIdAssignationStrategy.Default(new StringBackedAttachmentIdFactory()));
this.cassandraMessageMapper = new CassandraMessageMapper(
uidProvider,
modSeqProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,33 @@
public class CassandraAttachmentMapper implements AttachmentMapper {
private static final Logger LOGGER = LoggerFactory.getLogger(CassandraAttachmentMapper.class);

public interface AttachmentIdAssignationStrategy {
AttachmentId assign(ParsedAttachment parsedAttachment, MessageId messageId);

class Default implements AttachmentIdAssignationStrategy {
private final AttachmentIdFactory attachmentIdFactory;

@Inject
public Default(AttachmentIdFactory attachmentIdFactory) {
this.attachmentIdFactory = attachmentIdFactory;
}

@Override
public AttachmentId assign(ParsedAttachment parsedAttachment, MessageId messageId) {
return attachmentIdFactory.random();
}
}
}

private final CassandraAttachmentDAOV2 attachmentDAOV2;
private final BlobStore blobStore;
private final AttachmentIdFactory attachmentIdFactory;
private final AttachmentIdAssignationStrategy attachmentIdAssignationStrategy;

@Inject
public CassandraAttachmentMapper(CassandraAttachmentDAOV2 attachmentDAOV2, BlobStore blobStore, AttachmentIdFactory attachmentIdFactory) {
public CassandraAttachmentMapper(CassandraAttachmentDAOV2 attachmentDAOV2, BlobStore blobStore, AttachmentIdAssignationStrategy attachmentIdAssignationStrategy) {
this.attachmentDAOV2 = attachmentDAOV2;
this.blobStore = blobStore;
this.attachmentIdFactory = attachmentIdFactory;
this.attachmentIdAssignationStrategy = attachmentIdAssignationStrategy;
}

@Override
Expand Down Expand Up @@ -130,7 +148,7 @@ public Mono<List<MessageAttachmentMetadata>> storeAttachmentsReactive(Collection

private Mono<MessageAttachmentMetadata> storeAttachmentAsync(ParsedAttachment parsedAttachment, MessageId ownerMessageId) {
try {
AttachmentId attachmentId = attachmentIdFactory.random();
AttachmentId attachmentId = attachmentIdAssignationStrategy.assign(parsedAttachment, ownerMessageId);
ByteSource content = parsedAttachment.getContent();
long size = content.size();
return Mono.from(blobStore.save(blobStore.getDefaultBucketName(), content, LOW_COST))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.james.mailbox.cassandra.ids.CassandraMessageId;
import org.apache.james.mailbox.cassandra.mail.ACLMapper;
import org.apache.james.mailbox.cassandra.mail.CassandraACLMapper;
import org.apache.james.mailbox.cassandra.mail.CassandraAttachmentMapper;
import org.apache.james.mailbox.cassandra.mail.CassandraModSeqProvider;
import org.apache.james.mailbox.cassandra.mail.CassandraUidProvider;
import org.apache.james.mailbox.cassandra.mail.eventsourcing.acl.ACLModule;
Expand Down Expand Up @@ -85,6 +86,7 @@ public static Module commonModules(CqlSession session, CassandraTypesProvider ty
binder -> binder.bind(MessageId.Factory.class).toInstance(messageIdFactory),
binder -> binder.bind(BatchSizes.class).toInstance(BatchSizes.defaultValues()),
binder -> binder.bind(UidProvider.class).to(CassandraUidProvider.class),
binder -> binder.bind(CassandraAttachmentMapper.AttachmentIdAssignationStrategy.class).to(CassandraAttachmentMapper.AttachmentIdAssignationStrategy.Default.class),
binder -> binder.bind(ModSeqProvider.class).to(CassandraModSeqProvider.class),
binder -> binder.bind(ACLMapper.class).to(CassandraACLMapper.class),
binder -> binder.bind(BlobId.Factory.class).toInstance(new PlainBlobId.Factory()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.apache.james.mailbox.cassandra.mail.CassandraACLMapper;
import org.apache.james.mailbox.cassandra.mail.CassandraApplicableFlagDAO;
import org.apache.james.mailbox.cassandra.mail.CassandraAttachmentDAOV2;
import org.apache.james.mailbox.cassandra.mail.CassandraAttachmentMapper;
import org.apache.james.mailbox.cassandra.mail.CassandraDeletedMessageDAO;
import org.apache.james.mailbox.cassandra.mail.CassandraFirstUnseenDAO;
import org.apache.james.mailbox.cassandra.mail.CassandraMailboxCounterDAO;
Expand Down Expand Up @@ -218,6 +219,7 @@ protected void configure() {
bind(AttachmentContentLoader.class).to(AttachmentManager.class);
bind(MailboxCounterCorrector.class).to(CassandraMailboxCounterCorrector.class);
bind(MessageParser.class).toInstance(new MessageParserImpl());
bind(CassandraAttachmentMapper.AttachmentIdAssignationStrategy.class).to(CassandraAttachmentMapper.AttachmentIdAssignationStrategy.Default.class);

bind(Limit.class).annotatedWith(Names.named(CassandraEmailChangeRepository.LIMIT_NAME)).toInstance(Limit.of(256));
bind(Limit.class).annotatedWith(Names.named(CassandraMailboxChangeRepository.LIMIT_NAME)).toInstance(Limit.of(256));
Expand Down

0 comments on commit 6b2b119

Please sign in to comment.