1 // Copyright 2018 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain 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, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package bigquery 16 17 import ( 18 "math/rand" 19 "os" 20 "sync" 21 "time" 22 ) 23 24 // Support for random values (typically job IDs and insert IDs). 25 26 const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" 27 28 var ( 29 rngMu sync.Mutex 30 rng = rand.New(rand.NewSource(time.Now().UnixNano() ^ int64(os.Getpid()))) 31 ) 32 33 // For testing. 34 var randomIDFn = randomID 35 36 // As of August 2017, the BigQuery service uses 27 alphanumeric characters for 37 // suffixes. 38 const randomIDLen = 27 39 40 func randomID() string { 41 // This is used for both job IDs and insert IDs. 42 var b [randomIDLen]byte 43 rngMu.Lock() 44 for i := 0; i < len(b); i++ { 45 b[i] = alphanum[rng.Intn(len(alphanum))] 46 } 47 rngMu.Unlock() 48 return string(b[:]) 49 } 50 51 // Seed seeds this package's random number generator, used for generating job and 52 // insert IDs. Use Seed to obtain repeatable, deterministic behavior from bigquery 53 // clients. Seed should be called before any clients are created. 54 func Seed(s int64) { 55 rng = rand.New(rand.NewSource(s)) 56 } 57