|
24 | 24 | -include("nouveau.hrl"). |
25 | 25 | -import(nouveau_util, [index_path/1]). |
26 | 26 |
|
27 | | -search(DbName, #index{} = Index0, QueryArgs0) -> |
| 27 | +search(DbName, #index{} = Index, #{} = QueryArgs) -> |
| 28 | + search(DbName, #index{} = Index, QueryArgs, 0). |
| 29 | + |
| 30 | +search(DbName, #index{} = Index0, QueryArgs0, UpdateLatency) -> |
28 | 31 | %% Incorporate the shard name into the record. |
29 | 32 | Index1 = Index0#index{dbname = DbName}, |
30 | 33 |
|
31 | | - %% get minimum seqs for search |
32 | | - {MinUpdateSeq, MinPurgeSeq} = nouveau_index_updater:get_db_info(Index1), |
| 34 | + Update = maps:get(update, QueryArgs0, true), |
33 | 35 |
|
34 | 36 | %% Incorporate min seqs into the query args. |
35 | | - QueryArgs1 = QueryArgs0#{ |
36 | | - min_update_seq => MinUpdateSeq, |
37 | | - min_purge_seq => MinPurgeSeq |
38 | | - }, |
39 | | - Update = maps:get(update, QueryArgs1, true), |
40 | | - |
41 | | - %% check if index is up to date |
42 | | - T0 = erlang:monotonic_time(), |
43 | | - case Update andalso nouveau_index_updater:outdated(Index1) of |
44 | | - true -> |
45 | | - case nouveau_index_manager:update_index(Index1) of |
46 | | - ok -> |
47 | | - ok; |
48 | | - {error, Reason} -> |
49 | | - rexi:reply({error, Reason}) |
50 | | - end; |
51 | | - false -> |
52 | | - ok; |
53 | | - {error, Reason} -> |
54 | | - rexi:reply({error, Reason}) |
55 | | - end, |
56 | | - T1 = erlang:monotonic_time(), |
57 | | - UpdateLatency = erlang:convert_time_unit(T1 - T0, native, millisecond), |
| 37 | + QueryArgs1 = |
| 38 | + case Update of |
| 39 | + true -> |
| 40 | + %% get minimum seqs for search |
| 41 | + {MinUpdateSeq, MinPurgeSeq} = nouveau_index_updater:get_db_info(Index1), |
| 42 | + QueryArgs0#{ |
| 43 | + min_update_seq => MinUpdateSeq, |
| 44 | + min_purge_seq => MinPurgeSeq |
| 45 | + }; |
| 46 | + false -> |
| 47 | + QueryArgs0#{ |
| 48 | + min_update_seq => 0, |
| 49 | + min_purge_seq => 0 |
| 50 | + } |
| 51 | + end, |
58 | 52 |
|
59 | 53 | %% Run the search |
60 | 54 | case nouveau_api:search(Index1, QueryArgs1) of |
61 | 55 | {ok, Response} -> |
62 | 56 | rexi:reply({ok, Response#{update_latency => UpdateLatency}}); |
63 | | - {error, stale_index} -> |
64 | | - %% try again. |
65 | | - search(DbName, Index0, QueryArgs0); |
| 57 | + {error, stale_index} when Update -> |
| 58 | + update_and_retry(DbName, Index0, QueryArgs0, UpdateLatency); |
| 59 | + {error, {not_found, _}} when Update -> |
| 60 | + update_and_retry(DbName, Index0, QueryArgs0, UpdateLatency); |
| 61 | + Else -> |
| 62 | + rexi:reply(Else) |
| 63 | + end. |
| 64 | + |
| 65 | +update_and_retry(DbName, Index, QueryArgs, UpdateLatency) -> |
| 66 | + T0 = erlang:monotonic_time(), |
| 67 | + case nouveau_index_manager:update_index(Index#index{dbname = DbName}) of |
| 68 | + ok -> |
| 69 | + T1 = erlang:monotonic_time(), |
| 70 | + search( |
| 71 | + DbName, |
| 72 | + Index, |
| 73 | + QueryArgs, |
| 74 | + UpdateLatency + |
| 75 | + erlang:convert_time_unit(T1 - T0, native, millisecond) |
| 76 | + ); |
66 | 77 | Else -> |
67 | 78 | rexi:reply(Else) |
68 | 79 | end. |
|
0 commit comments