|
9 | 9 | protected |
10 | 10 | ]). |
11 | 11 |
|
12 | | --record(state, {table}). |
| 12 | +-record(state, {table, options}). |
13 | 13 |
|
14 | 14 | %%============================================================================= |
15 | 15 | %% API Function Exports |
16 | 16 | %%============================================================================= |
17 | 17 | -export([start_link/0, |
| 18 | + start_link/1, |
18 | 19 | ops_info/0, |
19 | 20 | ops_list/0, |
20 | 21 | sync_set/2, |
|
43 | 44 | start_link() -> |
44 | 45 | gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). |
45 | 46 |
|
| 47 | +-spec start_link(list({atom(), any()})) |
| 48 | + -> 'ignore' | {'error', term()} | {'ok', pid()}. |
| 49 | +start_link(CustomOptions) -> |
| 50 | + gen_server:start_link({local, ?SERVER}, ?MODULE, [CustomOptions], []). |
| 51 | + |
46 | 52 | -spec ops_info() -> list(). |
47 | 53 | ops_info() -> |
48 | 54 | gen_server:call(?MODULE, ops_info). |
@@ -107,7 +113,12 @@ sync_flush() -> |
107 | 113 | %% gen_server Function Definitions |
108 | 114 | %%============================================================================= |
109 | 115 | init([]) -> |
110 | | - {ok, #state{table = ets:new(?SERVER, ?ETS_OPTIONS)}}. |
| 116 | + {ok, #state{table = ets:new(?SERVER, ?ETS_OPTIONS), |
| 117 | + options = ?ETS_OPTIONS}}; |
| 118 | +init(CustomOptions) -> |
| 119 | + Options = merge_options(?ETS_OPTIONS, CustomOptions), |
| 120 | + {ok, #state{table = ets:new(?SERVER, Options), |
| 121 | + options = Options}}. |
111 | 122 |
|
112 | 123 | handle_call(ops_info, _From, #state{table = Table} = State) -> |
113 | 124 | {reply, ets:info(Table), State}; |
@@ -172,3 +183,8 @@ get_by_key(Table, Key) -> |
172 | 183 | [] -> |
173 | 184 | {error, not_found} |
174 | 185 | end. |
| 186 | + |
| 187 | +merge_options(ExistingOptions, NewOptions) -> |
| 188 | + orddict:merge(fun (_, X, Y) -> X + Y end, |
| 189 | + orddict:from_list(ExistingOptions), |
| 190 | + orddict:from_list(NewOptions)). |
0 commit comments