zhenxing1114

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

写一个简单的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]

  1 package main
  2 
  3 import (
  4     "fmt"
  5 )
  6 
  7 type M_Rcd map[string]string
  8 
  9 type T_Tbl struct {
 10     tblname string
 11     record  M_Rcd
 12 }
 13 
 14 type T_Kvdb struct {
 15     dbname string
 16     tbl    []T_Tbl
 17 }
 18 
 19 func (this *T_Kvdb) c(db T_Kvdb) {
 20     this.dbname = db.dbname
 21     this.tbl = make([]T_Tbl, 5)
 22     fmt.Printf("\ncreate db \"%s\" ok\n", this.dbname)
 23 }
 24 
 25 func (this *T_Kvdb) new(tblname string) T_Tbl {
 26     var tbl T_Tbl
 27     var length int
 28     tbl.tblname = tblname
 29     tbl.record = make(M_Rcd)
 30 
 31     this.tbl = append(this.tbl, tbl)
 32     length = len(this.tbl)
 33     fmt.Printf("add new table \"%s\" to db \"%s\" ok\n", tblname, this.dbname)
 34     return this.tbl[length-1]
 35 }
 36 
 37 func (this *T_Kvdb) destroy(tbl T_Tbl) {
 38     for i, _ := range this.tbl {
 39         if tbl.tblname == this.tbl[i].tblname {
 40             this.tbl = append(this.tbl[:i], this.tbl[i+1:]...)
 41             fmt.Println("destroy table ", tbl.tblname, " ok.\n")
 42             return
 43         }
 44     }
 45     fmt.Printf("no table \"%s\".\n", tbl.tblname)
 46 }
 47 
 48 func (this *T_Kvdb) read(key string, tbl T_Tbl) {
 49     for i, _ := range this.tbl {
 50         if tbl.tblname == this.tbl[i].tblname {
 51             for k, v := range this.tbl[i].record {
 52                 if k == key {
 53                     fmt.Println("{", v, "}")
 54                     return
 55                 }
 56             }
 57         }
 58     }
 59     fmt.Println("error, instance")
 60 }
 61 
 62 func (this *T_Kvdb) write(key string, value string, tbl T_Tbl) {
 63     fmt.Printf("add {%s,%s} to table \"%s\"", key, value, tbl.tblname)
 64     for i, _ := range this.tbl {
 65         if tbl.tblname == this.tbl[i].tblname {
 66             this.tbl[i].record[key] = value
 67             for k, v1 := range this.tbl[i].record {
 68                 fmt.Print("{", k, ",", v1, "}")
 69             }
 70             fmt.Printf("\n")
 71             return
 72         }
 73     }
 74     fmt.Print("no table \n", tbl.tblname)
 75 }
 76 
 77 func (this *T_Kvdb) delete(key string, tbl T_Tbl) {
 78     fmt.Printf("delete %s from table %s\n", key, tbl.tblname)
 79     fmt.Printf("[")
 80     for i, _ := range this.tbl {
 81         if tbl.tblname == this.tbl[i].tblname {
 82             delete(this.tbl[i].record, key)
 83             for k, v1 := range this.tbl[i].record {
 84                 fmt.Print("{", k, ",", v1, "}")
 85             }
 86             fmt.Printf("]\n")
 87             return
 88         }
 89     }
 90 }
 91 
 92 func (this *T_Kvdb) match(value string, tbl T_Tbl) {
 93     var value_in []string
 94 
 95     for i, _ := range this.tbl {
 96         if tbl.tblname == this.tbl[i].tblname {
 97             for k, v1 := range this.tbl[i].record {
 98                 if v1 == value {
 99                     value_in = append(value_in, k)
100                 }
101             }
102             fmt.Printf("%v\n", value_in)
103             return
104         }
105     }
106     value_in = append(value_in, "]")
107     fmt.Printf("%v\n", value_in)
108 }
109 
110 func (this *T_Kvdb) show() {
111     fmt.Print("show [")
112     for i, _ := range this.tbl {
113         for k, v1 := range this.tbl[i].record {
114             fmt.Print("{", k, ",", v1, "}")
115         }
116     }
117     fmt.Print("]\n\n")
118 }
dblib.go
 1 package main
 2 
 3 import ()
 4 
 5 func main() {
 6     var db1 T_Kvdb
 7     var tbl1 T_Tbl
 8     db1.dbname = "db1"
 9     db1.c(db1)
10     tbl1 = db1.new("tbl1")
11 
12     db1.write("111", "222", tbl1)
13     db1.show()
14     db1.read("111", tbl1)
15 
16     db1.write("333", "444", tbl1)
17     db1.show()
18     db1.read("333", tbl1)
19 
20     db1.read("555", tbl1)
21 
22     db1.write("555", "444", tbl1)
23     db1.show()
24 
25     db1.match("444", tbl1)
26 
27     db1.delete("333", tbl1)
28     db1.show()
29 
30     db1.destroy(tbl1)
31     db1.show()
32     tbl1.tblname = "a"
33     db1.destroy(tbl1)
34     db1.show()
35     db1.read("555", tbl1)
36 
37     db1.show()
38 }

 

posted on 2014-11-20 09:44  zhenxing1114  阅读(397)  评论(0编辑  收藏  举报