搜索时的一种记录最近几次搜索记录的实现方法
在短信搜索或者其他搜索时我们为了提高搜索效率,尽可能少的对数据库进行操作,常常采用记录最近几次搜索结果的方式来实现高效率搜索,以下是SHP平台中新建短信时输入发送地址中,进行搜索的实现逻辑,此处采用了一个始终具有三个节点的链表,来保存搜索记录,只要当前搜索的字段在这个链表里有保存,那就不去访问数据库,直接返回之前保存的结果,否则再对数据库进行访问。
代码片段 第一层
--------------------------------------------------
static BOOL
__MsgSvcSearchPredictiveListTaskSearch ( _MsgAsyncRequest* pAsyncRequest ) //搜索服务的入口函数,所有搜索都从这个函数开始
{
__MsgSvcSearchPredictiveListTaskSearch ( _MsgAsyncRequest* pAsyncRequest ) //搜索服务的入口函数,所有搜索都从这个函数开始
{
...........
pResult =
__MsgSvcSearchPredictiveListCreate(pSearchRequestDetail->szSearch, pSearchRequestDetail->searchTargetOption, pSearchRequestDetail->searchResultOption);
//创建搜索的SQL以及执行SQL,访问数据库的操作在这个函数中进行
...........
__MsgSvcSearchPredictiveListLoadResultItems( pResult, pResult->itemCount + gMsgPredictiveListResultUnitCnt, TRUE);
//取的对应的搜索结果,返回给上层APP
...........
}
代码片段 第二层
--------------------------------------------------
static __MsgPredictiveListResult *
__MsgSvcSearchPredictiveListCreate(char* szSearch, int searchTargetOption, int searchResultOption)
{
__MsgSvcSearchPredictiveListCreate(char* szSearch, int searchTargetOption, int searchResultOption)
{
...........
pResult =
__MsgSvcSearchPredictiveListSearchGetSavedResult
( szSearch, searchTargetOption, searchResultOption );
if(pResult)
{
SysDebug(( MID_MSG|DBG_MSG_COMMON, "__MsgSvcSearchPredictiveListCreate : Saved Result is found.));
return pResult; //如果保存的结果被正常取出,则直接返回结果
}
if(pResult)
{
SysDebug(( MID_MSG|DBG_MSG_COMMON, "__MsgSvcSearchPredictiveListCreate : Saved Result is found.));
return pResult; //如果保存的结果被正常取出,则直接返回结果
}
............
//否则进行访问数据库等一些列操作
__MsgSvcSearchPredictiveListSaveResult
(pResult);
//保存新搜索出的结果
}
代码片段 第三层
--------------------------------------------------
static __MsgPredictiveListResult *
__MsgSvcSearchPredictiveListSearchGetSavedResult( char* szSearch, int searchTargetOption, int searchResultOption )
{
int i = 0;
int count = ListGetCount(&gMsgSvcSearchPredictListResultList); //取得结果历史链表里的节点个数,也就是记录的前几次搜索的个数
__MsgPredictiveListResult *pTempResult;
SysDebug(( MID_MSG|DBG_MSG_COMMON, "__MsgSvcSearchPredictiveListSearchGetSavedResult : "));
for(i=0; i<count; i++) //判断此次搜索的字串是不是之前已经搜索过,也就是从历史链表里找有没有与此次搜索字段相同的记录
{
pTempResult = (__MsgPredictiveListResult*) ListGetNthObject(&gMsgSvcSearchPredictListResultList, i);
if(pTempResult == NULL)
continue;
if(AcStrcmp(pTempResult->szSearchString, szSearch) != 0) continue;
if(pTempResult->searchTargetOption != searchTargetOption) continue;
if(pTempResult->searchResultOption != searchResultOption) continue;
// same predictive list result found
// move to first and return result
if(i != 0) //如果找到相同的结果,将其放到前面,也就是更新一次链表的排列顺序
{
ListDeleteNthObject(&gMsgSvcSearchPredictListResultList, i);
ListInsertObject(&gMsgSvcSearchPredictListResultList, pTempResult, 0);
}
SysDebug(( MID_MSG|DBG_MSG_COMMON, "Saved Record is FOUND!!!\n"));
return pTempResult;
}
SysDebug(( MID_MSG|DBG_MSG_COMMON, "Saved Record is not found\n"));
return NULL;
}
__MsgSvcSearchPredictiveListSearchGetSavedResult( char* szSearch, int searchTargetOption, int searchResultOption )
{
int i = 0;
int count = ListGetCount(&gMsgSvcSearchPredictListResultList); //取得结果历史链表里的节点个数,也就是记录的前几次搜索的个数
__MsgPredictiveListResult *pTempResult;
SysDebug(( MID_MSG|DBG_MSG_COMMON, "__MsgSvcSearchPredictiveListSearchGetSavedResult : "));
for(i=0; i<count; i++) //判断此次搜索的字串是不是之前已经搜索过,也就是从历史链表里找有没有与此次搜索字段相同的记录
{
pTempResult = (__MsgPredictiveListResult*) ListGetNthObject(&gMsgSvcSearchPredictListResultList, i);
if(pTempResult == NULL)
continue;
if(AcStrcmp(pTempResult->szSearchString, szSearch) != 0) continue;
if(pTempResult->searchTargetOption != searchTargetOption) continue;
if(pTempResult->searchResultOption != searchResultOption) continue;
// same predictive list result found
// move to first and return result
if(i != 0) //如果找到相同的结果,将其放到前面,也就是更新一次链表的排列顺序
{
ListDeleteNthObject(&gMsgSvcSearchPredictListResultList, i);
ListInsertObject(&gMsgSvcSearchPredictListResultList, pTempResult, 0);
}
SysDebug(( MID_MSG|DBG_MSG_COMMON, "Saved Record is FOUND!!!\n"));
return pTempResult;
}
SysDebug(( MID_MSG|DBG_MSG_COMMON, "Saved Record is not found\n"));
return NULL;
}
.........
__MsgSvcSearchPredictiveListSaveResult( __MsgPredictiveListResult *pResult )
{
__MsgPredictiveListResult *pResultToDelete;
ListInsertObject(&gMsgSvcSearchPredictListResultList, pResult, 0);
while(ListGetCount(&gMsgSvcSearchPredictListResultList) > gMsgPredictiveListSavedResultCnt) //始终确保链表里有3个记录,当有最新的记录进来时,把链表末端的节点删除
{
pResultToDelete = (__MsgPredictiveListResult *)ListDeleteNthObject(&gMsgSvcSearchPredictListResultList, gMsgPredictiveListSavedResultCnt);
__MsgSvcSearchPredictiveListFreeResult(pResultToDelete); //释放结果集
}
return TRUE;
}
--------------------------------------
此方法可以用于对搜索效率有要求的场合,此外此方法的使用提高了代码的逻辑性,除了提高了访问数据库的效率,也提供了一种较好的APP搜索设计思路。