Advertisement
saifutdinov

Untitled

Jul 15th, 2020
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.32 KB | None | 0 0
  1. package cache
  2.  
  3. import (
  4.     "../tools"
  5.     "database/sql"
  6.     "sync"
  7.     "time"
  8. )
  9.  
  10. type Actor struct {
  11.     Id       uint32
  12.     Name     string
  13.     JobTitle string
  14.  
  15.     valid    bool
  16. }
  17.  
  18. type actorCache struct {
  19.     actors map[uint32]Actor
  20. }
  21.  
  22. type Protocol0Key struct {
  23.     ProjectId uint32
  24.     QuestionId uint32
  25.     SubjectId uint32
  26. }
  27.  
  28. type Protocol1Key struct {
  29.     ProjectId  uint32
  30.     QuestionId uint32
  31.     SubjectId  uint32
  32.     ObjectId   uint32
  33. }
  34.  
  35. type Protocol2Key struct {
  36.     ProjectId uint32
  37.     QuestionId uint32
  38.     SubjectId uint32
  39.     SourceId uint32
  40.     TargetId uint32
  41. }
  42.  
  43. type Protocol struct {
  44.     Answer sql.NullInt32
  45.     CreatedAt time.Time
  46.     FilledAt sql.NullTime
  47.  
  48.     valid bool
  49. }
  50.  
  51. type projectCache struct {
  52.     protocol0 map[Protocol0Key]Protocol
  53.     protocol1 map[Protocol1Key]Protocol
  54.     protocol2 map[Protocol2Key]Protocol
  55.     actor map[uint32]Actor
  56. }
  57.  
  58. type Transaction interface {
  59.     ReadProtocol0(key Protocol0Key) (Protocol, error)
  60.     InsertProtocol0(key Protocol0Key, value Protocol) error
  61.  
  62.     ReadProtocol1(key Protocol1Key) (Protocol, error)
  63.     InsertProtocol1(key Protocol1Key, value Protocol) error
  64.  
  65.     ReadActor(key uint32) (Actor, error)
  66.     InsertActor(key uint32, value Actor) error
  67.  
  68.  
  69.     Commit() error
  70.     Rollback() error
  71. }
  72.  
  73. type Project interface {
  74.     Begin() (Transaction, error)
  75. }
  76.  
  77. type project struct {
  78.     mutex sync.Mutex
  79.     cache projectCache
  80. }
  81.  
  82. func (project *project) Begin() (Transaction, error) {
  83.     tx, err := tools.DBS.Begin()
  84.     if err != nil {
  85.         return nil, err
  86.     }
  87.     return &transaction{
  88.         project: project,
  89.         tx: tx,
  90.     }, nil
  91. }
  92.  
  93. type transaction struct {
  94.     project *project
  95.     tx *sql.Tx
  96. }
  97.  
  98. func (tx *transaction) Commit() error {
  99.     return tx.tx.Commit()
  100. }
  101.  
  102. func (tx *transaction) Rollback() error {
  103.     return tx.tx.Rollback()
  104. }
  105.  
  106. func (tx *transaction) ReadProtocol0(key Protocol0Key) (Protocol, error) {
  107.     tx.project.mutex.Lock()
  108.     protocol := tx.project.cache.protocol0[key]
  109.     if protocol.valid {
  110.         return protocol, nil
  111.     } else {
  112.         err := tx.tx.QueryRow("read from db into protocol").Scan(&protocol)
  113.         if err == nil {
  114.             protocol.valid = true
  115.             tx.project.cache.protocol0[key] = protocol
  116.         } else if err == sql.ErrNoRows {
  117.             delete(tx.project.cache.protocol0, key)
  118.         } else {
  119.             return Protocol{}, err
  120.         }
  121.     }
  122.     tx.project.mutex.Unlock()
  123.     return protocol, nil
  124. }
  125. func (tx *transaction) InsertProtocol0(key Protocol0Key, value Protocol) error {
  126.     tx.project.mutex.Lock()
  127.     tx.tx.Exec("insert protocol0")
  128.     protocol := tx.project.cache.protocol0[key]
  129.     protocol.valid = false
  130.     tx.project.cache.protocol0[key] = protocol
  131.     tx.project.mutex.Unlock()
  132.     return nil
  133. }
  134.  
  135. func (tx *transaction) ReadProtocol1(key Protocol1Key) (Protocol, error) {
  136.     tx.project.mutex.Lock()
  137.     protocol := tx.project.cache.protocol1[key]
  138.     if protocol.valid {
  139.         return protocol, nil
  140.     } else {
  141.         err := tx.tx.QueryRow("read from db into protocol").Scan(&protocol)
  142.         if err == nil {
  143.             protocol.valid = true
  144.             tx.project.cache.protocol1[key] = protocol
  145.         } else if err == sql.ErrNoRows {
  146.             delete(tx.project.cache.protocol1, key)
  147.         } else {
  148.             return Protocol{}, err
  149.         }
  150.     }
  151.     tx.project.mutex.Unlock()
  152.     return protocol, nil
  153. }
  154. func (tx *transaction) InsertProtocol1(key Protocol1Key, value Protocol) error {
  155.     tx.project.mutex.Lock()
  156.     tx.tx.Exec("insert protocol0")
  157.     protocol := tx.project.cache.protocol1[key]
  158.     protocol.valid = false
  159.     tx.project.cache.protocol1[key] = protocol
  160.     tx.project.mutex.Unlock()
  161.     return nil
  162. }
  163.  
  164. func (tx *transaction) ReadActor(key uint32) (Actor, error) {
  165.     tx.project.mutex.Lock()
  166.     actor := tx.project.cache.actor[key]
  167.     if actor.valid {
  168.         return actor, nil
  169.     } else {
  170.         err := tx.tx.QueryRow("read from db into actor").Scan(&actor)
  171.         if err == nil {
  172.             actor.valid = true
  173.             tx.project.cache.actor[key] = actor
  174.         } else if err == sql.ErrNoRows {
  175.             delete(tx.project.cache.actor, key)
  176.         } else {
  177.             return Actor{}, err
  178.         }
  179.     }
  180.     tx.project.mutex.Unlock()
  181.     return actor, nil
  182. }
  183. func (tx *transaction) InsertActor( key uint32, value Actor) error {
  184.     tx.project.mutex.Lock()
  185.     tx.tx.Exec("insert protocol0")
  186.     actor := tx.project.cache.actor[key]
  187.     actor.valid = false
  188.     tx.project.cache.actor[key] = actor
  189.     tx.project.mutex.Unlock()
  190.     return nil
  191. }
  192.  
  193. type Azimuth struct {
  194.     project map[uint32]project
  195. }
  196.  
  197. func (azimuth Azimuth) GetProject(projectId uint32) Project {
  198.     project := azimuth.project[projectId]
  199.     return &project
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement