2
0

file_string_table.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package opencost
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. util "github.com/opencost/opencost/core/pkg/util"
  7. )
  8. // fileStringRef maps a bingen string-table index to a payload stored in a temp file.
  9. type fileStringRef struct {
  10. off int64
  11. length int
  12. }
  13. // FileStringTable holds string-table payloads on disk and resolves indices on demand.
  14. type FileStringTable struct {
  15. f *os.File
  16. refs []fileStringRef
  17. }
  18. // NewFileStringTableFromBuffer reads exactly tl length-prefixed (uint16) string payloads from buff
  19. // and appends each payload to a new temp file. It does not retain full strings in memory.
  20. func NewFileStringTableFromBuffer(buff *util.Buffer, tl int) (*FileStringTable, error) {
  21. os.MkdirAll("/var/lib/clickhouse/tmp", 0755)
  22. f, err := os.CreateTemp("/var/lib/clickhouse/tmp", "opencost-bgst-*")
  23. if err != nil {
  24. return nil, fmt.Errorf("opencost: create string table file: %w", err)
  25. }
  26. t := &FileStringTable{f: f, refs: make([]fileStringRef, tl)}
  27. var writeErr error
  28. defer func() {
  29. if writeErr != nil {
  30. _ = t.Close()
  31. }
  32. }()
  33. for i := 0; i < tl; i++ {
  34. payload, err := buff.ReadStringBytes()
  35. if err != nil {
  36. writeErr = err
  37. return nil, fmt.Errorf("opencost: read string table entry %d: %w", i, err)
  38. }
  39. var off int64
  40. if len(payload) > 0 {
  41. off, err = f.Seek(0, io.SeekEnd)
  42. if err != nil {
  43. writeErr = err
  44. return nil, fmt.Errorf("opencost: seek string table file: %w", err)
  45. }
  46. if _, err := f.Write(payload); err != nil {
  47. writeErr = err
  48. return nil, fmt.Errorf("opencost: write string table entry %d: %w", i, err)
  49. }
  50. }
  51. t.refs[i] = fileStringRef{off: off, length: len(payload)}
  52. }
  53. return t, nil
  54. }
  55. // Len returns the number of strings in the table.
  56. func (t *FileStringTable) Len() int {
  57. if t == nil {
  58. return 0
  59. }
  60. return len(t.refs)
  61. }
  62. // StringAt returns the string for wire index i, reading from the backing file.
  63. func (t *FileStringTable) StringAt(i int) (string, error) {
  64. if t == nil || t.f == nil {
  65. return "", fmt.Errorf("opencost: closed or nil string table")
  66. }
  67. if i < 0 || i >= len(t.refs) {
  68. return "", fmt.Errorf("opencost: string table index %d out of range [0,%d)", i, len(t.refs))
  69. }
  70. ref := t.refs[i]
  71. if ref.length == 0 {
  72. return "", nil
  73. }
  74. buf := make([]byte, ref.length)
  75. n, err := t.f.ReadAt(buf, ref.off)
  76. if err != nil {
  77. return "", err
  78. }
  79. if n != ref.length {
  80. return "", fmt.Errorf("opencost: short read in string table at %d", i)
  81. }
  82. return string(buf), nil
  83. }
  84. // Close releases the backing file (and removes the temp file).
  85. func (t *FileStringTable) Close() error {
  86. if t == nil || t.f == nil {
  87. return nil
  88. }
  89. path := t.f.Name()
  90. err := t.f.Close()
  91. t.f = nil
  92. t.refs = nil
  93. if path != "" {
  94. _ = os.Remove(path)
  95. }
  96. return err
  97. }
  98. func newDecodingContextFromBytes(data []byte) (*DecodingContext, error) {
  99. buff := util.NewBufferFromBytes(data)
  100. if !isBinaryTag(data, BinaryTagStringTable) {
  101. return &DecodingContext{Buffer: buff}, nil
  102. }
  103. buff.ReadBytes(len(BinaryTagStringTable))
  104. tl := buff.ReadInt()
  105. if tl <= 0 {
  106. return &DecodingContext{Buffer: buff}, nil
  107. }
  108. ft, err := NewFileStringTableFromBuffer(buff, tl)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return &DecodingContext{Buffer: buff, FileTable: ft}, nil
  113. }
  114. // IsStringTable returns true if a non-empty string table is present (in-memory and/or file-backed).
  115. func (dc *DecodingContext) IsStringTable() bool {
  116. return dc != nil && (len(dc.Table) > 0 || (dc.FileTable != nil && dc.FileTable.Len() > 0))
  117. }
  118. // CloseFileTable closes and removes the backing file for the string table, if any.
  119. func (dc *DecodingContext) CloseFileTable() {
  120. if dc == nil || dc.FileTable == nil {
  121. return
  122. }
  123. _ = dc.FileTable.Close()
  124. dc.FileTable = nil
  125. }
  126. func (dc *DecodingContext) tableString(i int) string {
  127. if len(dc.Table) > 0 {
  128. if i < 0 || i >= len(dc.Table) {
  129. panic(fmt.Sprintf("opencost: string table index %d out of range [0,%d)", i, len(dc.Table)))
  130. }
  131. return dc.Table[i]
  132. }
  133. if dc.FileTable == nil {
  134. panic(fmt.Sprintf("opencost: string table lookup with no file table (index %d)", i))
  135. }
  136. s, err := dc.FileTable.StringAt(i)
  137. if err != nil {
  138. panic(err)
  139. }
  140. return s
  141. }
  142. func newDecodingContextFromReader(reader io.Reader) (*DecodingContext, error) {
  143. buff := util.NewBufferFromReader(reader)
  144. if !isReaderBinaryTag(buff, BinaryTagStringTable) {
  145. return &DecodingContext{Buffer: buff}, nil
  146. }
  147. buff.ReadBytes(len(BinaryTagStringTable))
  148. tl := buff.ReadInt()
  149. if tl <= 0 {
  150. return &DecodingContext{Buffer: buff}, nil
  151. }
  152. ft, err := NewFileStringTableFromBuffer(buff, tl)
  153. if err != nil {
  154. return nil, err
  155. }
  156. return &DecodingContext{Buffer: buff, FileTable: ft}, nil
  157. }