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