1 // A Volume is an interface representing a Keep back-end storage unit:
2 // for example, a single mounted disk, a RAID array, an Amazon S3 volume,
15 type Volume interface {
16 Get(loc string) ([]byte, error)
17 Put(loc string, block []byte) error
18 Touch(loc string) error
19 Index(prefix string) string
20 Delete(loc string) error
21 Status() *VolumeStatus
25 // MockVolumes are Volumes used to test the Keep front end.
27 // If the Bad field is true, this volume should return an error
28 // on all writes and puts.
30 type MockVolume struct {
31 Store map[string][]byte
32 Timestamps map[string]time.Time
36 func CreateMockVolume() *MockVolume {
38 make(map[string][]byte),
39 make(map[string]time.Time),
44 func (v *MockVolume) Get(loc string) ([]byte, error) {
46 return nil, errors.New("Bad volume")
47 } else if block, ok := v.Store[loc]; ok {
50 return nil, os.ErrNotExist
53 func (v *MockVolume) Put(loc string, block []byte) error {
55 return errors.New("Bad volume")
61 func (v *MockVolume) Touch(loc string) error {
63 return errors.New("Bad volume")
65 v.Timestamps[loc] = time.Now()
69 func (v *MockVolume) Index(prefix string) string {
71 for loc, block := range v.Store {
72 if IsValidLocator(loc) && strings.HasPrefix(loc, prefix) {
73 result = result + fmt.Sprintf("%s+%d %d\n",
74 loc, len(block), 123456789)
80 func (v *MockVolume) Delete(loc string) error {
81 if _, ok := v.Store[loc]; ok {
88 func (v *MockVolume) Status() *VolumeStatus {
90 for _, block := range v.Store {
91 used = used + uint64(len(block))
93 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
96 func (v *MockVolume) String() string {
100 // A VolumeManager manages a collection of volumes.
102 // - Volumes is a slice of available Volumes.
103 // - Choose() returns a Volume suitable for writing to.
104 // - Quit() instructs the VolumeManager to shut down gracefully.
106 type VolumeManager interface {
112 type RRVolumeManager struct {
114 nextwrite chan Volume
118 func MakeRRVolumeManager(vols []Volume) *RRVolumeManager {
119 // Create a new VolumeManager struct with the specified volumes,
120 // and with new Nextwrite and Quit channels.
121 // The Quit channel is buffered with a capacity of 1 so that
122 // another routine may write to it without blocking.
123 vm := &RRVolumeManager{vols, make(chan Volume), make(chan int, 1)}
125 // This goroutine implements round-robin volume selection.
126 // It sends each available Volume in turn to the Nextwrite
127 // channel, until receiving a notification on the Quit channel
128 // that it should terminate.
135 case vm.nextwrite <- vm.volumes[i]:
136 i = (i + 1) % len(vm.volumes)
144 func (vm *RRVolumeManager) Volumes() []Volume {
148 func (vm *RRVolumeManager) Choose() Volume {
149 return <-vm.nextwrite
152 func (vm *RRVolumeManager) Quit() {