Bend.SegmentBlockDecoderRecordOffsetList.sortedWalk C# (CSharp) Method

sortedWalk() public method

public sortedWalk ( ) : RecordUpdate>>.IEnumerable
return RecordUpdate>>.IEnumerable
        public IEnumerable<KeyValuePair<RecordKey, RecordUpdate>> sortedWalk()
        {
            for (int x=0;x<this.number_of_records;x++) {
                SegmentBlockEncoderRecordOffsetList.RecordInfo ri = new SegmentBlockEncoderRecordOffsetList.RecordInfo();
                int position = -(4 + ((number_of_records-x) * Util.structSize(ref ri)));
                Console.WriteLine("pos : " + position);
                this.datastream.Seek(position,  SeekOrigin.End);
                ri = Util.readStruct<SegmentBlockEncoderRecordOffsetList.RecordInfo>(this.datastream);

                this.datastream.Seek(ri.record_start_pos, SeekOrigin.Begin);
                byte[] key_bytes = new byte[ri.key_len];
                this.datastream.Read(key_bytes, 0, ri.key_len);
                byte[] data_bytes = new byte[ri.data_len];
                this.datastream.Read(data_bytes, 0, ri.data_len);

                RecordKey key = new RecordKey(key_bytes);
                RecordUpdate update = RecordUpdate.FromEncodedData(data_bytes);
                yield return new KeyValuePair<RecordKey,RecordUpdate>(key, update);
            }

            // return this.scanForward(ScanRange<RecordKey>.All());
        }

Usage Example

        public void T01_RecordOffsetList_sortedWalk()
        {
            string[] testvalues = { "test/1", "test/2", "test/3" };
            byte[] databuffer;

            // encode a buffer
            {
                MemoryStream ms = new MemoryStream();
                // add some values to the block encoder
                SegmentBlockEncoderRecordOffsetList enc = new SegmentBlockEncoderRecordOffsetList();
                enc.setStream(ms);
                for (int i = 0; i < testvalues.Length; i++) {
                    RecordKey tkey = new RecordKey().appendParsedKey(testvalues[i]);
                    RecordUpdate tupdate = RecordUpdate.WithPayload("data: " + testvalues[i]);

                    enc.add(tkey, tupdate);
                }
                enc.flush();

                databuffer = ms.ToArray();
            }

            Console.WriteLine("databuffer len : " + databuffer.Length);
            Console.WriteLine("Hex: " + Lsd.ToHexString(databuffer));

            // test sortedWalk
            {
                BlockAccessor rs = new BlockAccessor(databuffer);
                var decoder = new SegmentBlockDecoderRecordOffsetList(rs);
                int count = 0;
                foreach (var row in decoder.sortedWalk()) {
                    Console.WriteLine(row);
                    count++;
                }

                Assert.AreEqual(testvalues.Length, count, "wrong number of elements in sorted walk");
            }
        }