Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mongodb/mongo-go-driver into GODR…
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Aug 11, 2023
2 parents 61347c6 + 13004f7 commit 79d7437
Show file tree
Hide file tree
Showing 36 changed files with 1,779 additions and 563 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: PR API Report
on:
pull_request_target:

jobs:
comment:
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
steps:
- uses: actions/setup-go@v4
- name: Find Comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: "## API Change Report"

- name: Create the comment body
run: |
set -eux
git clone https://github.com/mongodb/mongo-go-driver
cd mongo-go-driver
git remote add source https://github.com/$GITHUB_ACTOR/mongo-go-driver
git fetch origin $GITHUB_BASE_REF
git fetch source $GITHUB_HEAD_REF
git checkout $GITHUB_HEAD_REF
make api-report
cat api-report.md
- name: Create or update comment
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body-path: 'mongo-go-driver/api-report.md'
edit-mode: replace
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ internal/test/faas/awslambda/events/event.json
# Ignore compiled binaries from the compilecheck
internal/test/compilecheck/compilecheck
internal/test/compilecheck/compilecheck.so

# Ignore api report files
api-report.md
api-report.txt
16 changes: 7 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
run:
timeout: 5m
skip-dirs-use-default: false
skip-dirs:
- (^|/)vendor($|/)
- (^|/)testdata($|/)
- (^|/)etc($|/)
# Disable all linters for "golang.org/x/exp/rand" package in internal/rand.
- internal/rand

linters:
disable-all: true
Expand Down Expand Up @@ -83,11 +90,6 @@ issues:
- "ineffectual assignment to wm"
- "ineffectual assignment to rem"

skip-dirs-use-default: false
skip-dirs:
- (^|/)vendor($|/)
- (^|/)testdata($|/)
- (^|/)etc($|/)
exclude-rules:
# Ignore some linters for example code that is intentionally simplified.
- path: examples/
Expand All @@ -114,7 +116,3 @@ issues:
# Ignore missing package comments for directories that aren't frequently used by external users.
- path: (internal\/|benchmark\/|x\/|cmd\/|mongo\/integration\/)
text: should have a package comment
# Disable unused linter for "golang.org/x/exp/rand" package in internal/rand.
- path: internal/rand
linters:
- unused
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ build: cross-compile build-tests build-compile-check
build-tests:
go test -short $(BUILD_TAGS) -run ^$$ ./...

.PHONY: api-report
api-report:
etc/api_report.sh

.PHONY: build-compile-check
build-compile-check:
etc/compile_check.sh
Expand Down
1 change: 1 addition & 0 deletions bson/bsontype/bsontype.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
BinaryMD5 byte = 0x05
BinaryEncrypted byte = 0x06
BinaryColumn byte = 0x07
BinarySensitive byte = 0x08
BinaryUserDefined byte = 0x80
)

Expand Down
1 change: 1 addition & 0 deletions bson/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ const (
TypeBinaryMD5 = bsontype.BinaryMD5
TypeBinaryEncrypted = bsontype.BinaryEncrypted
TypeBinaryColumn = bsontype.BinaryColumn
TypeBinarySensitive = bsontype.BinarySensitive
TypeBinaryUserDefined = bsontype.BinaryUserDefined
)
84 changes: 84 additions & 0 deletions cmd/parse-api-report/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (C) MongoDB, Inc. 2023-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package main

import (
"bufio"
"fmt"
"log"
"os"
"strings"
)

func main() {
var line string
var suppress bool
var foundChange = false
var foundSummary = false

// open file to read
fRead, err := os.Open("api-report.txt")
if err != nil {
log.Fatal(err)
}
// remember to close the file at the end of the program
defer fRead.Close()

// open file to write
fWrite, err := os.Create("api-report.md")
if err != nil {
log.Fatal(err)
}
// remember to close the file at the end of the program
defer fWrite.Close()

fmt.Fprint(fWrite, "## API Change Report\n")

// read the file line by line using scanner
scanner := bufio.NewScanner(fRead)

for scanner.Scan() {
// do something with a line
line = scanner.Text()
if strings.Index(line, "## ") == 0 {
line = "##" + line
}

if strings.Contains(line, "/mongo/integration/") {
suppress = true
}
if strings.Index(line, "# summary") == 0 {
suppress = true
foundSummary = true
}

if strings.Contains(line, "go.mongodb.org/mongo-driver") {
line = strings.Replace(line, "go.mongodb.org/mongo-driver", ".", -1)
line = "##" + line
}
if !suppress {
fmt.Fprintf(fWrite, "%s\n", line)
foundChange = true
}
if len(line) == 0 {
suppress = false
}
}

if !foundChange {
fmt.Fprint(fWrite, "No changes found!\n")
}

if !foundSummary {
log.Fatal("Could not parse api summary")
}

if err := scanner.Err(); err != nil {
log.Fatal(err)
}

}
19 changes: 19 additions & 0 deletions etc/api_report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# api-report
# Generates a report of Go Driver API changes for the current branch.
set -eux

