github.com/XiaoMi/Gaea@v1.2.5/models/numkey.go (about) 1 // Copyright 2016 The kingshard Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package models 16 17 import ( 18 "fmt" 19 "math" 20 "strconv" 21 "strings" 22 "time" 23 24 "github.com/XiaoMi/Gaea/core/errors" 25 ) 26 27 const ( 28 // MinNumKey min int64 29 MinNumKey = math.MinInt64 30 // MaxNumKey max int64 31 MaxNumKey = math.MaxInt64 32 ) 33 34 // NumKeyRange [start,end) 35 type NumKeyRange struct { 36 Start int64 37 End int64 38 } 39 40 // MapKey key range 41 func (kr NumKeyRange) MapKey() string { 42 return fmt.Sprintf("%d-%d", kr.Start, kr.End) 43 } 44 45 // Contains check if range contains i 46 func (kr NumKeyRange) Contains(i int64) bool { 47 return kr.Start <= i && (kr.End == MaxNumKey || i < kr.End) 48 } 49 50 func (kr NumKeyRange) String() string { 51 return fmt.Sprintf("{Start: %d, End: %d}", kr.Start, kr.End) 52 } 53 54 // ParseNumSharding parse num shard 55 func ParseNumSharding(Locations []int, TableRowLimit int) ([]NumKeyRange, error) { 56 tableCount := 0 57 length := len(Locations) 58 59 for i := 0; i < length; i++ { 60 tableCount += Locations[i] 61 } 62 63 ranges := make([]NumKeyRange, tableCount) 64 for i := 0; i < tableCount; i++ { 65 ranges[i].Start = int64(i * TableRowLimit) 66 ranges[i].End = int64((i + 1) * TableRowLimit) 67 } 68 return ranges, nil 69 } 70 71 // ParseDayRange return date of daynumber by order 72 //20151201-20151205 73 //20151201,20151202,20151203,20151204,20151205 74 func ParseDayRange(dateRange string) ([]int, error) { 75 timeFormat := "20060102" 76 dateDays := make([]int, 0) 77 dateLength := 8 78 79 dateTmp := strings.SplitN(dateRange, "-", 2) 80 if len(dateTmp) == 1 { 81 if len(dateTmp[0]) != dateLength { 82 return nil, errors.ErrDateRangeIllegal 83 } 84 if _, err := time.Parse(timeFormat, dateTmp[0]); err != nil { 85 return nil, errors.ErrDateRangeIllegal 86 } 87 dateNum, err := strconv.Atoi(dateTmp[0]) 88 if err != nil { 89 return nil, err 90 } 91 return []int{dateNum}, nil 92 } 93 if len(dateTmp) != 2 { 94 return nil, errors.ErrDateRangeIllegal 95 } 96 if len(dateTmp[0]) != dateLength || len(dateTmp[1]) != dateLength { 97 return nil, errors.ErrDateRangeIllegal 98 } 99 //change the begin day and the end day 100 if dateTmp[1] < dateTmp[0] { 101 dateTmp[0], dateTmp[1] = dateTmp[1], dateTmp[0] 102 } 103 104 begin, err := time.Parse(timeFormat, dateTmp[0]) 105 if err != nil { 106 return nil, err 107 } 108 end, err := time.Parse(timeFormat, dateTmp[1]) 109 if err != nil { 110 return nil, err 111 } 112 113 duration := end.Sub(begin) 114 daysCount := int(duration.Hours() / 24) 115 116 for i := 0; i <= daysCount; i++ { 117 date := begin.Add(time.Hour * time.Duration(24*i)) 118 dateStr := date.Format("20060102") 119 dateNum, err := strconv.Atoi(dateStr) 120 if err != nil { 121 return nil, err 122 } 123 dateDays = append(dateDays, dateNum) 124 } 125 return dateDays, nil 126 } 127 128 // ParseMonthRange return date of month by order 129 //201510-201512 130 //201510,201511,201512 131 func ParseMonthRange(dateRange string) ([]int, error) { 132 timeFormat := "200601" 133 dateMonth := make([]int, 0) 134 dateLength := 6 135 136 dateTmp := strings.SplitN(dateRange, "-", 2) 137 if len(dateTmp) == 1 { 138 if len(dateTmp[0]) != dateLength { 139 return nil, errors.ErrDateRangeIllegal 140 } 141 if _, err := time.Parse(timeFormat, dateTmp[0]); err != nil { 142 return nil, errors.ErrDateRangeIllegal 143 } 144 dateNum, err := strconv.Atoi(dateTmp[0]) 145 if err != nil { 146 return nil, err 147 } 148 return []int{dateNum}, nil 149 } 150 if len(dateTmp) != 2 { 151 return nil, errors.ErrDateRangeIllegal 152 } 153 if len(dateTmp[0]) != dateLength || len(dateTmp[1]) != dateLength { 154 return nil, errors.ErrDateRangeIllegal 155 } 156 //change the begin month and the end month 157 if dateTmp[1] < dateTmp[0] { 158 dateTmp[0], dateTmp[1] = dateTmp[1], dateTmp[0] 159 } 160 161 if _, err := time.Parse(timeFormat, dateTmp[0]); err != nil { 162 return nil, errors.ErrDateRangeIllegal 163 } 164 beginYear, err := strconv.Atoi(dateTmp[0][:4]) 165 if err != nil { 166 return nil, err 167 } 168 beginMonth, err := strconv.Atoi(dateTmp[0][4:]) 169 if err != nil { 170 return nil, err 171 } 172 173 if _, err := time.Parse(timeFormat, dateTmp[1]); err != nil { 174 return nil, errors.ErrDateRangeIllegal 175 } 176 endYear, err := strconv.Atoi(dateTmp[1][:4]) 177 if err != nil { 178 return nil, err 179 } 180 endMonth, err := strconv.Atoi(dateTmp[1][4:]) 181 if err != nil { 182 return nil, err 183 } 184 // how many months between the two date 185 monthCount := (endYear-beginYear)*12 + endMonth - beginMonth + 1 186 monthTmp := beginMonth 187 for i := 0; i < monthCount; i++ { 188 if 12 < monthTmp { 189 monthTmp = monthTmp % 12 190 beginYear++ 191 } 192 193 monthNum := beginYear*100 + monthTmp 194 dateMonth = append(dateMonth, monthNum) 195 monthTmp++ 196 } 197 198 return dateMonth, nil 199 } 200 201 // ParseYearRange return date of year by order 202 //2013-2015 203 //2013,2014,2015 204 func ParseYearRange(dateRange string) ([]int, error) { 205 timeFormat := "2006" 206 dateYear := make([]int, 0) 207 dateLength := 4 208 209 dateTmp := strings.SplitN(dateRange, "-", 2) 210 if len(dateTmp) == 1 { 211 if len(dateTmp[0]) != dateLength { 212 return nil, errors.ErrDateRangeIllegal 213 } 214 if _, err := time.Parse(timeFormat, dateTmp[0]); err != nil { 215 return nil, errors.ErrDateRangeIllegal 216 } 217 dateNum, err := strconv.Atoi(dateTmp[0]) 218 if err != nil { 219 return nil, err 220 } 221 return []int{dateNum}, nil 222 } 223 if len(dateTmp) != 2 { 224 return nil, errors.ErrDateRangeIllegal 225 } 226 //change the begin year and the end year 227 if dateTmp[1] < dateTmp[0] { 228 dateTmp[0], dateTmp[1] = dateTmp[1], dateTmp[0] 229 } 230 231 if _, err := time.Parse(timeFormat, dateTmp[0]); err != nil { 232 return nil, errors.ErrDateRangeIllegal 233 } 234 beginYear, err := strconv.Atoi(dateTmp[0]) 235 if err != nil { 236 return nil, err 237 } 238 239 if _, err := time.Parse(timeFormat, dateTmp[1]); err != nil { 240 return nil, errors.ErrDateRangeIllegal 241 } 242 endYear, err := strconv.Atoi(dateTmp[1]) 243 if err != nil { 244 return nil, err 245 } 246 247 for i := beginYear; i <= endYear; i++ { 248 dateYear = append(dateYear, i) 249 } 250 251 return dateYear, nil 252 }