/[LeafOK_CVS]/lbbs/src/section_list_loader.c
ViewVC logotype

Diff of /lbbs/src/section_list_loader.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.8 by sysadm, Wed May 28 07:30:23 2025 UTC Revision 1.25 by sysadm, Tue Jun 17 07:43:36 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _POSIX_C_SOURCE 200809L
18    
19  #include "section_list_loader.h"  #include "section_list_loader.h"
20    #include "article_cache.h"
21    #include "bbs.h"
22  #include "log.h"  #include "log.h"
23  #include "database.h"  #include "database.h"
24  #include "menu.h"  #include "menu.h"
# Line 23  Line 27 
27  #include <errno.h>  #include <errno.h>
28  #include <signal.h>  #include <signal.h>
29  #include <stdlib.h>  #include <stdlib.h>
30    #include <string.h>
31  #include <strings.h>  #include <strings.h>
32  #include <unistd.h>  #include <unistd.h>
33    
 #define _POSIX_C_SOURCE 200809L  
 #include <string.h>  
   
 #define SECTION_LIST_LOAD_INTERVAL 10 // second  
   
34  int section_list_loader_pid;  int section_list_loader_pid;
35  int last_article_op_log_mid;  int last_article_op_log_mid;
36    
37  int load_section_config_from_db(void)  int load_section_config_from_db(void)
38  {  {
39          MYSQL *db;          MYSQL *db = NULL;
40          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs = NULL, *rs2 = NULL;
41          MYSQL_ROW row, row2;          MYSQL_ROW row, row2;
42          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
43          int32_t sid;          int32_t sid;
44          char master_list[(BBS_username_max_len + 1) * 3 + 1];          char master_list[(BBS_username_max_len + 1) * 3 + 1];
45          SECTION_LIST *p_section;          SECTION_LIST *p_section;
46          int ret;          int ret = 0;
47    
48          db = db_open();          db = db_open();
49          if (db == NULL)          if (db == NULL)
50          {          {
51                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
52                  return -2;                  ret = -1;
53                    goto cleanup;
54          }          }
55    
56          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 61  int load_section_config_from_db(void) Line 62  int load_section_config_from_db(void)
62          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
63          {          {
64                  log_error("Query section_list error: %s\n", mysql_error(db));                  log_error("Query section_list error: %s\n", mysql_error(db));
65                  return -3;                  ret = -2;
66                    goto cleanup;
67          }          }
68          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
69          {          {
70                  log_error("Get section_list data failed\n");                  log_error("Get section_list data failed\n");
71                  return -3;                  ret = -2;
72                    goto cleanup;
73          }          }
74    
75          ret = 0;          ret = 0;
# Line 102  int load_section_config_from_db(void) Line 105  int load_section_config_from_db(void)
105                          strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));                          strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
106                  }                  }
107                  mysql_free_result(rs2);                  mysql_free_result(rs2);
108                    rs2 = NULL;
109    
110                  p_section = section_list_find_by_sid(sid);                  p_section = section_list_find_by_sid(sid);
111    
# Line 151  int load_section_config_from_db(void) Line 155  int load_section_config_from_db(void)
155                          break;                          break;
156                  }                  }
157          }          }
         mysql_free_result(rs);  
