Commit f6962dc8 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

refactor: removed a database query when saving revocation update

parent 661dc7ad
...@@ -135,7 +135,7 @@ func (rs *RevocationStorage) EnableRevocation(typ CredentialTypeIdentifier, sk * ...@@ -135,7 +135,7 @@ func (rs *RevocationStorage) EnableRevocation(typ CredentialTypeIdentifier, sk *
return err return err
} }
if err = rs.AddUpdate(typ, update); err != nil { if err = rs.addUpdate(rs.db, typ, update, true); err != nil {
return err return err
} }
return nil return nil
...@@ -212,10 +212,10 @@ func (*RevocationStorage) newUpdate(acc *revocation.SignedAccumulator, events [] ...@@ -212,10 +212,10 @@ func (*RevocationStorage) newUpdate(acc *revocation.SignedAccumulator, events []
} }
func (rs *RevocationStorage) AddUpdate(typ CredentialTypeIdentifier, record *revocation.Update) error { func (rs *RevocationStorage) AddUpdate(typ CredentialTypeIdentifier, record *revocation.Update) error {
return rs.addUpdate(rs.db, typ, record) return rs.addUpdate(rs.db, typ, record, false)
} }
func (rs *RevocationStorage) addUpdate(tx revStorage, typ CredentialTypeIdentifier, update *revocation.Update) error { func (rs *RevocationStorage) addUpdate(tx revStorage, typ CredentialTypeIdentifier, update *revocation.Update, create bool) error {
// Unmarshal and verify the record against the appropriate public key // Unmarshal and verify the record against the appropriate public key
pk, err := rs.Keys.PublicKey(typ.IssuerIdentifier(), update.SignedAccumulator.PKIndex) pk, err := rs.Keys.PublicKey(typ.IssuerIdentifier(), update.SignedAccumulator.PKIndex)
if err != nil { if err != nil {
...@@ -227,7 +227,11 @@ func (rs *RevocationStorage) addUpdate(tx revStorage, typ CredentialTypeIdentifi ...@@ -227,7 +227,11 @@ func (rs *RevocationStorage) addUpdate(tx revStorage, typ CredentialTypeIdentifi
// Save record // Save record
if rs.sqlMode { if rs.sqlMode {
if err = tx.Upsert(&AccumulatorRecord{SignedAccumulator: update.SignedAccumulator, CredType: typ}); err != nil { save := tx.Save
if create {
save = tx.Insert
}
if err = save(&AccumulatorRecord{SignedAccumulator: update.SignedAccumulator, CredType: typ}); err != nil {
return err return err
} }
for _, event := range update.Events { for _, event := range update.Events {
...@@ -307,7 +311,7 @@ func (rs *RevocationStorage) revokeAttr(tx revStorage, typ CredentialTypeIdentif ...@@ -307,7 +311,7 @@ func (rs *RevocationStorage) revokeAttr(tx revStorage, typ CredentialTypeIdentif
if err != nil { if err != nil {
return err return err
} }
if err = rs.addUpdate(tx, typ, update); err != nil { if err = rs.addUpdate(tx, typ, update, false); err != nil {
return err return err
} }
return nil return nil
......
...@@ -21,7 +21,6 @@ type ( ...@@ -21,7 +21,6 @@ type (
Insert(o interface{}) error Insert(o interface{}) error
// Save an existing record. // Save an existing record.
Save(o interface{}) error Save(o interface{}) error
Upsert(o interface{}) error
// Last deserializes the last record into o. // Last deserializes the last record into o.
Last(typ CredentialTypeIdentifier, o interface{}) error Last(typ CredentialTypeIdentifier, o interface{}) error
// Exists checks whether records exist satisfying col = key. // Exists checks whether records exist satisfying col = key.
...@@ -182,16 +181,6 @@ func (s sqlRevStorage) Save(o interface{}) error { ...@@ -182,16 +181,6 @@ func (s sqlRevStorage) Save(o interface{}) error {
return s.gorm.Save(o).Error return s.gorm.Save(o).Error
} }
func (s sqlRevStorage) Upsert(o interface{}) error {
var c int
s.gorm.Model(o).Count(&c)
if c == 0 {
return s.Insert(o)
} else {
return s.Save(o)
}
}
func (s sqlRevStorage) Last(typ CredentialTypeIdentifier, o interface{}) error { func (s sqlRevStorage) Last(typ CredentialTypeIdentifier, o interface{}) error {
return s.gorm.Last(o, "cred_type = ?", typ).Error return s.gorm.Last(o, "cred_type = ?", typ).Error
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment