mirror of
https://github.com/btdig/dhtcrawler2.git
synced 2025-01-31 18:41:37 +00:00
IMPORTANT: add number id to torrent database
This commit is contained in:
parent
2944019ca1
commit
8f04efed5d
@ -25,9 +25,14 @@
|
|||||||
init(Host, Port) ->
|
init(Host, Port) ->
|
||||||
{ok, Conn} = mongo_connection:start_link({Host, Port}),
|
{ok, Conn} = mongo_connection:start_link({Host, Port}),
|
||||||
?I(?FMT("connect mongodb ~p:~p success", [Host, Port])),
|
?I(?FMT("connect mongodb ~p:~p success", [Host, Port])),
|
||||||
|
init(Conn),
|
||||||
|
Conn.
|
||||||
|
|
||||||
|
init(Conn) ->
|
||||||
enable_text_search(Conn),
|
enable_text_search(Conn),
|
||||||
ensure_search_index(Conn),
|
ensure_search_index(Conn),
|
||||||
Conn.
|
% TODO: numid index ?
|
||||||
|
ok.
|
||||||
|
|
||||||
close(Conn) ->
|
close(Conn) ->
|
||||||
mongo_connection:stop(Conn).
|
mongo_connection:stop(Conn).
|
||||||
@ -89,16 +94,15 @@ index(Conn, Hash) when is_list(Hash) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
insert(Conn, Hash, Name, Length, Files) when is_list(Hash) ->
|
insert(Conn, Hash, Name, Length, Files) when is_list(Hash) ->
|
||||||
NewDoc = create_torrent_desc(Hash, Name, Length, 1, Files),
|
NewDoc = create_torrent_desc(Conn, Hash, Name, Length, 1, Files),
|
||||||
mongo_do(Conn, fun() ->
|
mongo_do(Conn, fun() ->
|
||||||
%mongo:insert(?COLLNAME, NewDoc)
|
% the doc may already exist because the other process has inserted before
|
||||||
% since the doc may already exist (inc_announce failed), i update the doc here
|
|
||||||
Sel = {'_id', list_to_binary(Hash)},
|
Sel = {'_id', list_to_binary(Hash)},
|
||||||
mongo:update(?COLLNAME, Sel, NewDoc, true)
|
mongo:update(?COLLNAME, Sel, NewDoc, true)
|
||||||
end).
|
end).
|
||||||
|
|
||||||
unsafe_insert(Conn, Tors) when is_list(Tors) ->
|
unsafe_insert(Conn, Tors) when is_list(Tors) ->
|
||||||
Docs = [create_torrent_desc(Hash, Name, Length, 1, Files) ||
|
Docs = [create_torrent_desc(Conn, Hash, Name, Length, 1, Files) ||
|
||||||
{Hash, Name, Length, Files} <- Tors],
|
{Hash, Name, Length, Files} <- Tors],
|
||||||
mongo:do(unsafe, master, Conn, ?DBNAME, fun() ->
|
mongo:do(unsafe, master, Conn, ?DBNAME, fun() ->
|
||||||
mongo:insert(?COLLNAME, Docs)
|
mongo:insert(?COLLNAME, Docs)
|
||||||
@ -135,7 +139,7 @@ enable_text_search(Conn) ->
|
|||||||
mongo:command(Cmd)
|
mongo:command(Cmd)
|
||||||
end).
|
end).
|
||||||
|
|
||||||
create_torrent_desc(Hash, Name, Length, Announce, Files) ->
|
create_torrent_desc(Conn, Hash, Name, Length, Announce, Files) ->
|
||||||
NameArray = case string_split:split(Name) of
|
NameArray = case string_split:split(Name) of
|
||||||
{error, L, D} ->
|
{error, L, D} ->
|
||||||
?E(?FMT("string split failed(error): ~p ~p", [L, D])),
|
?E(?FMT("string split failed(error): ~p ~p", [L, D])),
|
||||||
@ -146,6 +150,8 @@ create_torrent_desc(Hash, Name, Length, Announce, Files) ->
|
|||||||
{ok, R} -> R
|
{ok, R} -> R
|
||||||
end,
|
end,
|
||||||
{'_id', list_to_binary(Hash),
|
{'_id', list_to_binary(Hash),
|
||||||
|
% steven told me it's necessary for sphinx, what if the doc already exists ?
|
||||||
|
numid, db_system:get_torrent_id(Conn),
|
||||||
name, list_to_binary(Name),
|
name, list_to_binary(Name),
|
||||||
name_array, NameArray,
|
name_array, NameArray,
|
||||||
length, Length,
|
length, Length,
|
||||||
|
@ -13,11 +13,30 @@
|
|||||||
stats_day_at/2,
|
stats_day_at/2,
|
||||||
stats_day_at_slave/2,
|
stats_day_at_slave/2,
|
||||||
stats_get_peers/1]).
|
stats_get_peers/1]).
|
||||||
|
-export([get_torrent_id/1]).
|
||||||
|
-compile(export_all).
|
||||||
-define(DBNAME, dht_system).
|
-define(DBNAME, dht_system).
|
||||||
-define(COLLNAME, system).
|
-define(COLLNAME, system).
|
||||||
-define(HASH_BATCH_KEY, <<"hashbatch">>).
|
-define(HASH_BATCH_KEY, <<"hashbatch">>).
|
||||||
-define(STATS_COLLNAME, stats).
|
-define(STATS_COLLNAME, stats).
|
||||||
|
-define(TORRENT_ID_KEY, <<"torrentid">>).
|
||||||
|
|
||||||
|
% increase the seed and return the new id
|
||||||
|
get_torrent_id(Conn) ->
|
||||||
|
Cmd = {findAndModify, ?COLLNAME, query, {'_id', ?TORRENT_ID_KEY},
|
||||||
|
update, {'$inc', {seed, 1}}, new, false, upsert, true},
|
||||||
|
Ret = mongo:do(safe, master, Conn, ?DBNAME, fun() ->
|
||||||
|
mongo:command(Cmd)
|
||||||
|
end),
|
||||||
|
case bson:lookup(value, Ret) of
|
||||||
|
{undefined} ->
|
||||||
|
0;
|
||||||
|
{} ->
|
||||||
|
0;
|
||||||
|
{Obj} ->
|
||||||
|
{Seed} = bson:lookup(seed, Obj),
|
||||||
|
Seed
|
||||||
|
end.
|
||||||
|
|
||||||
%% batch index
|
%% batch index
|
||||||
inc_batch_rindex(Conn) ->
|
inc_batch_rindex(Conn) ->
|
||||||
@ -104,4 +123,10 @@ stats_ensure_today(TodaySecs) ->
|
|||||||
Doc
|
Doc
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%%
|
||||||
|
test_torrent_id() ->
|
||||||
|
{ok, Conn} = mongo_connection:start_link({localhost, 27017}),
|
||||||
|
ID = get_torrent_id(Conn),
|
||||||
|
ID.
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,6 +45,9 @@ start_link(IP, Port, Size) ->
|
|||||||
start_link(IP, Port, Size, OtherProcess) ->
|
start_link(IP, Port, Size, OtherProcess) ->
|
||||||
PoolName = mongodb_conn_pool_name,
|
PoolName = mongodb_conn_pool_name,
|
||||||
mongo_sup:start_pool(PoolName, 5, {IP, Port}),
|
mongo_sup:start_pool(PoolName, 5, {IP, Port}),
|
||||||
|
% ensure index
|
||||||
|
Conn = mongo_pool:get(PoolName),
|
||||||
|
db_store_mongo:init(Conn),
|
||||||
supervisor:start_link({local, srv_name()}, ?MODULE, [PoolName, Size, OtherProcess]).
|
supervisor:start_link({local, srv_name()}, ?MODULE, [PoolName, Size, OtherProcess]).
|
||||||
|
|
||||||
srv_name() ->
|
srv_name() ->
|
||||||
|
Loading…
Reference in New Issue
Block a user