Push adds an item to the stack.
(value []byte)
| 56 | |
| 57 | // Push adds an item to the stack. |
| 58 | func (s *Stack) Push(value []byte) (*Item, error) { |
| 59 | s.Lock() |
| 60 | defer s.Unlock() |
| 61 | |
| 62 | // Check if stack is closed. |
| 63 | if !s.isOpen { |
| 64 | return nil, ErrDBClosed |
| 65 | } |
| 66 | |
| 67 | // Create new Item. |
| 68 | item := &Item{ |
| 69 | ID: s.head + 1, |
| 70 | Key: idToKey(s.head + 1), |
| 71 | Value: value, |
| 72 | } |
| 73 | |
| 74 | // Add it to the stack. |
| 75 | if err := s.db.Put(item.Key, item.Value, nil); err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | |
| 79 | // Increment head position. |
| 80 | s.head++ |
| 81 | |
| 82 | return item, nil |
| 83 | } |
| 84 | |
| 85 | // PushString is a helper function for Push that accepts a |
| 86 | // value as a string rather than a byte slice. |