【问题标题】:How to save to DB in batches using erlang如何使用erlang批量保存到数据库
【发布时间】:2017-11-27 01:31:47
【问题描述】:

我在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 中实现这一点?

【问题讨论】:

    标签: python functional-programming erlang


    【解决方案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}.
    

    在外壳中:

    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>
    

    【讨论】:

    • 非常感谢,但不知道为什么我被否决了
    • @m.awad,因为您没有提供任何自己的代码来表明您尝试解决问题,所以有人否决了您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2020-05-15
    相关资源
    最近更新 更多