Browse Source
add TTL support
add TTL support
The volume TTL and file TTL are not necessarily the same. as long as file TTL is smaller than volume TTL, it'll be fine. volume TTL is used when assigning file id, e.g. http://.../dir/assign?ttl=3h file TTL is used when uploadingpull/15/head
Chris Lu
11 years ago
31 changed files with 416 additions and 105 deletions
-
5go/operation/assign_file_id.go
-
11go/operation/submit.go
-
12go/operation/system_message.pb.go
-
1go/proto/system_message.proto
-
9go/storage/needle.go
-
21go/storage/needle_read_write.go
-
8go/storage/replica_placement.go
-
47go/storage/store.go
-
72go/storage/volume.go
-
2go/storage/volume_info.go
-
10go/storage/volume_super_block.go
-
5go/storage/volume_super_block_test.go
-
147go/storage/volume_ttl.go
-
14go/storage/volume_vacuum.go
-
7go/topology/allocate_volume.go
-
23go/topology/collection.go
-
6go/topology/data_node.go
-
21go/topology/topology.go
-
6go/topology/topology_event_handling.go
-
2go/topology/topology_map.go
-
2go/topology/topology_vacuum.go
-
12go/topology/volume_growth.go
-
13go/topology/volume_layout.go
-
2go/weed/benchmark.go
-
2go/weed/compact.go
-
6go/weed/upload.go
-
4go/weed/weed_server/common.go
-
12go/weed/weed_server/filer_server_handlers.go
-
9go/weed/weed_server/master_server_handlers_admin.go
-
2go/weed/weed_server/volume_server_handlers_admin.go
@ -0,0 +1,147 @@ |
|||
package storage |
|||
|
|||
import ( |
|||
"strconv" |
|||
) |
|||
|
|||
var ( |
|||
TtlRange []uint16 |
|||
) |
|||
|
|||
const ( |
|||
//stored unit types
|
|||
Empty byte = iota |
|||
Minute |
|||
Hour |
|||
Day |
|||
Week |
|||
Month |
|||
Year |
|||
) |
|||
|
|||
func init() { |
|||
TtlRange = []uint16{3, 10, 30, |
|||
60 /*1 hour*/, 60 * 3, 60 * 6, 60 * 12, |
|||
1440 /*1 day*/, 1440 * 3, 1440 * 7, 1440 * 15, 1440 * 31, |
|||
44888 /*1 month*/, 65535, |
|||
} |
|||
} |
|||
|
|||
type TTL struct { |
|||
count byte |
|||
unit byte |
|||
} |
|||
|
|||
var EMPTY_TTL = &TTL{} |
|||
|
|||
// translate a readable ttl to internal ttl
|
|||
// Supports format example:
|
|||
// 3m: 3 minutes
|
|||
// 4h: 4 hours
|
|||
// 5d: 5 days
|
|||
// 6w: 6 weeks
|
|||
// 7M: 7 months
|
|||
// 8y: 8 years
|
|||
func ReadTTL(ttlString string) (*TTL, error) { |
|||
if ttlString == "" { |
|||
return EMPTY_TTL, nil |
|||
} |
|||
ttlBytes := []byte(ttlString) |
|||
unitByte := ttlBytes[len(ttlBytes)-1] |
|||
countBytes := ttlBytes[0 : len(ttlBytes)-1] |
|||
if '0' <= unitByte && unitByte <= '9' { |
|||
countBytes = ttlBytes |
|||
unitByte = 'm' |
|||
} |
|||
count, err := strconv.Atoi(string(countBytes)) |
|||
unit := toStoredByte(unitByte) |
|||
return &TTL{count: byte(count), unit: unit}, err |
|||
} |
|||
|
|||
// read stored bytes to a ttl
|
|||
func LoadTTLFromBytes(input []byte) (t *TTL) { |
|||
return &TTL{count: input[0], unit: input[1]} |
|||
} |
|||
|
|||
// read stored bytes to a ttl
|
|||
func LoadTTLFromUint32(ttl uint32) (t *TTL) { |
|||
input := make([]byte, 2) |
|||
input[1] = byte(ttl) |
|||
input[0] = byte(ttl >> 8) |
|||
return LoadTTLFromBytes(input) |
|||
} |
|||
|
|||
// save stored bytes to an output with 2 bytes
|
|||
func (t TTL) ToBytes(output []byte) { |
|||
output[0] = t.count |
|||
output[1] = t.unit |
|||
} |
|||
|
|||
func (t TTL) ToUint32() (output uint32) { |
|||
output = uint32(t.count) << 8 |
|||
output += uint32(t.unit) |
|||
return output |
|||
} |
|||
|
|||
func (t TTL) String() string { |
|||
if t.count == 0 { |
|||
return "" |
|||
} |
|||
if t.unit == Empty { |
|||
return "" |
|||
} |
|||
countString := strconv.Itoa(int(t.count)) |
|||
switch t.unit { |
|||
case Minute: |
|||
return countString + "m" |
|||
case Hour: |
|||
return countString + "h" |
|||
case Day: |
|||
return countString + "d" |
|||
case Week: |
|||
return countString + "w" |
|||
case Month: |
|||
return countString + "M" |
|||
case Year: |
|||
return countString + "y" |
|||
} |
|||
return "" |
|||
} |
|||
|
|||
func toStoredByte(readableUnitByte byte) byte { |
|||
switch readableUnitByte { |
|||
case 'm': |
|||
return Minute |
|||
case 'h': |
|||
return Hour |
|||
case 'd': |
|||
return Day |
|||
case 'w': |
|||
return Week |
|||
case 'M': |
|||
return Month |
|||
case 'Y': |
|||
return Year |
|||
} |
|||
return 0 |
|||
} |
|||
|
|||
func (t TTL) Minutes() uint32 { |
|||
switch t.unit { |
|||
case Empty: |
|||
return 0 |
|||
case Minute: |
|||
return uint32(t.count) |
|||
case Hour: |
|||
return uint32(t.count) * 60 |
|||
case Day: |
|||
return uint32(t.count) * 60 * 24 |
|||
case Week: |
|||
return uint32(t.count) * 60 * 24 * 7 |
|||
case Month: |
|||
return uint32(t.count) * 60 * 24 * 31 |
|||
case Year: |
|||
return uint32(t.count) * 60 * 24 * 31 * 365 |
|||
} |
|||
return 0 |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue