1
%% ``The contents of this file are subject to the Erlang Public License,
2
%% Version 1.1, (the "License"); you may not use this file except in
3
%% compliance with the License. You should have received a copy of the
4
%% Erlang Public License along with this software. If not, it can be
5
%% retrieved via the world wide web at http://www.erlang.org/.
7
%% Software distributed under the License is distributed on an "AS IS"
8
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
%% the License for the specific language governing rights and limitations
12
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
%% AB. All Rights Reserved.''
16
%% $Id: asn1_db.erl,v 1.1 2008/12/17 09:53:29 mikpe Exp $
19
%-compile(export_all).
20
-export([dbnew/1,dbsave/2,dbload/1,dbput/3,dbget/2,dbget_all/1]).
21
-export([dbget_all_mod/1,dbstop/0,dbclear/0,dberase_module/1,dbstart/1,stop_server/1]).
23
-export([dbloop0/1,dbloop/2]).
27
start_server(asn1db, asn1_db, dbloop0, [Includes]).
30
dbloop(Includes, ets:new(asn1, [set,named_table])).
32
opentab(Tab,Mod,[]) ->
33
opentab(Tab,Mod,["."]);
34
opentab(Tab,Mod,Includes) ->
35
Base = lists:concat([Mod,".asn1db"]),
36
opentab2(Tab,Base,Mod,Includes,ok).
38
opentab2(_Tab,_Base,_Mod,[],Error) ->
40
opentab2(Tab,Base,Mod,[Ih|It],_Error) ->
41
File = filename:join(Ih,Base),
42
case ets:file2tab(File) of
44
ets:insert(Tab,{Mod, Modtab}),
47
opentab2(Tab,Base,Mod,It,NewErr)
51
dbloop(Includes, Tab) ->
53
{From,{set, Mod, K2, V}} ->
54
[{_,Modtab}] = ets:lookup(Tab,Mod),
55
ets:insert(Modtab,{K2, V}),
57
dbloop(Includes, Tab);
58
{From, {get, Mod, K2}} ->
59
Result = case ets:lookup(Tab,Mod) of
61
opentab(Tab,Mod,Includes);
62
[{_,Modtab}] -> {ok,Modtab}
66
From ! {asn1db, lookup(Newtab, K2)};
68
From ! {asn1db, undefined}
70
dbloop(Includes, Tab);
71
{From, {all_mod, Mod}} ->
72
[{_,Modtab}] = ets:lookup(Tab,Mod),
73
From ! {asn1db, ets:tab2list(Modtab)},
74
dbloop(Includes, Tab);
75
{From, {delete_mod, Mod}} ->
76
[{_,Modtab}] = ets:lookup(Tab,Mod),
80
dbloop(Includes, Tab);
81
{From, {save, OutFile,Mod}} ->
82
[{_,Mtab}] = ets:lookup(Tab,Mod),
83
{From ! {asn1db, ets:tab2file(Mtab,OutFile)}},
85
{From, {load, Mod}} ->
86
Result = case ets:lookup(Tab,Mod) of
88
opentab(Tab,Mod,Includes);
89
[{_,Modtab}] -> {ok,Modtab}
91
{From, {asn1db,Result}},
94
case ets:lookup(Tab,Mod) of
100
Tabname = list_to_atom(lists:concat(["asn1_",Mod])),
101
ets:new(Tabname, [set,named_table]),
102
ets:insert(Tab,{Mod,Tabname}),
104
dbloop(Includes,Tab);
106
From ! {asn1db, ok}; %% nothing to store
108
ModTabList = [Mt||{_,Mt} <- ets:tab2list(Tab)],
109
lists:foreach(fun(T) -> ets:delete(T) end,ModTabList),
111
From ! {asn1db, cleared},
112
dbloop(Includes, ets:new(asn1, [set]))
117
%% pickup(K, ets:match(Tab, {{K, '$1'}, '$2'})).
118
%%pickup(K, []) -> [];
119
%%pickup(K, [[V1,V2] |T]) ->
120
%% [{{K,V1},V2} | pickup(K, T)].
123
case ets:lookup(Tab, K) of
129
dbnew(Module) -> req({new,Module}).
130
dbsave(OutFile,Module) -> req({save,OutFile,Module}).
131
dbload(Module) -> req({load,Module}).
133
dbput(Module,K,V) -> req({set, Module, K, V}).
134
dbget(Module,K) -> req({get, Module, K}).
135
dbget_all(K) -> req({get_all, K}).
136
dbget_all_mod(Mod) -> req({all_mod,Mod}).
137
dbstop() -> stop_server(asn1db).
138
dbclear() -> req(clear).
139
dberase_module({module,M})->
140
req({delete_mod, M}).
143
asn1db ! {self(), R},
144
receive {asn1db, Reply} -> Reply end.
147
stop_server(Name, whereis(Name)).
148
stop_server(_, undefined) -> stopped;
149
stop_server(Name, _Pid) ->
150
Name ! {self(), stop},
151
receive {Name, _} -> stopped end.
154
start_server(Name,Mod,Fun,Args) ->
155
case whereis(Name) of
157
register(Name, spawn(Mod,Fun, Args));