2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-12 09:14:16 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
2019-05-19 16:10:46 +01:00
|
|
|
"database/sql/driver"
|
|
|
|
|
2018-11-12 09:14:16 +00:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-05-19 16:10:46 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
2019-12-16 17:59:01 +00:00
|
|
|
"storj.io/storj/private/dbutil"
|
2018-11-12 09:14:16 +00:00
|
|
|
)
|
|
|
|
|
2019-05-19 16:10:46 +01:00
|
|
|
type postgresNodeIDList storj.NodeIDList
|
|
|
|
|
|
|
|
// Value converts a NodeIDList to a postgres array
|
|
|
|
func (nodes postgresNodeIDList) Value() (driver.Value, error) {
|
|
|
|
const hextable = "0123456789abcdef"
|
|
|
|
|
|
|
|
if nodes == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return []byte("{}"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var wp, x int
|
|
|
|
out := make([]byte, 2+len(nodes)*(6+storj.NodeIDSize*2)-1)
|
|
|
|
|
|
|
|
x = copy(out[wp:], []byte(`{"\\x`))
|
|
|
|
wp += x
|
|
|
|
|
|
|
|
for i := range nodes {
|
|
|
|
for _, v := range nodes[i] {
|
|
|
|
out[wp] = hextable[v>>4]
|
|
|
|
out[wp+1] = hextable[v&0xf]
|
|
|
|
wp += 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if i+1 < len(nodes) {
|
|
|
|
x = copy(out[wp:], []byte(`","\\x`))
|
|
|
|
wp += x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x = copy(out[wp:], `"}`)
|
|
|
|
wp += x
|
|
|
|
|
|
|
|
if wp != len(out) {
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
2019-09-12 15:19:30 +01:00
|
|
|
|
|
|
|
// uuidScan used to represent uuid scan struct
|
|
|
|
type uuidScan struct {
|
|
|
|
uuid *uuid.UUID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan is used to wrap logic of db scan with uuid conversion
|
|
|
|
func (s *uuidScan) Scan(src interface{}) (err error) {
|
|
|
|
b, ok := src.([]byte)
|
2019-12-16 17:59:01 +00:00
|
|
|
|
2019-09-12 15:19:30 +01:00
|
|
|
if !ok {
|
|
|
|
return Error.New("unexpected type %T for uuid", src)
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:59:01 +00:00
|
|
|
*s.uuid, err = dbutil.BytesToUUID(b)
|
2019-09-12 15:19:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|