{"record":{"id":"85c93309c2d7802f","repo":"cayleygraph/cayley","slug":"trying-to-add-to-map-bucket-s-with-key-0","errorCode":null,"errorMessage":"trying to add to map bucket %s with key 0","messagePattern":"trying to add to map bucket (.+?) with key 0","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graph/kv/indexing.go","lineNumber":960,"sourceCode":"\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}\n\nfunc (qs *QuadStore) flushMapBucket(ctx context.Context, tx kv.Tx) error {\n\tbs := make([]string, 0, len(qs.mapBucket))\n\tfor k := range qs.mapBucket {","sourceCodeStart":942,"sourceCodeEnd":978,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/graph/kv/indexing.go#L942-L978","documentation":"This error means addToMapBucket received a key whose second element (the in-bucket key) is empty. The map bucket needs a non-empty value key to store the uint64 ID under; an empty key signals an upstream value was not resolved before indexing.","triggerScenarios":"indexLink calls addToMapBucket with a key whose second component is a zero-length byte slice — typically when the node value could not be encoded to bytes (missing/nil value, failed quad.Value key derivation).","commonSituations":"Quads containing nil or unencodable node values, values not yet registered in the value map before indexing, or custom key-encoding functions returning empty bytes.","solutions":["Ensure the node value is resolved to a non-empty byte key (via the value-to-key encoding) before calling addToMapBucket.","Skip/defer indexing of values that cannot be encoded, and log them instead of indexing an empty key.","Check for nil quad.Value entries in incoming quads and reject or normalize them at write time."],"exampleFix":"// before\nk := valueKeyFor(v) // may return []byte{}\nreturn qs.addToMapBucket(tx, kv.Key{bucket, k}, id)\n// after\nk := valueKeyFor(v)\nif len(k) == 0 { return fmt.Errorf(\"cannot index value %v: empty key\", v) }\nreturn qs.addToMapBucket(tx, kv.Key{bucket, k}, id)","handlingStrategy":"validation","validationCode":"if v == nil || len(valueKeyFor(v)) == 0 { skip or normalize before indexing }","typeGuard":"func hasEncodableKey(v quad.Value) bool { return v != nil && len(valueKeyFor(v)) > 0 }","tryCatchPattern":"err := qs.addToMapBucket(tx, key, id)\nif err != nil { return fmt.Errorf(\"indexing value %d: %w\", id, err) }","preventionTips":["Normalize/reject quads with nil or empty node values at ingestion.","Resolve all node values to their stored IDs before writing indexes.","Log unencodable values instead of silently indexing empty keys."],"tags":["kv-store","indexing","empty-key"],"backgroundTag":"empty-required-field","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"}