Cleaned up git
[iotcloud.git] / version1 / src / js / iotjs / orig / slotindexer.js
diff --git a/version1/src/js/iotjs/orig/slotindexer.js b/version1/src/js/iotjs/orig/slotindexer.js
new file mode 100644 (file)
index 0000000..da738ff
--- /dev/null
@@ -0,0 +1,27 @@
+class SlotIndexer{
+       constructor(_updates,_buffer){
+               // updates is an array of slot objects
+               // buffer is an instanceof slotbuffer constructor object in slotbuffer.js
+               this.updates = _updates;
+               if(_buffer && _buffer instanceof SlotBuffer){
+                       this.buffer = _buffer;
+               }else{
+                       throw new Error("Argument error Buffer should be an instance of SlotBuffer");
+               }
+               this.firstslotseqnum = this.updates[0].getSequenceNumber();     
+       }
+
+       getSlot(seqnum){
+               if(seqnum >= this.firstslotseqnum){
+                       var offset = seqnum - this.firstslotseqnum;
+                       if(offset >= this.updates.length){
+                               throw new Error('Invalid Slot Sequence Number Reference');
+                       }else{
+                               return this.updates[offset];
+                       }
+               }else{
+                       return this.buffer.getSlot(seqnum);
+               }
+       }
+
+}
\ No newline at end of file