edits
[iotcloud.git] / version2 / src / C / SlotBuffer.h
1
2 /**
3  * Circular buffer that holds the live set of slots.
4  * @author Brian Demsky
5  * @version 1.0
6  */
7
8 class SlotBuffer {
9         static final int DEFAULT_SIZE = 16;
10
11         private Slot[] array;
12         private int head;
13         private int tail;
14         public int64_t oldestseqn;
15
16         SlotBuffer() {
17                 array = new Slot[DEFAULT_SIZE + 1];
18                 head = tail = 0;
19                 oldestseqn = 0;
20         }
21
22         int size() {
23                 if (head >= tail)
24                         return head - tail;
25                 return (array.length + head) - tail;
26         }
27
28         int capacity() {
29                 return array.length - 1;
30         }
31
32         void resize(int newsize) {
33                 if (newsize == (array.length - 1))
34                         return;
35
36                 Slot[] newarray = new Slot[newsize + 1];
37                 int currsize = size();
38                 int index = tail;
39                 for (int i = 0; i < currsize; i++) {
40                         newarray[i] = array[index];
41                         if ((++index) == array.length)
42                                 index = 0;
43                 }
44                 array = newarray;
45                 tail = 0;
46                 head = currsize;
47         }
48
49         private void incrementHead() {
50                 head++;
51                 if (head >= array.length)
52                         head = 0;
53         }
54
55         private void incrementTail() {
56                 tail++;
57                 if (tail >= array.length)
58                         tail = 0;
59         }
60
61         void putSlot(Slot s) {
62
63                 int64_t checkNum = (getNewestSeqNum() + 1);
64
65                 if (checkNum != s.getSequenceNumber()) {
66                         // We have a gap so expunge all our slots
67                         oldestseqn = s.getSequenceNumber();
68                         tail = 0;
69                         head = 1;
70                         array[0] = s;
71                         return;
72                 }
73
74                 array[head] = s;
75                 incrementHead();
76
77                 if (oldestseqn == 0) {
78                         oldestseqn = s.getSequenceNumber();
79                 }
80
81                 if (head == tail) {
82                         incrementTail();
83                         oldestseqn++;
84                 }
85         }
86
87         Slot getSlot(int64_t seqnum) {
88                 int diff = (int) (seqnum - oldestseqn);
89                 int index = diff + tail;
90
91                 if (index < 0) {
92                         // Really old message so we dont have it anymore
93                         return NULL;
94                 }
95
96                 if (index >= array.length) {
97                         if (head >= tail) {
98                                 return NULL;
99                         }
100                         index -= array.length;
101                 }
102
103                 if (index >= array.length) {
104
105                         return NULL;
106                 }
107                 if (head >= tail && index >= head) {
108                         return NULL;
109                 }
110
111                 return array[index];
112         }
113
114         int64_t getOldestSeqNum() {
115                 return oldestseqn;
116         }
117
118         int64_t getNewestSeqNum() {
119                 return oldestseqn + size() - 1;
120         }
121 }