158    
159    cleanup:
160            mysql_free_result(rs);
161          mysql_close(db);          mysql_close(db);
162    
163          return ret;          return ret;
164  }  }
165    
166  int append_articles_from_db(int32_t start_aid, int global_lock)  int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit)
167  {  {
168          MYSQL *db;          MYSQL *db = NULL;
169          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
170          MYSQL_ROW row;          MYSQL_ROW row;
171          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
172          ARTICLE article;          ARTICLE article;
173          ARTICLE *p_topic;          ARTICLE *p_topic;
174          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
175          int32_t last_sid = 0;          int32_t last_sid = 0;
176            char sub_ip[IP_ADDR_LEN];
177            int article_count = 0;
178          int ret = 0;          int ret = 0;
179          int i;          int i;
180    
# Line 175  int append_articles_from_db(int32_t star Line 182  int append_articles_from_db(int32_t star
182          if (db == NULL)          if (db == NULL)
183          {          {
184                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
185                  return -2;                  ret = -1;
186                    goto cleanup;
187          }          }
188    
189          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
190                           "SELECT AID, TID, SID, CID, UID, visible, excerption, ontop, `lock`, "                           "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "
191                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt "                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "
192                           "FROM bbs WHERE AID >= %d ORDER BY AID",                           "sub_ip, bbs_content.content "
193                           start_aid);                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
194                             "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",
195                             start_aid, article_count_limit);
196    
197          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
198          {          {
199                  log_error("Query article list error: %s\n", mysql_error(db));                  log_error("Query article list error: %s\n", mysql_error(db));
200                  return -3;                  ret = -2;
201                    goto cleanup;
202          }          }
203          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
204          {          {
205                  log_error("Get article list data failed\n");                  log_error("Get article list data failed\n");
206                  return -3;                  ret = -2;
207                    goto cleanup;
208          }          }
209    
210          // acquire global lock          // acquire global lock
211          if (global_lock)          if (global_lock && (ret = section_list_rw_lock(NULL)) < 0)
212          {          {
213                  if ((ret = section_list_rw_lock(NULL)) < 0)                  log_error("section_list_rw_lock(sid = 0) error\n");
214                  {                  goto cleanup;
                         log_error("section_list_rw_lock(sid = 0) error\n");  
                         goto cleanup;  
                 }  
215          }          }
216    
217          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
218          {          {
219                  bzero(&article, sizeof(ARTICLE));                  memset(&article, 0, sizeof(ARTICLE));
220    
221                  // copy data of article                  // copy data of article
222                  i = 0;                  i = 0;
# Line 232  int append_articles_from_db(int32_t star Line 241  int append_articles_from_db(int32_t star
241    
242                  article.sub_dt = atol(row[i++]);                  article.sub_dt = atol(row[i++]);
243    
244                    strncpy(sub_ip, row[i++], sizeof(sub_ip) - 1);
245                    sub_ip[sizeof(sub_ip) - 1] = '\0';
246                    ip_mask(sub_ip, 1, '*');
247    
248                  // release lock of last section if different from current one                  // release lock of last section if different from current one
249                  if (!global_lock && article.sid != last_sid && last_sid != 0)                  if (!global_lock && article.sid != last_sid && last_sid != 0)
250                  {                  {
251                          if ((ret = section_list_rw_unlock(p_section)) < 0)                          if ((ret = section_list_rw_unlock(p_section)) < 0)
252                          {                          {
253                                  log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);                                  log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
254                                  break;                                  break;
255                          }                          }
256                  }                  }
# Line 266  int append_articles_from_db(int32_t star Line 279  int append_articles_from_db(int32_t star
279                  {                  {
280                          if ((ret = section_list_rw_lock(p_section)) < 0)                          if ((ret = section_list_rw_lock(p_section)) < 0)
281                          {                          {
282                                  log_error("section_list_rw_lock(sid = 0) error\n");                                  log_error("section_list_rw_lock(sid=0) error\n");
283                                  break;                                  break;
284                          }                          }
285                  }                  }
# Line 276  int append_articles_from_db(int32_t star Line 289  int append_articles_from_db(int32_t star
289    
290                  if (section_list_append_article(p_section, &article) < 0)                  if (section_list_append_article(p_section, &article) < 0)
291                  {                  {
292                          log_error("section_list_append_article(sid = %d, aid = %d) error\n",                          log_error("section_list_append_article(sid=%d, aid=%d) error\n",
293                                            p_section->sid, article.aid);                                            p_section->sid, article.aid);
294                          ret = -3;                          ret = -3;
295                          break;                          break;
296                  }                  }
297    
298                    article_count++;
299    
300                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], sub_ip, 0) < 0)
301                    {
302                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid);
303                            ret = -4;
304                            break;
305                    }
306          }          }
307    
308          // release lock of last section          // release lock of last section
# Line 288  int append_articles_from_db(int32_t star Line 310  int append_articles_from_db(int32_t star
310          {          {
311                  if ((ret = section_list_rw_unlock(p_section)) < 0)                  if ((ret = section_list_rw_unlock(p_section)) < 0)
312                  {                  {
313                          log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);                          log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
314                  }                  }
315          }          }
316    
317          // release global lock          // release global lock
318          if (global_lock)          if (global_lock && (ret = section_list_rw_unlock(NULL)) < 0)
319          {          {
320                  if ((ret = section_list_rw_unlock(NULL)) < 0)                  log_error("section_list_rw_unlock(sid=0) error\n");
                 {  
                         log_error("section_list_rw_unlock(sid = 0) error\n");  
                 }  
321          }          }
322    
323  cleanup:  cleanup:
324          mysql_free_result(rs);          mysql_free_result(rs);
   
325          mysql_close(db);          mysql_close(db);
326    
327          return ret;          return (ret < 0 ? ret : article_count);
328  }  }
329    
330  int set_last_article_op_log_from_db(void)  int set_last_article_op_log_from_db(void)
331  {  {
332          MYSQL *db;          MYSQL *db = NULL;
333          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
334          MYSQL_ROW row;          MYSQL_ROW row;
335          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
336            int ret = 0;
337    
338          db = db_open();          db = db_open();
339          if (db == NULL)          if (db == NULL)
340          {          {
341                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
342                  return -1;                  ret = -1;
343                    goto cleanup;
344          }          }
345    
346          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 329  int set_last_article_op_log_from_db(void Line 349  int set_last_article_op_log_from_db(void
349          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
350          {          {
351                  log_error("Query article op error: %s\n", mysql_error(db));                  log_error("Query article op error: %s\n", mysql_error(db));
352                  return -2;                  ret = -2;
353                    goto cleanup;
354          }          }
355          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
356          {          {
357                  log_error("Get article op data failed\n");                  log_error("Get article op data failed\n");
358                  return -2;                  ret = -2;
359                    goto cleanup;
360          }          }
361    
362          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 342  int set_last_article_op_log_from_db(void Line 364  int set_last_article_op_log_from_db(void
364                  last_article_op_log_mid = atoi(row[0]);                  last_article_op_log_mid = atoi(row[0]);
365          }          }
366    
367    cleanup:
368          mysql_free_result(rs);          mysql_free_result(rs);
   
369          mysql_close(db);          mysql_close(db);
370    
371          return last_article_op_log_mid;          return (ret < 0 ? ret : last_article_op_log_mid);
372  }  }
373    
374  int apply_article_op_log_from_db(void)  int apply_article_op_log_from_db(int op_count_limit)
375  {  {
376          MYSQL *db;          MYSQL *db = NULL;
377          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs = NULL, *rs2 = NULL;
378          MYSQL_ROW row, row2;          MYSQL_ROW row, row2;
379          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
380          ARTICLE *p_article;          ARTICLE *p_article;
381          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
382          SECTION_LIST *p_section_dest;          SECTION_LIST *p_section_dest;
383            char sub_ip[IP_ADDR_LEN];
384          int32_t last_sid = 0;          int32_t last_sid = 0;
385          int32_t sid_dest;          int32_t sid_dest;
386            int op_count = 0;
387          int ret = 0;          int ret = 0;
388    
389          db = db_open();          db = db_open();
390          if (db == NULL)          if (db == NULL)
391          {          {
392                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
393                  return -3;                  ret = -1;
394                    goto cleanup;
395          }          }
396    
397          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
398                           "SELECT MID, AID, type FROM bbs_article_op "                           "SELECT MID, AID, type FROM bbs_article_op "
399                           "WHERE MID > %d AND type NOT IN ('A', 'M') ORDER BY MID",                           "WHERE MID > %d AND type NOT IN ('A') "
400                           last_article_op_log_mid);                           "ORDER BY MID LIMIT %d",
401                             last_article_op_log_mid, op_count_limit);
402    
403          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
404          {          {
405                  log_error("Query article log error: %s\n", mysql_error(db));                  log_error("Query article log error: %s\n", mysql_error(db));
406                  return -3;                  ret = -2;
407                    goto cleanup;
408          }          }
409          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
410          {          {
411                  log_error("Get article log data failed\n");                  log_error("Get article log data failed\n");
412                  return -3;                  ret = -2;
413                    goto cleanup;
414          }          }
415    
416          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
# Line 430  int apply_article_op_log_from_db(void) Line 458  int apply_article_op_log_from_db(void)
458                          break;                          break;
459                  case 'D': // Delete article                  case 'D': // Delete article
460                  case 'X': // Delete article by Admin                  case 'X': // Delete article by Admin
461                          p_article->visible = 0;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 0) < 0)
                         if (p_article->tid == 0)  
