从Qt客户端向Erlang服务器发起数据库分页查询的请求(3)

最后,进入最重要的主题,就是实际执行分页查询的模块。

-module(sc_element).

-behaviour(gen_server).

-export([
         start_link/2,
         create/2,
         create/1,
         fetch/1,
         replace/2,
         delete/1
        ]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-include_lib("stdlib/include/qlc.hrl").

-define(SERVER, ?MODULE).
-define(DEFAULT_LEASE_TIME, 60).

-record(state, {value, lease_time, start_time, cursor}).
-record(user_data,{acc,icon,sex,calendar,year,month,day,nation,province,city,question1,question2,question3,answer1,answer2,answer3,nickname,signature=1}).
-record(available_acc,{acc,is_available}).

start_link(Value, LeaseTime) ->
    gen_server:start_link(?MODULE, [Value, LeaseTime], []).

create(Value, LeaseTime) ->
    sc_element_sup:start_child(Value, LeaseTime).

create(Value) ->
    create(Value, ?DEFAULT_LEASE_TIME).

fetch(Pid) ->
    gen_server:call(Pid, fetch).

replace(Pid, Value) ->
    gen_server:cast(Pid, {replace, Value}).

delete(Pid) ->
    gen_server:cast(Pid, delete).

init([Value, LeaseTime]) ->
    Now = calendar:local_time(),
    StartTime = calendar:datetime_to_gregorian_seconds(Now),
    Cursor=make_cursor(),
    {ok,
     #state{value = Value,
            lease_time = LeaseTime,
            start_time = StartTime,
	    cursor=Cursor},
     time_left(StartTime, LeaseTime)}.

time_left(_StartTime, infinity) ->
    infinity;
time_left(StartTime, LeaseTime) ->
    Now = calendar:local_time(),
    CurrentTime =  calendar:datetime_to_gregorian_seconds(Now),
    TimeElapsed = CurrentTime - StartTime,
    case LeaseTime - TimeElapsed of
        Time when Time =< 0 -> 0;
        Time                -> Time * 1000
    end.

handle_call(fetch, _From,  State) ->
    #state{
           lease_time = LeaseTime,
           start_time = StartTime,
	   cursor = Cursor} = State,
    TimeLeft = time_left(StartTime, LeaseTime),
    Value = get_next(Cursor),
    {reply, {ok, Value}, State, TimeLeft}.

handle_cast({replace, Value}, State) ->
    #state{lease_time = LeaseTime,
           start_time = StartTime} = State,
    TimeLeft = time_left(StartTime, LeaseTime),
    {noreply, State#state{value = Value}, TimeLeft};
handle_cast(delete, State) ->
    {stop, normal, State}.

handle_info(timeout, State) ->
    {stop, normal, State}.

terminate(_Reason, _State) ->
    sc_store:delete(self()),
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%Internal Functions

make_cursor()->
    Q=qlc:q([X||X<-mnesia:table(available_acc)]),
    mnesia:activity(async_dirty,
	fun()->qlc:cursor(Q,[]) end,
	mnesia_frag).

get_next(Cursor)->
    Get=fun()->
        qlc:next_answers(Cursor,5)
	end,
    mnesia:activity(async_dirty,Get,mnesia_frag).


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值