shard.go 3.05 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 13 14 15 16 17 18 19
var IPFS_DEF_SHARD = NextToLast(2)
var IPFS_DEF_SHARD_STR = IPFS_DEF_SHARD.String()

const PREFIX = "/repo/flatfs/shard/"

const SHARDING_FN = "SHARDING"
const README_FN = "README"

20
type ShardIdV1 struct {
21
	funName string
22 23
	param   int
	fun     ShardFunc
24 25
}

26
func (f *ShardIdV1) String() string {
27
	return fmt.Sprintf("%sv1/%s/%d", PREFIX, f.funName, f.param)
28 29 30 31
}

func (f *ShardIdV1) Func() ShardFunc {
	return f.fun
32 33
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
func Prefix(prefixLen int) *ShardIdV1 {
	padding := strings.Repeat("_", prefixLen)
	return &ShardIdV1{
		funName: "prefix",
		param:   prefixLen,
		fun: func(noslash string) string {
			return (noslash + padding)[:prefixLen]
		},
	}
}

func Suffix(suffixLen int) *ShardIdV1 {
	padding := strings.Repeat("_", suffixLen)
	return &ShardIdV1{
		funName: "suffix",
		param:   suffixLen,
		fun: func(noslash string) string {
			str := padding + noslash
			return str[len(str)-suffixLen:]
		},
	}
}

func NextToLast(suffixLen int) *ShardIdV1 {
	padding := strings.Repeat("_", suffixLen+1)
	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]
		},
	}
}

70
func ParseShardFunc(str string) (*ShardIdV1, error) {
71
	str = strings.TrimSpace(str)
72

73 74 75 76
	if len(str) == 0 {
		return nil, fmt.Errorf("empty shard identifier")
	}
	if str[0] == '/' {
Kevin Atkinson's avatar
Kevin Atkinson committed
77 78
		trimmed := strings.TrimPrefix(str, PREFIX)
		if str == trimmed { // nothing trimmed
79 80
			return nil, fmt.Errorf("invalid prefix in shard identifier: %s", str)
		}
Kevin Atkinson's avatar
Kevin Atkinson committed
81
		str = trimmed
82
	}
83

84
	parts := strings.Split(str, "/")
85
	if len(parts) != 3 {
86
		return nil, fmt.Errorf("invalid shard identifier: %s", str)
87
	}
88

89 90 91 92 93
	version := parts[0]
	if version != "v1" {
		return nil, fmt.Errorf("expected 'v1' for version string got: %s\n", version)
	}

94
	funName := parts[1]
95

96
	param, err := strconv.Atoi(parts[2])
97
	if err != nil {
98
		return nil, fmt.Errorf("invalid parameter: %v", err)
99
	}
100

101
	switch funName {
102
	case "prefix":
103
		return Prefix(param), nil
104
	case "suffix":
105
		return Suffix(param), nil
106
	case "next-to-last":
107
		return NextToLast(param), nil
108
	default:
109
		return nil, fmt.Errorf("expected 'prefix', 'suffix' or 'next-to-last' got: %s", funName)
110 111 112 113
	}

}

114
func ReadShardFunc(dir string) (*ShardIdV1, error) {
115
	buf, err := ioutil.ReadFile(filepath.Join(dir, SHARDING_FN))
116
	if os.IsNotExist(err) {
Kevin Atkinson's avatar
Kevin Atkinson committed
117
		return nil, ErrShardingFileMissing
118
	} else if err != nil {
119
		return nil, err
120
	}
121
	return ParseShardFunc(string(buf))
122 123
}

124
func WriteShardFunc(dir string, id *ShardIdV1) error {
125
	file, err := os.Create(filepath.Join(dir, SHARDING_FN))
126 127 128 129
	if err != nil {
		return err
	}
	defer file.Close()
130
	_, err = file.WriteString(id.String())
131 132 133 134
	if err != nil {
		return err
	}
	_, err = file.WriteString("\n")
135 136 137 138
	return err
}

func WriteReadme(dir string, id *ShardIdV1) error {
139
	if id.String() == IPFS_DEF_SHARD.String() {
140
		err := ioutil.WriteFile(filepath.Join(dir, README_FN), []byte(README_IPFS_DEF_SHARD), 0444)
141 142 143 144
		if err != nil {
			return err
		}
	}
145 146
	return nil
}