Permalink
Browse files

remote files not needed

  • Loading branch information...
1 parent bb0239e commit 9370e3cd1cc243b519be7ba5c3f702a9c2198cc1 @echou committed Jul 9, 2012
@@ -1,6 +0,0 @@
-#NATIVE=1
-
-APP_NAME=mcache
-#ERL_COMPILE_FLAGS += -I ../include
-
-include $(ERL_TOP)/make/app.mk
@@ -1,106 +0,0 @@
--module(mcache).
--author('echou327@gmail.com').
-
-%-compile([inline]).
-%-compile([native, {hipe, o3}]).
-
--export([get_server/2, get/2, mget/2, set/5, set/4, delete/2]).
-
--define(SEP, ":").
--define(MGET_TIMEOUT, 500).
-
-get(Class, Key) ->
- {Key1, Server, _DefaultExpiry} = get_server(Class, Key),
- {_, Value} = mcache_client:mc_get(Server, Key1),
- %io:format("get ~p~n", [Value]),
- mcache_util:decode_value(Value).
-
-mget(Class, [_|_]=Keys) ->
- erlang:yield(),
- {KeyDict, ServerDict} = lists:foldl(
- fun(K, {KAcc,SAcc}) ->
- {K1, Server, _DefaultExpiry} = get_server(Class, K),
- {dict:store(K1, K, KAcc), dict:append(Server, K1, SAcc)}
- end, {dict:new(), dict:new()}, Keys),
- Ref = erlang:make_ref(),
- dict:map(fun(Server, Ks) ->
- mcache_client:ab_mget(Server, Ref, Ks)
- end, ServerDict),
- Results = mget_receive(dict:size(KeyDict), Ref, ?MGET_TIMEOUT*1000, []),
- %io:format("~p~n", [Results]),
- flat_foldl(
- fun({RealKey, Val}, LAcc) ->
- case dict:find(RealKey, KeyDict) of
- {ok, Key} ->
- [{Key, mcache_util:decode_value(Val)}|LAcc];
- _ ->
- LAcc
- end
- end, [], Results).
-
-mget2(Class, [_|_]=Keys) ->
- ServerDict = lists:foldl(
- fun(K, SAcc) ->
- {K1, Server, _DefaultExpiry} = get_server(Class, K),
- dict:append(Server, K1, SAcc)
- end, dict:new(), Keys),
- Ref = erlang:make_ref(),
- dict:map(fun(Server, Ks) ->
- mcache_client:ab_mget(Server, Ref, Ks)
- end, ServerDict),
- Results = mget_receive(length(Keys), Ref, ?MGET_TIMEOUT*1000, []).
-
-set(Class, Key, Value, Format, Expiry) ->
- {Key1, Server, DefaultExpiry} = get_server(Class, Key),
- {Value1, Flags} = mcache_util:encode_value(Value, Format),
- Expiry1 = mcache_util:encode_expiry(Expiry, DefaultExpiry),
- mcache_client:ab_set(Server, Key1, Value1, Flags, Expiry1),
- ok.
-
-set(Class, Key, Value, Format) ->
- set(Class, Key, Value, Format, default).
-
-delete(Class, Key) ->
- {Key1, Server} = get_server(Class, Key),
- mcache_client:ab_delete(Server, Key1),
- ok.
-
-% internal functions
-
-flat_foldl(_Fun, Acc, []) ->
- Acc;
-flat_foldl(Fun, Acc, [H|T]) ->
- Acc1 = case H of
- [] -> Acc;
- [_|_] -> flat_foldl(Fun, Acc, H);
- _ -> Fun(H, Acc)
- end,
- flat_foldl(Fun, Acc1, T).
-
-my_now() ->
- erlang:now().
-
-wrap_items(nil, L) ->
- L;
-wrap_items(Items, L) ->
- [Items|L].
-
-mget_receive(N, _Ref, Timeout, L) when N =< 0; Timeout =< 0 ->
- L;
-mget_receive(N, Ref, Timeout, L) ->
- Now = my_now(),
- TimeoutMillis = Timeout div 1000,
- receive
- {Ref, {mget, NumKeys, Items}} ->
- Now1 = my_now(),
- T1 = Timeout - timer:now_diff(Now1, Now),
- mget_receive(N-NumKeys, Ref, T1, wrap_items(Items, L))
- after TimeoutMillis ->
- L
- end.
-
-get_server(Class, Key) ->
- Key1 = mcache_util:map_key(Class, Key),
- {Pool, Expiry} = mcache_expires:expire(Class),
- Server = mcache_continuum:find(Pool, mcache_util:hash(Key1, md5)),
- {Key1, Server, Expiry}.
@@ -1,55 +0,0 @@
-%
-% wraps memcached_drv
-%
-
--module(mcache2).
--author('echou327@gmail.com').
-
--export([get/2, mget/2, mget2/2, set/5, delete/3]).
-
--define(DICT, dict).
-
-get(Class, Key) ->
- {Pool, _Expiry} = mcache_expires:expire(Class),
- {mc_async, 0, {ok, Value}} = memcached_drv:get(Pool, 0, mcache_util:map_key(Class, Key)),
- mcache_util:decode_value(Value).
-
-mget(Class, [_|_]=Keys) ->
- {Pool, _Expiry} = mcache_expires:expire(Class),
- {RealKeys, KeyDict} = lists:foldl(fun(K, {RK,D}) ->
- K1 = mcache_util:map_key(Class, K),
- {[K1|RK], ?DICT:store(K1, K, D)}
- end, {[], ?DICT:new()}, Keys),
-
- {mc_async, 0, {ok, Values}} = memcached_drv:mget(Pool, 0, RealKeys),
- lists:foldl(fun({Key, Val, Flag}, Acc) ->
- case ?DICT:find(Key, KeyDict) of
- error -> Acc;
- {ok, K} -> [{K, mcache_util:decode_value({Val, Flag})}|Acc]
- end
- end, [], Values).
-
-decode_values([], [], L) ->
- lists:reverse(L);
-decode_values([K|Ks], [undefined|Vs], L) ->
- decode_values(Ks, Vs, L);
-decode_values([K|Ks], [V|Vs], L) ->
- decode_values(Ks, Vs, [{K, mcache_util:decode_value(V)}|L]).
-
-mget2(Class, [_|_]=Keys) ->
- {Pool, _Expiry} = mcache_expires:expire(Class),
- {mc_async, 0, {ok, Values}} = memcached_drv:mget2(Pool, 0, Class, Keys),
- decode_values(Keys, Values, []).
-
-set(Class, Key, Value, Format, Expiry) ->
- {Pool, DefaultExpiry} = mcache_expires:expire(Class),
- {Value1, Flags} = mcache_util:encode_value(Value, Format),
- Expiry1 = mcache_util:encode_expiry(Expiry, DefaultExpiry),
- {mc_async, 0, ok} = memcached_drv:set(Pool, 0, mcache_util:map_key(Class, Key), Value1, Flags, Expiry1),
- ok.
-
-delete(Class, Key, Expiry) ->
- {Pool, _} = mcache_expires:expire(Class),
- Expiry1 = mcache_util:encode_expiry(Expiry, 0),
- {mc_async, 0, ok} = memcached_drv:delete(Pool, 0, mcache_util:map_key(Class, Key), Expiry1),
- ok.
@@ -1,65 +0,0 @@
--module(mcache_app).
--author('echou327@gmail.com').
-
--behaviour(application).
--behaviour(supervisor).
-
--export([start/2, stop/1, config_change/3]).
--export([init/1]).
-
-
--export([restart/0, start/0, start_link/0, stop/0]).
-
-% API
-
-start_link() ->
- supervisor:start_link({local, ?MODULE}, ?MODULE, []).
-
-start() ->
- application:start(mcache).
-
-stop() ->
- application:stop(mcache).
-
-restart() ->
- io:format("[MCACHE_APP] restart() ~n", []),
- mcache_util:reload_config([mcache]),
- ?MODULE:stop(),
- ?MODULE:start().
-
-% application callbacks
-
-start(_Type, _Args) ->
- ?MODULE:start_link().
-
-stop(_State) ->
- ok.
-
-config_change(_Changed, _New, _Removed) ->
- ok.
-
-% supervisor callback
-init([]) ->
- io:format("[MCACHE] Starting~n"),
- Specs = specs([{mcache_client_sup, 1}, % must be 1
- {mcache_config, 1},
- {mcache_memcached_drv_sup, 1}
- ]), % must be 1
- {ok, {{one_for_one, 10, 10}, Specs}}.
-
-% supervisor local functions
-specs(Specs) ->
- lists:foldl(fun({Module, Count}, Acc) ->
- Acc ++ mcache_util:sup_child_spec(Module, fun one_spec/2, Count)
- end, [], Specs).
-
-one_spec(mcache_config, Id) ->
- PoolsConfig = mcache_util:get_app_env(pools, []),
- ExpiresConfig = mcache_util:get_app_env(expires, []),
- {Id, {mcache_config, start_link, [ {PoolsConfig, ExpiresConfig} ]}, permanent, 2000, worker, []};
-one_spec(mcache_client_sup, Id) ->
- {Id, {mcache_client_sup, start_link, []}, permanent, infinity, supervisor, []};
-one_spec(mcache_memcached_drv_sup, Id) ->
- {Id, {mcache_memcached_drv_sup, start_link, []}, permanent, infinity, supervisor, []};
-one_spec(Module, Id) ->
- {Id, {Module, start_link, []}, permanent, 2000, worker, []}.
@@ -1,62 +0,0 @@
--module(mcache_binary_frame).
--author('echou327@gmail.com').
-
--compile(bin_opt_info).
-
--export([initial_state/0, parse/2, encode/1]).
-
--include("mcache_binary_frame.hrl").
-
--define(HEADER_LEN, 24). % magic (0x81) + header (23 bytes)
-
--record(frame, {data_len=?HEADER_LEN, data= <<>>, resp=nil}).
--record(packet, {seq,opcode,status,key_len,extra_len,body}).
-
-% parse response packets
-
-initial_state() ->
- {header, #frame{data_len=?HEADER_LEN, data= <<>>, resp=nil}, []}.
-
-
-parse({State, Frame, Acc}, <<>>) ->
- { {State, Frame, []}, lists:reverse(Acc)};
-
-parse({State, Frame, Acc}, Data) ->
- case parse_fsm(State, Frame, Data) of
- {done, Rest, Frame1} ->
- parse({header, #frame{}, [Frame1#frame.resp|Acc]}, Rest);
- {State1, Rest1, Frame1} ->
- parse({State1, Frame1, Acc}, Rest1)
- end.
-
-parse_fsm(header, #frame{data_len=Len, data=Data}=Frame, Bin) when byte_size(Bin) >= Len ->
- <<16#81, Opcode, KeyLen:16, ExtraLen, DataType, Status:16, TotalBodyLen:32, Seq:32, CAS:64, Rest/binary>> = <<Data/binary, Bin/binary>>,
- Resp = #resp{opcode=Opcode,
- status=Status,
- key_len=KeyLen, extra_len=ExtraLen,
- body_len=TotalBodyLen-KeyLen-ExtraLen,
- seq=Seq, data_type=DataType, cas=CAS},
- State = if TotalBodyLen =:= 0 -> done; true -> body end,
- {State, Rest, Frame#frame{data_len=TotalBodyLen, data= <<>>, resp=Resp}};
-
-parse_fsm(body, #frame{data_len=Len, data=Data, resp=Resp}=Frame, Bin) when byte_size(Bin) >= Len ->
- KeyLen = Resp#resp.key_len,
- ExtraLen = Resp#resp.extra_len,
- BodyLen = Resp#resp.body_len,
- <<Extra:ExtraLen/binary, Key:KeyLen/binary, Body:BodyLen/binary, Rest/binary>> = <<Data/binary, Bin/binary>>,
- {done, Rest, Frame#frame{data_len=0, data= <<>>, resp=Resp#resp{key=Key, extra=Extra, body=Body}}};
-
-parse_fsm(State, #frame{data_len=Len, data=Data}=Frame, Bin) ->
- {State, <<>>, Frame#frame{data_len=Len-byte_size(Bin), data= <<Data/binary, Bin/binary>>}}.
-
-
-% encode
-encode(#req{opcode=Opcode, data_type=DataType, seq=Seq, cas=CAS, extra=Extra, key=Key, body=Body}) ->
- KeyLen = iolist_size(Key),
- BodyLen = iolist_size(Body),
- ExtraLen = iolist_size(Extra),
- TotalBodyLen = KeyLen + BodyLen + ExtraLen,
- %OpcodeNum = mcache_proto:opcode(Opcode),
- [ <<16#80, Opcode:8, KeyLen:16, ExtraLen:8, DataType:8, 0:16, TotalBodyLen:32, Seq:32, CAS:64>>, Extra, Key, Body ].
-
-
Oops, something went wrong.

0 comments on commit 9370e3c

Please sign in to comment.