fn do_fetch(
        iter: &mut Peekable<&mut DBIterator>,
        value_key: &str,
        mut key: Box<[u8]>,
        mut value: Box<[u8]>,
    ) -> JsonValue {
        if key.len() == value_key.len() {
            // we have a key match!
            return JsonFetcher::bytes_to_json_value(value.as_ref());
        }
        let segment = {
            let key_str = unsafe { str::from_utf8_unchecked(&key) };
            let remaining = &key_str[value_key.len()..];
            KeyBuilder::parse_first_kp_value_segment(remaining)
        };

        match segment {
            Some((Segment::ObjectKey(mut unescaped), escaped)) => {
                let mut object: Vec<(String, JsonValue)> = Vec::new();

                let mut value_key_next = value_key.to_string() + &escaped;
                loop {
                    let json_val = JsonFetcher::do_fetch(iter, &value_key_next, key, value);
                    object.push((unescaped, json_val));

                    let segment = match iter.peek() {
                        Some(&(ref k, ref _v)) => {
                            let key = unsafe { str::from_utf8_unchecked(k) };
                            if !KeyBuilder::is_kp_value_key_prefix(value_key, key) {
                                return JsonValue::Object(object);
                            }

                            let key_str = unsafe { str::from_utf8_unchecked(k) };
                            let remaining = &key_str[value_key.len()..];

                            KeyBuilder::parse_first_kp_value_segment(remaining)
                        }
                        None => return JsonValue::Object(object),
                    };

                    if let Some((Segment::ObjectKey(unescaped2), escaped2)) = segment {
                        unescaped = unescaped2;
                        // advance the peeked iter
                        match iter.next() {
                            Some((k, v)) => {
                                key = k;
                                value = v;
                            }
                            None => panic!("couldn't advanced already peeked iter"),
                        };
                        value_key_next.truncate(value_key.len());
                        value_key_next.push_str(&escaped2);
                    } else {
                        return JsonValue::Object(object);
                    }
                }
            }
            Some((Segment::Array(mut i), escaped)) => {
                // we use a tuple with ordinal because we encounter
                // elements in lexical sorting order instead of ordinal order
                let mut array: Vec<(u64, JsonValue)> = Vec::new();

                let mut value_key_next = value_key.to_string() + &escaped;
                loop {
                    let json_val = JsonFetcher::do_fetch(iter, &value_key_next, key, value);
                    array.push((i, json_val));

                    let segment = match iter.peek() {
                        Some(&(ref k, ref _v)) => {
                            let key = unsafe { str::from_utf8_unchecked(k) };
                            if !KeyBuilder::is_kp_value_key_prefix(value_key, key) {
                                return JsonFetcher::return_array(array);
                            }

                            let key_str = unsafe { str::from_utf8_unchecked(k) };
                            let remaining = &key_str[value_key.len()..];

                            KeyBuilder::parse_first_kp_value_segment(remaining)
                        }
                        None => return JsonFetcher::return_array(array),
                    };

                    if let Some((Segment::Array(i2), escaped2)) = segment {
                        i = i2;
                        // advance the already peeked iter
                        match iter.next() {
                            Some((k, v)) => {
                                key = k;
                                value = v;
                            }
                            None => panic!("couldn't advanced already peeked iter"),
                        };
                        value_key_next.truncate(value_key.len());
                        value_key_next.push_str(&escaped2);
                    } else {
                        return JsonFetcher::return_array(array);
                    }
                }
            }
            None => {
                let key_str = unsafe { str::from_utf8_unchecked(&key) };
                panic!(
                    "somehow couldn't parse key segment {} {}",
                    value_key, key_str
                );
            }
        }
    }
/// When do_fetch is called it means we know we are going to find a value because
/// we prefix matched the keypath. What we are doing here is parsing the remaining
/// keypath to figure out the nested structure of the remaining keypath. So we
/// depth first recursively parse the keypath and return the value and inserting into
/// containers (arrays or objects) then iterate keys until the keypath no longer matches.
When do_fetch is called it means we know we are going to find a value because
we prefix matched the keypath. What we are doing here is parsing the remaining
keypath to figure out the nested structure of the remaining keypath. So we
depth first recursively parse the keypath and return the value and inserting into
containers (arrays or objects) then iterate keys until the keypath no longer matches.
When do_fetch is called it means we know we are going to find a value because we prefix matched the keypath . What we are doing here is parsing the remaining keypath to figure out the nested structure of the remaining keypath . So we depth first recursively parse the keypath and return the value and inserting into containers ( arrays or objects ) then iterate keys until the keypath no longer matches .
fn do_fetch(
        iter: &mut Peekable<&mut DBIterator>,
        value_key: &str,
        mut key: Box<[u8]>,
        mut value: Box<[u8]>,
    ) -> JsonValue {
        if key.len() == value_key.len() {
            // we have a key match!
            return JsonFetcher::bytes_to_json_value(value.as_ref());
        }
        let segment = {
            let key_str = unsafe { str::from_utf8_unchecked(&key) };
            let remaining = &key_str[value_key.len()..];
            KeyBuilder::parse_first_kp_value_segment(remaining)
        };
        match segment {
            Some((Segment::ObjectKey(mut unescaped), escaped)) => {
                let mut object: Vec<(String, JsonValue)> = Vec::new();
                let mut value_key_next = value_key.to_string() + &escaped;
                loop {
                    let json_val = JsonFetcher::do_fetch(iter, &value_key_next, key, value);
                    object.push((unescaped, json_val));
                    let segment = match iter.peek() {
                        Some(&(ref k, ref _v)) => {
                            let key = unsafe { str::from_utf8_unchecked(k) };
                            if !KeyBuilder::is_kp_value_key_prefix(value_key, key) {
                                return JsonValue::Object(object);
                            }
                            let key_str = unsafe { str::from_utf8_unchecked(k) };
                            let remaining = &key_str[value_key.len()..];
                            KeyBuilder::parse_first_kp_value_segment(remaining)
                        }
                        None => return JsonValue::Object(object),
                    };
                    if let Some((Segment::ObjectKey(unescaped2), escaped2)) = segment {
                        unescaped = unescaped2;
                        // advance the peeked iter
                        match iter.next() {
                            Some((k, v)) => {
                                key = k;
                                value = v;
                            }
                            None => panic!("couldn't advanced already peeked iter"),
                        };
                        value_key_next.truncate(value_key.len());
                        value_key_next.push_str(&escaped2);
                    } else {
                        return JsonValue::Object(object);
                    }
                }
            }
            Some((Segment::Array(mut i), escaped)) => {
                // we use a tuple with ordinal because we encounter
                // elements in lexical sorting order instead of ordinal order
                let mut array: Vec<(u64, JsonValue)> = Vec::new();
                let mut value_key_next = value_key.to_string() + &escaped;
                loop {
                    let json_val = JsonFetcher::do_fetch(iter, &value_key_next, key, value);
                    array.push((i, json_val));
                    let segment = match iter.peek() {
                        Some(&(ref k, ref _v)) => {
                            let key = unsafe { str::from_utf8_unchecked(k) };
                            if !KeyBuilder::is_kp_value_key_prefix(value_key, key) {
                                return JsonFetcher::return_array(array);
                            }
                            let key_str = unsafe { str::from_utf8_unchecked(k) };
                            let remaining = &key_str[value_key.len()..];
                            KeyBuilder::parse_first_kp_value_segment(remaining)
                        }
                        None => return JsonFetcher::return_array(array),
                    };
                    if let Some((Segment::Array(i2), escaped2)) = segment {
                        i = i2;
                        // advance the already peeked iter
                        match iter.next() {
                            Some((k, v)) => {
                                key = k;
                                value = v;
                            }
                            None => panic!("couldn't advanced already peeked iter"),
                        };
                        value_key_next.truncate(value_key.len());
                        value_key_next.push_str(&escaped2);
                    } else {
                        return JsonFetcher::return_array(array);
                    }
                }
            }
            None => {
                let key_str = unsafe { str::from_utf8_unchecked(&key) };
                panic!(
                    "somehow couldn't parse key segment {} {}",
                    value_key, key_str
                );
            }
        }
    }
