Changes
[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 = 16;
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
37                 Slot[] newarray = new Slot[newsize + 1];
38                 int currsize = size();
39                 int index = tail;
40                 for (int i = 0; i < currsize; i++) {
41                         newarray[i] = array[index];
42                         if ((++index) == array.length)
43                                 index = 0;
44                 }
45                 array = newarray;
46                 tail = 0;
47                 head = currsize;
48         }
49
50         private void incrementHead() {
51                 head++;
52                 if (head >= array.length)
53                         head = 0;
54         }
55
56         private void incrementTail() {
57                 tail++;
58                 if (tail >= array.length)
59                         tail = 0;
60         }
61
62         void putSlot(Slot s) {
63
64                 long checkNum = (getNewestSeqNum() + 1);
65
66                 if (checkNum != s.getSequenceNumber()) {
67                         // We have a gap so expunge all our slots
68                         oldestseqn = s.getSequenceNumber();
69                         tail = 0;
70                         head = 1;
71                         array[0] = s;
72                         return;
73                 }
74
75                 array[head] = s;
76                 incrementHead();
77
78                 if (oldestseqn == 0) {
79                         oldestseqn = s.getSequenceNumber();
80                 }
81
82                 if (head == tail) {
83                         incrementTail();
84                         oldestseqn++;
85                 }
86         }
87
88         Slot getSlot(long seqnum) {
89                 int diff = (int) (seqnum - oldestseqn);
90                 int index = diff + tail;
91
92                 if (index < 0) {
93                         // Really old message so we dont have it anymore
94                         return null;
95                 }
96
97                 if (index >= array.length) {
98                         if (head >= tail) {
99                                 return null;
100                         }
101                         index -= array.length;
102                 }
103
104                 if (index >= array.length) {
105
106                         return null;
107                 }
108                 if (head >= tail && index >= head) {
109                         return null;
110                 }
111
112                 return array[index];
113         }
114
115         long getOldestSeqNum() {
116                 return oldestseqn;
117         }
118
119         long getNewestSeqNum() {
120                 return oldestseqn + size() - 1;
121         }
122 }