Skip to content

Commit

Permalink
Revert back to one DB file
Browse files Browse the repository at this point in the history
  • Loading branch information
fabxc committed Mar 15, 2016
1 parent d0e35c1 commit d2c0cd6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
16 changes: 5 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,23 @@ func main() {
if err != nil {
log.Fatal(err)
}
alertDB, err := sql.Open("sqlite3", filepath.Join(*dataDir, "alerts.db"))
db, err := sql.Open("sqlite3", filepath.Join(*dataDir, "am.db"))
if err != nil {
log.Fatal(err)
}
defer alertDB.Close()

silenceDB, err := sql.Open("sqlite3", filepath.Join(*dataDir, "silence.db"))
if err != nil {
log.Fatal(err)
}
defer alertDB.Close()
defer db.Close()

marker := types.NewMarker()

alerts, err := sqlite.NewAlerts(alertDB)
alerts, err := sqlite.NewAlerts(db)
if err != nil {
log.Fatal(err)
}
notifies, err := sqlite.NewNotifies(alertDB)
notifies, err := sqlite.NewNotifies(db)
if err != nil {
log.Fatal(err)
}
silences, err := sqlite.NewSilences(silenceDB, marker)
silences, err := sqlite.NewSilences(db, marker)
if err != nil {
log.Fatal(err)
}
Expand Down
46 changes: 23 additions & 23 deletions provider/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CREATE INDEX IF NOT EXISTS alerts_end ON alerts (ends_at);
CREATE INDEX IF NOT EXISTS alerts_updated ON alerts (updated_at);
`

var alertMtx, silenceMtx sync.Mutex
var dbmtx sync.Mutex

type Alerts struct {
db *sql.DB
Expand All @@ -43,8 +43,8 @@ type Alerts struct {
}

func NewAlerts(db *sql.DB) (*Alerts, error) {
alertMtx.Lock()
defer alertMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

tx, err := db.Begin()
if err != nil {
Expand Down Expand Up @@ -126,8 +126,8 @@ func (a *Alerts) GetPending() provider.AlertIterator {
}

func (a *Alerts) getPending() ([]*types.Alert, error) {
alertMtx.Lock()
defer alertMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

// Get the last instance for each alert.
rows, err := a.db.Query(`
Expand Down Expand Up @@ -182,8 +182,8 @@ func (a *Alerts) Get(model.Fingerprint) (*types.Alert, error) {

// Put implements the Alerts interface.
func (a *Alerts) Put(alerts ...*types.Alert) error {
alertMtx.Lock()
defer alertMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

tx, err := a.db.Begin()
if err != nil {
Expand Down Expand Up @@ -346,8 +346,8 @@ type Notifies struct {
}

func NewNotifies(db *sql.DB) (*Notifies, error) {
alertMtx.Lock()
defer alertMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

tx, err := db.Begin()
if err != nil {
Expand All @@ -364,8 +364,8 @@ func NewNotifies(db *sql.DB) (*Notifies, error) {

// Get implements the Notifies interface.
func (n *Notifies) Get(dest string, fps ...model.Fingerprint) ([]*types.NotifyInfo, error) {
alertMtx.Lock()
defer alertMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

var result []*types.NotifyInfo

Expand Down Expand Up @@ -403,8 +403,8 @@ func (n *Notifies) Get(dest string, fps ...model.Fingerprint) ([]*types.NotifyIn

// Set implements the Notifies interface.
func (n *Notifies) Set(ns ...*types.NotifyInfo) error {
alertMtx.Lock()
defer alertMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

tx, err := n.db.Begin()
if err != nil {
Expand Down Expand Up @@ -473,8 +473,8 @@ type Silences struct {

// NewSilences returns a new Silences based on the provided SQL DB.
func NewSilences(db *sql.DB, mk types.Marker) (*Silences, error) {
silenceMtx.Lock()
defer silenceMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

tx, err := db.Begin()
if err != nil {
Expand Down Expand Up @@ -511,8 +511,8 @@ func (s *Silences) Mutes(lset model.LabelSet) bool {

// All implements the Silences interface.
func (s *Silences) All() ([]*types.Silence, error) {
silenceMtx.Lock()
defer silenceMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

rows, err := s.db.Query(`
SELECT id, matchers, starts_at, ends_at, created_at, created_by, comment
Expand Down Expand Up @@ -559,8 +559,8 @@ func (s *Silences) All() ([]*types.Silence, error) {

// Set impelements the Silences interface.
func (s *Silences) Set(sil *types.Silence) (uint64, error) {
silenceMtx.Lock()
defer silenceMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

mb, err := json.Marshal(sil.Silence.Matchers)
if err != nil {
Expand Down Expand Up @@ -601,8 +601,8 @@ func (s *Silences) Set(sil *types.Silence) (uint64, error) {

// Del implements the Silences interface.
func (s *Silences) Del(sid uint64) error {
silenceMtx.Lock()
defer silenceMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

tx, err := s.db.Begin()
if err != nil {
Expand All @@ -620,8 +620,8 @@ func (s *Silences) Del(sid uint64) error {

// Get implements the Silences interface.
func (s *Silences) Get(sid uint64) (*types.Silence, error) {
silenceMtx.Lock()
defer silenceMtx.Unlock()
dbmtx.Lock()
defer dbmtx.Unlock()

row := s.db.QueryRow(`
SELECT id, matchers, starts_at, ends_at, created_at, created_by, comment
Expand Down

0 comments on commit d2c0cd6

Please sign in to comment.