Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions rdb/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,28 @@ func (s *Storage) SetStudyDirection(studyID int, direction goptuna.StudyDirectio

// SetStudyUserAttr to store the value for the user.
func (s *Storage) SetStudyUserAttr(studyID int, key string, value string) error {
return s.db.Create(&studyUserAttributeModel{
var result studyUserAttributeModel
return s.db.Where(&studyUserAttributeModel{
UserAttributeReferStudy: studyID,
Key: key,
}).Assign(&studyUserAttributeModel{
UserAttributeReferStudy: studyID,
Key: key,
ValueJSON: encodeAttrValue(value),
}).Error
}).FirstOrCreate(&result).Error
}

// SetStudySystemAttr to store the value for the system.
func (s *Storage) SetStudySystemAttr(studyID int, key string, value string) error {
return s.db.Create(&studySystemAttributeModel{
var result studySystemAttributeModel
return s.db.Where(&studySystemAttributeModel{
SystemAttributeReferStudy: studyID,
Key: key,
}).Assign(&studySystemAttributeModel{
SystemAttributeReferStudy: studyID,
Key: key,
ValueJSON: encodeAttrValue(value),
}).Error
}).FirstOrCreate(&result).Error
}

// GetStudyIDFromName return the study id from study name.
Expand Down Expand Up @@ -550,20 +558,28 @@ func (s *Storage) SetTrialState(trialID int, state goptuna.TrialState) error {

// SetTrialUserAttr to store the value for the user.
func (s *Storage) SetTrialUserAttr(trialID int, key string, value string) error {
return s.db.Create(&trialUserAttributeModel{
var result trialUserAttributeModel
return s.db.Where(&trialUserAttributeModel{
UserAttributeReferTrial: trialID,
Key: key,
}).Assign(&trialUserAttributeModel{
UserAttributeReferTrial: trialID,
Key: key,
ValueJSON: encodeAttrValue(value),
}).Error
}).FirstOrCreate(&result).Error
}

// SetTrialSystemAttr to store the value for the system.
func (s *Storage) SetTrialSystemAttr(trialID int, key string, value string) error {
return s.db.Create(&trialSystemAttributeModel{
var result trialSystemAttributeModel
return s.db.Where(&trialSystemAttributeModel{
SystemAttributeReferTrial: trialID,
Key: key,
}).Assign(&trialSystemAttributeModel{
SystemAttributeReferTrial: trialID,
Key: key,
ValueJSON: encodeAttrValue(value),
}).Error
}).FirstOrCreate(&result).Error
}

// GetTrialNumberFromID returns the trial's number.
Expand Down
12 changes: 12 additions & 0 deletions rdb/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ func TestStorage_TrialUserAttrs(t *testing.T) {
if !reflect.DeepEqual(got, want) {
t.Errorf("want %#v, but got %#v", want, got)
}

err = storage.SetTrialUserAttr(trialID, "key", "value")
if err != nil {
t.Errorf("error: %v != nil", err)
return
}
}

func TestStorage_TrialSystemAttrs(t *testing.T) {
Expand Down Expand Up @@ -298,6 +304,12 @@ func TestStorage_TrialSystemAttrs(t *testing.T) {
if v, ok := got["key"]; !ok || v != "value" {
t.Errorf("want %#v, but got %v %v", "value", ok, got)
}

err = storage.SetTrialSystemAttr(trialID, "key", "value")
if err != nil {
t.Errorf("error: %v != nil", err)
return
}
}

func TestStorage_GetAllTrials(t *testing.T) {
Expand Down