You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
9.6 KiB

12 years ago
12 years ago
12 years ago
  1. package storage
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "log"
  8. "os"
  9. "path"
  10. "sync"
  11. )
  12. const (
  13. SuperBlockSize = 8
  14. )
  15. type SuperBlock struct {
  16. Version Version
  17. ReplicaType ReplicationType
  18. }
  19. func (s *SuperBlock) Bytes() []byte {
  20. header := make([]byte, SuperBlockSize)
  21. header[0] = byte(s.Version)
  22. header[1] = s.ReplicaType.Byte()
  23. return header
  24. }
  25. type Volume struct {
  26. Id VolumeId
  27. dir string
  28. dataFile *os.File
  29. nm NeedleMapper
  30. readOnly bool
  31. SuperBlock
  32. accessLock sync.Mutex
  33. }
  34. func NewVolume(dirname string, id VolumeId, replicationType ReplicationType) (v *Volume, e error) {
  35. v = &Volume{dir: dirname, Id: id}
  36. v.SuperBlock = SuperBlock{ReplicaType: replicationType}
  37. e = v.load(true)
  38. return
  39. }
  40. func LoadVolumeOnly(dirname string, id VolumeId) (v *Volume, e error) {
  41. v = &Volume{dir: dirname, Id: id}
  42. v.SuperBlock = SuperBlock{ReplicaType: CopyNil}
  43. e = v.load(false)
  44. return
  45. }
  46. func (v *Volume) load(alsoLoadIndex bool) error {
  47. var e error
  48. fileName := path.Join(v.dir, v.Id.String())
  49. v.dataFile, e = os.OpenFile(fileName+".dat", os.O_RDWR|os.O_CREATE, 0644)
  50. if e != nil {
  51. if !os.IsPermission(e) {
  52. return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
  53. }
  54. if v.dataFile, e = os.Open(fileName + ".dat"); e != nil {
  55. return fmt.Errorf("cannot open Volume Data %s.dat: %s", fileName, e)
  56. }
  57. log.Printf("opening " + fileName + ".dat in READONLY mode")
  58. v.readOnly = true
  59. }
  60. if v.ReplicaType == CopyNil {
  61. e = v.readSuperBlock()
  62. } else {
  63. e = v.maybeWriteSuperBlock()
  64. }
  65. if e == nil && alsoLoadIndex {
  66. var indexFile *os.File
  67. if v.readOnly {
  68. if indexFile, e = os.Open(fileName + ".idx"); e != nil && !os.IsNotExist(e) {
  69. return fmt.Errorf("cannot open index file %s.idx: %s", fileName, e)
  70. }
  71. if indexFile != nil {
  72. log.Printf("converting %s.idx to %s.cdb", fileName, fileName)
  73. if e = ConvertIndexToCdb(fileName+".cdb", indexFile); e != nil {
  74. log.Printf("error converting %s.idx to %s.cdb: %s", fileName, fileName)
  75. } else {
  76. indexFile.Close()
  77. os.Remove(indexFile.Name())
  78. indexFile = nil
  79. }
  80. }
  81. v.nm, e = OpenCdbMap(fileName + ".cdb")
  82. return e
  83. } else {
  84. indexFile, e = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644)
  85. if e != nil {
  86. return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
  87. }
  88. }
  89. v.nm, e = LoadNeedleMap(indexFile)
  90. }
  91. return e
  92. }
  93. func (v *Volume) Version() Version {
  94. return v.SuperBlock.Version
  95. }
  96. func (v *Volume) Size() int64 {
  97. v.accessLock.Lock()
  98. defer v.accessLock.Unlock()
  99. stat, e := v.dataFile.Stat()
  100. if e == nil {
  101. return stat.Size()
  102. }
  103. log.Printf("Failed to read file size %s %s\n", v.dataFile.Name(), e.Error())
  104. return -1
  105. }
  106. func (v *Volume) Close() {
  107. v.accessLock.Lock()
  108. defer v.accessLock.Unlock()
  109. v.nm.Close()
  110. _ = v.dataFile.Close()
  111. }
  112. func (v *Volume) maybeWriteSuperBlock() error {
  113. stat, e := v.dataFile.Stat()
  114. if e != nil {
  115. log.Printf("failed to stat datafile %s: %s", v.dataFile, e)
  116. return e
  117. }
  118. if stat.Size() == 0 {
  119. v.SuperBlock.Version = CurrentVersion
  120. _, e = v.dataFile.Write(v.SuperBlock.Bytes())
  121. if e != nil && os.IsPermission(e) {
  122. //read-only, but zero length - recreate it!
  123. if v.dataFile, e = os.Create(v.dataFile.Name()); e == nil {
  124. if _, e = v.dataFile.Write(v.SuperBlock.Bytes()); e == nil {
  125. v.readOnly = false
  126. }
  127. }
  128. }
  129. }
  130. return e
  131. }
  132. func (v *Volume) readSuperBlock() (err error) {
  133. if _, err = v.dataFile.Seek(0, 0); err != nil {
  134. return fmt.Errorf("cannot seek to the beginning of %s: %s", v.dataFile, err)
  135. }
  136. header := make([]byte, SuperBlockSize)
  137. if _, e := v.dataFile.Read(header); e != nil {
  138. return fmt.Errorf("cannot read superblock: %s", e)
  139. }
  140. v.SuperBlock, err = ParseSuperBlock(header)
  141. return err
  142. }
  143. func ParseSuperBlock(header []byte) (superBlock SuperBlock, err error) {
  144. superBlock.Version = Version(header[0])
  145. if superBlock.ReplicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
  146. err = fmt.Errorf("cannot read replica type: %s", err)
  147. }
  148. return
  149. }
  150. func (v *Volume) NeedToReplicate() bool {
  151. return v.ReplicaType.GetCopyCount() > 1
  152. }
  153. func (v *Volume) isFileUnchanged(n *Needle) bool {
  154. nv, ok := v.nm.Get(n.Id)
  155. if ok && nv.Offset > 0 {
  156. if _, err := v.dataFile.Seek(int64(nv.Offset)*NeedlePaddingSize, 0); err != nil {
  157. return false
  158. }
  159. oldNeedle := new(Needle)
  160. oldNeedle.Read(v.dataFile, nv.Size, v.Version())
  161. if oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
  162. n.Size = oldNeedle.Size
  163. return true
  164. }
  165. }
  166. return false
  167. }
  168. func (v *Volume) write(n *Needle) (size uint32, err error) {
  169. if v.readOnly {
  170. err = fmt.Errorf("%s is read-only", v.dataFile)
  171. return
  172. }
  173. v.accessLock.Lock()
  174. defer v.accessLock.Unlock()
  175. if v.isFileUnchanged(n) {
  176. size = n.Size
  177. return
  178. }
  179. var offset int64
  180. if offset, err = v.dataFile.Seek(0, 2); err != nil {
  181. return
  182. }
  183. //ensure file writing starting from aligned positions
  184. if offset%NeedlePaddingSize != 0 {
  185. offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
  186. if offset, err = v.dataFile.Seek(offset, 0); err != nil {
  187. return
  188. }
  189. }
  190. if size, err = n.Append(v.dataFile, v.Version()); err != nil {
  191. if e := v.dataFile.Truncate(offset); e != nil {
  192. err = fmt.Errorf("%s\ncannot truncate %s: %s", err, v.dataFile, e)
  193. }
  194. return
  195. }
  196. nv, ok := v.nm.Get(n.Id)
  197. if !ok || int64(nv.Offset)*NeedlePaddingSize < offset {
  198. _, err = v.nm.Put(n.Id, uint32(offset/NeedlePaddingSize), n.Size)
  199. }
  200. return
  201. }
  202. func (v *Volume) delete(n *Needle) (uint32, error) {
  203. if v.readOnly {
  204. return 0, fmt.Errorf("%s is read-only", v.dataFile)
  205. }
  206. v.accessLock.Lock()
  207. defer v.accessLock.Unlock()
  208. nv, ok := v.nm.Get(n.Id)
  209. //fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
  210. if ok {
  211. size := nv.Size
  212. if err:= v.nm.Delete(n.Id); err != nil {
  213. return size, err
  214. }
  215. if _, err:= v.dataFile.Seek(0, 2); err != nil {
  216. return size, err
  217. }
  218. n.Data = make([]byte, 0)
  219. _, err := n.Append(v.dataFile, v.Version())
  220. return size, err
  221. }
  222. return 0, nil
  223. }
  224. func (v *Volume) read(n *Needle) (int, error) {
  225. v.accessLock.Lock()
  226. defer v.accessLock.Unlock()
  227. nv, ok := v.nm.Get(n.Id)
  228. if ok && nv.Offset > 0 {
  229. if _, err := v.dataFile.Seek(int64(nv.Offset)*NeedlePaddingSize, 0); err != nil {
  230. return -1, err
  231. }
  232. return n.Read(v.dataFile, nv.Size, v.Version())
  233. }
  234. return -1, errors.New("Not Found")
  235. }
  236. func (v *Volume) garbageLevel() float64 {
  237. return float64(v.nm.DeletedSize()) / float64(v.ContentSize())
  238. }
  239. func (v *Volume) compact() error {
  240. v.accessLock.Lock()
  241. defer v.accessLock.Unlock()
  242. filePath := path.Join(v.dir, v.Id.String())
  243. return v.copyDataAndGenerateIndexFile(filePath+".cpd", filePath+".cpx")
  244. }
  245. func (v *Volume) commitCompact() error {
  246. v.accessLock.Lock()
  247. defer v.accessLock.Unlock()
  248. _ = v.dataFile.Close()
  249. var e error
  250. if e = os.Rename(path.Join(v.dir, v.Id.String()+".cpd"), path.Join(v.dir, v.Id.String()+".dat")); e != nil {
  251. return e
  252. }
  253. if e = os.Rename(path.Join(v.dir, v.Id.String()+".cpx"), path.Join(v.dir, v.Id.String()+".idx")); e != nil {
  254. return e
  255. }
  256. if e = v.load(true); e != nil {
  257. return e
  258. }
  259. return nil
  260. }
  261. func (v *Volume) freeze() error {
  262. if v.readOnly {
  263. return nil
  264. }
  265. nm, ok := v.nm.(*NeedleMap)
  266. if !ok {
  267. return nil
  268. }
  269. v.accessLock.Lock()
  270. defer v.accessLock.Unlock()
  271. bn, _ := nakeFilename(v.dataFile.Name())
  272. cdbFn := bn + ".cdb"
  273. log.Printf("converting %s to %s", nm.indexFile.Name(), cdbFn)
  274. err := DumpNeedleMapToCdb(cdbFn, nm)
  275. if err != nil {
  276. return err
  277. }
  278. if v.nm, err = OpenCdbMap(cdbFn); err != nil {
  279. return err
  280. }
  281. nm.indexFile.Close()
  282. os.Remove(nm.indexFile.Name())
  283. v.readOnly = true
  284. return nil
  285. }
  286. func ScanVolumeFile(dirname string, id VolumeId,
  287. visitSuperBlock func(SuperBlock) error,
  288. visitNeedle func(n *Needle, offset uint32) error) (err error) {
  289. var v *Volume
  290. if v, err = LoadVolumeOnly(dirname, id); err != nil {
  291. return
  292. }
  293. if err = visitSuperBlock(v.SuperBlock); err != nil {
  294. return
  295. }
  296. version := v.Version()
  297. offset := uint32(SuperBlockSize)
  298. n, rest, e := ReadNeedleHeader(v.dataFile, version)
  299. if e != nil {
  300. err = fmt.Errorf("cannot read needle header: %s", e)
  301. return
  302. }
  303. for n != nil {
  304. if err = n.ReadNeedleBody(v.dataFile, version, rest); err != nil {
  305. err = fmt.Errorf("cannot read needle body: %s", err)
  306. return
  307. }
  308. if err = visitNeedle(n, offset); err != nil {
  309. return
  310. }
  311. offset += NeedleHeaderSize + rest
  312. if n, rest, err = ReadNeedleHeader(v.dataFile, version); err != nil {
  313. if err == io.EOF {
  314. return nil
  315. }
  316. return fmt.Errorf("cannot read needle header: %s", err)
  317. }
  318. }
  319. return
  320. }
  321. func (v *Volume) copyDataAndGenerateIndexFile(dstName, idxName string) (err error) {
  322. var (
  323. dst, idx *os.File
  324. )
  325. if dst, err = os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644); err != nil {
  326. return
  327. }
  328. defer dst.Close()
  329. if idx, err = os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE, 0644); err != nil {
  330. return
  331. }
  332. defer idx.Close()
  333. nm := NewNeedleMap(idx)
  334. new_offset := uint32(SuperBlockSize)
  335. err = ScanVolumeFile(v.dir, v.Id, func(superBlock SuperBlock) error {
  336. _, err = dst.Write(superBlock.Bytes())
  337. return err
  338. }, func(n *Needle, offset uint32) error {
  339. nv, ok := v.nm.Get(n.Id)
  340. //log.Println("file size is", n.Size, "rest", rest)
  341. if ok && nv.Offset*NeedlePaddingSize == offset {
  342. if nv.Size > 0 {
  343. if _, err = nm.Put(n.Id, new_offset/NeedlePaddingSize, n.Size); err != nil {
  344. return fmt.Errorf("cannot put needle: %s", err)
  345. }
  346. if _, err = n.Append(dst, v.Version()); err != nil {
  347. return fmt.Errorf("cannot append needle: %s", err)
  348. }
  349. new_offset += n.DiskSize()
  350. //log.Println("saving key", n.Id, "volume offset", old_offset, "=>", new_offset, "data_size", n.Size, "rest", rest)
  351. }
  352. }
  353. return nil
  354. })
  355. return
  356. }
  357. func (v *Volume) ContentSize() uint64 {
  358. return v.nm.ContentSize()
  359. }