strategy.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. The "strategy" package in Kurz provides the aliasing strategies.
  3. Files
  4. - strategy.go contains the interface and base implementation
  5. - strategies.go contains the strategy instances and utilities
  6. - manual.go contains the "manual" strategy
  7. - hexrcr32.go contains the "hexcrc32" strategy
  8. */
  9. package strategy
  10. import (
  11. "code.osinet.fr/fgm/kurz/storage"
  12. "code.osinet.fr/fgm/kurz/url"
  13. "log"
  14. "time"
  15. )
  16. /*
  17. AliasingStrategy defines the operations provided by the various aliasing implementations:
  18. The options parameter for Alias() MAY be used by some strategies, in which case they
  19. have to define their expectations about it.
  20. */
  21. type AliasingStrategy interface {
  22. Name() string // Return the name of the strategy object
  23. Alias(url *url.LongUrl, s storage.Storage, options ...interface{}) (url.ShortUrl, error) // Return the short URL (alias) for a given long (source) URL
  24. UseCount(storage storage.Storage) int // Return the number of short URLs (aliases) using this strategy.
  25. }
  26. type baseStrategy struct{}
  27. func (y baseStrategy) Name() string {
  28. return "base"
  29. }
  30. /*
  31. Make sure a longurl instance has a DB ID, allocating it if needed.
  32. For speed reasons, assumes nonzero IDs to be valid without checking.
  33. */
  34. func (y baseStrategy) ensureLongId(long *url.LongUrl, s storage.Storage) error {
  35. var err error
  36. var long_id int64
  37. // If long does not have an Id, check if it is already known.
  38. if long.Id == 0 {
  39. sql := `
  40. SELECT id
  41. FROM longurl
  42. WHERE url = ?
  43. `
  44. err = s.DB.QueryRow(sql, y.Name()).Scan(&long_id)
  45. if err != nil {
  46. long_id = 0
  47. // log.Printf("Failed querying database for long url %s: %v\n", long.Value, err)
  48. }
  49. sql = `
  50. INSERT INTO longurl(url)
  51. VALUES (?)
  52. `
  53. result, err := s.DB.Exec(sql, long.Value)
  54. if err != nil {
  55. log.Printf("Failed inserting long URL %s: %+v", long.Value, err)
  56. return err
  57. } else {
  58. long_id, _ = result.LastInsertId()
  59. }
  60. long.Id = long_id
  61. }
  62. return nil
  63. }
  64. func (y baseStrategy) Alias(long *url.LongUrl, s storage.Storage, options ...interface{}) (url.ShortUrl, error) {
  65. var short url.ShortUrl
  66. var sql string
  67. var err error
  68. err = y.ensureLongId(long, s)
  69. if err != nil {
  70. return short, err
  71. }
  72. /** TODO
  73. * - validate alias is available
  74. */
  75. short = url.ShortUrl{
  76. Value: long.Value,
  77. ShortFor: *long,
  78. Domain: long.Domain(),
  79. Strategy: y.Name(),
  80. SubmittedBy: storage.CurrentUser(),
  81. SubmittedOn: time.Now().UTC().Unix(),
  82. IsEnabled: true,
  83. }
  84. sql = `
  85. INSERT INTO shorturl(url, longurl, domain, strategy, submittedBy, submittedInfo, isEnabled)
  86. VALUES (?, ?, ?, ?, ?, ?, ?)
  87. `
  88. result, err := s.DB.Exec(sql, short.Value, short.ShortFor.Id, short.Domain, short.Strategy, short.SubmittedBy.Id, short.SubmittedOn, short.IsEnabled)
  89. if err != nil {
  90. log.Printf("Failed inserting short %s: %#v", short.Value, err)
  91. } else {
  92. short.Id, _ = result.LastInsertId()
  93. }
  94. return short, err
  95. }
  96. /**
  97. Any nonzero result is likely an error.
  98. */
  99. func (y baseStrategy) UseCount(s storage.Storage) int {
  100. sql := `
  101. SELECT COUNT(*)
  102. FROM shorturl
  103. WHERE strategy = ?
  104. `
  105. var count int
  106. err := s.DB.QueryRow(sql, y.Name()).Scan(&count)
  107. if err != nil {
  108. count = 0
  109. log.Printf("Failed querying database for base strategy use count: %v\n", err)
  110. }
  111. return count
  112. }