value attribution DB interface Insert & Get method support (#2200)

* value attribution DB interface methods support
This commit is contained in:
aligeti 2019-06-18 09:06:33 -04:00 committed by GitHub
parent 8398fae9b5
commit 09940d4e0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 444 additions and 5 deletions

View File

@ -0,0 +1,24 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package valueattribution
import (
"context"
"time"
)
// PartnerInfo describing connector/partner key info in the database
type PartnerInfo struct {
PartnerID []byte
BucketName []byte
CreatedAt time.Time
}
// DB implements the database for value attribution table
type DB interface {
// Get retrieves partner id using bucket name
Get(ctx context.Context, buckname []byte) (*PartnerInfo, error)
// Insert creates and stores new ConnectorKeyInfo
Insert(ctx context.Context, info *PartnerInfo) (*PartnerInfo, error)
}

View File

@ -0,0 +1,63 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package valueattribution_test
import (
"testing"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testcontext"
"storj.io/storj/pkg/valueattribution"
"storj.io/storj/satellite"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
)
func TestValueAttribution(t *testing.T) {
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
valAttrdb := db.ValueAttribution()
// unique partner and bucket
partnerInfo := &valueattribution.PartnerInfo{
PartnerID: []byte("valueattribution testcase partnerID"),
BucketName: []byte("valueattribution testcase bucketname"),
}
// same partner and dfferent bucket
partnerInfo1 := &valueattribution.PartnerInfo{
PartnerID: []byte("valueattribution testcase partnerID"),
BucketName: []byte("valueattribution testcase different bucketname"),
}
// different partner and existing bucket
partnerInfo2 := &valueattribution.PartnerInfo{
PartnerID: []byte("valueattribution testcase different partnerID"),
BucketName: []byte("valueattribution testcase different bucketname"),
}
{ // Insert
_, err := valAttrdb.Insert(ctx, partnerInfo)
assert.NoError(t, err)
_, err = valAttrdb.Insert(ctx, partnerInfo1)
assert.NoError(t, err)
_, err = valAttrdb.Insert(ctx, partnerInfo2)
assert.Error(t, err)
}
{ // Get
info, err := valAttrdb.Get(ctx, partnerInfo.BucketName)
assert.NoError(t, err)
assert.Equal(t, partnerInfo.PartnerID, info.PartnerID)
info, err = valAttrdb.Get(ctx, partnerInfo1.BucketName)
assert.NoError(t, err)
assert.Equal(t, partnerInfo1.PartnerID, info.PartnerID)
}
})
}

View File