462                          {                          {
463                                  // Set articles in the topic to be invisible                                  log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=0) error\n", p_section->sid, atoi(row[1]));
464                                  do                          }
465                                  {                          if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
466                                          p_article = p_article->p_topic_next;                          {
467                                          p_article->visible = 0;                                  log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
                                 } while (p_article->tid != 0);  
468                          }                          }
469                          break;                          break;
470                  case 'S': // Restore article                  case 'S': // Restore article
471                          p_article->visible = 1;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 1) < 0)
472                            {
473                                    log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=1) error\n", p_section->sid, atoi(row[1]));
474                            }
475                            if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
476                            {
477                                    log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
478                            }
479                          break;                          break;
480                  case 'L': // Lock article                  case 'L': // Lock article
481                          p_article->lock = 1;                          p_article->lock = 1;
# Line 451  int apply_article_op_log_from_db(void) Line 484  int apply_article_op_log_from_db(void)
484                          p_article->lock = 0;                          p_article->lock = 0;
485                          break;                          break;
486                  case 'M': // Modify article                  case 'M': // Modify article
487                          log_error("Operation type=M should not be found\n");                          snprintf(sql, sizeof(sql),
488                                             "SELECT bbs.CID, sub_ip, bbs_content.content "
489                                             "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
490                                             "WHERE bbs.AID = %d",
491                                             p_article->aid);
492    
493                            if (mysql_query(db, sql) != 0)
494                            {
495                                    log_error("Query article error: %s\n", mysql_error(db));
496                                    ret = -3;
497                                    break;
498                            }
499                            if ((rs2 = mysql_use_result(db)) == NULL)
500                            {
501                                    log_error("Get article data failed\n");
502                                    ret = -3;
503                                    break;
504                            }
505                            if ((row2 = mysql_fetch_row(rs2)))
506                            {
507                                    p_article->cid = atoi(row2[0]);
508    
509                                    strncpy(sub_ip, row2[1], sizeof(sub_ip) - 1);
510                                    sub_ip[sizeof(sub_ip) - 1] = '\0';
511                                    ip_mask(sub_ip, 1, '*');
512    
513                                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[2], sub_ip, 0) < 0)
514                                    {
515                                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
516                                            ret = -4;
517                                    }
518                            }
519                            else
520                            {
521                                    p_article->cid = 0;
522                                    ret = -4;
523                            }
524                            mysql_free_result(rs2);
525                            rs2 = NULL;
526    
527                            p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition
528                          break;                          break;
529                  case 'T': // Move article                  case 'T': // Move article
530                          snprintf(sql, sizeof(sql),                          snprintf(sql, sizeof(sql),
# Line 480  int apply_article_op_log_from_db(void) Line 553  int apply_article_op_log_from_db(void)
553                                  ret = -4;                                  ret = -4;
554                          }                          }
555                          mysql_free_result(rs2);                          mysql_free_result(rs2);
556                            rs2 = NULL;
557    
558                          if (sid_dest > 0 && sid_dest != p_article->sid)                          if (sid_dest > 0 && sid_dest != p_article->sid)
559                          {                          {
# Line 536  int apply_article_op_log_from_db(void) Line 610  int apply_article_op_log_from_db(void)
610    
611                  // Update MID with last successfully proceeded article_op_log                  // Update MID with last successfully proceeded article_op_log
612                  last_article_op_log_mid = atoi(row[0]);                  last_article_op_log_mid = atoi(row[0]);
613    
614                    op_count++;
615          }          }
616    
617          // release lock of last section          // release lock of last section
# Line 547  int apply_article_op_log_from_db(void) Line 623  int apply_article_op_log_from_db(void)
623                  }                  }
624          }          }
625    
626    cleanup:
627            mysql_free_result(rs2);
628          mysql_free_result(rs);          mysql_free_result(rs);
   
