...

Source file src/github.com/peterbourgon/diskv/v3/examples/git-like-store/git-like-store.go

Documentation: github.com/peterbourgon/diskv/v3/examples/git-like-store

     1  package main
     2  
     3  /* This example uses a more advanced transform function that simulates a bit
     4   how Git stores objects:
     5  
     6  * places hash-like keys under the objects directory
     7  * any other key is placed in the base directory. If the key
     8  * contains slashes, these are converted to subdirectories
     9  
    10  */
    11  
    12  import (
    13  	"fmt"
    14  	"regexp"
    15  	"strings"
    16  
    17  	"github.com/peterbourgon/diskv/v3"
    18  )
    19  
    20  var hex40 = regexp.MustCompile("[0-9a-fA-F]{40}")
    21  
    22  func hexTransform(s string) *diskv.PathKey {
    23  	if hex40.MatchString(s) {
    24  		return &diskv.PathKey{Path: []string{"objects", s[0:2]},
    25  			FileName: s,
    26  		}
    27  	}
    28  
    29  	folders := strings.Split(s, "/")
    30  	lfolders := len(folders)
    31  	if lfolders > 1 {
    32  		return &diskv.PathKey{Path: folders[:lfolders-1],
    33  			FileName: folders[lfolders-1],
    34  		}
    35  	}
    36  
    37  	return &diskv.PathKey{Path: []string{},
    38  		FileName: s,
    39  	}
    40  }
    41  
    42  func hexInverseTransform(pathKey *diskv.PathKey) string {
    43  	if hex40.MatchString(pathKey.FileName) {
    44  		return pathKey.FileName
    45  	}
    46  
    47  	if len(pathKey.Path) == 0 {
    48  		return pathKey.FileName
    49  	}
    50  
    51  	return strings.Join(pathKey.Path, "/") + "/" + pathKey.FileName
    52  }
    53  
    54  func main() {
    55  	d := diskv.New(diskv.Options{
    56  		BasePath:          "my-data-dir",
    57  		AdvancedTransform: hexTransform,
    58  		InverseTransform:  hexInverseTransform,
    59  		CacheSizeMax:      1024 * 1024,
    60  	})
    61  
    62  	// Write some text to the key "alpha/beta/gamma".
    63  	key := "1bd88421b055327fcc8660c76c4894c4ea4c95d7"
    64  	d.WriteString(key, "¡Hola!") // will be stored in "<basedir>/objects/1b/1bd88421b055327fcc8660c76c4894c4ea4c95d7"
    65  
    66  	d.WriteString("refs/heads/master", "some text") // will be stored in "<basedir>/refs/heads/master"
    67  
    68  	fmt.Println("Enumerating All keys:")
    69  	c := d.Keys(nil)
    70  
    71  	for key := range c {
    72  		value := d.ReadString(key)
    73  		fmt.Printf("Key: %s, Value: %s\n", key, value)
    74  	}
    75  }
    76  

View as plain text