@ -43,6 +43,7 @@ import (
"storj.io/storj/pkg/server"
"storj.io/storj/pkg/storj"
"storj.io/storj/pkg/transport"
"storj.io/storj/pkg/valueattribution"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/console/consoleauth"
"storj.io/storj/satellite/console/consoleweb"
@ -79,6 +80,8 @@ type DB interface {
CertDB() certdb.DB
// OverlayCache returns database for caching overlay information
OverlayCache() overlay.DB
// ValueAttribution returns database for partner keys information
ValueAttribution() valueattribution.DB
// StoragenodeAccounting returns database for storing information about storagenode use
StoragenodeAccounting() accounting.StoragenodeAccounting
// ProjectAccounting returns database for storing information about project data use

View File

@ -15,6 +15,7 @@ import (
"storj.io/storj/pkg/datarepair/irreparable"
"storj.io/storj/pkg/datarepair/queue"
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/valueattribution"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/marketing"
@ -97,6 +98,11 @@ func (db *DB) CertDB() certdb.DB {
return &certDB{db: db.db}
}
// ValueAttribution is a getter for valueattribution repository
func (db *DB) ValueAttribution() valueattribution.DB {
return &valueattributionDB{db: db.db}
}
// OverlayCache is a getter for overlay cache repository
func (db *DB) OverlayCache() overlay.DB {
return &overlaycache{db: db.db}

View File

@ -7,7 +7,15 @@ model value_attribution (
field project_id blob
field bucket_name blob
field partner_id blob
field last_updated utimestamp
field last_updated utimestamp ( autoinsert, autoupdate )
)
create value_attribution ()
delete value_attribution ( where value_attribution.bucket_name = ? )
read one (
select value_attribution
where value_attribution.bucket_name = ?
)
//--- containment ---//

View File

@ -4810,6 +4810,32 @@ type Value_Row struct {
Value time.Time
}
func (obj *postgresImpl) Create_ValueAttribution(ctx context.Context,
value_attribution_project_id ValueAttribution_ProjectId_Field,
value_attribution_bucket_name ValueAttribution_BucketName_Field,
value_attribution_partner_id ValueAttribution_PartnerId_Field) (
value_attribution *ValueAttribution, err error) {
__now := obj.db.Hooks.Now().UTC()
__project_id_val := value_attribution_project_id.value()
__bucket_name_val := value_attribution_bucket_name.value()
__partner_id_val := value_attribution_partner_id.value()
__last_updated_val := __now.UTC()
var __embed_stmt = __sqlbundle_Literal("INSERT INTO value_attributions ( project_id, bucket_name, partner_id, last_updated ) VALUES ( ?, ?, ?, ? ) RETURNING value_attributions.project_id, value_attributions.bucket_name, value_attributions.partner_id, value_attributions.last_updated")
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __project_id_val, __bucket_name_val, __partner_id_val, __last_updated_val)
value_attribution = &ValueAttribution{}
err = obj.driver.QueryRow(__stmt, __project_id_val, __bucket_name_val, __partner_id_val, __last_updated_val).Scan(&value_attribution.ProjectId, &value_attribution.BucketName, &value_attribution.PartnerId, &value_attribution.LastUpdated)
if err != nil {
return nil, obj.makeErr(err)
}
return value_attribution, nil
}
func (obj *postgresImpl) Create_PendingAudits(ctx context.Context,
pending_audits_node_id PendingAudits_NodeId_Field,
pending_audits_piece_id PendingAudits_PieceId_Field,
@ -5472,6 +5498,49 @@ func (obj *postgresImpl) Create_UserCredit(ctx context.Context,
}
func (obj *postgresImpl) Get_ValueAttribution_By_BucketName(ctx context.Context,
value_attribution_bucket_name ValueAttribution_BucketName_Field) (
value_attribution *ValueAttribution, err error) {
var __embed_stmt = __sqlbundle_Literal("SELECT value_attributions.project_id, value_attributions.bucket_name, value_attributions.partner_id, value_attributions.last_updated FROM value_attributions WHERE value_attributions.bucket_name = ? LIMIT 2")
var __values []interface{}
__values = append(__values, value_attribution_bucket_name.value())
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
__rows, err := obj.driver.Query(__stmt, __values...)
if err != nil {
return nil, obj.makeErr(err)
}
defer __rows.Close()
if !__rows.Next() {
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return nil, makeErr(sql.ErrNoRows)
}
value_attribution = &ValueAttribution{}
err = __rows.Scan(&value_attribution.ProjectId, &value_attribution.BucketName, &value_attribution.PartnerId, &value_attribution.LastUpdated)
if err != nil {
return nil, obj.makeErr(err)
}
if __rows.Next() {
return nil, tooManyRows("ValueAttribution_By_BucketName")
}
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return value_attribution, nil
}
func (obj *postgresImpl) Get_PendingAudits_By_NodeId(ctx context.Context,
pending_audits_node_id PendingAudits_NodeId_Field) (
pending_audits *PendingAudits, err error) {
@ -7422,6 +7491,32 @@ func (obj *postgresImpl) Update_Offer_By_Id(ctx context.Context,
return offer, nil
}
func (obj *postgresImpl) Delete_ValueAttribution_By_BucketName(ctx context.Context,
value_attribution_bucket_name ValueAttribution_BucketName_Field) (
count int64, err error) {
var __embed_stmt = __sqlbundle_Literal("DELETE FROM value_attributions WHERE value_attributions.bucket_name = ?")
var __values []interface{}
__values = append(__values, value_attribution_bucket_name.value())
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
__res, err := obj.driver.Exec(__stmt, __values...)
if err != nil {
return 0, obj.makeErr(err)
}
count, err = __res.RowsAffected()
if err != nil {
return 0, obj.makeErr(err)
}
return count, nil
}
func (obj *postgresImpl) Delete_PendingAudits_By_NodeId(ctx context.Context,
pending_audits_node_id PendingAudits_NodeId_Field) (
deleted bool, err error) {
@ -8039,6 +8134,35 @@ func (obj *postgresImpl) deleteAll(ctx context.Context) (count int64, err error)
}
func (obj *sqlite3Impl) Create_ValueAttribution(ctx context.Context,
value_attribution_project_id ValueAttribution_ProjectId_Field,
value_attribution_bucket_name ValueAttribution_BucketName_Field,
value_attribution_partner_id ValueAttribution_PartnerId_Field) (
value_attribution *ValueAttribution, err error) {
__now := obj.db.Hooks.Now().UTC()
__project_id_val := value_attribution_project_id.value()
__bucket_name_val := value_attribution_bucket_name.value()
__partner_id_val := value_attribution_partner_id.value()
__last_updated_val := __now.UTC()
var __embed_stmt = __sqlbundle_Literal("INSERT INTO value_attributions ( project_id, bucket_name, partner_id, last_updated ) VALUES ( ?, ?, ?, ? )")
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __project_id_val, __bucket_name_val, __partner_id_val, __last_updated_val)
__res, err := obj.driver.Exec(__stmt, __project_id_val, __bucket_name_val, __partner_id_val, __last_updated_val)
if err != nil {
return nil, obj.makeErr(err)
}
__pk, err := __res.LastInsertId()
if err != nil {
return nil, obj.makeErr(err)
}
return obj.getLastValueAttribution(ctx, __pk)
}
func (obj *sqlite3Impl) Create_PendingAudits(ctx context.Context,
pending_audits_node_id PendingAudits_NodeId_Field,
pending_audits_piece_id PendingAudits_PieceId_Field,
@ -8767,6 +8891,49 @@ func (obj *sqlite3Impl) Create_UserCredit(ctx context.Context,
}
func (obj *sqlite3Impl) Get_ValueAttribution_By_BucketName(ctx context.Context,
value_attribution_bucket_name ValueAttribution_BucketName_Field) (
value_attribution *ValueAttribution, err error) {
var __embed_stmt = __sqlbundle_Literal("SELECT value_attributions.project_id, value_attributions.bucket_name, value_attributions.partner_id, value_attributions.last_updated FROM value_attributions WHERE value_attributions.bucket_name = ? LIMIT 2")
var __values []interface{}
__values = append(__values, value_attribution_bucket_name.value())
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
__rows, err := obj.driver.Query(__stmt, __values...)
if err != nil {
return nil, obj.makeErr(err)
}
defer __rows.Close()
if !__rows.Next() {
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return nil, makeErr(sql.ErrNoRows)
}
value_attribution = &ValueAttribution{}
err = __rows.Scan(&value_attribution.ProjectId, &value_attribution.BucketName, &value_attribution.PartnerId, &value_attribution.LastUpdated)
if err != nil {
return nil, obj.makeErr(err)
}
if __rows.Next() {
return nil, tooManyRows("ValueAttribution_By_BucketName")
}
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return value_attribution, nil
}
func (obj *sqlite3Impl) Get_PendingAudits_By_NodeId(ctx context.Context,
pending_audits_node_id PendingAudits_NodeId_Field) (
pending_audits *PendingAudits, err error) {
@ -10817,6 +10984,32 @@ func (obj *sqlite3Impl) Update_Offer_By_Id(ctx context.Context,
return offer, nil
}
func (obj *sqlite3Impl) Delete_ValueAttribution_By_BucketName(ctx context.Context,
value_attribution_bucket_name ValueAttribution_BucketName_Field) (
count int64, err error) {
var __embed_stmt = __sqlbundle_Literal("DELETE FROM value_attributions WHERE value_attributions.bucket_name = ?")
var __values []interface{}
__values = append(__values, value_attribution_bucket_name.value())
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
__res, err := obj.driver.Exec(__stmt, __values...)
if err != nil {
return 0, obj.makeErr(err)
}
count, err = __res.RowsAffected()
if err != nil {
return 0, obj.makeErr(err)
}
return count, nil
}
func (obj *sqlite3Impl) Delete_PendingAudits_By_NodeId(ctx context.Context,
pending_audits_node_id PendingAudits_NodeId_Field) (
deleted bool, err error) {
@ -11156,6 +11349,24 @@ func (obj *sqlite3Impl) Delete_ResetPasswordToken_By_Secret(ctx context.Context,
}
func (obj *sqlite3Impl) getLastValueAttribution(ctx context.Context,
pk int64) (
value_attribution *ValueAttribution, err error) {
var __embed_stmt = __sqlbundle_Literal("SELECT value_attributions.project_id, value_attributions.bucket_name, value_attributions.partner_id, value_attributions.last_updated FROM value_attributions WHERE _rowid_ = ?")
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, pk)
value_attribution = &ValueAttribution{}
err = obj.driver.QueryRow(__stmt, pk).Scan(&value_attribution.ProjectId, &value_attribution.BucketName, &value_attribution.PartnerId, &value_attribution.LastUpdated)
if err != nil {
return nil, obj.makeErr(err)
}
return value_attribution, nil
}
func (obj *sqlite3Impl) getLastPendingAudits(ctx context.Context,
pk int64) (
pending_audits *PendingAudits, err error) {
@ -12371,6 +12582,19 @@ func (rx *Rx) Create_UserPayment(ctx context.Context,
}
func (rx *Rx) Create_ValueAttribution(ctx context.Context,
value_attribution_project_id ValueAttribution_ProjectId_Field,
value_attribution_bucket_name ValueAttribution_BucketName_Field,
value_attribution_partner_id ValueAttribution_PartnerId_Field) (
value_attribution *ValueAttribution, err error) {
var tx *Tx
if tx, err = rx.getTx(ctx); err != nil {
return
}
return tx.Create_ValueAttribution(ctx, value_attribution_project_id, value_attribution_bucket_name, value_attribution_partner_id)
}
func (rx *Rx) Delete_AccountingRollup_By_Id(ctx context.Context,
accounting_rollup_id AccountingRollup_Id_Field) (
deleted bool, err error) {
@ -12503,6 +12727,17 @@ func (rx *Rx) Delete_User_By_Id(ctx context.Context,
return tx.Delete_User_By_Id(ctx, user_id)
}
func (rx *Rx) Delete_ValueAttribution_By_BucketName(ctx context.Context,
value_attribution_bucket_name ValueAttribution_BucketName_Field) (
count int64, err error) {
var tx *Tx
if tx, err = rx.getTx(ctx); err != nil {
return
}
return tx.Delete_ValueAttribution_By_BucketName(ctx, value_attribution_bucket_name)
}
func (rx *Rx) Find_AccountingTimestamps_Value_By_Name(ctx context.Context,
accounting_timestamps_name AccountingTimestamps_Name_Field) (
row *Value_Row, err error) {
@ -12769,6 +13004,16 @@ func (rx *Rx) Get_User_By_Id(ctx context.Context,
return tx.Get_User_By_Id(ctx, user_id)
}
func (rx *Rx) Get_ValueAttribution_By_BucketName(ctx context.Context,
value_attribution_bucket_name ValueAttribution_BucketName_Field) (
value_attribution *ValueAttribution, err error) {
var tx *Tx
if tx, err = rx.getTx(ctx); err != nil {
return
}
return tx.Get_ValueAttribution_By_BucketName(ctx, value_attribution_bucket_name)
}
func (rx *Rx) Limited_BucketUsage_By_BucketId_And_RollupEndTime_Greater_And_RollupEndTime_LessOrEqual_OrderBy_Asc_RollupEndTime(ctx context.Context,
bucket_usage_bucket_id BucketUsage_BucketId_Field,
bucket_usage_rollup_end_time_greater BucketUsage_RollupEndTime_Field,
@ -13188,6 +13433,12 @@ type Methods interface {
user_payment_customer_id UserPayment_CustomerId_Field) (
user_payment *UserPayment, err error)
Create_ValueAttribution(ctx context.Context,
value_attribution_project_id ValueAttribution_ProjectId_Field,
value_attribution_bucket_name ValueAttribution_BucketName_Field,
value_attribution_partner_id ValueAttribution_PartnerId_Field) (
value_attribution *ValueAttribution, err error)
Delete_AccountingRollup_By_Id(ctx context.Context,
accounting_rollup_id AccountingRollup_Id_Field) (
deleted bool, err error)
@ -13241,6 +13492,10 @@ type Methods interface {
user_id User_Id_Field) (
deleted bool, err error)
Delete_ValueAttribution_By_BucketName(ctx context.Context,
value_attribution_bucket_name ValueAttribution_BucketName_Field) (
count int64, err error)
Find_AccountingTimestamps_Value_By_Name(ctx context.Context,
accounting_timestamps_name AccountingTimestamps_Name_Field) (
row *Value_Row, err error)
@ -13351,6 +13606,10 @@ type Methods interface {
user_id User_Id_Field) (
user *User, err error)
Get_ValueAttribution_By_BucketName(ctx context.Context,
value_attribution_bucket_name ValueAttribution_BucketName_Field) (
value_attribution *ValueAttribution, err error)
Limited_BucketUsage_By_BucketId_And_RollupEndTime_Greater_And_RollupEndTime_LessOrEqual_OrderBy_Asc_RollupEndTime(ctx context.Context,
bucket_usage_bucket_id BucketUsage_BucketId_Field,
bucket_usage_rollup_end_time_greater BucketUsage_RollupEndTime_Field,

View File

@ -22,6 +22,7 @@ import (
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
"storj.io/storj/pkg/valueattribution"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/marketing"
@ -653,10 +654,10 @@ func (m *lockedOffers) Create(ctx context.Context, offer *marketing.NewOffer) (*
return m.db.Create(ctx, offer)
}
func (m *lockedOffers) Finish(ctx context.Context, offerId int) error {
func (m *lockedOffers) Finish(ctx context.Context, offerID int) error {
m.Lock()
defer m.Unlock()
return m.db.Finish(ctx, offerId)
return m.db.Finish(ctx, offerID)
}
func (m *lockedOffers) GetCurrentByType(ctx context.Context, offerType marketing.OfferType) (*marketing.Offer, error) {
@ -671,10 +672,10 @@ func (m *lockedOffers) ListAll(ctx context.Context) ([]marketing.Offer, error) {
return m.db.ListAll(ctx)
}
func (m *lockedOffers) Redeem(ctx context.Context, offerId int) error {
func (m *lockedOffers) Redeem(ctx context.Context, offerID int) error {
m.Lock()
defer m.Unlock()
return m.db.Redeem(ctx, offerId)
return m.db.Redeem(ctx, offerID)
}
// Orders returns database for orders
@ -1007,3 +1008,30 @@ func (m *lockedStoragenodeAccounting) SaveTallies(ctx context.Context, latestTal
defer m.Unlock()
return m.db.SaveTallies(ctx, latestTally, nodeData)
}
// ValueAttribution returns database for partner keys information
func (m *locked) ValueAttribution() valueattribution.DB {
m.Lock()
defer m.Unlock()
return &lockedValueAttribution{m.Locker, m.db.ValueAttribution()}
}
// lockedValueAttribution implements locking wrapper for valueattribution.DB
type lockedValueAttribution struct {
sync.Locker
db valueattribution.DB
}
// Get retrieves partner id using bucket name
func (m *lockedValueAttribution) Get(ctx context.Context, buckname []byte) (*valueattribution.PartnerInfo, error) {
m.Lock()
defer m.Unlock()
return m.db.Get(ctx, buckname)
}
// Insert creates and stores new ConnectorKeyInfo
func (m *lockedValueAttribution) Insert(ctx context.Context, info *valueattribution.PartnerInfo) (*valueattribution.PartnerInfo, error) {
m.Lock()
defer m.Unlock()
return m.db.Insert(ctx, info)
}

View File

@ -0,0 +1,48 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"storj.io/storj/pkg/valueattribution"
dbx "storj.io/storj/satellite/satellitedb/dbx"
)
type valueattributionDB struct {
db *dbx.DB
}
// Get reads the partner info
func (keys *valueattributionDB) Get(ctx context.Context, bucketname []byte) (info *valueattribution.PartnerInfo, err error) {
defer mon.Task()(&ctx)(&err)
dbxInfo, err := keys.db.Get_ValueAttribution_By_BucketName(ctx, dbx.ValueAttribution_BucketName(bucketname))
if err != nil {
return nil, err
}
return &valueattribution.PartnerInfo{
PartnerID: dbxInfo.PartnerId,
BucketName: dbxInfo.BucketName,
CreatedAt: dbxInfo.LastUpdated,
}, nil
}
// Insert implements create partner info
func (keys *valueattributionDB) Insert(ctx context.Context, partnerinfo *valueattribution.PartnerInfo) (info *valueattribution.PartnerInfo, err error) {
defer mon.Task()(&ctx)(&err)
dbxInfo, err := keys.db.Create_ValueAttribution(ctx, dbx.ValueAttribution_ProjectId(partnerinfo.BucketName[:16]),
dbx.ValueAttribution_BucketName(partnerinfo.BucketName), dbx.ValueAttribution_PartnerId(partnerinfo.PartnerID))
if err != nil {
return nil, err
}
return &valueattribution.PartnerInfo{
PartnerID: dbxInfo.PartnerId,
BucketName: dbxInfo.BucketName,
CreatedAt: dbxInfo.LastUpdated,
}, nil
}