Looks very interesting James. Keep us posted. Getting rid of the global
message uid sequence gets my vote.



James Cloos wrote:
> This is where I am at in converting to per-mailbox uid values for imap.
> 
> I still need to port the trigger to sqlite and write some equivilent
> code for mysql (I presume compatability with versions of mysql which
> do not support stored procedures is required?), write a transition
> schema for each, and finish the imap side of things.
> 
> I first tried to replace all use of message_idnr as a priamry key with
> the tuple (mailbox_idnr, message_uid), but found a couple of places in
> the C which would require substantial refactoring.  So instead I added
> a message_uid column to messages and a next_uid column to mailboxes.
> 
> With this patch, dbmail-smtp works perfectly.  I've not tested lmptd,
> but expect that to work as well.
> 
> But I'm still having some issues getting the mail back out.  I have to
> audit every use of mailbox_idnr to determine whether it or mailbox_uid
> should be used there.  (As an example, in dbmail-imapsession.c's
> dbmail_imap_session_get_msginfo(), the lo and hi variables end up with
> idnr values rather than uid values, suggesting that something is still
> passing idnr rather than uid to the imap client....)
> 
> -JimC
> 
> Index: db.c
> ===================================================================
> --- db.c      (revision 2603)
> +++ db.c      (working copy)
> @@ -305,6 +305,32 @@
>  }
>  
>  
> +int db_get_message_uid(u64_t message_idnr, u64_t * message_uid)
> +{
> +     char query[DEF_QUERYSIZE]; 
> +     memset(query,0,DEF_QUERYSIZE);
> +
> +     snprintf(query, DEF_QUERYSIZE,
> +              "SELECT message_uid from %smessages WHERE message_idnr = %d", 
> DBPFX, message_idnr);
> +
> +     if (db_query(query) == -1) {
> +             TRACE(TRACE_ERROR, "error getting message_uid");
> +             return DM_EQUERY;
> +     }
> +
> +     if (db_num_rows() < 1) {
> +             db_free_result();
> +             return DM_EGENERAL;
> +     }
> +
> +     *message_uid = db_get_result_u64(0, 0);
> +
> +     db_free_result();
> +
> +     return DM_SUCCESS;
> +}
> +
> +
>  int db_get_quotum_used(u64_t user_idnr, u64_t * curmail_size)
>  {
>       char query[DEF_QUERYSIZE]; 
> @@ -2912,9 +2938,8 @@
>        * */
>  
>       memset(query,0,DEF_QUERYSIZE);
> -     snprintf(query, DEF_QUERYSIZE, "SELECT message_idnr+1 FROM %smessages "
> -                     "WHERE mailbox_idnr=%llu "
> -                     "ORDER BY message_idnr DESC LIMIT 1",DBPFX, mb->uid);
> +     snprintf(query, DEF_QUERYSIZE, "SELECT next_uid FROM %smailboxes "
> +                     "WHERE mailbox_idnr=%llu", DBPFX, mb->uid);
>  
>       if (db_query(query) == -1)
>               return DM_EQUERY;
> Index: db.h
> ===================================================================
> --- db.h      (revision 2603)
> +++ db.h      (working copy)
> @@ -317,6 +317,20 @@
>  int db_get_physmessage_id(u64_t message_idnr, /[EMAIL PROTECTED]@*/ u64_t * 
> physmessage_id);
>  
>  /**
> + * \brief get the message_uid from a message_idnr
> + * \param message_idnr
> + * \param message_uid will hold message_uid on return. Must hold a valid
> + * pointer on call.
> + * \return 
> + *     - -1 on error
> + *     -  0 if a message_uid was found
> + *     -  1 if no message with this message_idnr found
> + * \attention function will fail and halt program if message_uid is
> + * NULL on call.
> + */
> +int db_get_message_uid(u64_t message_idnr, /[EMAIL PROTECTED]@*/ u64_t * 
> message_uid);
> +
> +/**
>   * \brief return number of bytes used by user identified by userid
>   * \param user_idnr id of the user (from users table)
>   * \param curmail_size will hold current mailsize used (should hold a valid 
> Index: sql/sqlite/create_tables.sqlite
> ===================================================================
> --- sql/sqlite/create_tables.sqlite   (revision 2603)
> +++ sql/sqlite/create_tables.sqlite   (working copy)
> @@ -54,7 +54,8 @@
>     draft_flag BOOLEAN default '0' not null,
>     no_inferiors BOOLEAN default '0' not null,
>     no_select BOOLEAN default '0' not null,
> -   permission BOOLEAN default '2'
> +   permission BOOLEAN default '2',
> +   next_uid INTEGER default '1' not null
>  );
>  CREATE INDEX dbmail_mailboxes_1 ON dbmail_mailboxes(name);
>  CREATE INDEX dbmail_mailboxes_2 ON dbmail_mailboxes(owner_idnr);
> @@ -243,6 +244,7 @@
>     message_idnr INTEGER PRIMARY KEY,
>     mailbox_idnr INTEGER DEFAULT '0' NOT NULL,
>     physmessage_id INTEGER DEFAULT '0' NOT NULL,
> +   message_uid INTEGER DEFAULT '0' NOT NULL,
>     seen_flag BOOLEAN default '0' not null,
>     answered_flag BOOLEAN default '0' not null,
>     deleted_flag BOOLEAN default '0' not null,
> Index: sql/postgresql/create_tables.pgsql
> ===================================================================
> --- sql/postgresql/create_tables.pgsql        (revision 2603)
> +++ sql/postgresql/create_tables.pgsql        (working copy)
> @@ -21,6 +21,8 @@
>  
>  BEGIN TRANSACTION;
>  
> +CREATE PROCEDURAL LANGUAGE plpgsql;
> +
>  CREATE SEQUENCE dbmail_alias_idnr_seq;
>  CREATE TABLE dbmail_aliases (
>      alias_idnr INT8 DEFAULT nextval('dbmail_alias_idnr_seq'),
> @@ -72,6 +74,7 @@
>     no_inferiors INT2 DEFAULT '0' NOT NULL,
>     no_select INT2 DEFAULT '0' NOT NULL,
>     permission INT2 DEFAULT '2' NOT NULL,
> +   next_uid INT8 DEFAULT '1' NOT NULL,
>     PRIMARY KEY (mailbox_idnr)
>  );
>  CREATE INDEX dbmail_mailboxes_owner_idx ON dbmail_mailboxes(owner_idnr);
> @@ -79,6 +82,17 @@
>  CREATE UNIQUE INDEX dbmail_mailboxes_owner_name_idx 
>       ON dbmail_mailboxes(owner_idnr, name);
>  
> +CREATE OR REPLACE FUNCTION dbmail_get_or_set_mailbox_next_uid(idnr INT8, uid 
> INT8) RETURNS INT8 AS $$
> +DECLARE
> +old_next_uid INT8;
> +BEGIN
> +SELECT INTO old_next_uid next_uid FROM dbmail_mailboxes WHERE mailbox_idnr = 
> idnr FOR UPDATE;
> +UPDATE dbmail_mailboxes SET next_uid = GREATEST(uid, old_next_uid) + 1 WHERE 
> mailbox_idnr = idnr;
> +RETURN GREATEST(uid, old_next_uid);
> +END;
> +$$
> +     LANGUAGE plpgsql;
> +
>  CREATE TABLE dbmail_subscription (
>     user_id INT8 REFERENCES dbmail_users(user_idnr) ON DELETE CASCADE ON 
> UPDATE CASCADE,
>     mailbox_id INT8 REFERENCES dbmail_mailboxes(mailbox_idnr)
> @@ -118,6 +132,7 @@
>       ON DELETE CASCADE ON UPDATE CASCADE,
>     physmessage_id INT8 REFERENCES dbmail_physmessage(id)
>       ON DELETE CASCADE ON UPDATE CASCADE,
> +   message_uid INT8 NOT NULL DEFAULT '0',
>     seen_flag INT2 DEFAULT '0' NOT NULL,
>     answered_flag INT2 DEFAULT '0' NOT NULL,
>     deleted_flag INT2 DEFAULT '0' NOT NULL,
> @@ -137,6 +152,17 @@
>  CREATE INDEX dbmail_messages_7 ON 
> dbmail_messages(mailbox_idnr,status,seen_flag);
>  CREATE INDEX dbmail_messages_8 ON 
> dbmail_messages(mailbox_idnr,status,recent_flag);
>  
> +CREATE OR REPLACE FUNCTION dbmail_messages_set_message_uid() RETURNS TRIGGER 
> AS $$
> +BEGIN
> +SELECT INTO NEW.message_uid 
> dbmail_get_or_set_mailbox_next_uid(NEW.mailbox_idnr, NEW.message_uid);
> +RETURN NEW;
> +END;
> +$$
> +   LANGUAGE 'plpgsql';
> +
> +CREATE TRIGGER dbmail_messages_uid_trigger BEFORE INSERT ON dbmail_messages
> +   FOR EACH ROW EXECUTE PROCEDURE dbmail_messages_set_message_uid();
> +
>  CREATE SEQUENCE dbmail_messageblk_idnr_seq;
>  CREATE TABLE dbmail_messageblks (
>     messageblk_idnr INT8 DEFAULT nextval('dbmail_messageblk_idnr_seq'),
> Index: sql/mysql/create_tables.mysql
> ===================================================================
> --- sql/mysql/create_tables.mysql     (revision 2603)
> +++ sql/mysql/create_tables.mysql     (working copy)
> @@ -77,6 +77,7 @@
>       no_inferiors tinyint(1) NOT NULL default '0',
>       no_select tinyint(1) NOT NULL default '0',
>       permission tinyint(1) default '2',
> +     next_uid bigint(21) NOT NULL default '1',
>       PRIMARY KEY  (mailbox_idnr),
>       INDEX name_index (name),
>       INDEX owner_idnr_index (owner_idnr),
> @@ -134,6 +135,7 @@
>       message_idnr bigint(21) NOT NULL auto_increment,
>       mailbox_idnr bigint(21) NOT NULL default '0',
>       physmessage_id bigint(21) NOT NULL default '0',
> +     message_uid bigint(21) NOT NULL default '0',
>       seen_flag tinyint(1) NOT NULL default '0',
>       answered_flag tinyint(1) NOT NULL default '0',
>       deleted_flag tinyint(1) NOT NULL default '0',
> Index: dbmail-message.c
> ===================================================================
> --- dbmail-message.c  (revision 2603)
> +++ dbmail-message.c  (working copy)
> @@ -491,6 +491,16 @@
>       return self->physid;
>  }
>  
> +void dbmail_message_set_uid(struct DbmailMessage *self, u64_t uid)
> +{
> +     self->uid = uid;
> +}
> +
> +u64_t dbmail_message_get_uid(const struct DbmailMessage *self)
> +{
> +     return self->uid;
> +}
> +
>  void dbmail_message_set_internal_date(struct DbmailMessage *self, char 
> *internal_date)
>  {
>       if (internal_date)
> @@ -858,6 +868,7 @@
>  {
>       u64_t mailboxid;
>       u64_t physmessage_id;
> +     u64_t uid;
>       char *internal_date = NULL;
>       char query[DEF_QUERYSIZE];
>       memset(query,0,DEF_QUERYSIZE);
> @@ -905,6 +916,9 @@
>       }
>  
>       self->id = db_insert_result("message_idnr");
> +     if (db_get_message_uid(self->id, &uid) == DM_SUCCESS)
> +             self->uid = uid;
> +
>       return 1;
>  }
>  
> Index: dbmail-message.h
> ===================================================================
> --- dbmail-message.h  (revision 2603)
> +++ dbmail-message.h  (working copy)
> @@ -63,6 +63,7 @@
>  struct DbmailMessage {
>       u64_t id;
>       u64_t physid;
> +     u64_t uid;
>       time_t internal_date;
>       int *internal_date_gmtoff;
>       GString *envelope_recipient;
> @@ -103,6 +104,9 @@
>  void dbmail_message_set_physid(struct DbmailMessage *self, u64_t physid);
>  u64_t dbmail_message_get_physid(const struct DbmailMessage *self);
>  
> +void dbmail_message_set_uid(struct DbmailMessage *self, u64_t uid);
> +u64_t dbmail_message_get_uid(const struct DbmailMessage *self);
> +
>  void dbmail_message_set_envelope_recipient(struct DbmailMessage *self, const 
> char *envelope);
>  gchar * dbmail_message_get_envelope_recipient(const struct DbmailMessage 
> *self);
>       
> Index: modules/dbpgsql.c
> ===================================================================
> --- modules/dbpgsql.c (revision 2603)
> +++ modules/dbpgsql.c (working copy)
> @@ -314,7 +314,7 @@
>       char the_query[DEF_QUERYSIZE];
>  
>       for (i = 0; i < num_tables; i++) {
> -             snprintf(the_query, DEF_QUERYSIZE, "VACUUM %s%s", 
> _db_params.pfx,tables[i]);
> +             snprintf(the_query, DEF_QUERYSIZE, "VACUUM ANALYSE %s%s", 
> _db_params.pfx,tables[i]);
>               if (db_query(the_query) == -1) {
>                       TRACE(TRACE_ERROR, "error vacuuming table [%s%s]", 
>                               _db_params.pfx,tables[i]);
> Index: dbmail-imapsession.c
> ===================================================================
> --- dbmail-imapsession.c      (revision 2603)
> +++ dbmail-imapsession.c      (working copy)
> @@ -736,12 +736,12 @@
>  
>       snprintf(query, DEF_QUERYSIZE,
>                "SELECT seen_flag, answered_flag, deleted_flag, flagged_flag, "
> -              "draft_flag, recent_flag, %s, rfcsize, message_idnr "
> +              "draft_flag, recent_flag, %s, rfcsize, message_uid "
>                "FROM %smessages msg, %sphysmessage pm "
>                "WHERE pm.id = msg.physmessage_id "
> -              "AND message_idnr BETWEEN %llu AND %llu "
> +              "AND message_uid BETWEEN %llu AND %llu "
>                "AND mailbox_idnr = %llu AND status IN (%d,%d) "
> -              "ORDER BY message_idnr ASC",to_char_str,DBPFX,DBPFX,
> +              "ORDER BY message_uid ASC",to_char_str,DBPFX,DBPFX,
>                *lo, *hi, ud->mailbox.uid,
>                MESSAGE_STATUS_NEW, MESSAGE_STATUS_SEEN);
>       g_free(to_char_str);
> @@ -1061,7 +1061,7 @@
>                       "FROM %senvelope e "
>                       "JOIN %smessages m ON m.physmessage_id=e.physmessage_id 
> "
>                       "WHERE m.mailbox_idnr = %llu "
> -                     "AND message_idnr BETWEEN %llu AND %llu ",
> +                     "AND message_uid BETWEEN %llu AND %llu ",
>                       DBPFX, DBPFX,  
>                       self->mailbox->id, self->msg_idnr, hi);
>       
> @@ -1196,7 +1196,7 @@
>                       "JOIN %smessages m ON v.physmessage_id=m.physmessage_id 
> "
>                       "JOIN %sheadername n ON v.headername_id=n.id "
>                       "WHERE m.mailbox_idnr = %llu "
> -                     "AND message_idnr BETWEEN %llu AND %llu "
> +                     "AND message_uid BETWEEN %llu AND %llu "
>                       "AND lower(headername) %s IN ('%s')",
>                       DBPFX, DBPFX, DBPFX,
>                       self->mailbox->id,
> @@ -1943,7 +1943,7 @@
>       db_begin_transaction();
>       while (slices) {
>               snprintf(query, DEF_QUERYSIZE, "UPDATE %smessages SET 
> recent_flag = 0 "
> -                             "WHERE message_idnr IN (%s) AND recent_flag = 
> 1", 
> +                             "WHERE message_uid IN (%s) AND recent_flag = 
> 1", 
>                               DBPFX, (gchar *)slices->data);
>               if (db_query(query) == -1) {
>                       db_rollback_transaction();
> 
> ===================================================================
> 


-- 
  ________________________________________________________________
  Paul Stevens                                      paul at nfg.nl
  NET FACILITIES GROUP                     GPG/PGP: 1024D/11F8CD31
  The Netherlands________________________________http://www.nfg.nl
_______________________________________________
Dbmail-dev mailing list
[email protected]
http://twister.fastxs.net/mailman/listinfo/dbmail-dev

Reply via email to