{"record":{"id":"69bdd91793d51ba9","repo":"cayleygraph/cayley","slug":"trying-to-add-to-map-bucket-with-invalid-key-v","errorCode":null,"errorMessage":"trying to add to map bucket with invalid key: %v","messagePattern":"trying to add to map bucket with invalid key: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graph/kv/indexing.go","lineNumber":956,"sourceCode":"\t\t\t\tboff++\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif x < b[boff] {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tif x == b[boff] {\n\t\t\t\tc = append(c, x)\n\t\t\t\tboff++\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t}\n\treturn c\n}\n\nfunc (qs *QuadStore) addToMapBucket(tx kv.Tx, key kv.Key, value uint64) error {\n\tif len(key) != 2 {\n\t\treturn fmt.Errorf(\"trying to add to map bucket with invalid key: %v\", key)\n\t}\n\tb, k := key[0], key[1]\n\tif len(k) == 0 {\n\t\treturn fmt.Errorf(\"trying to add to map bucket %s with key 0\", b)\n\t}\n\tif qs.mapBucket == nil {\n\t\tqs.mapBucket = make(map[string]map[string][]uint64)\n\t}\n\tbucket := string(b)\n\tm, ok := qs.mapBucket[bucket]\n\tif !ok {\n\t\tm = make(map[string][]uint64)\n\t\tqs.mapBucket[bucket] = m\n\t}\n\tm[string(k)] = append(m[string(k)], value)\n\tmIndexWriteBufferEntries.WithLabelValues(bucket).Inc()\n\treturn nil\n}","sourceCodeStart":938,"sourceCodeEnd":974,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/graph/kv/indexing.go#L938-L974","documentation":"This error means addToMapBucket was called with a key that does not have exactly two elements (bucket name + in-bucket key). The KV index writer requires a 2-part kv.Key to locate the map bucket; any other length indicates a caller bug in index construction.","triggerScenarios":"indexLink builds a malformed 2-level key (e.g. appending only the bucket prefix without the value key, or concatenating parts into a single-element key) and calls qs.addToMapBucket(tx, key, value).","commonSituations":"Custom or patched index definitions in the KV quadstore, refactors of indexLink/key-building helpers that changed the key arity, or swapping backends where key layouts differ.","solutions":["Inspect the caller (indexLink) and ensure it always builds the key as kv.Key{bucket, valueKey} with exactly 2 elements.","Verify any custom index definitions produce a bucket prefix plus a non-empty value key.","Re-run against upstream cayley code; this is an internal invariant, so update patched code if it diverges."],"exampleFix":"// before\nkey := kv.Key{qs.indexBucket(dirs)}\nreturn qs.addToMapBucket(tx, key, id)\n// after\nkey := kv.Key{qs.indexBucket(dirs), valueKey}\nreturn qs.addToMapBucket(tx, key, id)","handlingStrategy":"validation","validationCode":"func validMapKey(key kv.Key) bool { return len(key) == 2 && len(key[1]) > 0 }\nif !validMapKey(key) { return fmt.Errorf(\"bad map bucket key: %v\", key) }","typeGuard":"func isTwoPartKey(k kv.Key) bool { return len(k) == 2 }","tryCatchPattern":null,"preventionTips":["Always build index keys via a single helper that returns kv.Key{bucket, valueKey}.","Add a unit test asserting key arity for every index definition.","Treat addToMapBucket errors during indexing as fatal bugs, not recoverable conditions."],"tags":["kv-store","indexing","internal-invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}