Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
check entry before storing in leveldb
Browse files Browse the repository at this point in the history
  • Loading branch information
farshidtz committed Feb 28, 2020
1 parent 043655d commit 800a46d
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions catalog/ldbstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ func (s *LevelDBStorage) add(td *ThingDescription) error {
return err
}

_, err = s.db.Get([]byte(td.ID), nil)
if err == nil {
return &ConflictError{"Device id is not unique."}
} else if err != leveldb.ErrNotFound {
found, err := s.db.Has([]byte(td.ID), nil)
if err != nil {
return err
}
if found {
return &ConflictError{fmt.Sprintf("%s is not unique", td.ID)}
}

err = s.db.Put([]byte(td.ID), bytes, nil)
if err != nil {
Expand All @@ -60,7 +61,7 @@ func (s *LevelDBStorage) get(id string) (*ThingDescription, error) {

bytes, err := s.db.Get([]byte(id), nil)
if err == leveldb.ErrNotFound {
return nil, &NotFoundError{fmt.Sprintf("Device with id %s is not found", id)}
return nil, &NotFoundError{fmt.Sprintf("%s is not found", id)}
} else if err != nil {
return nil, err
}
Expand All @@ -80,10 +81,17 @@ func (s *LevelDBStorage) update(id string, td *ThingDescription) error {
if err != nil {
return err
}

found, err := s.db.Has([]byte(id), nil)
if err != nil {
return err
}
if !found {
return &NotFoundError{fmt.Sprintf("%s is not found", id)}
}

err = s.db.Put([]byte(id), bytes, nil)
if err == leveldb.ErrNotFound {
return &NotFoundError{fmt.Sprintf("Device with id %s is not found", id)}
} else if err != nil {
if err != nil {
return err
}

Expand All @@ -94,7 +102,7 @@ func (s *LevelDBStorage) delete(id string) error {

err := s.db.Delete([]byte(id), nil)
if err == leveldb.ErrNotFound {
return &NotFoundError{fmt.Sprintf("Device with id %s is not found", id)}
return &NotFoundError{fmt.Sprintf("%s is not found", id)}
} else if err != nil {
return err
}
Expand Down

0 comments on commit 800a46d

Please sign in to comment.