Push copies entry at the end of queue and moves tail pointer. Allocates more space if needed. Returns index for pushed data or error if maximum size queue limit is reached.
(data []byte)
| 86 | // Push copies entry at the end of queue and moves tail pointer. Allocates more space if needed. |
| 87 | // Returns index for pushed data or error if maximum size queue limit is reached. |
| 88 | func (q *BytesQueue) Push(data []byte) (int, error) { |
| 89 | neededSize := getNeededSize(len(data)) |
| 90 | |
| 91 | if !q.canInsertAfterTail(neededSize) { |
| 92 | if q.canInsertBeforeHead(neededSize) { |
| 93 | q.tail = leftMarginIndex |
| 94 | } else if q.capacity+neededSize >= q.maxCapacity && q.maxCapacity > 0 { |
| 95 | return -1, &queueError{"Full queue. Maximum size limit reached."} |
| 96 | } else { |
| 97 | q.allocateAdditionalMemory(neededSize) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | index := q.tail |
| 102 | |
| 103 | q.push(data, neededSize) |
| 104 | |
| 105 | return index, nil |
| 106 | } |
| 107 | |
| 108 | func (q *BytesQueue) allocateAdditionalMemory(minimum int) { |
| 109 | start := time.Now() |