2021-03-08 13:09:32 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metabase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/uuid"
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/tagsql"
|
2021-03-08 13:09:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetStreamPieceCountByNodeID contains arguments for GetStreamPieceCountByNodeID.
|
|
|
|
type GetStreamPieceCountByNodeID struct {
|
|
|
|
StreamID uuid.UUID
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStreamPieceCountByNodeID returns piece count by node id.
|
|
|
|
func (db *DB) GetStreamPieceCountByNodeID(ctx context.Context, opts GetStreamPieceCountByNodeID) (result map[storj.NodeID]int64, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if opts.StreamID.IsZero() {
|
|
|
|
return nil, ErrInvalidRequest.New("StreamID missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
countByAlias := map[NodeAlias]int64{}
|
|
|
|
result = map[storj.NodeID]int64{}
|
2021-07-28 14:44:22 +01:00
|
|
|
err = withRows(db.db.QueryContext(ctx, `
|
2021-03-08 13:09:32 +00:00
|
|
|
SELECT remote_alias_pieces
|
|
|
|
FROM segments
|
|
|
|
WHERE stream_id = $1 AND remote_alias_pieces IS NOT null
|
2022-12-13 08:57:49 +00:00
|
|
|
UNION
|
|
|
|
SELECT remote_alias_pieces
|
|
|
|
FROM segments
|
|
|
|
WHERE stream_id = (SELECT ancestor_stream_id FROM segment_copies WHERE stream_id = $1) AND remote_alias_pieces IS NOT null
|
2021-03-08 13:09:32 +00:00
|
|
|
`, opts.StreamID))(func(rows tagsql.Rows) error {
|
|
|
|
for rows.Next() {
|
|
|
|
var aliasPieces AliasPieces
|
|
|
|
err = rows.Scan(&aliasPieces)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("failed to scan segments: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range aliasPieces {
|
|
|
|
countByAlias[aliasPieces[i].Alias]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, Error.New("unable to fetch object segments: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for alias, count := range countByAlias {
|
|
|
|
nodeID, err := db.aliasCache.Nodes(ctx, []NodeAlias{alias})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.New("unable to convert aliases to pieces: %w", err)
|
|
|
|
}
|
|
|
|
result[nodeID[0]] = count
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|