shard.go 2.8 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
	parts := strings.Split(str, "/")
42 43 44 45 46 47
	if len(parts) == 3 {
		version := parts[0]
		if version != "v1" {
			return nil, fmt.Errorf("expected 'v1' for version string got: %s\n", version)
		}
		parts = parts[1:]
48
	}
49 50
	if len(parts) != 2 {
		return nil, fmt.Errorf("invalid shard identifier: %s", str)
51
	}
52 53

	id := &ShardIdV1{funName: parts[0]}
54 55

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

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

72
	return id, nil
73 74
}

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

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

func WriteReadme(dir string, id *ShardIdV1) error {
	if id.String() == IPFS_DEF_SHARD {
101 102 103 104 105
		err := ioutil.WriteFile(filepath.Join(dir, "_README"), []byte(README_IPFS_DEF_SHARD), 0444)
		if err != nil {
			return err
		}
	}
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 131
	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]
	}
}