Squashed Last bugs
[iotcloud.git] / version2 / src / java / iotcloud / SlotBuffer.java
1 package iotcloud;
2
3 /**
4  * Circular buffer that holds the live set of slots.
5  * @author Brian Demsky
6  * @version 1.0
7  */
8
9 class SlotBuffer {
10         static final int DEFAULT_SIZE = 128;
11
12         private Slot[] array;
13         private int head;
14         private int tail;
15         public long oldestseqn;
16
17         SlotBuffer() {
18                 array = new Slot[DEFAULT_SIZE + 1];
19                 head = tail = 0;
20                 oldestseqn = 0;
21         }
22
23         int size() {
24                 if (head >= tail)
25                         return head - tail;
26                 return (array.length + head) - tail;
27         }
28
29         int capacity() {
30                 return array.length - 1;
31         }
32
33         void resize(int newsize) {
34                 if (newsize == (array.length - 1))
35                         return;
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                 long 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(long 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         long getOldestSeqNum() {
115                 return oldestseqn;
116         }
117
118         long getNewestSeqNum() {
119                 return oldestseqn + size() - 1;
120         }
121 }