shard.go 2.99 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
const SHARDING_FN = "SHARDING"
const README_FN = "README"
21 22

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

func (f *ShardIdV1) Func() ShardFunc {
	return f.fun
28 29
}

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

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

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

54
	funName := parts[1]
55

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

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

}

74
func ReadShardFunc(dir string) (*ShardIdV1, error) {
75
	buf, err := ioutil.ReadFile(filepath.Join(dir, SHARDING_FN))
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
	file, err := os.Create(filepath.Join(dir, SHARDING_FN))
86 87 88 89
	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
	return err
}

func WriteReadme(dir string, id *ShardIdV1) error {
99
	if id.String() == IPFS_DEF_SHARD.String() {
100
		err := ioutil.WriteFile(filepath.Join(dir, README_FN), []byte(README_IPFS_DEF_SHARD), 0444)
101 102 103 104
		if err != nil {
			return err
		}
	}
105 106 107
	return nil
}

108
func Prefix(prefixLen int) *ShardIdV1 {
109
	padding := strings.Repeat("_", prefixLen)
110 111 112 113 114 115
	return &ShardIdV1{
		funName: "prefix",
		param:   prefixLen,
		fun: func(noslash string) string {
			return (noslash + padding)[:prefixLen]
		},
116 117 118
	}
}

119
func Suffix(suffixLen int) *ShardIdV1 {
120
	padding := strings.Repeat("_", suffixLen)
121 122 123 124 125 126 127
	return &ShardIdV1{
		funName: "suffix",
		param:   suffixLen,
		fun: func(noslash string) string {
			str := padding + noslash
			return str[len(str)-suffixLen:]
		},
128 129 130
	}
}

131
func NextToLast(suffixLen int) *ShardIdV1 {
132
	padding := strings.Repeat("_", suffixLen+1)
133 134 135 136 137 138 139 140
	return &ShardIdV1{
		funName: "next-to-last",
		param:   suffixLen,
		fun: func(noslash string) string {
			str := padding + noslash
			offset := len(str) - suffixLen - 1
			return str[offset : offset+suffixLen]
		},
141 142
	}
}