@@ -1320,30 +1320,38 @@ public List<JSONObject> getIndexRecentArticles(int fetchSize, int page) {
13201320 try {
13211321 Stopwatchs .start ("Query index recent articles" );
13221322 try {
1323- Query query = new Query ().
1324- setFilter (CompositeFilterOperator .and (
1325- new PropertyFilter (Article .ARTICLE_TYPE , FilterOperator .NOT_EQUAL , Article .ARTICLE_TYPE_C_DISCUSSION ),
1326- new PropertyFilter (Article .ARTICLE_TYPE , FilterOperator .NOT_EQUAL , Article .ARTICLE_TYPE_C_LONG ),
1327- new PropertyFilter (Article .ARTICLE_STATUS , FilterOperator .EQUAL , Article .ARTICLE_STATUS_C_VALID ),
1328- new PropertyFilter (Article .ARTICLE_SHOW_IN_LIST , FilterOperator .NOT_EQUAL , Article .ARTICLE_SHOW_IN_LIST_C_NOT ),
1329- new PropertyFilter (Article .ARTICLE_CREATE_TIME , FilterOperator .GREATER_THAN_OR_EQUAL , String .valueOf (DateUtils .addDays (new Date (), -30 ).getTime ())))).
1330- setPageCount (1 ).setPage (page , fetchSize ).
1331- addSort (Article .ARTICLE_LATEST_CMT_TIME , SortDirection .DESCENDING );
1332- ret = articleRepository .getList (query );
1333-
1323+ final int normalizedPage = Math .max (page , 1 );
13341324 final List <JSONObject > stickArticles = getStickArticles ();
1335- if (!stickArticles .isEmpty () && page == 1 ) {
1336- final Iterator <JSONObject > i = ret .iterator ();
1337- while (i .hasNext ()) {
1338- final JSONObject article = i .next ();
1339- for (final JSONObject stickArticle : stickArticles ) {
1340- if (article .optString (Keys .OBJECT_ID ).equals (stickArticle .optString (Keys .OBJECT_ID ))) {
1341- i .remove ();
1325+ final Set <String > stickArticleIds = buildArticleIdSet (stickArticles );
1326+
1327+ if (1 == normalizedPage ) {
1328+ final Query query = buildIndexRecentArticlesQuery (1 , fetchSize );
1329+ ret = articleRepository .getList (query );
1330+
1331+ if (!stickArticles .isEmpty ()) {
1332+ final Iterator <JSONObject > iterator = ret .iterator ();
1333+ while (iterator .hasNext ()) {
1334+ final JSONObject article = iterator .next ();
1335+ if (stickArticleIds .contains (article .optString (Keys .OBJECT_ID ))) {
1336+ iterator .remove ();
13421337 }
13431338 }
1344- }
13451339
1346- ret .addAll (0 , stickArticles );
1340+ ret .addAll (0 , stickArticles );
1341+ }
1342+ } else {
1343+ final int extraSize = calcIndexRecentExtraSize (fetchSize , stickArticleIds );
1344+ final int actualFetchSize = fetchSize + extraSize ;
1345+ final int fromIndex = (normalizedPage - 1 ) * fetchSize + (normalizedPage - 2 ) * extraSize ;
1346+ final int toIndex = fromIndex + actualFetchSize ;
1347+
1348+ final Query query = buildIndexRecentArticlesQuery (1 , toIndex );
1349+ final List <JSONObject > queried = articleRepository .getList (query );
1350+ if (queried .size () <= fromIndex ) {
1351+ ret = new ArrayList <>();
1352+ } else {
1353+ ret = new ArrayList <>(queried .subList (fromIndex , Math .min (queried .size (), toIndex )));
1354+ }
13471355 }
13481356 } finally {
13491357 Stopwatchs .end ();
@@ -1359,6 +1367,44 @@ public List<JSONObject> getIndexRecentArticles(int fetchSize, int page) {
13591367 }
13601368 }
13611369
1370+ private Query buildIndexRecentArticlesQuery (final int page , final int fetchSize ) {
1371+ return new Query ().
1372+ setFilter (CompositeFilterOperator .and (
1373+ new PropertyFilter (Article .ARTICLE_TYPE , FilterOperator .NOT_EQUAL , Article .ARTICLE_TYPE_C_DISCUSSION ),
1374+ new PropertyFilter (Article .ARTICLE_TYPE , FilterOperator .NOT_EQUAL , Article .ARTICLE_TYPE_C_LONG ),
1375+ new PropertyFilter (Article .ARTICLE_STATUS , FilterOperator .EQUAL , Article .ARTICLE_STATUS_C_VALID ),
1376+ new PropertyFilter (Article .ARTICLE_SHOW_IN_LIST , FilterOperator .NOT_EQUAL , Article .ARTICLE_SHOW_IN_LIST_C_NOT ),
1377+ new PropertyFilter (Article .ARTICLE_CREATE_TIME , FilterOperator .GREATER_THAN_OR_EQUAL , String .valueOf (DateUtils .addDays (new Date (), -30 ).getTime ())))).
1378+ setPageCount (1 ).setPage (page , fetchSize ).
1379+ addSort (Article .ARTICLE_LATEST_CMT_TIME , SortDirection .DESCENDING );
1380+ }
1381+
1382+ private Set <String > buildArticleIdSet (final List <JSONObject > articles ) {
1383+ final Set <String > articleIds = new HashSet <>();
1384+ for (final JSONObject article : articles ) {
1385+ articleIds .add (article .optString (Keys .OBJECT_ID ));
1386+ }
1387+
1388+ return articleIds ;
1389+ }
1390+
1391+ private int calcIndexRecentExtraSize (final int fetchSize , final Set <String > stickArticleIds ) throws RepositoryException {
1392+ if (stickArticleIds .isEmpty ()) {
1393+ return 0 ;
1394+ }
1395+
1396+ final Query firstPageQuery = buildIndexRecentArticlesQuery (1 , fetchSize );
1397+ final List <JSONObject > firstPageArticles = articleRepository .getList (firstPageQuery );
1398+ int duplicatedCount = 0 ;
1399+ for (final JSONObject article : firstPageArticles ) {
1400+ if (stickArticleIds .contains (article .optString (Keys .OBJECT_ID ))) {
1401+ duplicatedCount ++;
1402+ }
1403+ }
1404+
1405+ return Math .max (0 , stickArticleIds .size () - duplicatedCount );
1406+ }
1407+
13621408 /**
13631409 * Gets the long articles (articleType=6) for index display.
13641410 *
0 commit comments