static khint_t mrb_hash_ht_hash_func(mrb_state *mrb, mrb_value key) { char type = mrb_type(key); mrb_value s1 = mrb_str_new(mrb, &type, 1); mrb_value s2 = mrb_inspect(mrb, key); s1 = mrb_str_cat(mrb, s1, RSTRING_PTR(s2), RSTRING_LEN(s2)); return kh_str_hash_func(mrb, RSTRING_PTR(s1)); }
XR _xr_sym_alloc_n(const char *str, size_t len) { size_t size = len + sizeof('\0'); struct XRSymbol *sym = xr_alloc(sizeof(struct XRSymbol) + size*sizeof(char)); memcpy(sym->chars, str, len); sym->chars[len] = '\0'; sym->len = len; sym->alloc = size; /* FIXME: change to reflect use of khash */ sym->hash = kh_str_hash_func(str); /* Just allocing, symbol not necessarily interned */ sym->interned = 0; sym->mt[-1] = symbol_vt; return (XR) sym; }
int findWorker(char* clientIdStr) { khint_t hash = kh_str_hash_func(clientIdStr); // Do a crypto hash on the client int worker = hash % WORKERS; // Use the hash value to determine which worker they'll be on }