fn do_fetch ( iter : & mut Peekable < & mut DBIterator > , value_key : & str , mut key : Box < [ u8 ] > , mut value : Box < [ u8 ] > , ) -> JsonValue { if key . len ( ) == value_key . len ( ) { // we have a key match! return JsonFetcher :: bytes_to_json_value ( value . as_ref ( ) ) ; } let segment = { let key_str = unsafe { str :: from_utf8_unchecked ( & key ) } ; let remaining = & key_str [ value_key . len ( ) .. ] ; KeyBuilder :: parse_first_kp_value_segment ( remaining ) } ; match segment { Some ( ( Segment :: ObjectKey ( mut unescaped ) , escaped ) ) => { let mut object : Vec < ( String , JsonValue ) > = Vec :: new ( ) ; let mut value_key_next = value_key . to_string ( ) + & escaped ; loop { let json_val = JsonFetcher :: do_fetch ( iter , & value_key_next , key , value ) ; object . push ( ( unescaped , json_val ) ) ; let segment = match iter . peek ( ) { Some ( & ( ref k , ref _v ) ) => { let key = unsafe { str :: from_utf8_unchecked ( k ) } ; if ! KeyBuilder :: is_kp_value_key_prefix ( value_key , key ) { return JsonValue :: Object ( object ) ; } let key_str = unsafe { str :: from_utf8_unchecked ( k ) } ; let remaining = & key_str [ value_key . len ( ) .. ] ; KeyBuilder :: parse_first_kp_value_segment ( remaining ) } None => return JsonValue :: Object ( object ) , } ; if let Some ( ( Segment :: ObjectKey ( unescaped2 ) , escaped2 ) ) = segment { unescaped = unescaped2 ; // advance the peeked iter match iter . next ( ) { Some ( ( k , v ) ) => { key = k ; value = v ; } None => panic ! ( " " ) , } ; value_key_next . truncate ( value_key . len ( ) ) ; value_key_next . push_str ( & escaped2 ) ; } else { return JsonValue :: Object ( object ) ; } } } Some ( ( Segment :: Array ( mut i ) , escaped ) ) => { // we use a tuple with ordinal because we encounter // elements in lexical sorting order instead of ordinal order let mut array : Vec < ( u64 , JsonValue ) > = Vec :: new ( ) ; let mut value_key_next = value_key . to_string ( ) + & escaped ; loop { let json_val = JsonFetcher :: do_fetch ( iter , & value_key_next , key , value ) ; array . push ( ( i , json_val ) ) ; let segment = match iter . peek ( ) { Some ( & ( ref k , ref _v ) ) => { let key = unsafe { str :: from_utf8_unchecked ( k ) } ; if ! KeyBuilder :: is_kp_value_key_prefix ( value_key , key ) { return JsonFetcher :: return_array ( array ) ; } let key_str = unsafe { str :: from_utf8_unchecked ( k ) } ; let remaining = & key_str [ value_key . len ( ) .. ] ; KeyBuilder :: parse_first_kp_value_segment ( remaining ) } None => return JsonFetcher :: return_array ( array ) , } ; if let Some ( ( Segment :: Array ( i2 ) , escaped2 ) ) = segment { i = i2 ; // advance the already peeked iter match iter . next ( ) { Some ( ( k , v ) ) => { key = k ; value = v ; } None => panic ! ( " " ) , } ; value_key_next . truncate ( value_key . len ( ) ) ; value_key_next . push_str ( & escaped2 ) ; } else { return JsonFetcher :: return_array ( array ) ; } } } None => { let key_str = unsafe { str :: from_utf8_unchecked ( & key ) } ; panic ! ( " " value_key key_str ) ; } } }
When do_fetch is called it means we know we are going to find a value because
we prefix matched the keypath.
When do_fetch is called it means we know we are going to find a value because we prefix matched the keypath .
iter
&mut Peekable<&mut DBIterator>
value_key
&str
key
Box<[u8]>
value
Box<[u8]>
iter
&mut Peekable<&mut DBIterator>
value_key
&str
key
Box<[u8]>
value
Box<[u8]>