|
| 1 | +package exported |
| 2 | + |
| 3 | +import "C" |
| 4 | +import ( |
| 5 | + "cloud.google.com/go/spanner/apiv1/spannerpb" |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "google.golang.org/protobuf/proto" |
| 9 | + "sync" |
| 10 | + "sync/atomic" |
| 11 | + |
| 12 | + "spannerlib/backend" |
| 13 | +) |
| 14 | + |
| 15 | +var pools = sync.Map{} |
| 16 | +var poolsIdx = atomic.Int64{} |
| 17 | + |
| 18 | +type Pool struct { |
| 19 | + backend *backend.Pool |
| 20 | + connections *sync.Map |
| 21 | + connectionsIdx atomic.Int64 |
| 22 | +} |
| 23 | + |
| 24 | +func CreatePool() *Message { |
| 25 | + backendPool := &backend.Pool{} |
| 26 | + id := poolsIdx.Add(1) |
| 27 | + pool := &Pool{ |
| 28 | + backend: backendPool, |
| 29 | + connections: &sync.Map{}, |
| 30 | + } |
| 31 | + pools.Store(id, pool) |
| 32 | + return idMessage(id) |
| 33 | +} |
| 34 | + |
| 35 | +func ClosePool(id int64) *Message { |
| 36 | + p, ok := pools.LoadAndDelete(id) |
| 37 | + if !ok { |
| 38 | + return errMessage(fmt.Errorf("pool %v not found", id)) |
| 39 | + } |
| 40 | + pool := p.(*Pool) |
| 41 | + pool.connections.Range(func(key, value interface{}) bool { |
| 42 | + conn := value.(*Connection) |
| 43 | + conn.close() |
| 44 | + return true |
| 45 | + }) |
| 46 | + return &Message{} |
| 47 | +} |
| 48 | + |
| 49 | +func CreateConnection(poolId int64, database string) *Message { |
| 50 | + p, ok := pools.Load(poolId) |
| 51 | + if !ok { |
| 52 | + return errMessage(fmt.Errorf("pool %v not found", poolId)) |
| 53 | + } |
| 54 | + pool := p.(*Pool) |
| 55 | + sqlConn, err := pool.backend.Conn(context.Background(), "appdev-soda-spanner-staging", "knut-test-ycsb", "knut-test-db") |
| 56 | + if err != nil { |
| 57 | + return errMessage(err) |
| 58 | + } |
| 59 | + id := poolsIdx.Add(1) |
| 60 | + conn := &Connection{ |
| 61 | + backend: &backend.SpannerConnection{Conn: sqlConn}, |
| 62 | + results: &sync.Map{}, |
| 63 | + } |
| 64 | + pool.connections.Store(id, conn) |
| 65 | + |
| 66 | + return idMessage(id) |
| 67 | +} |
| 68 | + |
| 69 | +func CloseConnection(poolId, connId int64) *Message { |
| 70 | + conn, err := findConnection(poolId, connId) |
| 71 | + if err != nil { |
| 72 | + return errMessage(err) |
| 73 | + } |
| 74 | + return conn.close() |
| 75 | +} |
| 76 | + |
| 77 | +func Execute(poolId, connId int64, statementBytes []byte) *Message { |
| 78 | + statement := spannerpb.ExecuteBatchDmlRequest_Statement{} |
| 79 | + if err := proto.Unmarshal(statementBytes, &statement); err != nil { |
| 80 | + return errMessage(err) |
| 81 | + } |
| 82 | + fmt.Printf("Statement: %v\n", statement.Sql) |
| 83 | + fmt.Printf("Params: %v\n", statement.Params) |
| 84 | + conn, err := findConnection(poolId, connId) |
| 85 | + if err != nil { |
| 86 | + return errMessage(err) |
| 87 | + } |
| 88 | + return conn.Execute(&statement) |
| 89 | +} |
| 90 | + |
| 91 | +func Next(poolId, connId, rowsId int64) *Message { |
| 92 | + res, err := findRows(poolId, connId, rowsId) |
| 93 | + if err != nil { |
| 94 | + return errMessage(err) |
| 95 | + } |
| 96 | + return res.Next() |
| 97 | +} |
| 98 | + |
| 99 | +func CloseRows(poolId, connId, rowsId int64) *Message { |
| 100 | + res, err := findRows(poolId, connId, rowsId) |
| 101 | + if err != nil { |
| 102 | + return errMessage(err) |
| 103 | + } |
| 104 | + return res.Close() |
| 105 | +} |
| 106 | + |
| 107 | +func findConnection(poolId, connId int64) (*Connection, error) { |
| 108 | + p, ok := pools.Load(poolId) |
| 109 | + if !ok { |
| 110 | + return nil, fmt.Errorf("pool %v not found", poolId) |
| 111 | + } |
| 112 | + pool := p.(*Pool) |
| 113 | + c, ok := pool.connections.Load(connId) |
| 114 | + if !ok { |
| 115 | + return nil, fmt.Errorf("connection %v not found", connId) |
| 116 | + } |
| 117 | + conn := c.(*Connection) |
| 118 | + return conn, nil |
| 119 | +} |
| 120 | + |
| 121 | +func findRows(poolId, connId, rowsId int64) (*rows, error) { |
| 122 | + conn, err := findConnection(poolId, connId) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + r, ok := conn.results.Load(rowsId) |
| 127 | + if !ok { |
| 128 | + return nil, fmt.Errorf("rows %v not found", rowsId) |
| 129 | + } |
| 130 | + res := r.(*rows) |
| 131 | + return res, nil |
| 132 | +} |
0 commit comments