summaryrefslogtreecommitdiff
path: root/src/fabric/src/fabric_doc_atts.erl
blob: 80a36ee51469e1b4a461e9ad763648fd48e044d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(fabric_doc_atts).

-compile(tuple_calls).

-include_lib("fabric/include/fabric.hrl").
-include_lib("couch/include/couch_db.hrl").

-export([
    receiver/3,
    receiver_callback/2
]).

receiver(_Req, _DbName, undefined) ->
    <<"">>;
receiver(_Req, _DbName, {unknown_transfer_encoding, Unknown}) ->
    exit({unknown_transfer_encoding, Unknown});
receiver(Req, DbName, chunked) ->
    MiddleMan = spawn(fun() -> middleman(Req, DbName, chunked) end),
    {fabric_attachment_receiver, MiddleMan, chunked};
receiver(_Req, _DbName, 0) ->
    <<"">>;
receiver(Req, DbName, Length) when is_integer(Length) ->
    maybe_send_continue(Req),
    Middleman = spawn(fun() -> middleman(Req, DbName, Length) end),
    {fabric_attachment_receiver, Middleman, Length};
receiver(_Req, _DbName, Length) ->
    exit({length_not_integer, Length}).

receiver_callback(Middleman, chunked) ->
    fun(4096, ChunkFun, State) ->
        write_chunks(Middleman, ChunkFun, State)
    end;
receiver_callback(Middleman, Length) when is_integer(Length) ->
    fun() ->
        Middleman ! {self(), gimme_data},
        Timeout = fabric_util:attachments_timeout(),
        receive
            {Middleman, Data} ->
                rexi:reply(attachment_chunk_received),
                Data
        after Timeout ->
            exit(timeout)
        end
    end.

%%
%% internal
%%

maybe_send_continue(#httpd{mochi_req = MochiReq} = Req) ->
    case couch_httpd:header_value(Req, "expect") of
        undefined ->
            ok;
        Expect ->
            case string:to_lower(Expect) of
                "100-continue" ->
                    MochiReq:start_raw_response({100, gb_trees:empty()});
                _ ->
                    ok
            end
    end.

write_chunks(MiddleMan, ChunkFun, State) ->
    MiddleMan ! {self(), gimme_data},
    Timeout = fabric_util:attachments_timeout(),
    receive
        {MiddleMan, ChunkRecordList} ->
            rexi:reply(attachment_chunk_received),
            case flush_chunks(ChunkRecordList, ChunkFun, State) of
                {continue, NewState} ->
                    write_chunks(MiddleMan, ChunkFun, NewState);
                {done, NewState} ->
                    NewState
            end
    after Timeout ->
        exit(timeout)
    end.

flush_chunks([], _ChunkFun, State) ->
    {continue, State};
flush_chunks([{0, _}], _ChunkFun, State) ->
    {done, State};
flush_chunks([Chunk | Rest], ChunkFun, State) ->
    NewState = ChunkFun(Chunk, State),
    flush_chunks(Rest, ChunkFun, NewState).

receive_unchunked_attachment(_Req, 0) ->
    ok;
receive_unchunked_attachment(Req, Length) ->
    receive
        {MiddleMan, go} ->
            Data = couch_httpd:recv(Req, 0),
            MiddleMan ! {self(), Data}
    end,
    receive_unchunked_attachment(Req, Length - size(Data)).

middleman(Req, DbName, chunked) ->
    % spawn a process to actually receive the uploaded data
    RcvFun = fun(ChunkRecord, ok) ->
        receive
            {From, go} -> From ! {self(), ChunkRecord}
        end,
        ok
    end,
    Receiver = spawn(fun() -> couch_httpd:recv_chunked(Req, 4096, RcvFun, ok) end),

    % take requests from the DB writers and get data from the receiver
    N = mem3:n(DbName),
    Timeout = fabric_util:attachments_timeout(),
    middleman_loop(Receiver, N, [], [], Timeout);
middleman(Req, DbName, Length) ->
    Receiver = spawn(fun() -> receive_unchunked_attachment(Req, Length) end),
    N = mem3:n(DbName),
    Timeout = fabric_util:attachments_timeout(),
    middleman_loop(Receiver, N, [], [], Timeout).

middleman_loop(Receiver, N, Counters0, ChunkList0, Timeout) ->
    receive
        {From, gimme_data} ->
            % Figure out how far along this writer (From) is in the list
            ListIndex =
                case fabric_dict:lookup_element(From, Counters0) of
                    undefined -> 0;
                    I -> I
                end,

            % Talk to the receiver to get another chunk if necessary
            ChunkList1 =
                if
                    ListIndex == length(ChunkList0) ->
                        Receiver ! {self(), go},
                        receive
                            {Receiver, ChunkRecord} ->
                                ChunkList0 ++ [ChunkRecord]
                        end;
                    true ->
                        ChunkList0
                end,

            % reply to the writer
            Reply = lists:nthtail(ListIndex, ChunkList1),
            From ! {self(), Reply},

            % Update the counter for this writer
            Counters1 = fabric_dict:update_counter(From, length(Reply), Counters0),

            % Drop any chunks that have been sent to all writers
            Size = fabric_dict:size(Counters1),
            NumToDrop = lists:min([I || {_, I} <- Counters1]),

            {ChunkList3, Counters3} =
                if
                    Size == N andalso NumToDrop > 0 ->
                        ChunkList2 = lists:nthtail(NumToDrop, ChunkList1),
                        Counters2 = [{F, I - NumToDrop} || {F, I} <- Counters1],
                        {ChunkList2, Counters2};
                    true ->
                        {ChunkList1, Counters1}
                end,

            middleman_loop(Receiver, N, Counters3, ChunkList3, Timeout)
    after Timeout ->
        exit(Receiver, kill),
        ok
    end.