Update updates an item in the stack without changing its position.
(id uint64, newValue []byte)
| 189 | |
| 190 | // Update updates an item in the stack without changing its position. |
| 191 | func (s *Stack) Update(id uint64, newValue []byte) (*Item, error) { |
| 192 | s.Lock() |
| 193 | defer s.Unlock() |
| 194 | |
| 195 | // Check if stack is closed. |
| 196 | if !s.isOpen { |
| 197 | return nil, ErrDBClosed |
| 198 | } |
| 199 | |
| 200 | // Check if item exists in stack. |
| 201 | if id > s.head || id <= s.tail { |
| 202 | return nil, ErrOutOfBounds |
| 203 | } |
| 204 | |
| 205 | // Create new Item. |
| 206 | item := &Item{ |
| 207 | ID: id, |
| 208 | Key: idToKey(id), |
| 209 | Value: newValue, |
| 210 | } |
| 211 | |
| 212 | // Update this item in the stack. |
| 213 | if err := s.db.Put(item.Key, item.Value, nil); err != nil { |
| 214 | return nil, err |
| 215 | } |
| 216 | |
| 217 | return item, nil |
| 218 | } |
| 219 | |
| 220 | // UpdateString is a helper function for Update that accepts a value |
| 221 | // as a string rather than a byte slice. |