如何使用erlang批量保存到DB

时间:2017-06-23 12:51:41

标签: python functional-programming erlang

我在python中有以下类,每个buffer_size个记录

批量插入数据库
class Persister(object):

    def __init__(self, buffer_size):
        self.collection = []
        self.buffer_size = buffer_size

    def persist(self):
        # code to save self.collection to some database goes here...

    def save(self, new_record):
        self.collection.append(new_record)
        if len(self.collection) > self.buffer_size:
            self.persist()
            self.collection = []

我可以这样使用它:

MyTablePersister = Persister(1000)
records = [] #imaging a list of thousands of records here
for record in records:
    MyTablePersister.save(record)

我的问题是如何在Erlang中实现它?

1 个答案:

答案 0 :(得分:1)

您可以使用gen_server

-module(s3).
-behaviour(gen_server).
-export([start/1, stop/0, save/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-define(SERVER_NAME, ?MODULE).
-define(CALLBACK_MODULE, ?MODULE).

%%Interface functions:
start(BuffSize) ->
    gen_server:start_link({local, ?SERVER_NAME}, ?CALLBACK_MODULE, [BuffSize], []).

stop() ->
    gen_server:call(?CALLBACK_MODULE, stop).

save(Record) ->
    gen_server:call(?CALLBACK_MODULE, {save, Record}).

persist(Records) ->
    %%Code to save Records to db
    io:format("BuffSize exceeded. Saving to database.~n").

%%Callback functions:
init([BuffSize]) ->
    {ok, {BuffSize, 0, []} }.

handle_call({save, Record}, _From, {BuffSize, Count, Records}) ->
    NewRecords = [Record|Records],
    NewState = 
        case Count+1 > BuffSize of
            true -> 
                persist(NewRecords),
                {BuffSize, 0, []};
            false ->
                {BuffSize, Count+1, NewRecords}
        end,
    {reply, NewRecords, NewState};

handle_call(stop, _From, State) ->
    {stop, normal, stopped, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.
handle_info(_Info, State) ->
    {noreply, State}.
terminate(_Reason, _State) ->
    ok.
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

在shell中:

1> c(s3).
s3.erl:48: Warning: variable 'Records' is unused
{ok,s3}

2> s3:start(3).
{ok,<0.64.0>}

3> s3:save(["John", 1]).
[["John",1]]

4> s3:save(["Sally", 2]).
[["Sally",2],["John",1]]

5> s3:save(["Joe", 3]).
[["Joe",3],["Sally",2],["John",1]]

6> s3:save(["Sue", 4]).
BuffSize exceeded. Saving to database.
[["Sue",4],["Joe",3],["Sally",2],["John",1]]

7> s3:save(["Jill", 5]).
[["Jill",5]]

8>