{"record":{"id":"c58ea350358fa090","repo":"lionsoul2014/ip2region","slug":"seek-to-vector-index-w","errorCode":null,"errorMessage":"seek to vector index: %w","messagePattern":"seek to vector index: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binding/golang/xdb/util.go","lineNumber":237,"sourceCode":"\n\treturn header, nil\n}\n\n// LoadHeaderFromBuff wrap the header info from the content buffer\nfunc LoadHeaderFromBuff(cBuff []byte) (*Header, error) {\n\tif len(cBuff) < HeaderInfoLength {\n\t\treturn nil, fmt.Errorf(\"invalid content buffer: %d bytes, %d expected\", len(cBuff), HeaderInfoLength)\n\t}\n\n\treturn NewHeader(cBuff[0:HeaderInfoLength])\n}\n\n// LoadVectorIndex util function to load the vector index from the specified file handle\nfunc LoadVectorIndex(handle io.ReadSeeker) ([]byte, error) {\n\t// load all the vector index block\n\t_, err := handle.Seek(HeaderInfoLength, 0)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"seek to vector index: %w\", err)\n\t}\n\n\tvar buff = make([]byte, VectorIndexRows*VectorIndexCols*VectorIndexSize)\n\trLen, err := handle.Read(buff)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif rLen != len(buff) {\n\t\treturn nil, fmt.Errorf(\"incomplete read: readed bytes should be %d\", len(buff))\n\t}\n\n\treturn buff, nil\n}\n\n// LoadVectorIndexFromFile load vector index from a specified file path\nfunc LoadVectorIndexFromFile(dbFile string) ([]byte, error) {\n\thandle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/golang/xdb/util.go#L219-L255","documentation":"LoadVectorIndex seeks to offset HeaderInfoLength (right after the header) to read the vector index region. If the Seek call fails, the error is wrapped as 'seek to vector index: %w'. Same root cause family as error 100 but for the vector-index region of the file.","triggerScenarios":"Calling LoadVectorIndex (or LoadVectorIndexFromFile / Searcher setup) with a non-seekable io.ReadSeeker, or a handle whose seek fails due to a closed/invalid descriptor.","commonSituations":"Streaming an xdb over HTTP and passing resp.Body directly; using io.SectionReader incorrectly; passing a bytes.Buffer that was wrapped in an unseekable reader.","solutions":["Supply a real io.ReadSeeker (*os.File, bytes.Reader) for the xdb file.","Load the entire content once with LoadContentFromFile and use a content-based searcher instead of streaming.","Inspect the wrapped cause with errors.Unwrap to identify the underlying seek failure."],"exampleFix":"// before\nvIdx, err := xdb.LoadVectorIndex(pipeReader) // not seekable\n// after\ncBuff, _ := xdb.LoadContentFromFile(\"ip2region.xdb\")\nsearcher, err := xdb.NewWithContent(cBuff)","handlingStrategy":"type-guard","validationCode":"func isSeekable(r io.Reader) bool {\n    _, ok := r.(io.Seeker)\n    return ok\n}","typeGuard":"func asReadSeeker(r io.Reader) (io.ReadSeeker, bool) {\n    s, ok := r.(io.ReadSeeker)\n    return s, ok\n}","tryCatchPattern":"vIndex, err := xdb.LoadVectorIndex(handle)\nif err != nil {\n    if strings.Contains(err.Error(), \"seek to vector index\") {\n        return fmt.Errorf(\"handle is not seekable: %w\", err)\n    }\n    return err\n}","preventionTips":["Ensure the xdb is at least HeaderInfoLength + 262144 bytes before index loading","Use *os.File or bytes.Reader, never pipes/stream bodies","Prefer NewWithContent/LoadContentFromFile over streaming the vector index"],"tags":["golang","io","seek","vector-index","xdb"],"backgroundTag":"seek-failed-nonseekable-stream","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}