コード例 #1
0
ファイル: cas.go プロジェクト: x2q/diskv
func main() {
	s := diskv.NewStore("data-dir", BlockTransform, 1024*1024)

	data := []string{
		"I am the very model of a modern Major-General",
		"I've information vegetable, animal, and mineral",
		"I know the kings of England, and I quote the fights historical",
		"From Marathon to Waterloo, in order categorical",
		"I'm very well acquainted, too, with matters mathematical",
		"I understand equations, both the simple and quadratical",
		"About binomial theorem I'm teeming with a lot o' news",
		"With many cheerful facts about the square of the hypotenuse",
	}
	for _, valueStr := range data {
		key, value := md5sum(valueStr), bytes.NewBufferString(valueStr).Bytes()
		s.Write(key, value)
	}

	keyChan, keyCount := s.Keys(), 0
	for key, ok := <-keyChan; ok; key, ok = <-keyChan {
		value, err := s.Read(key)
		if err != nil {
			panic(fmt.Sprintf("key %s had no value", key))
		}
		fmt.Printf("%s: %s\n", key, value)
		keyCount++
	}
	fmt.Printf("%d total keys\n", keyCount)

	// s.Flush() // leave it commented out to see how data is kept on disk
}
コード例 #2
0
ファイル: super-simple-store.go プロジェクト: x2q/diskv
func main() {
	flatTransform := func(s string) []string { return []string{""} }
	s := diskv.NewStore("my-store-dir", flatTransform, 1024*1024)

	key := "alpha"
	writeErr := s.Write(key, []byte{'1', '2', '3'})
	if writeErr != nil {
		panic(fmt.Sprintf("%s", writeErr))
	}

	value, readErr := s.Read(key)
	if readErr != nil {
		panic(fmt.Sprintf("%s", readErr))
	}
	fmt.Printf("%v\n", value)

	eraseErr := s.Erase(key)
	if eraseErr != nil {
		panic(fmt.Sprintf("%s", eraseErr))
	}
}
コード例 #3
0
ファイル: diskv_bench.go プロジェクト: rif/gocmd
func main() {
	// Simple transform function to put all of the data files into the root directory.
	flatTransform := func(s string) []string { return []string{""} }

	// Initialize a new diskv store, rooted at "my-store-dir", with a 1MB cache.
	s := diskv.NewStore("my-store-dir", flatTransform, 1024*1024)

	start := time.Now()

	for i := 0; i < 100; i++ {
		s.Write(fmt.Sprintf("a%v", i), []byte(strconv.Itoa(i)))
	}
	for i := 0; i < 100; i++ {
		s.Read(fmt.Sprintf("a%v", i))
	}
	for i := 0; i < 100; i++ {
		s.Erase(fmt.Sprintf("a%v", i))
	}
	duration := time.Since(start)
	log.Printf("Elapsed: %v.", duration)
}