|
| 1 | +package rowcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/binary" |
| 6 | + "io" |
| 7 | + "io/ioutil" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/vertica/vertica-sql-go/msgs" |
| 11 | +) |
| 12 | + |
| 13 | +// FileCache stores rows from the wire and puts excess into a temporary file |
| 14 | +type FileCache struct { |
| 15 | + maxInMemory int |
| 16 | + rowCount int |
| 17 | + readIdx int |
| 18 | + resultData []*msgs.BEDataRowMsg |
| 19 | + file *os.File |
| 20 | + rwbuffer *bufio.ReadWriter |
| 21 | + scratch [512]byte |
| 22 | +} |
| 23 | + |
| 24 | +// NewFileCache returns a file cache with a set row limit |
| 25 | +func NewFileCache(rowLimit int) (*FileCache, error) { |
| 26 | + file, err := ioutil.TempFile("", ".vertica-sql-go.*.dat") |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + return &FileCache{ |
| 31 | + maxInMemory: rowLimit, |
| 32 | + resultData: make([]*msgs.BEDataRowMsg, 0, rowLimit), |
| 33 | + file: file, |
| 34 | + rwbuffer: bufio.NewReadWriter(bufio.NewReader(file), bufio.NewWriterSize(file, 1<<16)), |
| 35 | + }, nil |
| 36 | +} |
| 37 | + |
| 38 | +func (f *FileCache) writeCached(msg *msgs.BEDataRowMsg) { |
| 39 | + sizeBuf := f.scratch[:4] |
| 40 | + binary.LittleEndian.PutUint32(sizeBuf, uint32(len(*msg))) |
| 41 | + f.rwbuffer.Write(sizeBuf) |
| 42 | + f.rwbuffer.Write(*msg) |
| 43 | +} |
| 44 | + |
| 45 | +// AddRow adds a row to the cache |
| 46 | +func (f *FileCache) AddRow(msg *msgs.BEDataRowMsg) { |
| 47 | + f.rowCount++ |
| 48 | + if len(f.resultData) >= f.maxInMemory { |
| 49 | + f.writeCached(msg) |
| 50 | + return |
| 51 | + } |
| 52 | + f.resultData = append(f.resultData, msg) |
| 53 | +} |
| 54 | + |
| 55 | +// Finalize signals the end of rows from the wire and readies the cache for reading |
| 56 | +func (f *FileCache) Finalize() error { |
| 57 | + var err error |
| 58 | + name := f.file.Name() |
| 59 | + f.rwbuffer.Flush() |
| 60 | + f.file.Close() |
| 61 | + f.file, err = os.OpenFile(name, os.O_RDONLY|os.O_EXCL, 0600) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + f.rwbuffer = bufio.NewReadWriter(bufio.NewReader(f.file), bufio.NewWriter(f.file)) |
| 66 | + return err |
| 67 | +} |
| 68 | + |
| 69 | +// GetRow pulls a row message out of the cache, returning nil of none remain |
| 70 | +func (f *FileCache) GetRow() *msgs.BEDataRowMsg { |
| 71 | + if f.readIdx >= len(f.resultData) { |
| 72 | + if !f.reloadFromCache() { |
| 73 | + return nil |
| 74 | + } |
| 75 | + } |
| 76 | + result := f.resultData[f.readIdx] |
| 77 | + f.readIdx++ |
| 78 | + return result |
| 79 | +} |
| 80 | + |
| 81 | +// Peek returns the next row without changing the state |
| 82 | +func (f *FileCache) Peek() *msgs.BEDataRowMsg { |
| 83 | + if len(f.resultData) == 0 { |
| 84 | + return nil |
| 85 | + } |
| 86 | + return f.resultData[0] |
| 87 | +} |
| 88 | + |
| 89 | +// Close clears resources associated with the cache, deleting the temp file |
| 90 | +func (f *FileCache) Close() error { |
| 91 | + name := f.file.Name() |
| 92 | + f.rwbuffer.Flush() |
| 93 | + f.file.Close() |
| 94 | + return os.Remove(name) |
| 95 | +} |
| 96 | + |
| 97 | +func (f *FileCache) reloadFromCache() bool { |
| 98 | + hadData := false |
| 99 | + |
| 100 | + f.readIdx = 0 |
| 101 | + indexCount := 0 |
| 102 | + |
| 103 | + for { |
| 104 | + sizeBuf := f.scratch[:4] |
| 105 | + |
| 106 | + if _, err := io.ReadFull(f.rwbuffer, sizeBuf); err != nil { |
| 107 | + if err == io.EOF { |
| 108 | + if indexCount == 0 { |
| 109 | + return false |
| 110 | + } |
| 111 | + f.resultData = f.resultData[0:indexCount] |
| 112 | + return true |
| 113 | + } |
| 114 | + return false |
| 115 | + } |
| 116 | + |
| 117 | + rowDataSize := binary.LittleEndian.Uint32(sizeBuf) |
| 118 | + |
| 119 | + var rowBuf []byte |
| 120 | + rowBytes := f.scratch[4:] |
| 121 | + if rowDataSize <= uint32(len(rowBytes)) { |
| 122 | + rowBuf = rowBytes[:rowDataSize] |
| 123 | + } else { |
| 124 | + rowBuf = make([]byte, rowDataSize) |
| 125 | + } |
| 126 | + if _, err := io.ReadFull(f.rwbuffer, rowBuf); err != nil { |
| 127 | + return false |
| 128 | + } |
| 129 | + |
| 130 | + msgBuf := msgs.NewMsgBufferFromBytes(rowBuf) |
| 131 | + |
| 132 | + drm := &msgs.BEDataRowMsg{} |
| 133 | + |
| 134 | + msg, _ := drm.CreateFromMsgBody(msgBuf) |
| 135 | + |
| 136 | + f.resultData[indexCount] = msg.(*msgs.BEDataRowMsg) |
| 137 | + indexCount++ |
| 138 | + |
| 139 | + hadData = true |
| 140 | + |
| 141 | + // If we've reached the original capacity of the slice, we're done. |
| 142 | + if indexCount == len(f.resultData) { |
| 143 | + break |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return hadData |
| 148 | +} |
0 commit comments