file.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright 2020 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package z
  17. import (
  18. "encoding/binary"
  19. "fmt"
  20. "io"
  21. "os"
  22. "path/filepath"
  23. "github.com/pkg/errors"
  24. )
  25. // MmapFile represents an mmapd file and includes both the buffer to the data
  26. // and the file descriptor.
  27. type MmapFile struct {
  28. Data []byte
  29. Fd *os.File
  30. }
  31. var NewFile = errors.New("Create a new file")
  32. func OpenMmapFileUsing(fd *os.File, sz int, writable bool) (*MmapFile, error) {
  33. filename := fd.Name()
  34. fi, err := fd.Stat()
  35. if err != nil {
  36. return nil, errors.Wrapf(err, "cannot stat file: %s", filename)
  37. }
  38. var rerr error
  39. fileSize := fi.Size()
  40. if sz > 0 && fileSize == 0 {
  41. // If file is empty, truncate it to sz.
  42. if err := fd.Truncate(int64(sz)); err != nil {
  43. return nil, errors.Wrapf(err, "error while truncation")
  44. }
  45. fileSize = int64(sz)
  46. rerr = NewFile
  47. }
  48. // fmt.Printf("Mmaping file: %s with writable: %v filesize: %d\n", fd.Name(), writable, fileSize)
  49. buf, err := Mmap(fd, writable, fileSize) // Mmap up to file size.
  50. if err != nil {
  51. return nil, errors.Wrapf(err, "while mmapping %s with size: %d", fd.Name(), fileSize)
  52. }
  53. if fileSize == 0 {
  54. dir, _ := filepath.Split(filename)
  55. go SyncDir(dir)
  56. }
  57. return &MmapFile{
  58. Data: buf,
  59. Fd: fd,
  60. }, rerr
  61. }
  62. // OpenMmapFile opens an existing file or creates a new file. If the file is
  63. // created, it would truncate the file to maxSz. In both cases, it would mmap
  64. // the file to maxSz and returned it. In case the file is created, z.NewFile is
  65. // returned.
  66. func OpenMmapFile(filename string, flag int, maxSz int) (*MmapFile, error) {
  67. // fmt.Printf("opening file %s with flag: %v\n", filename, flag)
  68. fd, err := os.OpenFile(filename, flag, 0666)
  69. if err != nil {
  70. return nil, errors.Wrapf(err, "unable to open: %s", filename)
  71. }
  72. writable := true
  73. if flag == os.O_RDONLY {
  74. writable = false
  75. }
  76. return OpenMmapFileUsing(fd, maxSz, writable)
  77. }
  78. type mmapReader struct {
  79. Data []byte
  80. offset int
  81. }
  82. func (mr *mmapReader) Read(buf []byte) (int, error) {
  83. if mr.offset > len(mr.Data) {
  84. return 0, io.EOF
  85. }
  86. n := copy(buf, mr.Data[mr.offset:])
  87. mr.offset += n
  88. if n < len(buf) {
  89. return n, io.EOF
  90. }
  91. return n, nil
  92. }
  93. func (m *MmapFile) NewReader(offset int) io.Reader {
  94. return &mmapReader{
  95. Data: m.Data,
  96. offset: offset,
  97. }
  98. }
  99. // Bytes returns data starting from offset off of size sz. If there's not enough data, it would
  100. // return nil slice and io.EOF.
  101. func (m *MmapFile) Bytes(off, sz int) ([]byte, error) {
  102. if len(m.Data[off:]) < sz {
  103. return nil, io.EOF
  104. }
  105. return m.Data[off : off+sz], nil
  106. }
  107. // Slice returns the slice at the given offset.
  108. func (m *MmapFile) Slice(offset int) []byte {
  109. sz := binary.BigEndian.Uint32(m.Data[offset:])
  110. start := offset + 4
  111. next := start + int(sz)
  112. if next > len(m.Data) {
  113. return []byte{}
  114. }
  115. res := m.Data[start:next]
  116. return res
  117. }
  118. // AllocateSlice allocates a slice of the given size at the given offset.
  119. func (m *MmapFile) AllocateSlice(sz, offset int) ([]byte, int, error) {
  120. start := offset + 4
  121. // If the file is too small, double its size or increase it by 1GB, whichever is smaller.
  122. if start+sz > len(m.Data) {
  123. const oneGB = 1 << 30
  124. growBy := len(m.Data)
  125. if growBy > oneGB {
  126. growBy = oneGB
  127. }
  128. if growBy < sz+4 {
  129. growBy = sz + 4
  130. }
  131. if err := m.Truncate(int64(len(m.Data) + growBy)); err != nil {
  132. return nil, 0, err
  133. }
  134. }
  135. binary.BigEndian.PutUint32(m.Data[offset:], uint32(sz))
  136. return m.Data[start : start+sz], start + sz, nil
  137. }
  138. func (m *MmapFile) Sync() error {
  139. if m == nil {
  140. return nil
  141. }
  142. return Msync(m.Data)
  143. }
  144. func (m *MmapFile) Delete() error {
  145. // Badger can set the m.Data directly, without setting any Fd. In that case, this should be a
  146. // NOOP.
  147. if m.Fd == nil {
  148. return nil
  149. }
  150. if err := Munmap(m.Data); err != nil {
  151. return fmt.Errorf("while munmap file: %s, error: %v\n", m.Fd.Name(), err)
  152. }
  153. m.Data = nil
  154. if err := m.Fd.Truncate(0); err != nil {
  155. return fmt.Errorf("while truncate file: %s, error: %v\n", m.Fd.Name(), err)
  156. }
  157. if err := m.Fd.Close(); err != nil {
  158. return fmt.Errorf("while close file: %s, error: %v\n", m.Fd.Name(), err)
  159. }
  160. return os.Remove(m.Fd.Name())
  161. }
  162. // Close would close the file. It would also truncate the file if maxSz >= 0.
  163. func (m *MmapFile) Close(maxSz int64) error {
  164. // Badger can set the m.Data directly, without setting any Fd. In that case, this should be a
  165. // NOOP.
  166. if m.Fd == nil {
  167. return nil
  168. }
  169. if err := m.Sync(); err != nil {
  170. return fmt.Errorf("while sync file: %s, error: %v\n", m.Fd.Name(), err)
  171. }
  172. if err := Munmap(m.Data); err != nil {
  173. return fmt.Errorf("while munmap file: %s, error: %v\n", m.Fd.Name(), err)
  174. }
  175. if maxSz >= 0 {
  176. if err := m.Fd.Truncate(maxSz); err != nil {
  177. return fmt.Errorf("while truncate file: %s, error: %v\n", m.Fd.Name(), err)
  178. }
  179. }
  180. return m.Fd.Close()
  181. }
  182. func SyncDir(dir string) error {
  183. df, err := os.Open(dir)
  184. if err != nil {
  185. return errors.Wrapf(err, "while opening %s", dir)
  186. }
  187. if err := df.Sync(); err != nil {
  188. return errors.Wrapf(err, "while syncing %s", dir)
  189. }
  190. if err := df.Close(); err != nil {
  191. return errors.Wrapf(err, "while closing %s", dir)
  192. }
  193. return nil
  194. }