629          mysql_close(db);          mysql_close(db);
630    
631          return ret;          return (ret < 0 ? ret : op_count);
632  }  }
633    
634  int section_list_loader_launch(void)  int section_list_loader_launch(void)
# Line 576  int section_list_loader_launch(void) Line 653  int section_list_loader_launch(void)
653          {          {
654                  SYS_child_process_count++;                  SYS_child_process_count++;
655                  section_list_loader_pid = pid;                  section_list_loader_pid = pid;
656                  log_std("Section list loader process (%d) start\n", pid);                  log_common("Section list loader process (pid = %d) start\n", pid);
657                  return 0;                  return 0;
658          }          }
659          else if (pid < 0) // Error          else if (pid < 0) // Error
# Line 612  int section_list_loader_launch(void) Line 689  int section_list_loader_launch(void)
689                  }                  }
690    
691                  // Load section articles                  // Load section articles
                 last_aid = article_block_last_aid();  
692                  article_count = article_block_article_count();                  article_count = article_block_article_count();
693    
694                  if ((ret = append_articles_from_db(last_aid + 1, 0)) < 0)                  do
695                  {                  {
696                          log_error("append_articles_from_db(%d, 0) error\n", last_aid + 1);                          last_aid = article_block_last_aid();
697    
698                          if (ret == ERR_UNKNOWN_SECTION)                          if ((ret = append_articles_from_db(last_aid + 1, 0, LOAD_ARTICLE_COUNT_LIMIT)) < 0)
699                          {                          {
700                                  SYS_section_list_reload = 1; // Force reload section_list                                  log_error("append_articles_from_db(%d, 0, %d) error\n", last_aid + 1, LOAD_ARTICLE_COUNT_LIMIT);
701    
702                                    if (ret == ERR_UNKNOWN_SECTION)
703                                    {
704                                            SYS_section_list_reload = 1; // Force reload section_list
705                                    }
706                          }                          }
707                  }                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
708    
709                  load_count = article_block_article_count() - article_count;                  load_count = article_block_article_count() - article_count;
   
710                  if (load_count > 0)                  if (load_count > 0)
711                  {                  {
712                          log_std("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());                          log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());
713                  }                  }
714    
715                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 640  int section_list_loader_launch(void) Line 720  int section_list_loader_launch(void)
720                  // Load article_op log                  // Load article_op log
721                  last_mid = last_article_op_log_mid;                  last_mid = last_article_op_log_mid;
722    
723                  if ((ret = apply_article_op_log_from_db()) < 0)                  do
724                  {                  {
725                          log_error("apply_article_op_log_from_db() error\n");                          if ((ret = apply_article_op_log_from_db(LOAD_ARTICLE_COUNT_LIMIT)) < 0)
   
                         if (ret == ERR_UNKNOWN_SECTION)  
726                          {                          {
727                                  SYS_section_list_reload = 1; // Force reload section_list                                  log_error("apply_article_op_log_from_db() error\n");
728    
729                                    if (ret == ERR_UNKNOWN_SECTION)
730                                    {
731                                            SYS_section_list_reload = 1; // Force reload section_list
732                                    }
733                          }                          }
734                  }                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
735    
736                  if (last_article_op_log_mid > last_mid)                  if (last_article_op_log_mid > last_mid)
737                  {                  {
738                          log_std("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);                          log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);
739                  }                  }
740    
741                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 660  int section_list_loader_launch(void) Line 743  int section_list_loader_launch(void)
743                          continue;                          continue;
744                  }                  }
745    
746                  for (i = 0; i < SECTION_LIST_LOAD_INTERVAL && !SYS_server_exit && !SYS_section_list_reload; i++)                  for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++)
747                  {                  {
748                          sleep(1);                          sleep(1);
749                  }                  }
# Line 673  int section_list_loader_launch(void) Line 756  int section_list_loader_launch(void)
756          detach_article_block_shm();          detach_article_block_shm();
757          detach_trie_dict_shm();          detach_trie_dict_shm();
758    
759          log_std("Section list loader process exit normally\n");          log_common("Section list loader process exit normally\n");
760          log_end();          log_end();
761    
762          section_list_loader_pid = 0;          section_list_loader_pid = 0;
# Line 700  int section_list_loader_reload(void) Line 783  int section_list_loader_reload(void)
783          return 0;          return 0;
784  }  }
785    
786  int query_section_articles(SECTION_LIST *p_section, int32_t page_id, ARTICLE *p_articles[], int32_t *p_article_count)  int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count)
787  {  {
788          ARTICLE *p_article;          ARTICLE *p_article;
789          ARTICLE *p_next_page_first_article;          ARTICLE *p_next_page_first_article;
790          int ret = 0;          int ret = 0;
791    
792          if (p_section == NULL || p_articles == NULL || p_article_count == NULL)          if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)
793          {          {
794                  log_error("query_section_articles() NULL pointer error\n");                  log_error("query_section_articles() NULL pointer error\n");
795                  return -1;                  return -1;
# Line 719  int query_section_articles(SECTION_LIST Line 802  int query_section_articles(SECTION_LIST
802                  return -2;                  return -2;
803          }          }
804    
805          if (page_id < 0 || page_id >= p_section->page_count)          *p_page_count = p_section->page_count;
806    
807            if (p_section->visible_article_count == 0)
808            {
809                    *p_article_count = 0;
810            }
811            else if (page_id < 0 || page_id >= p_section->page_count)
812          {          {
813                  log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);                  log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);
814                  ret = -3;                  ret = -3;
# Line 757  int query_section_articles(SECTION_LIST Line 846  int query_section_articles(SECTION_LIST
846    
847          return ret;          return ret;
848  }  }
849    
850    int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction, int step,
851                                                              int *p_page_id, int *p_visible_offset, int *p_article_count)
852    {
853            const ARTICLE *p_article = NULL;
854            ARTICLE *p_tmp;
855            int32_t aid = 0;
856            int page_id = 0;
857            int offset = 0;
858            int ret = 0;
859            int i;
860    
861            if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)
862            {
863                    log_error("locate_article_in_section() NULL pointer error\n");
864                    return -1;
865            }
866    
867            // acquire lock of section
868            if ((ret = section_list_rd_lock(p_section)) < 0)
869            {
870                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
871                    return -2;
872            }
873    
874            if (direction == 0)
875            {
876                    aid = p_article_cur->aid;
877            }
878            else if (direction == 1)
879            {
880                    for (p_article = p_article_cur; step > 0 && p_article->p_topic_next->aid > p_article_cur->aid; p_article = p_article->p_topic_next)
881                    {
882                            if (p_article->visible)
883                            {
884                                    step--;
885                            }
886                    }
887    
888                    aid = (p_article->aid > p_article_cur->aid && p_article->visible ? p_article->aid : 0);
889            }
890            else if (direction == -1)
891            {
892                    for (p_article = p_article_cur; step > 0 && p_article->p_topic_prior->aid < p_article_cur->aid; p_article = p_article->p_topic_prior)
893                    {
894                            if (p_article->visible)
895                            {
896                                    step--;
897                            }
898                    }
899    
900                    aid = (p_article->aid < p_article_cur->aid && p_article->visible ? p_article->aid : 0);
901            }
902    
903            p_article = NULL;
904    
905            if (aid > 0)
906            {
907                    p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp);
908                    if (p_article != NULL)
909                    {
910                            *p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
911    
912                            p_article = p_section->p_page_first_article[page_id];
913                            for (i = 0; i < *p_article_count && p_article != NULL;)
914                            {
915                                    if (p_article->visible)
916                                    {
917                                            if (p_article->aid == aid)
918                                            {
919                                                    *p_page_id = page_id;
920                                                    *p_visible_offset = i;
921                                                    break;
922                                            }
923                                            i++;
924                                            if (i >= *p_article_count)
925                                            {
926                                                    log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);
927                                                    p_article = NULL;
928                                                    break;
929                                            }
930                                    }
931                                    p_article = p_article->p_next;
932                            }
933                    }
934            }
935    
936            // release lock of section
937            if (section_list_rd_unlock(p_section) < 0)
938            {
939                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
940                    ret = -2;
941            }
942    
943            return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
944    }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1