| 30 |
ashish |
1 |
%%
|
|
|
2 |
%% Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
%% or more contributor license agreements. See the NOTICE file
|
|
|
4 |
%% distributed with this work for additional information
|
|
|
5 |
%% regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
%% to you under the Apache License, Version 2.0 (the
|
|
|
7 |
%% "License"); you may not use this file except in compliance
|
|
|
8 |
%% with the License. You may obtain a copy of the License at
|
|
|
9 |
%%
|
|
|
10 |
%% http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
%%
|
|
|
12 |
%% Unless required by applicable law or agreed to in writing,
|
|
|
13 |
%% software distributed under the License is distributed on an
|
|
|
14 |
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
%% KIND, either express or implied. See the License for the
|
|
|
16 |
%% specific language governing permissions and limitations
|
|
|
17 |
%% under the License.
|
|
|
18 |
%%
|
|
|
19 |
|
|
|
20 |
-module(thrift_file_transport).
|
|
|
21 |
|
|
|
22 |
-behaviour(thrift_transport).
|
|
|
23 |
|
|
|
24 |
-export([new_reader/1,
|
|
|
25 |
new/1,
|
|
|
26 |
new/2,
|
|
|
27 |
write/2, read/2, flush/1, close/1]).
|
|
|
28 |
|
|
|
29 |
-record(t_file_transport, {device,
|
|
|
30 |
should_close = true,
|
|
|
31 |
mode = write}).
|
|
|
32 |
|
|
|
33 |
%%%% CONSTRUCTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
34 |
|
|
|
35 |
new_reader(Filename) ->
|
|
|
36 |
case file:open(Filename, [read, binary, {read_ahead, 1024*1024}]) of
|
|
|
37 |
{ok, IODevice} ->
|
|
|
38 |
new(IODevice, [{should_close, true}, {mode, read}]);
|
|
|
39 |
Error -> Error
|
|
|
40 |
end.
|
|
|
41 |
|
|
|
42 |
new(Device) ->
|
|
|
43 |
new(Device, []).
|
|
|
44 |
|
|
|
45 |
%% Device :: io_device()
|
|
|
46 |
%%
|
|
|
47 |
%% Device should be opened in raw and binary mode.
|
|
|
48 |
new(Device, Opts) when is_list(Opts) ->
|
|
|
49 |
State = parse_opts(Opts, #t_file_transport{device = Device}),
|
|
|
50 |
thrift_transport:new(?MODULE, State).
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
%% Parse options
|
|
|
54 |
parse_opts([{should_close, Bool} | Rest], State) when is_boolean(Bool) ->
|
|
|
55 |
parse_opts(Rest, State#t_file_transport{should_close = Bool});
|
|
|
56 |
parse_opts([{mode, Mode} | Rest], State)
|
|
|
57 |
when Mode =:= write;
|
|
|
58 |
Mode =:= read ->
|
|
|
59 |
parse_opts(Rest, State#t_file_transport{mode = Mode});
|
|
|
60 |
parse_opts([], State) ->
|
|
|
61 |
State.
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
%%%% TRANSPORT IMPL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
65 |
|
|
|
66 |
write(#t_file_transport{device = Device, mode = write}, Data) ->
|
|
|
67 |
file:write(Device, Data);
|
|
|
68 |
write(_T, _D) ->
|
|
|
69 |
{error, read_mode}.
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
read(#t_file_transport{device = Device, mode = read}, Len)
|
|
|
73 |
when is_integer(Len), Len >= 0 ->
|
|
|
74 |
file:read(Device, Len);
|
|
|
75 |
read(_T, _D) ->
|
|
|
76 |
{error, read_mode}.
|
|
|
77 |
|
|
|
78 |
flush(#t_file_transport{device = Device, mode = write}) ->
|
|
|
79 |
file:sync(Device).
|
|
|
80 |
|
|
|
81 |
close(#t_file_transport{device = Device, should_close = SC}) ->
|
|
|
82 |
case SC of
|
|
|
83 |
true ->
|
|
|
84 |
file:close(Device);
|
|
|
85 |
false ->
|
|
|
86 |
ok
|
|
|
87 |
end.
|