fix hash_date

This commit is contained in:
Kevin Lynx 2013-07-19 21:32:10 +08:00
parent 37ccb19575
commit 54a30122fa
2 changed files with 7 additions and 7 deletions

View File

@ -5,7 +5,7 @@
%% To track the most recently hashes %% To track the most recently hashes
%% %%
-module(db_daterange). -module(db_daterange).
-export([insert/3, -export([insert/4,
lookup/3]). lookup/3]).
-export([start_link/1, -export([start_link/1,
stop/0]). stop/0]).
@ -31,11 +31,11 @@ ensure_date_index(Conn) ->
end). end).
% '_id': Hash, date: DaySecs, reqs: RequestCount % '_id': Hash, date: DaySecs, reqs: RequestCount
insert(Conn, Hash, ReqCnt) when is_list(Hash) -> insert(Conn, Hash, ReqCnt, UpSert) when is_list(Hash) ->
DaySecs = time_util:now_day_seconds(), DaySecs = time_util:now_day_seconds(),
BHash = list_to_binary(Hash), BHash = list_to_binary(Hash),
% only record today new inserted torrent % only record today new inserted torrent
Cmd = {findAndModify, ?COLLNAME, query, {'_id', BHash}, % upsert, true, Cmd = {findAndModify, ?COLLNAME, query, {'_id', BHash}, upsert, UpSert,
update, {'$inc', {reqs, ReqCnt}, '$set', {?DATE_COL, DaySecs}}, fields, {'_id', 1}}, update, {'$inc', {reqs, ReqCnt}, '$set', {?DATE_COL, DaySecs}}, fields, {'_id', 1}},
IRet = mongo:do(safe, master, Conn, ?DBNAME, fun() -> IRet = mongo:do(safe, master, Conn, ?DBNAME, fun() ->
mongo:command(Cmd) mongo:command(Cmd)

View File

@ -116,7 +116,7 @@ index(Conn, Hash) when is_list(Hash) ->
insert(Conn, Hash, Name, Length, Files) when is_list(Hash) -> insert(Conn, Hash, Name, Length, Files) when is_list(Hash) ->
NewDoc = create_torrent_desc(Conn, Hash, Name, Length, 1, Files), NewDoc = create_torrent_desc(Conn, Hash, Name, Length, 1, Files),
% TODO: because of the hash_cache_writer, the new inserted torrent lost the req_cnt value % TODO: because of the hash_cache_writer, the new inserted torrent lost the req_cnt value
db_daterange:insert(Conn, Hash, 1), db_daterange:insert(Conn, Hash, 1, true),
mongo_do(Conn, fun() -> mongo_do(Conn, fun() ->
% the doc may already exist because the other process has inserted before % the doc may already exist because the other process has inserted before
Sel = {'_id', list_to_binary(Hash)}, Sel = {'_id', list_to_binary(Hash)},
@ -146,7 +146,7 @@ inc_announce(Conn, Hash, Inc) when is_list(Hash) ->
case Ret of case Ret of
{value, undefined, ok, 1.0} -> false; {value, undefined, ok, 1.0} -> false;
{value, _Obj, lastErrorObject, {updatedExisting, true, n, 1}, ok, 1.0} -> {value, _Obj, lastErrorObject, {updatedExisting, true, n, 1}, ok, 1.0} ->
db_daterange:insert(Conn, Hash, Inc), db_daterange:insert(Conn, Hash, Inc, false),
true; true;
_ -> false _ -> false
end. end.
@ -333,9 +333,9 @@ test_index(Hash) ->
index(Conn, Hash) index(Conn, Hash)
end). end).
test_insertdate(Hash) -> test_insertdate(Hash, UpSert) ->
test_content(fun(Conn) -> test_content(fun(Conn) ->
db_daterange:insert(Conn, Hash) db_daterange:insert(Conn, Hash, 1, UpSert)
end). end).
-ifdef(SPHINX). -ifdef(SPHINX).