Fix volume ttl (#6683)
This commit is contained in:
@@ -44,7 +44,48 @@ func ReadTTL(ttlString string) (*TTL, error) {
|
|||||||
}
|
}
|
||||||
count, err := strconv.Atoi(string(countBytes))
|
count, err := strconv.Atoi(string(countBytes))
|
||||||
unit := toStoredByte(unitByte)
|
unit := toStoredByte(unitByte)
|
||||||
return &TTL{Count: byte(count), Unit: unit}, err
|
return fitTtlCount(count, unit), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func fitTtlCount(count int, unit byte) *TTL {
|
||||||
|
seconds := ToSeconds(count, unit)
|
||||||
|
if seconds == 0 {
|
||||||
|
return EMPTY_TTL
|
||||||
|
}
|
||||||
|
if seconds%(3600*24*365) == 0 && seconds/(3600*24*365) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600 * 24 * 365)), Unit: Year}
|
||||||
|
}
|
||||||
|
if seconds%(3600*24*30) == 0 && seconds/(3600*24*30) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600 * 24 * 30)), Unit: Month}
|
||||||
|
}
|
||||||
|
if seconds%(3600*24*7) == 0 && seconds/(3600*24*7) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600 * 24 * 7)), Unit: Week}
|
||||||
|
}
|
||||||
|
if seconds%(3600*24) == 0 && seconds/(3600*24) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600 * 24)), Unit: Day}
|
||||||
|
}
|
||||||
|
if seconds%(3600) == 0 && seconds/(3600) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600)), Unit: Hour}
|
||||||
|
}
|
||||||
|
if seconds/60 < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / 60), Unit: Minute}
|
||||||
|
}
|
||||||
|
if seconds/(3600) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600)), Unit: Hour}
|
||||||
|
}
|
||||||
|
if seconds/(3600*24) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600 * 24)), Unit: Day}
|
||||||
|
}
|
||||||
|
if seconds/(3600*24*7) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600 * 24 * 7)), Unit: Week}
|
||||||
|
}
|
||||||
|
if seconds/(3600*24*30) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600 * 24 * 30)), Unit: Month}
|
||||||
|
}
|
||||||
|
if seconds/(3600*24*365) < 256 {
|
||||||
|
return &TTL{Count: byte(seconds / (3600 * 24 * 365)), Unit: Year}
|
||||||
|
}
|
||||||
|
return EMPTY_TTL
|
||||||
}
|
}
|
||||||
|
|
||||||
// read stored bytes to a ttl
|
// read stored bytes to a ttl
|
||||||
@@ -104,21 +145,25 @@ func (t *TTL) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TTL) ToSeconds() uint64 {
|
func (t *TTL) ToSeconds() uint64 {
|
||||||
switch t.Unit {
|
return ToSeconds(int(t.Count), t.Unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToSeconds(count int, unit byte) uint64 {
|
||||||
|
switch unit {
|
||||||
case Empty:
|
case Empty:
|
||||||
return 0
|
return 0
|
||||||
case Minute:
|
case Minute:
|
||||||
return uint64(t.Count) * 60
|
return uint64(count) * 60
|
||||||
case Hour:
|
case Hour:
|
||||||
return uint64(t.Count) * 60 * 60
|
return uint64(count) * 60 * 60
|
||||||
case Day:
|
case Day:
|
||||||
return uint64(t.Count) * 60 * 24 * 60
|
return uint64(count) * 60 * 24 * 60
|
||||||
case Week:
|
case Week:
|
||||||
return uint64(t.Count) * 60 * 24 * 7 * 60
|
return uint64(count) * 60 * 24 * 7 * 60
|
||||||
case Month:
|
case Month:
|
||||||
return uint64(t.Count) * 60 * 24 * 30 * 60
|
return uint64(count) * 60 * 24 * 30 * 60
|
||||||
case Year:
|
case Year:
|
||||||
return uint64(t.Count) * 60 * 24 * 365 * 60
|
return uint64(count) * 60 * 24 * 365 * 60
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,16 @@ func TestTTLReadWrite(t *testing.T) {
|
|||||||
t.Errorf("50d ttl:%v", ttl)
|
t.Errorf("50d ttl:%v", ttl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ttl, _ = ReadTTL("365d")
|
||||||
|
if ttl.Minutes() != 365*24*60 {
|
||||||
|
t.Errorf("365d ttl:%v", ttl)
|
||||||
|
}
|
||||||
|
|
||||||
|
ttl, _ = ReadTTL("730d")
|
||||||
|
if ttl.Minutes() != 730*24*60 {
|
||||||
|
t.Errorf("730d ttl:%v", ttl)
|
||||||
|
}
|
||||||
|
|
||||||
ttl, _ = ReadTTL("5w")
|
ttl, _ = ReadTTL("5w")
|
||||||
if ttl.Minutes() != 5*7*24*60 {
|
if ttl.Minutes() != 5*7*24*60 {
|
||||||
t.Errorf("5w ttl:%v", ttl)
|
t.Errorf("5w ttl:%v", ttl)
|
||||||
|
|||||||
Reference in New Issue
Block a user