EXPORTED int mailbox_annotation_write(struct mailbox *mailbox, uint32_t uid,
                                      const char *entry, const char *userid,
                                      const struct buf *value)
{
    annotate_state_t *state = NULL;
    int r = 0;
    struct buf oldvalue = BUF_INITIALIZER;

    annotatemore_msg_lookup(mailbox, uid, entry, userid, &oldvalue);
    if (oldvalue.len == value->len && (!value->len || !memcmp(oldvalue.s, value->s, value->len)))
        goto done;

    struct index_record record;
    memset(&record, 0, sizeof(struct index_record));
    r = mailbox_find_index_record(mailbox, uid, &record);
    if (r) goto done;

    r = mailbox_get_annotate_state(mailbox, uid, &state);
    if (r) goto done;

    r = annotate_state_write(state, entry, userid, value);
    if (r) goto done;

    /* need to touch the modseq */
    r = mailbox_rewrite_index_record(mailbox, &record);
    if (r) goto done;

done:
    buf_free(&oldvalue);
    return r;
}
EXPORTED int mailbox_annotation_write(struct mailbox *mailbox, uint32_t uid,
                                      const char *entry, const char *userid,
                                      const struct buf *value)
{
    annotate_state_t *state = NULL;
    int r = 0;
    struct buf oldvalue = BUF_INITIALIZER;

    annotatemore_msg_lookup(mailbox, uid, entry, userid, &oldvalue);
    if (oldvalue.len == value->len && (!value->len || !memcmp(oldvalue.s, value->s, value->len)))
        goto done;

    struct index_record record;
    memset(&record, 0, sizeof(struct index_record));
    r = mailbox_find_index_record(mailbox, uid, &record);
    if (r) goto done;

    r = mailbox_get_annotate_state(mailbox, uid, &state);
    if (r) goto done;

    r = annotate_state_write(state, entry, userid, value);
    if (r) goto done;

     
    r = mailbox_rewrite_index_record(mailbox, &record);
    if (r) goto done;

done:
    buf_free(&oldvalue);
    return r;
}