cmd=$(command -v gorelease || true)

if [ -z $cmd ]; then
go install golang.org/x/exp/cmd/gorelease@latest
fi

branch=${GITHUB_BASE_REF:-master}
sha=$(git merge-base $branch HEAD)

gorelease -base=$sha > api-report.txt || true

go run ./cmd/parse-api-report/main.go

rm api-report.txt
128 changes: 128 additions & 0 deletions internal/rand/arith128_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copied from https://cs.opensource.google/go/x/exp/+/24438e51023af3bfc1db8aed43c1342817e8cfcd:rand/arith128_test.go

// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package rand

import (
"math/big"
"math/rand"
"testing"
)

var bigMaxUint64 = big.NewInt(0).SetUint64(maxUint64)

func bigInt(xHi, xLo uint64) *big.Int {
b := big.NewInt(0).SetUint64(xHi)
b.Lsh(b, 64)
b.Or(b, big.NewInt(0).SetUint64(xLo))
return b
}

func splitBigInt(b *big.Int) (outHi, outLo uint64) {
outHi = big.NewInt(0).Rsh(b, 64).Uint64()
outLo = big.NewInt(0).And(b, bigMaxUint64).Uint64()
return
}

func bigMulMod128bits(xHi, xLo, yHi, yLo uint64) (outHi, outLo uint64) {
bigX := bigInt(xHi, xLo)
bigY := bigInt(yHi, yLo)
return splitBigInt(bigX.Mul(bigX, bigY))
}

func bigAddMod128bits(xHi, xLo, yHi, yLo uint64) (outHi, outLo uint64) {
bigX := bigInt(xHi, xLo)
bigY := bigInt(yHi, yLo)
return splitBigInt(bigX.Add(bigX, bigY))
}

type arithTest struct {
xHi, xLo uint64
}

const (
iLo = increment & maxUint64
iHi = (increment >> 64) & maxUint64
)

var arithTests = []arithTest{
{0, 0},
{0, 1},
{1, 0},
{0, maxUint64},
{maxUint64, 0},
{maxUint64, maxUint64},
// Randomly generated 64-bit integers.
{3757956613005209672, 17983933746665545631},
{511324141977587414, 5626651684620191081},
{1534313104606153588, 2415006486399353367},
{6873586429837825902, 13854394671140464137},
{6617134480561088940, 18421520694158684312},
}

func TestPCGAdd(t *testing.T) {
for i, test := range arithTests {
p := &PCGSource{
low: test.xLo,
high: test.xHi,
}
p.add()
expectHi, expectLo := bigAddMod128bits(test.xHi, test.xLo, iHi, iLo)
if p.low != expectLo || p.high != expectHi {
t.Errorf("%d: got hi=%d lo=%d; expect hi=%d lo=%d", i, p.high, p.low, expectHi, expectLo)
}
}
}

const (
mLo = multiplier & maxUint64
mHi = (multiplier >> 64) & maxUint64
)

func TestPCGMultiply(t *testing.T) {
for i, test := range arithTests {
p := &PCGSource{
low: test.xLo,
high: test.xHi,
}
p.multiply()
expectHi, expectLo := bigMulMod128bits(test.xHi, test.xLo, mHi, mLo)
if p.low != expectLo || p.high != expectHi {
t.Errorf("%d: got hi=%d lo=%d; expect hi=%d lo=%d", i, p.high, p.low, expectHi, expectLo)
}
}
}

func TestPCGMultiplyLong(t *testing.T) {
if testing.Short() {
return
}
for i := 0; i < 1e6; i++ {
low := rand.Uint64()
high := rand.Uint64()
p := &PCGSource{
low: low,
high: high,
}
p.multiply()
expectHi, expectLo := bigMulMod128bits(high, low, mHi, mLo)
if p.low != expectLo || p.high != expectHi {
t.Fatalf("%d: (%d,%d): got hi=%d lo=%d; expect hi=%d lo=%d", i, high, low, p.high, p.low, expectHi, expectLo)
}
}
}

func BenchmarkPCGMultiply(b *testing.B) {
low := rand.Uint64()
high := rand.Uint64()
p := &PCGSource{
low: low,
high: high,
}
for i := 0; i < b.N; i++ {
p.multiply()
}
}
Loading

0 comments on commit 79d7437

Please sign in to comment.