aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/coveragedb/spannerclient/spanner_client.go
blob: 4f655c6edce41ded3148a4a577d06b25d4563dd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package spannerclient

import (
	"context"
	"time"

	"cloud.google.com/go/spanner"
)

type SpannerClient interface {
	Close()
	Apply(ctx context.Context, ms []*spanner.Mutation, opts ...spanner.ApplyOption) (commitTimestamp time.Time, err error)
	Single() ReadOnlyTransaction
}

type ReadOnlyTransaction interface {
	Query(ctx context.Context, statement spanner.Statement) RowIterator
}

type RowIterator interface {
	Next() (Row, error)
	Stop()
}

type Row interface {
	ToStruct(p interface{}) error
}

type SpannerClientProxy struct {
	client *spanner.Client
}

func (proxy *SpannerClientProxy) Close() {
	proxy.client.Close()
}

func (proxy *SpannerClientProxy) Apply(ctx context.Context, ms []*spanner.Mutation, opts ...spanner.ApplyOption,
) (commitTimestamp time.Time, err error) {
	return proxy.client.Apply(ctx, ms, opts...)
}

func (proxy *SpannerClientProxy) Single() ReadOnlyTransaction {
	return &SpannerReadOnlyTransactionProxy{
		readOnlyTransaction: proxy.client.Single(),
	}
}

type SpannerReadOnlyTransactionProxy struct {
	readOnlyTransaction *spanner.ReadOnlyTransaction
}

func (proxy *SpannerReadOnlyTransactionProxy) Query(ctx context.Context, statement spanner.Statement) RowIterator {
	return &SpannerRowIteratorProxy{
		rowIterator: proxy.readOnlyTransaction.Query(ctx, statement),
	}
}

type SpannerRowIteratorProxy struct {
	rowIterator *spanner.RowIterator
}

func (proxy *SpannerRowIteratorProxy) Next() (Row, error) {
	return proxy.rowIterator.Next()
}

func (proxy *SpannerRowIteratorProxy) Stop() {
	proxy.rowIterator.Stop()
}

func NewClient(ctx context.Context, projectID string) (SpannerClient, error) {
	database := "projects/" + projectID + "/instances/syzbot/databases/coverage"
	client, err := spanner.NewClient(ctx, database)
	return &SpannerClientProxy{
		client: client,
	}, err
}