Example #1
0
func ExportDatabaseJSON(db interfaces.IDatabase, convertNames bool) error {
	fmt.Printf("Exporting the database\n")
	if db == nil {
		return nil
	}
	buckets, err := db.ListAllBuckets()
	if err != nil {
		return err
	}
	answer := map[string]interface{}{}
	for _, bucket := range buckets {
		m := map[string]interface{}{}
		data, keys, err := db.GetAll(bucket, new(primitives.ByteSlice))
		if err != nil {
			return err
		}
		for i, key := range keys {
			m[fmt.Sprintf("%x", key)] = data[i]
		}
		if convertNames == true {
			answer[KeyToName(bucket)] = m
		} else {
			answer[fmt.Sprintf("%x", bucket)] = m
		}
	}

	data, err := primitives.EncodeJSON(answer)
	if err != nil {
		return err
	}

	var out bytes.Buffer
	json.Indent(&out, data, "", "\t")
	data = out.Next(out.Len())
	/*
		dir := be.DataStorePath
		if dir != "" {
			if FileNotExists(dir) {
				err := os.MkdirAll(dir, 0777)
				if err == nil {
					fmt.Println("Created directory " + dir)
				} else {
					return err
				}
			}
		}
		if dir != "" {
			dir = dir + "/db.txt"
		} else {
			dir = "db.txt"
		}*/
	dir := "db.txt"
	err = ioutil.WriteFile(dir, data, 0777)
	if err != nil {
		return err
	}
	return nil
}
func CleanupLevelDB(t *testing.T, b interfaces.IDatabase) {
	err := b.Close()
	if err != nil {
		t.Errorf("%v", err)
	}
	err = os.RemoveAll(dbFilename)
	if err != nil {
		t.Errorf("%v", err)
	}
}