Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cache
- import (
- "../tools"
- "database/sql"
- "sync"
- "time"
- )
- type Actor struct {
- Id uint32
- Name string
- JobTitle string
- valid bool
- }
- type actorCache struct {
- actors map[uint32]Actor
- }
- type Protocol0Key struct {
- ProjectId uint32
- QuestionId uint32
- SubjectId uint32
- }
- type Protocol1Key struct {
- ProjectId uint32
- QuestionId uint32
- SubjectId uint32
- ObjectId uint32
- }
- type Protocol2Key struct {
- ProjectId uint32
- QuestionId uint32
- SubjectId uint32
- SourceId uint32
- TargetId uint32
- }
- type Protocol struct {
- Answer sql.NullInt32
- CreatedAt time.Time
- FilledAt sql.NullTime
- valid bool
- }
- type projectCache struct {
- protocol0 map[Protocol0Key]Protocol
- protocol1 map[Protocol1Key]Protocol
- protocol2 map[Protocol2Key]Protocol
- actor map[uint32]Actor
- }
- type Transaction interface {
- ReadProtocol0(key Protocol0Key) (Protocol, error)
- InsertProtocol0(key Protocol0Key, value Protocol) error
- ReadProtocol1(key Protocol1Key) (Protocol, error)
- InsertProtocol1(key Protocol1Key, value Protocol) error
- ReadActor(key uint32) (Actor, error)
- InsertActor(key uint32, value Actor) error
- Commit() error
- Rollback() error
- }
- type Project interface {
- Begin() (Transaction, error)
- }
- type project struct {
- mutex sync.Mutex
- cache projectCache
- }
- func (project *project) Begin() (Transaction, error) {
- tx, err := tools.DBS.Begin()
- if err != nil {
- return nil, err
- }
- return &transaction{
- project: project,
- tx: tx,
- }, nil
- }
- type transaction struct {
- project *project
- tx *sql.Tx
- }
- func (tx *transaction) Commit() error {
- return tx.tx.Commit()
- }
- func (tx *transaction) Rollback() error {
- return tx.tx.Rollback()
- }
- func (tx *transaction) ReadProtocol0(key Protocol0Key) (Protocol, error) {
- tx.project.mutex.Lock()
- protocol := tx.project.cache.protocol0[key]
- if protocol.valid {
- return protocol, nil
- } else {
- err := tx.tx.QueryRow("read from db into protocol").Scan(&protocol)
- if err == nil {
- protocol.valid = true
- tx.project.cache.protocol0[key] = protocol
- } else if err == sql.ErrNoRows {
- delete(tx.project.cache.protocol0, key)
- } else {
- return Protocol{}, err
- }
- }
- tx.project.mutex.Unlock()
- return protocol, nil
- }
- func (tx *transaction) InsertProtocol0(key Protocol0Key, value Protocol) error {
- tx.project.mutex.Lock()
- tx.tx.Exec("insert protocol0")
- protocol := tx.project.cache.protocol0[key]
- protocol.valid = false
- tx.project.cache.protocol0[key] = protocol
- tx.project.mutex.Unlock()
- return nil
- }
- func (tx *transaction) ReadProtocol1(key Protocol1Key) (Protocol, error) {
- tx.project.mutex.Lock()
- protocol := tx.project.cache.protocol1[key]
- if protocol.valid {
- return protocol, nil
- } else {
- err := tx.tx.QueryRow("read from db into protocol").Scan(&protocol)
- if err == nil {
- protocol.valid = true
- tx.project.cache.protocol1[key] = protocol
- } else if err == sql.ErrNoRows {
- delete(tx.project.cache.protocol1, key)
- } else {
- return Protocol{}, err
- }
- }
- tx.project.mutex.Unlock()
- return protocol, nil
- }
- func (tx *transaction) InsertProtocol1(key Protocol1Key, value Protocol) error {
- tx.project.mutex.Lock()
- tx.tx.Exec("insert protocol0")
- protocol := tx.project.cache.protocol1[key]
- protocol.valid = false
- tx.project.cache.protocol1[key] = protocol
- tx.project.mutex.Unlock()
- return nil
- }
- func (tx *transaction) ReadActor(key uint32) (Actor, error) {
- tx.project.mutex.Lock()
- actor := tx.project.cache.actor[key]
- if actor.valid {
- return actor, nil
- } else {
- err := tx.tx.QueryRow("read from db into actor").Scan(&actor)
- if err == nil {
- actor.valid = true
- tx.project.cache.actor[key] = actor
- } else if err == sql.ErrNoRows {
- delete(tx.project.cache.actor, key)
- } else {
- return Actor{}, err
- }
- }
- tx.project.mutex.Unlock()
- return actor, nil
- }
- func (tx *transaction) InsertActor( key uint32, value Actor) error {
- tx.project.mutex.Lock()
- tx.tx.Exec("insert protocol0")
- actor := tx.project.cache.actor[key]
- actor.valid = false
- tx.project.cache.actor[key] = actor
- tx.project.mutex.Unlock()
- return nil
- }
- type Azimuth struct {
- project map[uint32]project
- }
- func (azimuth Azimuth) GetProject(projectId uint32) Project {
- project := azimuth.project[projectId]
- return &project
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement