Some scenarios use multidimensional hash to store data, which is simple and brutal. Here's a note.
The following is a three-dimensional hash of the simple, the recommended number of layers not too much, otherwise the time is long, their own code is not known.
is a three-dimensional hash in memory storage form, HashMap nested 3 layers. An array of arrays holds the hashcode and the node pointers of the linked nodes, each of which holds an array, and the array holds the next layer of hashcode and the next level of linked list pointers. (sorry that the hash bucket list is not fully drawn, the focus of this figure is hash nesting)
This data structure I was used to resolve the configuration of different devices, each device has two configuration files. When the new data of the device (Base64 encrypted JSON serialization string), through the three-dimensional hash layer parsing, return to the user's intuitive appearance. This is not about business, but about how violence is stored and how it can be violently solved.
/***************************************************************************** structure Name: StringStu function Description: Device string file data Structure *****************************************************************************/type StringStu struct {Cat_name String ' JSON: ' Cat_name,omitempty ' ' Dev_name string ' json: ' Dev_name,omitempty ' ' Pid string ' json: ' P Id,omitempty "' Error interface{} ' JSON:" Error,omitempty "' Intfs interface{} ' json: ' Intfs,omitempty ' vendor_name String ' JSON: "Vendor_name,omitempty" '}/************************************************************************ Structure Name: Product string file related processing function Description:* ****************************************************************************/fun C getallproductstring () (Slice []profilesuidsstu) {stringstu: = Readstringfile () for _, Stringone: = Range Stringstu {LoadD Evstrings2cache (Stringone.pid, LIBF. Structtojsonstringone (STRINGONE.INTFS))}return}func readstringfile () []stringstu {var file string = "" var Stringslice = MakE ([]stringstu, 0) file = libf. Keyfilet (file). GetFileName ("Strings.json") Byte, Error: = Ioutil. ReadFile (file) if Error! = nil {prnlog.logprint (LIBF. Log_error, 0, False, False, "", "ReadFile error", error) return nil}msg, ret: = LIBF. Parsejsonbystructmsg (Byte, &stringslice) return stringslice}/************************************************ Structure Name: Stringintfsstu function Description: Device string file, device Intfs property ************************************ /type stringsintfsstu struct {Name string ' JSON: ' Name,omitempty ' The Chinese name of the field, for example: power, Background light unit string ' JSON: "Unit,omitempty" '//unit, e.g. temperature °cvalues map[string]string ' JSON: "Values,o Mitempty "'///field value corresponding to the Chinese enumeration, for example: 0 is off, 1 is open}type paramattr = Map[string]stringsintfsstuvar productstrings = Make (Map[string] ( paramattr), 0)/****************************************************************************** \author pxx* \date The 2018/08/25* \brief loads the strings configuration of all the devices into a three-dimensional hash. One Vihachi is proDuctstrings,key is PID, the two-dimensional hash is paramattr,key for the field English name, the three-dimensional hash is the value of the Values,key field in the STRINGINTFSSTU structure * \param[in]* \param[out]* \ return* \ingroup* \remarks******************************************************************************/func Loaddevstrings2cache (PID, string string) {var (keyliststring map[string]interface{} = Make (map[string]interface{}, 0) _ Param_attr = Make (paramattr, 0)) productstrings[pid] = _param_attrmsg, ret: = LIBF. Parsejsonbystructmsg ([]byte (String), &keyliststring) for key, value: = Range keyliststring {var string1 stringsintf Sstu//Note scope msg, ret = LIBF. Parsejsonbystructmsg ([]byte (LIBF. Structtojsonstringone (value)), &string1) if ret! = LIBF. Success {prnlog.logprint (LIBF. Log_debug, 0, False, False, "Msg=%v,ret=%v", MSG, ret) Continue}_param_attr[key] = string1}}
The above code is rude to put all the device's configuration file into memory, the following is based on this three-dimensional hash to parse the data.
/****************************************************************************** \author pxx* \date 2018/08/27* \brief JSON serialized unstructured data, mapped to structured data * \param[in]* \param[out]* \return* \ingroup* \remarks Open Source code Description: https://www.jb51.net/ Article/73996.htm******************************************************************************/func Formatuploaddata (StringData string) (Structdata structuploaddatastu) {structdata.did = Gojson. Json (StringData). Get ("did"). Tostring () Structdata.pid = Gojson. Json (StringData). Get ("pid"). Tostring () for k, V: = Range Productprofile[structdata.pid] {//k is the field name, and V is the sub-property of this field value: = Gojson. Json (StringData). Get (k). Tostring ()//based on the different properties of each field of the device, map the corresponding Chinese name, enumeration value, string value and integer value switch v.in[0] {Case 1, 3://1 represents an enumeration, 2 is a continuous type parameter, 3 Stringstructdata.data + = Productstrings[structdata.pid][k]. Name + ":" + productstrings[structdata.pid][k]. Values[value] + "," case 2:structdata.data + = Productstrings[structdata.pid][k]. Name + ":" + Value + "," Default:}}return}