写一个简单的key-value数据库,实现下面几个接口。
db:new() => Db.
db:destroy(Db) => ok.
db:write(Key, Element, Db) => NewDb.
db:delete(Key, Db) => NewDb.
db:read(Key, Db) =>{ok, Element} | {error, instance}.
db:match(Element, Db) => [Key1, ..., KeyN].
例子:
1> c(db).
{ok,db}
2> Db = db:new().
[]
3> Db1 = db:write(111,222, Db).
[{111,222}]
4> Db2 = db:write(333, 444, Db1).
[{333,444},{111,222}]
5> db:read(111, Db2).
{ok,222}
6> Db3 = db:write(555, 444, Db2).
[{555,444},{333,444},{111,222}]
7> db:read(ola, Db3).
{error,instance}
8> db:match(444, Db3).
[555,333]
9> Db4 = db:delete(333, Db3).
[{555,444},{111,222}]
10> db:match(444, Db4).
[555]
db:new() => Db.
db:destroy(Db) => ok.
db:write(Key, Element, Db) => NewDb.
db:delete(Key, Db) => NewDb.
db:read(Key, Db) =>{ok, Element} | {error, instance}.
db:match(Element, Db) => [Key1, ..., KeyN].
例子:
1> c(db).
{ok,db}
2> Db = db:new().
[]
3> Db1 = db:write(111,222, Db).
[{111,222}]
4> Db2 = db:write(333, 444, Db1).
[{333,444},{111,222}]
5> db:read(111, Db2).
{ok,222}
6> Db3 = db:write(555, 444, Db2).
[{555,444},{333,444},{111,222}]
7> db:read(ola, Db3).
{error,instance}
8> db:match(444, Db3).
[555,333]
9> Db4 = db:delete(333, Db3).
[{555,444},{111,222}]
10> db:match(444, Db4).
[555]
有疑问加站长微信联系(非本文作者)