You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

57 lines
1.5 KiB

  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. package bigquery
  15. import (
  16. "math/rand"
  17. "os"
  18. "sync"
  19. "time"
  20. )
  21. // Support for random values (typically job IDs and insert IDs).
  22. const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  23. var (
  24. rngMu sync.Mutex
  25. rng = rand.New(rand.NewSource(time.Now().UnixNano() ^ int64(os.Getpid())))
  26. )
  27. // For testing.
  28. var randomIDFn = randomID
  29. // As of August 2017, the BigQuery service uses 27 alphanumeric characters for
  30. // suffixes.
  31. const randomIDLen = 27
  32. func randomID() string {
  33. // This is used for both job IDs and insert IDs.
  34. var b [randomIDLen]byte
  35. rngMu.Lock()
  36. for i := 0; i < len(b); i++ {
  37. b[i] = alphanum[rng.Intn(len(alphanum))]
  38. }
  39. rngMu.Unlock()
  40. return string(b[:])
  41. }
  42. // Seed seeds this package's random number generator, used for generating job and
  43. // insert IDs. Use Seed to obtain repeatable, deterministic behavior from bigquery
  44. // clients. Seed should be called before any clients are created.
  45. func Seed(s int64) {
  46. rng = rand.New(rand.NewSource(s))
  47. }