context.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package context
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "sync"
  10. "time"
  11. "github.com/syndtr/goleveldb/leveldb"
  12. "github.com/syndtr/goleveldb/leveldb/opt"
  13. )
  14. const (
  15. routesDbFilename = "routes.db"
  16. idLogFilename = "id"
  17. )
  18. // Route is the value part of a shortcut.
  19. type Route struct {
  20. URL string `json:"url"`
  21. Time time.Time `json:"time"`
  22. }
  23. // Serialize this Route into the given Writer.
  24. func (o *Route) write(w io.Writer) error {
  25. if err := binary.Write(w, binary.LittleEndian, o.Time.UnixNano()); err != nil {
  26. return err
  27. }
  28. if _, err := w.Write([]byte(o.URL)); err != nil {
  29. return err
  30. }
  31. return nil
  32. }
  33. // Deserialize this Route from the given Reader.
  34. func (o *Route) read(r io.Reader) error {
  35. var t int64
  36. if err := binary.Read(r, binary.LittleEndian, &t); err != nil {
  37. return err
  38. }
  39. b, err := ioutil.ReadAll(r)
  40. if err != nil {
  41. return err
  42. }
  43. o.URL = string(b)
  44. o.Time = time.Unix(0, t)
  45. return nil
  46. }
  47. // Context provides access to the data store.
  48. type Context struct {
  49. path string
  50. db *leveldb.DB
  51. lck sync.Mutex
  52. id uint64
  53. }
  54. // Commit the given ID to the data store.
  55. func commit(filename string, id uint64) error {
  56. w, err := os.Create(filename)
  57. if err != nil {
  58. return err
  59. }
  60. defer w.Close()
  61. if err := binary.Write(w, binary.LittleEndian, id); err != nil {
  62. return err
  63. }
  64. return w.Sync()
  65. }
  66. // Load the current ID from the data store.
  67. func load(filename string) (uint64, error) {
  68. if _, err := os.Stat(filename); err != nil {
  69. return 0, commit(filename, 0)
  70. }
  71. r, err := os.Open(filename)
  72. if err != nil {
  73. return 0, err
  74. }
  75. defer r.Close()
  76. var id uint64
  77. if err := binary.Read(r, binary.LittleEndian, &id); err != nil {
  78. return 0, err
  79. }
  80. return id, nil
  81. }
  82. // Open the context using path as the data store location.
  83. func Open(path string) (*Context, error) {
  84. if _, err := os.Stat(path); err != nil {
  85. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  86. return nil, err
  87. }
  88. }
  89. // open the database
  90. db, err := leveldb.OpenFile(filepath.Join(path, routesDbFilename), nil)
  91. if err != nil {
  92. return nil, err
  93. }
  94. id, err := load(filepath.Join(path, idLogFilename))
  95. if err != nil {
  96. return nil, err
  97. }
  98. return &Context{
  99. path: path,
  100. db: db,
  101. id: id,
  102. }, nil
  103. }
  104. // Close the resources associated with this context.
  105. func (c *Context) Close() error {
  106. return c.db.Close()
  107. }
  108. // Get retreives a shortcut from the data store.
  109. func (c *Context) Get(name string) (*Route, error) {
  110. val, err := c.db.Get([]byte(name), nil)
  111. if err != nil {
  112. return nil, err
  113. }
  114. rt := &Route{}
  115. if err := rt.read(bytes.NewBuffer(val)); err != nil {
  116. return nil, err
  117. }
  118. return rt, nil
  119. }
  120. // Put stores a new shortcut in the data store.
  121. func (c *Context) Put(key string, rt *Route) error {
  122. var buf bytes.Buffer
  123. if err := rt.write(&buf); err != nil {
  124. return err
  125. }
  126. return c.db.Put([]byte(key), buf.Bytes(), &opt.WriteOptions{Sync: true})
  127. }
  128. // Del removes an existing shortcut from the data store.
  129. func (c *Context) Del(key string) error {
  130. return c.db.Delete([]byte(key), &opt.WriteOptions{Sync: true})
  131. }
  132. // get everything in the db so dump it out for backup purposes
  133. func (c *Context) GetAll() (map [string]Route, error) {
  134. golinks := map[string]Route{}
  135. iter := c.db.NewIterator(nil, nil)
  136. for iter.Next() {
  137. key := iter.Key()
  138. val := iter.Value()
  139. rt := &Route{}
  140. if err := rt.read(bytes.NewBuffer(val)); err != nil {
  141. return nil, err
  142. }
  143. golinks[string(key[:])] = *rt
  144. }
  145. iter.Release()
  146. err := iter.Error()
  147. return golinks, err
  148. }
  149. func (c *Context) commit(id uint64) error {
  150. w, err := os.Create(filepath.Join(c.path, idLogFilename))
  151. if err != nil {
  152. return err
  153. }
  154. defer w.Close()
  155. if err := binary.Write(w, binary.LittleEndian, id); err != nil {
  156. return err
  157. }
  158. return w.Sync()
  159. }
  160. // NextID generates the next numeric ID to be used for an auto-named shortcut.
  161. func (c *Context) NextID() (uint64, error) {
  162. c.lck.Lock()
  163. defer c.lck.Unlock()
  164. c.id++
  165. if err := commit(filepath.Join(c.path, idLogFilename), c.id); err != nil {
  166. return 0, err
  167. }
  168. return c.id, nil
  169. }