shard.go 2.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package flatfs

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

12
type ShardIdV1 struct {
13
	funName string
14 15
	param   int
	fun     ShardFunc
16 17
}

18
const PREFIX = "/repo/flatfs/shard/"
19 20

func (f *ShardIdV1) String() string {
21
	return fmt.Sprintf("%sv1/%s/%d", PREFIX, f.funName, f.param)
22 23 24 25
}

func (f *ShardIdV1) Func() ShardFunc {
	return f.fun
26 27
}

28
func ParseShardFunc(str string) (*ShardIdV1, error) {
29 30
	str = strings.TrimSpace(str)
	// ignore prefix for now
31 32 33 34
	if len(str) == 0 {
		return nil, fmt.Errorf("empty shard identifier")
	}
	if str[0] == '/' {
Kevin Atkinson's avatar
Kevin Atkinson committed
35 36
		trimmed := strings.TrimPrefix(str, PREFIX)
		if str == trimmed { // nothing trimmed
37 38
			return nil, fmt.Errorf("invalid prefix in shard identifier: %s", str)
		}
Kevin Atkinson's avatar
Kevin Atkinson committed
39
		str = trimmed
40
	}
41

42
	parts := strings.Split(str, "/")
43
	if len(parts) != 3 {
44
		return nil, fmt.Errorf("invalid shard identifier: %s", str)
45
	}
46

47 48 49 50 51 52
	version := parts[0]
	if version != "v1" {
		return nil, fmt.Errorf("expected 'v1' for version string got: %s\n", version)
	}

	id := &ShardIdV1{funName: parts[1]}
53

54
	param, err := strconv.Atoi(parts[2])
55
	if err != nil {
56
		return nil, fmt.Errorf("invalid parameter: %v", err)
57
	}
58 59 60
	id.param = param

	switch id.funName {
61
	case "prefix":
62
		id.fun = Prefix(param)
63
	case "suffix":
64
		id.fun = Suffix(param)
65
	case "next-to-last":
66
		id.fun = NextToLast(param)
67
	default:
68
		return nil, fmt.Errorf("expected 'prefix', 'suffix' or 'next-to-last' got: %s", id.funName)
69 70
	}

71
	return id, nil
72 73
}

74
func ReadShardFunc(dir string) (*ShardIdV1, error) {
75
	buf, err := ioutil.ReadFile(filepath.Join(dir, "SHARDING"))
76
	if os.IsNotExist(err) {
Kevin Atkinson's avatar
Kevin Atkinson committed
77
		return nil, ErrShardingFileMissing
78
	} else if err != nil {
79
		return nil, err
80
	}
81
	return ParseShardFunc(string(buf))
82 83
}

84
func WriteShardFunc(dir string, id *ShardIdV1) error {
85 86 87 88 89
	file, err := os.Create(filepath.Join(dir, "SHARDING"))
	if err != nil {
		return err
	}
	defer file.Close()
90
	_, err = file.WriteString(id.String())
91 92 93 94
	if err != nil {
		return err
	}
	_, err = file.WriteString("\n")
95 96 97 98 99
	return err
}

func WriteReadme(dir string, id *ShardIdV1) error {
	if id.String() == IPFS_DEF_SHARD {
100 101 102 103 104
		err := ioutil.WriteFile(filepath.Join(dir, "_README"), []byte(README_IPFS_DEF_SHARD), 0444)
		if err != nil {
			return err
		}
	}
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	return nil
}

func Prefix(prefixLen int) ShardFunc {
	padding := strings.Repeat("_", prefixLen)
	return func(noslash string) string {
		return (noslash + padding)[:prefixLen]
	}
}

func Suffix(suffixLen int) ShardFunc {
	padding := strings.Repeat("_", suffixLen)
	return func(noslash string) string {
		str := padding + noslash
		return str[len(str)-suffixLen:]
	}
}

func NextToLast(suffixLen int) ShardFunc {
	padding := strings.Repeat("_", suffixLen+1)
	return func(noslash string) string {
		str := padding + noslash
		offset := len(str) - suffixLen - 1
		return str[offset : offset+suffixLen]
	}
}