Ejemplo n.º 1
0
// 返回数据库中得所有key/value值
func getAll(db *kv.DB) map[string]string {
	m := map[string]string{}
	enum, err := db.SeekFirst()
	if err == io.EOF {
		fmt.Println("Empty Database")
		return m
	}
	for {
		k, v, err := enum.Next()
		if err == io.EOF {
			fmt.Printf("End of Database \n")
			return m
		}
		m[string(k)] = string(v)
		// fmt.Printf("%s = %s \n", string(k), string(v))
	}
	return m
}
Ejemplo n.º 2
0
func dumpBucket(w io.Writer, db *kv.DB) (err error) {
	//log.Printf("dumping %s", db)
	io.WriteString(w, `{"name": "`+db.Name()+`", "records": [`)
	enc := json.NewEncoder(w)
	enum, e := db.SeekFirst()
	if e != nil {
		if e != io.EOF {
			err = e
			log.Printf("error getting first: %s", err)
		}
		return
	}
	vi := new(weedutils.ValInfo)
	for {
		k, v, err := enum.Next()
		if err != nil {
			if err != io.EOF {
				log.Printf("error getting next: %s", err)
			}
			break
		}
		if err = vi.Decode(v); err != nil {
			log.Printf("error decoding %s: %s", v, err)
			continue
		}
		fmt.Fprintf(w, `{"object": %q, "value": `, k)
		if err = enc.Encode(vi); err != nil {
			log.Printf("error printing %v to json: %s", vi, err)
			continue
		}
		io.WriteString(w, "},\n")

	}
	io.WriteString(w, "]},\n")
	io.WriteString(w, "]},\n")
	return nil
}