/** 直接读取一个slice的值,这个slice的值可能是string也可能是数字 */ func getSliceValue(pre uint8, suf uint8, array *[]byte, inputFile string, param *Parameters) string { var returnValue string switch pre { case 0: //then the next 6 bits represent the length reIndex(array, inputFile, uint64(suf), param) returnValue = assembelString(getArraySlice((*param).SliceStart, (*param).SliceEnd, array)) case 64: //then an additional byte is read from the stream. The combined 14 bits represent the length reIndexSliceEnd(array, inputFile, 1, param) slice := getArraySlice((*param).SliceStart, (*param).SliceEnd, array) //把slice里边的两个字节转换成数字 slice_value := changeByteToUint64(slice, binary.BigEndian) value := slice_value & 0x3FFF reIndex(array, inputFile, uint64(value), param) returnValue = assembelString(getArraySlice((*param).SliceStart, (*param).SliceEnd, array)) case 128: //hen the remaining 6 bits are discarded. Additional 4 bytes are read from the stream, and those 4 bytes represent the length // (in big endian format in RDB version 6) var slice_value uint64 reIndex(array, inputFile, 4, param) slice := getArraySlice((*param).SliceStart, (*param).SliceEnd, array) //把slice里边的四个字节转换成数字 slice_value = changeByteToUint64(slice, binary.BigEndian) reIndex(array, inputFile, slice_value, param) returnValue = assembelString(getArraySlice((*param).SliceStart, (*param).SliceEnd, array)) case 192: //then the next object is encoded in a special format. The remaining 6 bits indicate the format switch suf { case 0: //0 indicates that an 8 bit integer follows. reIndex(array, inputFile, 1, param) returnValue = strconv.FormatInt(changeByteToInt64(getArraySlice((*param).SliceStart, (*param).SliceEnd, array), binary.LittleEndian), 10) case 1: //1 indicates that a 16 bit integer follows. reIndex(array, inputFile, 2, param) returnValue = strconv.FormatInt(changeByteToInt64(getArraySlice((*param).SliceStart, (*param).SliceEnd, array), binary.LittleEndian), 10) case 2: //2 indicates that a 32 bit integer follows. reIndex(array, inputFile, 4, param) returnValue = strconv.FormatInt(changeByteToInt64(getArraySlice((*param).SliceStart, (*param).SliceEnd, array), binary.LittleEndian), 10) case 3, 4: //实验证明C3代表压缩的字符串,不是C4 //读取压缩字符串的长度 compressed_length := readOneByteAndGetNum(array, inputFile, param) //读取未压缩字符串的长度 uncompressed_length := readOneByteAndGetNum(array, inputFile, param) //解压缩字符串 reIndex(array, inputFile, compressed_length, param) in_data := (*array)[(*param).SliceStart:(*param).SliceEnd] uncompressedByte := lzf.Lzf_dcompress(in_data, uncompressed_length) str := string(uncompressedByte) returnValue = str } } return returnValue }
/** 读取代表内容的字节,并根据值类型解析 此方法只被:ZipMap Encoding, ZipList Encoding, parseIntSetEncoding调用,因为 他们在压缩的时候是所有值联合压缩 */ func readContentByteAndParse(array *[]byte, types int, inputFile string, param *Parameters) int { var length int var pContentSlice *[]byte //首先读取一个字节,按照这个字节的前两个bit和后六个bit的值来判断 //改如何读取后边的Content内容 slice := readNextNByte(array, inputFile, 1, param) pre, suf := calByte(slice[0]) switch pre { case 0: //then the next 6 bits represent the length contentSlice := readNextNByte(array, inputFile, uint64(suf), param) pContentSlice = &contentSlice length = callParseFunc(pContentSlice, types, param) case 64: //then an additional byte is read from the stream. The combined 14 bits represent the length reIndexSliceEnd(array, inputFile, 1, param) slice := getArraySlice((*param).SliceStart, (*param).SliceEnd, array) //把slice里边的两个字节转换成数字 slice_value := changeByteToUint64(slice, binary.BigEndian) //获取后14个bit代表的数字 value := slice_value & 0x3FFF contentSlice := readNextNByte(array, inputFile, uint64(value), param) pContentSlice = &contentSlice length = callParseFunc(pContentSlice, types, param) case 128: //hen the remaining 6 bits are discarded. Additional 4 bytes are read from the stream, and those 4 bytes represent the length //(in big endian format in RDB version 6) var slice_value uint64 //读取后4个bytes reIndex(array, inputFile, 4, param) slice := getArraySlice((*param).SliceStart, (*param).SliceEnd, array) //把slice里边的四个字节转换成数字 slice_value = changeByteToUint64(slice, binary.BigEndian) //读取这4bytes代表的数字的字节 contentSlice := readNextNByte(array, inputFile, uint64(slice_value), param) pContentSlice = &contentSlice length = callParseFunc(pContentSlice, types, param) case 192: //then the next object is encoded in a special format. The remaining 6 bits indicate the format //在读取代表内容的字节里边,这里应该用不到,除了压缩 switch suf { case 0: //0 indicates that an 8 bit integer follows. reIndex(array, inputFile, 1, param) slice_value := changeByteToInt64(getArraySlice((*param).SliceStart, (*param).SliceEnd, array), binary.LittleEndian) contentSlice := readNextNByte(array, inputFile, uint64(slice_value), param) pContentSlice = &contentSlice length = callParseFunc(pContentSlice, types, param) case 1: //1 indicates that a 16 bit integer follows. reIndex(array, inputFile, 2, param) slice_value := changeByteToInt64(getArraySlice((*param).SliceStart, (*param).SliceEnd, array), binary.LittleEndian) contentSlice := readNextNByte(array, inputFile, uint64(slice_value), param) pContentSlice = &contentSlice length = callParseFunc(pContentSlice, types, param) case 2: //2 indicates that a 32 bit integer follows. reIndex(array, inputFile, 4, param) slice_value := changeByteToInt64(getArraySlice((*param).SliceStart, (*param).SliceEnd, array), binary.LittleEndian) contentSlice := readNextNByte(array, inputFile, uint64(slice_value), param) pContentSlice = &contentSlice length = callParseFunc(pContentSlice, types, param) case 3, 4: //4 indicates that a compressed string follows. //读取压缩字符串的长度 compressed_length := readOneByteAndGetNum(array, inputFile, param) //读取未压缩字符串的长度 uncompressed_length := readOneByteAndGetNum(array, inputFile, param) //解压缩字符串 reIndex(array, inputFile, compressed_length, param) in_data := (*array)[(*param).SliceStart:(*param).SliceEnd] unComByte := lzf.Lzf_dcompress(in_data, uncompressed_length) var pUnComByte *[]byte = &unComByte length = callParseFunc(pUnComByte, types, param) } } return length }