bson.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  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 http://www.apache.org/licenses/LICENSE-2.0
  6. package benchmark
  7. import (
  8. "errors"
  9. "io/ioutil"
  10. "path/filepath"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/x/bsonx"
  13. )
  14. const (
  15. perfDataDir = "perf"
  16. bsonDataDir = "extended_bson"
  17. flatBSONData = "flat_bson.json"
  18. deepBSONData = "deep_bson.json"
  19. fullBSONData = "full_bson.json"
  20. )
  21. // utility functions for the bson benchmarks
  22. func loadSourceDocument(pathParts ...string) (bsonx.Doc, error) {
  23. data, err := ioutil.ReadFile(filepath.Join(pathParts...))
  24. if err != nil {
  25. return nil, err
  26. }
  27. doc := bsonx.Doc{}
  28. err = bson.UnmarshalExtJSON(data, true, &doc)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if len(doc) == 0 {
  33. return nil, errors.New("empty bson document")
  34. }
  35. return doc, nil
  36. }
  37. func loadSourceRaw(pathParts ...string) (bson.Raw, error) {
  38. doc, err := loadSourceDocument(pathParts...)
  39. if err != nil {
  40. return nil, err
  41. }
  42. raw, err := doc.MarshalBSON()
  43. if err != nil {
  44. return nil, err
  45. }
  46. return bson.Raw(raw), nil
  47. }
  48. func loadSourceD(pathParts ...string) (bson.D, error) {
  49. data, err := ioutil.ReadFile(filepath.Join(pathParts...))
  50. if err != nil {
  51. return nil, err
  52. }
  53. doc := bson.D{}
  54. err = bson.UnmarshalExtJSON(data, true, &doc)
  55. if err != nil {
  56. return nil, err
  57. }
  58. if len(doc) == 0 {
  59. return nil, errors.New("empty bson document")
  60. }
  61. return doc, nil
  62. }