Skip to content
Snippets Groups Projects
Commit a3e430e0 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add smb_msg_type() and smb_msg_count() for index-based msg counting.

In support of message bases with vote-messages and poll-closures:

smb_msg_type() returns an enum smb_msg_type inferred from the combination
of attribute flags specified (should match msghdr_t.type).

smb_msg_count() calculates the number of message index records of the
specified type(s) (a bit-field) by reading the message base index file. This
value should only used for info/display purposes, so no locking is performed
to return the result as fast as possible.

This allows an accurate number of "posts" (posted *messages*) to be queried
and displayed to a user when appropriate (e.g. instead of
smb.status.total_msgs or just the size of the index file divided by the size
of an index record). In that case, since vote messages and poll closures
aren't displayed as messages to users, exclude those from the count to reduce
confusion. For message bases that don't support voting, this scheme is not
necessary.
parent 02c5040a
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
......@@ -365,6 +365,8 @@ typedef struct _PACK { /* Time with time-zone */
} when_t;
typedef uint16_t smb_msg_attr_t;
typedef struct _PACK { /* Index record */
union {
......@@ -378,7 +380,7 @@ typedef struct _PACK { /* Index record */
uint32_t remsg; /* number of message this vote is in response to */
};
};
uint16_t attr; /* attributes (read, permanent, etc.) */
smb_msg_attr_t attr; /* attributes (read, permanent, etc.) */
uint32_t offset; /* byte-offset of msghdr in header file */
uint32_t number; /* number of message (1 based) */
uint32_t time; /* time/date message was imported/posted */
......
......@@ -2141,4 +2141,46 @@ uint32_t SMBCALL smb_last_in_thread(smb_t* smb, smbmsg_t* remsg)
return smb_last_in_branch(smb, &msg);
}
SMBEXPORT enum smb_msg_type smb_msg_type(smb_msg_attr_t attr)
{
switch (attr&MSG_POLL_VOTE_MASK) {
case 0:
return SMB_MSG_TYPE_NORMAL;
case MSG_POLL:
return SMB_MSG_TYPE_POLL;
case MSG_POLL_CLOSURE:
return SMB_MSG_TYPE_POLL_CLOSURE;
default:
return SMB_MSG_TYPE_BALLOT;
}
}
// Return count of messages of the desired types (bit-mask), as read from index
// Does so as fast as possible, without locking
SMBEXPORT size_t SMBCALL smb_msg_count(smb_t* smb, unsigned types)
{
off_t index_length = filelength(fileno(smb->sid_fp));
if(index_length < sizeof(idxrec_t))
return 0;
uint32_t total = index_length / sizeof(idxrec_t);
if(total < 1)
return 0;
idxrec_t* idx;
if((idx = calloc(total, sizeof(*idx))) == NULL)
return 0;
rewind(smb->sid_fp);
size_t result = fread(idx, sizeof(*idx), total, smb->sid_fp);
size_t count = 0;
for(size_t i = 0; i < result; i++) {
if(types & (1 << smb_msg_type(idx[i].attr)))
count++;
}
free(idx);
return count;
}
/* End of SMBLIB.C */
......@@ -190,6 +190,8 @@ SMBEXPORT uint32_t SMBCALL smb_next_in_thread(smb_t*, smbmsg_t*, msghdr_t*);
SMBEXPORT uint32_t SMBCALL smb_last_in_branch(smb_t*, smbmsg_t*);
SMBEXPORT uint32_t SMBCALL smb_last_in_thread(smb_t*, smbmsg_t*);
SMBEXPORT BOOL SMBCALL smb_msg_is_utf8(const smbmsg_t*);
SMBEXPORT size_t SMBCALL smb_msg_count(smb_t*, unsigned types);
SMBEXPORT enum smb_msg_type smb_msg_type(smb_msg_attr_t);
/* smbadd.c */
SMBEXPORT int SMBCALL smb_addmsg(smb_t* smb, smbmsg_t* msg, int storage, long dupechk_hashes
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment