fn insert(&mut self, element: Vec<u8>, priority: u64) {
        // panic if element over max size (~2GB)
        if element.len() > u32::max_value() as usize {
            panic!("Element size {:?} greater than MAX: {:?}", element.len(), u32::max_value());
        }

        let PriorityQueueImpl(kv_store) = self;
        let key = priority.to_be_bytes().to_vec();
        let element_size = (element.len() as u32).to_be_bytes().to_vec();
        // insert first element if store !contains key, or append to byte array
        let val =
            if !kv_store.contains_key(&key) {
                vec![0,0,0,1]
                    .into_iter()
                    .chain(element_size)
                    .chain(element)
                    .collect()
            } else {
                let old_value: Vec<u8> = kv_store.get(&key).unwrap().to_vec();
                let (size, _, elements_slice) = size(&old_value);
                (size + 1).to_be_bytes().to_vec()
                    .into_iter()
                    .chain(elements_slice.to_vec())
                    .chain(element_size)
                    .chain(element)
                    .collect()
            };
        kv_store.insert(key, val);
    }
/// We store elements in a K.V store where K is the priority.
/// The underlying K.V store is implemented as BTreeMap that is ordered on keys (priority).
/// As multiple elements can have the same priority we need to accommodate multiple values (elements) for same key (priority)
///
/// Elements are represented in bytes like this:
/// [elements_size: &[u8; 4], element_1_size: &[u8; 4], element_1 &[u8], element_2_size: &[u8; 4], element_2 &[u8], ...]
///
/// For example:
///   queue.insert(vec![5], 10);
///   queue.insert(vec![6,7], 10);
///   queue.insert(vec![8,9,10], 10);
///
/// Elements with priority 10 would be represented as following:
///   [0,0,0,3,0,0,0,1,5,0,0,0,2,6,7,0,0,0,3,8,9,10]
/// ->[ 3 ELM ][u8; 1][5][u8; 2][6,7][u8; 3][8,9,10]
///
We store elements in a K.V store where K is the priority.
The underlying K.V store is implemented as BTreeMap that is ordered on keys (priority).
As multiple elements can have the same priority we need to accommodate multiple values (elements) for same key (priority)






We store elements in a K . V store where K is the priority . The underlying K . V store is implemented as BTreeMap that is ordered on keys ( priority ) . As multiple elements can have the same priority we need to accommodate multiple values ( elements ) for same key ( priority )
fn insert(&mut self, element: Vec<u8>, priority: u64) {
        // panic if element over max size (~2GB)
        if element.len() > u32::max_value() as usize {
            panic!("Element size {:?} greater than MAX: {:?}", element.len(), u32::max_value());
        }
        let PriorityQueueImpl(kv_store) = self;
        let key = priority.to_be_bytes().to_vec();
        let element_size = (element.len() as u32).to_be_bytes().to_vec();
        // insert first element if store !contains key, or append to byte array
        let val =
            if !kv_store.contains_key(&key) {
                vec![0,0,0,1]
                    .into_iter()
                    .chain(element_size)
                    .chain(element)
                    .collect()
            } else {
                let old_value: Vec<u8> = kv_store.get(&key).unwrap().to_vec();
                let (size, _, elements_slice) = size(&old_value);
                (size + 1).to_be_bytes().to_vec()
                    .into_iter()
                    .chain(elements_slice.to_vec())
                    .chain(element_size)
                    .chain(element)
                    .collect()
            };
        kv_store.insert(key, val);
    }
fn insert ( & mut self , element : Vec < u8 > , priority : u64 ) { // panic if element over max size (~2GB) if element . len ( ) > u32 :: max_value ( ) as usize { panic ! ( " " element len ( ) u32 max_value ( ) ) ; } let PriorityQueueImpl ( kv_store ) = self ; let key = priority . to_be_bytes ( ) . to_vec ( ) ; let element_size = ( element . len ( ) as u32 ) . to_be_bytes ( ) . to_vec ( ) ; // insert first element if store !contains key, or append to byte array let val = if ! kv_store . contains_key ( & key ) { vec ! [ 0 0 0 1 ] . into_iter ( ) . chain ( element_size ) . chain ( element ) . collect ( ) } else { let old_value : Vec < u8 > = kv_store . get ( & key ) . unwrap ( ) . to_vec ( ) ; let ( size , _ , elements_slice ) = size ( & old_value ) ; ( size + 1 ) . to_be_bytes ( ) . to_vec ( ) . into_iter ( ) . chain ( elements_slice . to_vec ( ) ) . chain ( element_size ) . chain ( element ) . collect ( ) } ; kv_store . insert ( key , val ) ; }
We store elements in a K.V store where K is the priority.
We store elements in a K . V store where K is the priority .
&mut self
element
Vec<u8>
priority
u64
&mut self
element
Vec<u8>
priority
u64