<commit_before>package main

// Generate errors.go which is used in returnedOnlyErrors() below.
//go:generate go run generrors.go

import (
	"database/sql"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"time"

	"github.com/hashicorp/raft"
	"github.com/robustirc/robustirc/ircserver"
	"github.com/robustirc/robustirc/raft_store"
	"github.com/robustirc/robustirc/types"
	"github.com/sorcix/irc"
	"github.com/stapelberg/glog"

	_ "github.com/mattn/go-sqlite3"
)

// returnedOnlyErrors returns true if and only if |msgid| resulted in at least
// one error and not a single non-error IRC reply.
func returnedOnlyErrors(msgid types.RobustId) bool {
	vmsgs, _ := ircServer.Get(msgid)
	for _, msg := range vmsgs {
		ircmsg := irc.ParseMessage(msg.Data)
		if ircmsg == nil {
			glog.Errorf("Output message not parsable\n")
			continue
		}
		if !errorCodes[ircmsg.Command] {
			return false
		}
	}
	return len(vmsgs) > 0
}

type robustSnapshot struct {
	firstIndex            uint64
	lastIndex             uint64
	store                 *raft_store.LevelDBStore
	del                   map[uint64]types.RobustId
	servers               map[int64]bool
	idToIdx               map[int64]uint64
	compactionEnd         time.Time
	skipDeletionForCanary bool
}

func (s *robustSnapshot) Persist(sink raft.SnapshotSink) error {
	log.Printf("Filtering and writing up to %d indexes\n", s.lastIndex-s.firstIndex)

	// Get a timestamp and keep it constant, so that we only compact messages
	// older than n days from compactionStart. If we used time.Since, new
	// messages would pour into the window on every compaction round, possibly
	// making the compaction never converge.
	compactionStart := time.Now()
	log.Printf("compactionStart %s\n", compactionStart.String())
	if *canaryCompactionStart > 0 {
		compactionStart = time.Unix(0, *canaryCompactionStart)
		log.Printf("compactionStart %s (overridden with -canary_compaction_start)\n", compactionStart.String())
	}

	s.compactionEnd = compactionStart.Add(-7 * 24 * time.Hour)
	f, err := ioutil.TempFile(*raftDir, "temp-compaction.sqlite3")
	if err != nil {
		sink.Cancel()
		return err
	}
	tempfile := f.Name()
	f.Close()
	db, err := sql.Open("sqlite3", tempfile)
	if err != nil {
		sink.Cancel()
		return err
	}
	defer func() {
		db.Close()
		os.Remove(tempfile)
	}()
	if _, err := db.Exec("pragma synchronous = off"); err != nil {
		sink.Cancel()
		return err
	}
	const nonIrcCommandStmt = `
CREATE TABLE createSession (msgid integer not null unique primary key, session integer not null);
CREATE TABLE deleteSession (msgid integer not null unique primary key, session integer not null);
CREATE TABLE allMessages (msgid integer not null unique primary key, session integer not null, irccommand string null);
CREATE VIEW allMessagesWin AS SELECT * FROM allMessages WHERE msgid < %d;
CREATE INDEX allMessagesSessionIdx ON allMessages (session);
CREATE VIEW createSessionWin AS SELECT * FROM createSession WHERE msgid < %d;
CREATE VIEW deleteSessionWin AS SELECT * FROM deleteSession WHERE msgid < %d;
`
	if _, err := db.Exec(
		fmt.Sprintf(nonIrcCommandStmt,
			s.compactionEnd.UnixNano(),
			s.compactionEnd.UnixNano(),
			s.compactionEnd.UnixNano())); err != nil {
		sink.Cancel()
		return err
	}

	var allMessagesStmt, createSessionStmt, deleteSessionStmt *sql.Stmt
	allMessagesStmt, err = db.Prepare("INSERT INTO allMessages (msgid, session, irccommand) VALUES (?, ?, ?)")
	if err == nil {
		defer allMessagesStmt.Close()
		createSessionStmt, err = db.Prepare("INSERT INTO createSession (msgid, session) VALUES (?, ?)")
	}
	if err == nil {
		defer createSessionStmt.Close()
		deleteSessionStmt, err = db.Prepare("INSERT INTO deleteSession (msgid, session) VALUES (?, ?)")
	}
	if err == nil {
		defer deleteSessionStmt.Close()
	}
	if err != nil {
		sink.Cancel()
		return err
	}

	// Let each IRC command prepare their tables and prepared statements.
	stmts := make(map[string]*sql.Stmt)
	for name, cmd := range ircserver.Commands {
		if cmd.CompactionPrepareStmt == nil {
			continue
		}
		var err error
		stmts[name], err = cmd.CompactionPrepareStmt(db)
		if err == nil {
			defer stmts[name].Close()
			err = cmd.CompactionPrepareViews(db, s.compactionEnd)
		}
		if err != nil {
			sink.Cancel()
			return err
		}
	}

	// First pass: insert all messages into database
	iterator := s.store.GetBulkIterator(s.firstIndex, s.lastIndex+1)
	defer iterator.Release()
	available := iterator.First()
	for available {
		var nlog raft.Log
		if err := iterator.Error(); err != nil {
			glog.Errorf("Error while iterating through the log: %v", err)
			available = iterator.Next()
			continue
		}
		i := binary.BigEndian.Uint64(iterator.Key())
		value := iterator.Value()
		if err := json.Unmarshal(value, &nlog); err != nil {
			glog.Errorf("Skipping log entry %d because of a JSON unmarshaling error: %v", i, err)
			continue
		}
		available = iterator.Next()

		// TODO: compact raft messages as well, so that peer changes are not kept forever
		if nlog.Type != raft.LogCommand {
			continue
		}

		parsed := types.NewRobustMessageFromBytes(nlog.Data)
		if time.Unix(0, parsed.Id.Id).After(s.compactionEnd) {
			break
		}
		s.idToIdx[parsed.Id.Id] = i

		msgid := parsed.Id
		session := parsed.Session
		ircCmdNullable := sql.NullString{Valid: false}

		switch parsed.Type {
		case types.RobustMessageOfDeath:
			s.del[i] = parsed.Id
			continue

		case types.RobustConfig:
			// TODO: implement compaction for RobustConfig
			continue

		case types.RobustIRCToClient:
			fallthrough
		case types.RobustPing:
			fallthrough
		case types.RobustAny:
			glog.Errorf("Compaction: saw unexpected message type %v (log id %d, message id %v)\n", parsed.Type, i, parsed.Id)
			continue

		default:
			glog.Errorf("Compaction: saw message of unknown type %v (log id %d, message id %v)\n", parsed.Type, i, parsed.Id)
			continue

		case types.RobustCreateSession:
			session = parsed.Id
			if _, err := createSessionStmt.Exec(msgid.Id, session.Id); err != nil {
				sink.Cancel()
				return err
			}

		case types.RobustDeleteSession:
			if _, err := deleteSessionStmt.Exec(msgid.Id, session.Id); err != nil {
				sink.Cancel()
				return err
			}

		case types.RobustIRCFromClient:
			if returnedOnlyErrors(types.RobustId{Id: parsed.Id.Id}) {
				s.del[i] = parsed.Id
				continue
			}

			ircmsg := irc.ParseMessage(parsed.Data)
			if ircmsg == nil {
				s.del[i] = parsed.Id
				continue
			}
			irccmd := strings.ToUpper(ircmsg.Command)
			ircCmdNullable = sql.NullString{String: irccmd, Valid: true}

			// Kind of a hack: we need to keep track of which sessions are
			// services connections and which are not, so that we can look at
			// the correct relevant-function (e.g. server_NICK vs. NICK).
			var serverPrefix string
			if irccmd == "SERVER" {
				s.servers[parsed.Session.Id] = true
			} else {
				if s.servers[session.Id] {
					serverPrefix = "server_"
				}
			}
			c, ok := ircserver.Commands[serverPrefix+irccmd]
			if !ok {
				// TODO: commands we don’t know should be okay to compact away, right?
				glog.Errorf("Could not find command %q", serverPrefix+irccmd)
				continue
			}
			if c.ImmediatelyCompactable {
				s.del[i] = parsed.Id
				continue
			}
			if c.CompactionInsert != nil {
				if err := c.CompactionInsert(msgid, session, ircmsg, stmts[serverPrefix+irccmd]); err != nil {
					sink.Cancel()
					return err
				}
			}
		}

		if _, err := allMessagesStmt.Exec(msgid.Id, session.Id, ircCmdNullable); err != nil {
			sink.Cancel()
			return err
		}
	}

	// We repeatedly compact, since the result of one compaction can affect the
	// result of other compactions (see compaction_test.go for examples).
	changed := true
	pass := 0
	for changed {
		log.Printf("Compaction pass %d\n", pass)
		pass++
		var err error
		changed, err = s.compact(db)
		if err != nil {
			sink.Cancel()
			return err
		}
	}

	log.Printf("Copying non-deleted messages into snapshot\n")

	available = iterator.First()
	for available {
		if err := iterator.Error(); err != nil {
			sink.Cancel()
			return err
		}
		i := binary.BigEndian.Uint64(iterator.Key())
		value := iterator.Value()
		if _, ok := s.del[i]; !ok {
			if _, err := sink.Write(value); err != nil {
				sink.Cancel()
				return err
			}
		}
		available = iterator.Next()
	}

	sink.Close()

	if s.skipDeletionForCanary {
		log.Printf("Skipping deletion (canary mode)\n")
		return nil
	}

	log.Printf("Deleting compacted messages from disk\n")
	for idx, id := range s.del {
		if err := ircServer.Delete(id); err != nil {
			log.Panicf("Could not delete outputstream message: %v\n", err)
		}
		s.store.DeleteRange(idx, idx)
	}

	log.Printf("Snapshot done\n")

	return nil
}

func (s *robustSnapshot) Release() {
}

// compact invokes all irc command’s |Compact| callbacks and runs the
// compaction for createSession and deleteSession messages.
func (s *robustSnapshot) compact(db *sql.DB) (bool, error) {
	changed := false

	for _, cmd := range ircserver.Commands {
		if cmd.Compact == nil {
			continue
		}
		if err := cmd.Compact(db); err != nil {
			return changed, err
		}
		c, err := s.markResultForCompaction(db)
		if err != nil {
			return changed, err
		}
		changed = changed || c
	}

	const stmt = `
-- Delete all deleteSession messages immediately preceded by a createSession
-- message (or commands in between which do not modify state of anything but
-- the session in question).
CREATE TEMPORARY TABLE candidates AS
SELECT
    a.msgid AS msgid,
    a.session AS session
FROM
    (
        SELECT
            d.msgid AS msgid,
            d.session AS session,
            MAX(a.msgid) AS next_msgid
        FROM
            deleteSessionWin AS d
            INNER JOIN allMessagesWin AS a
            ON (
                d.session = a.session AND
                (a.irccommand IS NULL OR
                 (a.irccommand != 'NICK' AND
                  a.irccommand != 'PASS' AND
                  a.irccommand != 'QUIT')) AND
                a.msgid < d.msgid
            )
        GROUP BY d.msgid
    ) AS a
    INNER JOIN createSessionWin AS c
    ON (
        a.session = c.session AND
        a.next_msgid = c.msgid
    );
DELETE FROM deleteSession WHERE msgid IN (SELECT msgid FROM candidates);

-- Delete all createSession messages for sessions which have no other message
-- (except for messages which do not modify state of anything but the session
-- in question).
INSERT INTO candidates
SELECT
    c.msgid,
    c.session
FROM
    createSession AS c
    LEFT JOIN allMessagesWin AS a
    ON (
        c.msgid != a.msgid AND
        c.session = a.session AND
        a.irccommand != 'NICK' AND
        a.irccommand != 'PASS' AND
        a.irccommand != 'QUIT'
    )
WHERE
    a.msgid IS NULL;

INSERT INTO candidates
SELECT
    a.msgid,
    a.session
FROM
    allMessagesWin AS a
    LEFT JOIN candidates AS d
    ON (
        a.session = d.session
    )
    WHERE d.session IS NOT NULL;

CREATE TEMPORARY TABLE deleteIds AS SELECT msgid FROM candidates;
DROP TABLE candidates;
DELETE FROM createSession WHERE msgid IN (SELECT msgid FROM deleteIds);
`

	_, err := db.Exec(stmt)
	if err != nil {
		return changed, err
	}

	c, err := s.markResultForCompaction(db)
	if err != nil {
		return changed, err
	}
	return changed || c, nil
}

// markResultForCompaction updates |s.del| with all messages whose ids are in
// the deleteIds table. It then removes these messages from the allMessages
// table and drops the deleteIds table.
func (s *robustSnapshot) markResultForCompaction(db *sql.DB) (bool, error) {
	changed := false
	rows, err := db.Query("SELECT msgid FROM deleteIds")
	if err != nil {
		return changed, err
	}
	defer rows.Close()
	for rows.Next() {
		var msgid int64
		if err := rows.Scan(&msgid); err != nil {
			return changed, err
		}
		idx, ok := s.idToIdx[msgid]
		if !ok {
			return changed, fmt.Errorf("msgid=%v not in idToIdx", msgid)
		}
		s.del[idx] = types.RobustId{Id: msgid}
		changed = true
	}
	if err := rows.Err(); err != nil {
		return changed, err
	}

	if _, err := db.Exec("DELETE FROM allMessages WHERE msgid IN (SELECT msgid FROM deleteIds)"); err != nil {
		return changed, err
	}

	_, err = db.Exec("DROP TABLE deleteIds")
	return changed, nil
}
<commit_msg>Refactor {create,delete}session compaction<commit_after>package main

// Generate errors.go which is used in returnedOnlyErrors() below.
//go:generate go run generrors.go

import (
	"database/sql"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"time"

	"github.com/hashicorp/raft"
	"github.com/robustirc/robustirc/ircserver"
	"github.com/robustirc/robustirc/raft_store"
	"github.com/robustirc/robustirc/types"
	"github.com/sorcix/irc"
	"github.com/stapelberg/glog"

	_ "github.com/mattn/go-sqlite3"
)

// returnedOnlyErrors returns true if and only if |msgid| resulted in at least
// one error and not a single non-error IRC reply.
func returnedOnlyErrors(msgid types.RobustId) bool {
	vmsgs, _ := ircServer.Get(msgid)
	for _, msg := range vmsgs {
		ircmsg := irc.ParseMessage(msg.Data)
		if ircmsg == nil {
			glog.Errorf("Output message not parsable\n")
			continue
		}
		if !errorCodes[ircmsg.Command] {
			return false
		}
	}
	return len(vmsgs) > 0
}

type robustSnapshot struct {
	firstIndex            uint64
	lastIndex             uint64
	store                 *raft_store.LevelDBStore
	del                   map[uint64]types.RobustId
	servers               map[int64]bool
	idToIdx               map[int64]uint64
	compactionEnd         time.Time
	skipDeletionForCanary bool
}

func (s *robustSnapshot) Persist(sink raft.SnapshotSink) error {
	log.Printf("Filtering and writing up to %d indexes\n", s.lastIndex-s.firstIndex)

	// Get a timestamp and keep it constant, so that we only compact messages
	// older than n days from compactionStart. If we used time.Since, new
	// messages would pour into the window on every compaction round, possibly
	// making the compaction never converge.
	compactionStart := time.Now()
	log.Printf("compactionStart %s\n", compactionStart.String())
	if *canaryCompactionStart > 0 {
		compactionStart = time.Unix(0, *canaryCompactionStart)
		log.Printf("compactionStart %s (overridden with -canary_compaction_start)\n", compactionStart.String())
	}

	s.compactionEnd = compactionStart.Add(-7 * 24 * time.Hour)
	f, err := ioutil.TempFile(*raftDir, "temp-compaction.sqlite3")
	if err != nil {
		sink.Cancel()
		return err
	}
	tempfile := f.Name()
	f.Close()
	db, err := sql.Open("sqlite3", tempfile)
	if err != nil {
		sink.Cancel()
		return err
	}
	defer func() {
		db.Close()
		os.Remove(tempfile)
	}()
	if _, err := db.Exec("pragma synchronous = off"); err != nil {
		sink.Cancel()
		return err
	}
	const nonIrcCommandStmt = `
CREATE TABLE createSession (msgid integer not null unique primary key, session integer not null);
CREATE TABLE deleteSession (msgid integer not null unique primary key, session integer not null);
CREATE TABLE allMessages (msgid integer not null unique primary key, session integer not null, irccommand string null);
CREATE VIEW allMessagesWin AS SELECT * FROM allMessages WHERE msgid < %d;
CREATE INDEX allMessagesSessionIdx ON allMessages (session);
CREATE VIEW createSessionWin AS SELECT * FROM createSession WHERE msgid < %d;
CREATE VIEW deleteSessionWin AS SELECT * FROM deleteSession WHERE msgid < %d;
`
	if _, err := db.Exec(
		fmt.Sprintf(nonIrcCommandStmt,
			s.compactionEnd.UnixNano(),
			s.compactionEnd.UnixNano(),
			s.compactionEnd.UnixNano())); err != nil {
		sink.Cancel()
		return err
	}

	var allMessagesStmt, createSessionStmt, deleteSessionStmt *sql.Stmt
	allMessagesStmt, err = db.Prepare("INSERT INTO allMessages (msgid, session, irccommand) VALUES (?, ?, ?)")
	if err == nil {
		defer allMessagesStmt.Close()
		createSessionStmt, err = db.Prepare("INSERT INTO createSession (msgid, session) VALUES (?, ?)")
	}
	if err == nil {
		defer createSessionStmt.Close()
		deleteSessionStmt, err = db.Prepare("INSERT INTO deleteSession (msgid, session) VALUES (?, ?)")
	}
	if err == nil {
		defer deleteSessionStmt.Close()
	}
	if err != nil {
		sink.Cancel()
		return err
	}

	// Let each IRC command prepare their tables and prepared statements.
	stmts := make(map[string]*sql.Stmt)
	for name, cmd := range ircserver.Commands {
		if cmd.CompactionPrepareStmt == nil {
			continue
		}
		var err error
		stmts[name], err = cmd.CompactionPrepareStmt(db)
		if err == nil {
			defer stmts[name].Close()
			err = cmd.CompactionPrepareViews(db, s.compactionEnd)
		}
		if err != nil {
			sink.Cancel()
			return err
		}
	}

	// First pass: insert all messages into database
	iterator := s.store.GetBulkIterator(s.firstIndex, s.lastIndex+1)
	defer iterator.Release()
	available := iterator.First()
	for available {
		var nlog raft.Log
		if err := iterator.Error(); err != nil {
			glog.Errorf("Error while iterating through the log: %v", err)
			available = iterator.Next()
			continue
		}
		i := binary.BigEndian.Uint64(iterator.Key())
		value := iterator.Value()
		if err := json.Unmarshal(value, &nlog); err != nil {
			glog.Errorf("Skipping log entry %d because of a JSON unmarshaling error: %v", i, err)
			continue
		}
		available = iterator.Next()

		// TODO: compact raft messages as well, so that peer changes are not kept forever
		if nlog.Type != raft.LogCommand {
			continue
		}

		parsed := types.NewRobustMessageFromBytes(nlog.Data)
		if time.Unix(0, parsed.Id.Id).After(s.compactionEnd) {
			break
		}
		s.idToIdx[parsed.Id.Id] = i

		msgid := parsed.Id
		session := parsed.Session
		ircCmdNullable := sql.NullString{Valid: false}

		switch parsed.Type {
		case types.RobustMessageOfDeath:
			s.del[i] = parsed.Id
			continue

		case types.RobustConfig:
			// TODO: implement compaction for RobustConfig
			continue

		case types.RobustIRCToClient:
			fallthrough
		case types.RobustPing:
			fallthrough
		case types.RobustAny:
			glog.Errorf("Compaction: saw unexpected message type %v (log id %d, message id %v)\n", parsed.Type, i, parsed.Id)
			continue

		default:
			glog.Errorf("Compaction: saw message of unknown type %v (log id %d, message id %v)\n", parsed.Type, i, parsed.Id)
			continue

		case types.RobustCreateSession:
			session = parsed.Id
			if _, err := createSessionStmt.Exec(msgid.Id, session.Id); err != nil {
				sink.Cancel()
				return err
			}

		case types.RobustDeleteSession:
			if _, err := deleteSessionStmt.Exec(msgid.Id, session.Id); err != nil {
				sink.Cancel()
				return err
			}

		case types.RobustIRCFromClient:
			if returnedOnlyErrors(types.RobustId{Id: parsed.Id.Id}) {
				s.del[i] = parsed.Id
				continue
			}

			ircmsg := irc.ParseMessage(parsed.Data)
			if ircmsg == nil {
				s.del[i] = parsed.Id
				continue
			}
			irccmd := strings.ToUpper(ircmsg.Command)
			ircCmdNullable = sql.NullString{String: irccmd, Valid: true}

			// Kind of a hack: we need to keep track of which sessions are
			// services connections and which are not, so that we can look at
			// the correct relevant-function (e.g. server_NICK vs. NICK).
			var serverPrefix string
			if irccmd == "SERVER" {
				s.servers[parsed.Session.Id] = true
			} else {
				if s.servers[session.Id] {
					serverPrefix = "server_"
				}
			}
			c, ok := ircserver.Commands[serverPrefix+irccmd]
			if !ok {
				// TODO: commands we don’t know should be okay to compact away, right?
				glog.Errorf("Could not find command %q", serverPrefix+irccmd)
				continue
			}
			if c.ImmediatelyCompactable {
				s.del[i] = parsed.Id
				continue
			}
			if c.CompactionInsert != nil {
				if err := c.CompactionInsert(msgid, session, ircmsg, stmts[serverPrefix+irccmd]); err != nil {
					sink.Cancel()
					return err
				}
			}
		}

		if _, err := allMessagesStmt.Exec(msgid.Id, session.Id, ircCmdNullable); err != nil {
			sink.Cancel()
			return err
		}
	}

	// We repeatedly compact, since the result of one compaction can affect the
	// result of other compactions (see compaction_test.go for examples).
	changed := true
	pass := 0
	for changed {
		log.Printf("Compaction pass %d\n", pass)
		pass++
		var err error
		changed, err = s.compact(db)
		if err != nil {
			sink.Cancel()
			return err
		}
	}

	log.Printf("Copying non-deleted messages into snapshot\n")

	available = iterator.First()
	for available {
		if err := iterator.Error(); err != nil {
			sink.Cancel()
			return err
		}
		i := binary.BigEndian.Uint64(iterator.Key())
		value := iterator.Value()
		if _, ok := s.del[i]; !ok {
			if _, err := sink.Write(value); err != nil {
				sink.Cancel()
				return err
			}
		}
		available = iterator.Next()
	}

	sink.Close()

	if s.skipDeletionForCanary {
		log.Printf("Skipping deletion (canary mode)\n")
		return nil
	}

	log.Printf("Deleting compacted messages from disk\n")
	for idx, id := range s.del {
		if err := ircServer.Delete(id); err != nil {
			log.Panicf("Could not delete outputstream message: %v\n", err)
		}
		s.store.DeleteRange(idx, idx)
	}

	log.Printf("Snapshot done\n")

	return nil
}

func (s *robustSnapshot) Release() {
}

// compact invokes all irc command’s |Compact| callbacks and runs the
// compaction for createSession and deleteSession messages.
func (s *robustSnapshot) compact(db *sql.DB) (bool, error) {
	changed := false

	for _, cmd := range ircserver.Commands {
		if cmd.Compact == nil {
			continue
		}
		if err := cmd.Compact(db); err != nil {
			return changed, err
		}
		c, err := s.markResultForCompaction(db)
		if err != nil {
			return changed, err
		}
		changed = changed || c
	}

	const stmt = `
-- Delete all deleteSession messages immediately preceded by a createSession
-- message (or commands in between which do not modify state of anything but
-- the session in question).
CREATE TEMPORARY TABLE candidates AS
SELECT
    a.msgid AS msgid,
    a.session AS session
FROM
    (
        SELECT
            d.msgid AS msgid,
            d.session AS session,
            MAX(a.msgid) AS next_msgid
        FROM
            (SELECT msgid, session FROM deleteSessionWin
             UNION SELECT msgid, session FROM paramsQuitWin) AS d
            INNER JOIN allMessagesWin AS a
            ON (
                d.session = a.session AND
                (a.irccommand IS NULL OR
                 (a.irccommand != 'NICK' AND
                  a.irccommand != 'PASS' AND
                  a.irccommand != 'QUIT')) AND
                a.msgid < d.msgid
            )
        GROUP BY d.msgid
    ) AS a
    INNER JOIN createSessionWin AS c
    ON (
        a.session = c.session AND
        a.next_msgid = c.msgid
    );
DELETE FROM deleteSession WHERE msgid IN (SELECT msgid FROM candidates);

INSERT INTO candidates
SELECT
    a.msgid,
    a.session
FROM
    allMessagesWin AS a
    LEFT JOIN candidates AS d
    ON (
        a.session = d.session
    )
    WHERE d.session IS NOT NULL;

CREATE TEMPORARY TABLE deleteIds AS SELECT msgid FROM candidates;
DROP TABLE candidates;
DELETE FROM createSession WHERE msgid IN (SELECT msgid FROM deleteIds);
`

	_, err := db.Exec(stmt)
	if err != nil {
		return changed, err
	}

	c, err := s.markResultForCompaction(db)
	if err != nil {
		return changed, err
	}
	return changed || c, nil
}

// markResultForCompaction updates |s.del| with all messages whose ids are in
// the deleteIds table. It then removes these messages from the allMessages
// table and drops the deleteIds table.
func (s *robustSnapshot) markResultForCompaction(db *sql.DB) (bool, error) {
	changed := false
	rows, err := db.Query("SELECT msgid FROM deleteIds")
	if err != nil {
		return changed, err
	}
	defer rows.Close()
	for rows.Next() {
		var msgid int64
		if err := rows.Scan(&msgid); err != nil {
			return changed, err
		}
		idx, ok := s.idToIdx[msgid]
		if !ok {
			return changed, fmt.Errorf("msgid=%v not in idToIdx", msgid)
		}
		s.del[idx] = types.RobustId{Id: msgid}
		changed = true
	}
	if err := rows.Err(); err != nil {
		return changed, err
	}

	if _, err := db.Exec("DELETE FROM allMessages WHERE msgid IN (SELECT msgid FROM deleteIds)"); err != nil {
		return changed, err
	}

	_, err = db.Exec("DROP TABLE deleteIds")
	return changed, nil
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"strings"
	"os"
	"errors"
)

var (
	silent bool
	// Declared in generate:
	// hashFunction HashValue
	// cwd string
)

var cmdVerify = &Command{
	Run: verify,
	Usage: `verify [-s] [-c=hash] [-D=dir]`,
	Short: "Verify using a checksum file",
	Long: `
Verify all files for the given directory against a checksum file.`,
}

var preferredHashes = []string{"SHA512", "SHA1", "MD5"}

func init() {
	const (
		silentUsage = "silent; don't output to STDERR"
		cwdUsage = "the directory for which to verify using the checksum file"
	)
	hashUsage := fmt.Sprintf("the hash to use; if unspecified, the following in tried in order: %s", strings.Join(preferredHashes, ", "))
	f := &cmdVerify.Flag
	f.BoolVar(&silent, "s", false, silentUsage)
	f.Var(&hashFunction, "c", hashUsage)
	f.StringVar(&cwd, "D", ".", cwdUsage)
}

func findChecksumFile() (hash *HashValue, file *os.File, err error) {
	for _, tryHash := range preferredHashes {
		hash := &HashValue{}
		hash.Set(tryHash)
		file, err := os.Open(hash.Filename())
		if err == nil {
			return hash, file, err
		}
	}
	return nil, nil, errors.New("No known checksum files found in directory")
}

func verify(cmd *Command, args []string) {
	if err := os.Chdir(cwd); err != nil {
		die(err)
	}

	var err error
	var hash *HashValue
	var checksums *os.File

	if hashFunction.Hash == 0x0 {
		hash, checksums, err = findChecksumFile()
		if err != nil {
			die(err)
		}
	} else {
		hash = &hashFunction
		checksums, err = os.Open(hash.Filename())
		if err != nil {
			die(err)
		}
	}

	allMatch := true
	r := NewReader(checksums)
	err = r.Each(func(entry *Entry) {
		if ok, err := entry.Verify(hash.Hash); err != nil {
			die(err)
		} else if !ok {
			if !silent {
				warn("%s does not match %s\n", entry.Filename, entry.Checksum)
			}
			allMatch = false
		}
	})
	if err != nil {
		die(err)
	}

	if allMatch {
		os.Exit(0)
	} else {
		os.Exit(1)
	}
}

<commit_msg>Be slightly more fault-tolerant and reportive in verify<commit_after>package main

import (
	"fmt"
	"strings"
	"os"
	"errors"
)

var (
	silent bool
	// Declared in generate:
	// hashFunction HashValue
	// cwd string
)

var cmdVerify = &Command{
	Run: verify,
	Usage: `verify [-s] [-c=hash] [-D=dir]`,
	Short: "Verify using a checksum file",
	Long: `
Verify all files for the given directory against a checksum file.`,
}

var preferredHashes = []string{"SHA512", "SHA1", "MD5"}

func init() {
	const (
		silentUsage = "silent; don't output to STDERR"
		cwdUsage = "the directory for which to verify using the checksum file"
	)
	hashUsage := fmt.Sprintf("the hash to use; if unspecified, the following in tried in order: %s", strings.Join(preferredHashes, ", "))
	f := &cmdVerify.Flag
	f.BoolVar(&silent, "s", false, silentUsage)
	f.Var(&hashFunction, "c", hashUsage)
	f.StringVar(&cwd, "D", ".", cwdUsage)
}

func findChecksumFile() (hash *HashValue, file *os.File, err error) {
	for _, tryHash := range preferredHashes {
		hash := &HashValue{}
		hash.Set(tryHash)
		file, err := os.Open(hash.Filename())
		if err == nil {
			return hash, file, err
		}
	}
	return nil, nil, errors.New("No known checksum files found in directory")
}

func verify(cmd *Command, args []string) {
	if err := os.Chdir(cwd); err != nil {
		die(err)
	}

	var err error
	var hash *HashValue
	var checksums *os.File

	if hashFunction.Hash == 0x0 {
		hash, checksums, err = findChecksumFile()
		if err != nil {
			die(err)
		}
	} else {
		hash = &hashFunction
		checksums, err = os.Open(hash.Filename())
		if err != nil {
			die(err)
		}
	}

	allMatch := true
	r := NewReader(checksums)
	err = r.Each(func(entry *Entry) {
		if ok, err := entry.Verify(hash.Hash); err != nil {
			if !silent {
				warn("%s\n", err)
			}
			allMatch = false
		} else if !ok {
			if !silent {
				warn("%s does not match %s\n", entry.Filename, entry.Checksum)
			}
			allMatch = false
		}
	})
	if err != nil {
		die(err)
	}

	if allMatch {
		os.Exit(0)
	} else {
		os.Exit(1)
	}
}

<|endoftext|>


<commit_before>package utils

import (
	"fmt"
	"time"
  "strings"
)

func GoStdTime()string{
	return "2006-01-02 15:04:05"
}

func GoStdUnixDate()string{
    return "Mon Jan _2 15:04:05 MST 2006"
}

func GoStdRubyDate()string{
    return "Mon Jan 02 15:04:05 -0700 2006"
}

func GetTmStr(tm time.Time,format string)(string){
	 patterns := []string{
    		"y","2006",
    		"m","01",
    		"d","02",

    		"Y","2006",
    		"M","01",
    		"D","02",

    		"h","03",	//12小时制
    		"H","15",	//24小时制

    		"i","04",
    		"s","05",

    		"t","pm",
    		"T","PM",
    	 }
    return convStr(tm,format,patterns)
}

func GetTmShortStr(tm time.Time,format string)(string){
		patterns := []string{
    		"y","06",
    		"m","1",
    		"d","2",

    		"Y","06",
    		"M","1",
    		"D","2",

    		"h","3",  //12小时制
    		"H","15", //24小时制

    		"i","4",
    		"s","5",

    		"t","pm",
    		"T","PM",
    	 }

    return convStr(tm,format,patterns)
}

func convStr(tm time.Time,format string,patterns []string)(string){
	replacer := strings.NewReplacer(patterns...)
    strfmt := replacer.Replace(format)
    return tm.Format(strfmt)
}

func GetLocaltimeStr()(string){
	now := time.Now().Local()
	year,mon,day := now.Date()
	hour,min,sec := now.Clock()
	zone,_ := now.Zone()
	return fmt.Sprintf("%d-%d-%d %02d:%02d:%02d %s",year,mon,day,hour,min,sec,zone)
}

func GetGmtimeStr()(string){
	now := time.Now()
	year,mon,day := now.UTC().Date()
	hour,min,sec := now.UTC().Clock()
	zone,_ := now.UTC().Zone()
	return fmt.Sprintf("%d-%d-%d %02d:%02d:%02d %s",year,mon,day,hour,min,sec,zone)
}

func GetUnixTimeStr(ut int64,format string)(string){
    t := time.Unix(ut,0)
    return GetTmStr(t,format)
}

func GetUnixTimeShortStr(ut int64,format string)(string){
    t := time.Unix(ut,0)
    return GetTmShortStr(t,format)
}

func Greatest(arr []time.Time)(time.Time){
    var temp time.Time
    for _,at := range arr {
        if temp.Before(at) {
            temp = at
        }
    }
    return temp;
}
<commit_msg>Update dtutil.go<commit_after>package utils

import (
	"fmt"
	"time"
        "strings"
)

func GoStdTime()string{
	return "2006-01-02 15:04:05"
}

func GoStdUnixDate()string{
    return "Mon Jan _2 15:04:05 MST 2006"
}

func GoStdRubyDate()string{
    return "Mon Jan 02 15:04:05 -0700 2006"
}

func GetTmStr(tm time.Time,format string)(string){
	 patterns := []string{
    		"y","2006",
    		"m","01",
    		"d","02",

    		"Y","2006",
    		"M","01",
    		"D","02",

    		"h","03",	//12小时制
    		"H","15",	//24小时制

    		"i","04",
    		"s","05",

    		"t","pm",
    		"T","PM",
    	 }
    return convStr(tm,format,patterns)
}

func GetTmShortStr(tm time.Time,format string)(string){
		patterns := []string{
    		"y","06",
    		"m","1",
    		"d","2",

    		"Y","06",
    		"M","1",
    		"D","2",

    		"h","3",  //12小时制
    		"H","15", //24小时制

    		"i","4",
    		"s","5",

    		"t","pm",
    		"T","PM",
    	 }

    return convStr(tm,format,patterns)
}

func convStr(tm time.Time,format string,patterns []string)(string){
	replacer := strings.NewReplacer(patterns...)
    strfmt := replacer.Replace(format)
    return tm.Format(strfmt)
}

func GetLocaltimeStr()(string){
	now := time.Now().Local()
	year,mon,day := now.Date()
	hour,min,sec := now.Clock()
	zone,_ := now.Zone()
	return fmt.Sprintf("%d-%d-%d %02d:%02d:%02d %s",year,mon,day,hour,min,sec,zone)
}

func GetGmtimeStr()(string){
	now := time.Now()
	year,mon,day := now.UTC().Date()
	hour,min,sec := now.UTC().Clock()
	zone,_ := now.UTC().Zone()
	return fmt.Sprintf("%d-%d-%d %02d:%02d:%02d %s",year,mon,day,hour,min,sec,zone)
}

func GetUnixTimeStr(ut int64,format string)(string){
    t := time.Unix(ut,0)
    return GetTmStr(t,format)
}

func GetUnixTimeShortStr(ut int64,format string)(string){
    t := time.Unix(ut,0)
    return GetTmShortStr(t,format)
}

func Greatest(arr []time.Time)(time.Time){
    var temp time.Time
    for _,at := range arr {
        if temp.Before(at) {
            temp = at
        }
    }
    return temp;
}

type TimeSlice []time.Time

func (s TimeSlice) Len() int {
     return len(s)
 }

func (s TimeSlice) Swap(i, j int) {
     s[i], s[j] = s[j], s[i]
 }

func (s TimeSlice) Less(i, j int) bool {
    if s[i].IsZero() {
        return false
    }
    if s[j].IsZero() {
        return true
    }
    return s[i].Before(s[j])
}

<|endoftext|>


<commit_before>package utl

import (
	"testing"
)

func TestDeDuplicate(t *testing.T) {
	if DeDuplicate("a") != "a" {
		t.Errorf("DeDuplicate fails to deduplicate the single letter a.")
	}
	if DeDuplicate("c") != "c" {
		t.Errorf("DeDuplicate fails to deduplicate the single letter c.")
	}
	if DeDuplicate("buzz") != "buz" {
		t.Errorf("DeDuplicate fails to deduplicate the letter z from buzz.")
	}
	if DeDuplicate("accept") != "accept" {
		t.Errorf("DeDuplicate failed to not deduplicate the letter c from accept.")
	}
}

func TestIsAlphabetic(t *testing.T) {
	if IsAlphabetic("abc3") != false {
		t.Errorf("Fauled to determine that abc3 is not alphabetic.")
	}
	if IsAlphabetic("abc") != true {
		t.Errorf("Fauled to determine that abc is alphabetic.")
	}
}

func TestIntersect(t *testing.T) {
	if Intersect("context", "contentcontent") != "contet" {
		t.Errorf("Failed to find the intersect of context and contentcontent.")
	}
}

func TestTokenize(t *testing.T) {
	token := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
		"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
	if stringArrayNotEqual(Tokenize("abcdefghijklmnopqrstuvwxyz", 1), token) {
		t.Errorf("Failed to tokenize string by ngram 1.")
	}
	token = []string{"ab", "bc", "cd", "de", "ef", "fg", "gh", "hi", "ij",
		"jk", "kl", "lm", "mn", "no", "op", "pq", "qr", "rs", "st", "tu", "uv",
		"vw", "wx", "xy", "yz"}
	if stringArrayNotEqual(Tokenize("abcdefghijklmnopqrstuvwxyz", 2), token) {
		t.Errorf("Failed to tokenize string by ngram 2.")
	}
	token = []string{"abc", "bcd", "cde", "def", "efg", "fgh", "ghi", "hij",
		"ijk", "jkl", "klm", "lmn", "mno", "nop", "opq", "pqr", "qrs", "rst",
		"stu", "tuv", "uvw", "vwx", "wxy", "xyz"}
	if stringArrayNotEqual(Tokenize("abcdefghijklmnopqrstuvwxyz", 3), token) {
		t.Errorf("Failed to tokenize string by ngram 3.")
	}
}

// Compares string slices.
func stringArrayNotEqual(a []string, b []string) bool {
	if len(a) != len(b) {
		return true
	}
	for i, v := range a {
		if v != b[i] {
			return true
		}
	}
	return false
}
<commit_msg>Added some examples to the utl code.<commit_after>package utl

import (
	"testing"
)

func TestDeDuplicate(t *testing.T) {
	if DeDuplicate("a") != "a" {
		t.Errorf("DeDuplicate fails to deduplicate the single letter a.")
	}
	if DeDuplicate("c") != "c" {
		t.Errorf("DeDuplicate fails to deduplicate the single letter c.")
	}
	if DeDuplicate("buzz") != "buz" {
		t.Errorf("DeDuplicate fails to deduplicate the letter z from buzz.")
	}
	if DeDuplicate("accept") != "accept" {
		t.Errorf("DeDuplicate failed to not deduplicate the letter c from accept.")
	}
}

func ExampleDeDuplicate(t *testing.T) {
	DeDuplicate("buzz")
	// Output: buz
}
func ExampleDeDuplicate_2(t *testing.T) {
	DeDuplicate("accept")
	// Output: accept
}

func TestIsAlphabetic(t *testing.T) {
	if IsAlphabetic("abc3") != false {
		t.Errorf("Fauled to determine that abc3 is not alphabetic.")
	}
	if IsAlphabetic("abc") != true {
		t.Errorf("Fauled to determine that abc is alphabetic.")
	}
}

func ExampleIsAlphabetic(t *testing.T) {
	IsAlphabetic("abc")
	// Output: true
}
func ExampleIsAlphabetic_2(t *testing.T) {
	IsAlphabetic("abc1")
	// Output: false
}

func TestIntersect(t *testing.T) {
	if Intersect("context", "contentcontent") != "contet" {
		t.Errorf("Failed to find the intersect of context and contentcontent.")
	}
}

func TestTokenize(t *testing.T) {
	token := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
		"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
	if stringArrayNotEqual(Tokenize("abcdefghijklmnopqrstuvwxyz", 1), token) {
		t.Errorf("Failed to tokenize string by ngram 1.")
	}
	token = []string{"ab", "bc", "cd", "de", "ef", "fg", "gh", "hi", "ij",
		"jk", "kl", "lm", "mn", "no", "op", "pq", "qr", "rs", "st", "tu", "uv",
		"vw", "wx", "xy", "yz"}
	if stringArrayNotEqual(Tokenize("abcdefghijklmnopqrstuvwxyz", 2), token) {
		t.Errorf("Failed to tokenize string by ngram 2.")
	}
	token = []string{"abc", "bcd", "cde", "def", "efg", "fgh", "ghi", "hij",
		"ijk", "jkl", "klm", "lmn", "mno", "nop", "opq", "pqr", "qrs", "rst",
		"stu", "tuv", "uvw", "vwx", "wxy", "xyz"}
	if stringArrayNotEqual(Tokenize("abcdefghijklmnopqrstuvwxyz", 3), token) {
		t.Errorf("Failed to tokenize string by ngram 3.")
	}
}

func ExampleTokenize(t *testing.T) {
	Tokenize("abcd", 2)
	// Output: []string{"ab", "bc", "cd"}]
}

// Compares string slices.
func stringArrayNotEqual(a []string, b []string) bool {
	if len(a) != len(b) {
		return true
	}
	for i, v := range a {
		if v != b[i] {
			return true
		}
	}
	return false
}
<|endoftext|>


<commit_before>package dev

import (
	"cloud.google.com/go/monitoring/apiv3"
	"github.com/evandbrown/gcp-tools-release/src/firehose-to-fluentd/Godeps/_workspace/src/golang.org/x/net/context"
	"google.golang.org/api/option"
	monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
)

func ClearMetricDescriptors() {
	ctx := context.Background()
	metricClient, err := monitoring.NewMetricClient(ctx, option.WithScopes("https://www.googleapis.com/auth/monitoring"))
	if err != nil {
		panic(err)
	}

	req := &monitoringpb.ListMetricDescriptorsRequest{
		Name:   "projects/evandbrown17",
		Filter: "metric.type = starts_with(\"custom.googleapis.com/\")",
	}
	it := metricClient.ListMetricDescriptors(ctx, req)
	for {
		resp, err := it.Next()
		if err == monitoring.Done {
			break
		}
		if err != nil {
			// TODO: Handle error.
			panic(err)
		}

		req := &monitoringpb.DeleteMetricDescriptorRequest{
			Name: resp.Name,
		}
		err = metricClient.DeleteMetricDescriptor(ctx, req)
		if err != nil {
			panic(err)
		}
	}
}
<commit_msg>Fix import that relied on deleted code<commit_after>package dev

import (
	"cloud.google.com/go/monitoring/apiv3"
	"golang.org/x/net/context"
	"google.golang.org/api/option"
	monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
)

func ClearMetricDescriptors() {
	ctx := context.Background()
	metricClient, err := monitoring.NewMetricClient(ctx, option.WithScopes("https://www.googleapis.com/auth/monitoring"))
	if err != nil {
		panic(err)
	}

	req := &monitoringpb.ListMetricDescriptorsRequest{
		Name:   "projects/evandbrown17",
		Filter: "metric.type = starts_with(\"custom.googleapis.com/\")",
	}
	it := metricClient.ListMetricDescriptors(ctx, req)
	for {
		resp, err := it.Next()
		if err == monitoring.Done {
			break
		}
		if err != nil {
			// TODO: Handle error.
			panic(err)
		}

		req := &monitoringpb.DeleteMetricDescriptorRequest{
			Name: resp.Name,
		}
		err = metricClient.DeleteMetricDescriptor(ctx, req)
		if err != nil {
			panic(err)
		}
	}
}
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
 * Author: Alex Yatskov <alex@foosoft.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package main

import (
	"bytes"
	"fmt"
	"log"
	"path"
	"regexp"
	"strconv"
	"strings"

	"github.com/russross/blackfriday"
)

const (
	defNumCols = 80
	defTabSize = 4
)

const (
	flagNoToc = 1 << iota
	flagNoRules
	flagPascal
)

type list struct {
	index int
}

type heading struct {
	text  []byte
	level int
}

type vimDoc struct {
	filename string
	title    string
	desc     string
	cols     int
	tabs     int
	flags    int
	tocPos   int
	lists    []*list
	headings []*heading
}

func VimDocRenderer(filename, desc string, cols, tabs, flags int) blackfriday.Renderer {
	filename = path.Base(filename)
	title := filename

	if index := strings.LastIndex(filename, "."); index > -1 {
		title = filename[:index]
		if flags&flagPascal == 0 {
			title = strings.ToLower(title)
		}
	}

	return &vimDoc{
		filename: filename,
		title:    title,
		desc:     desc,
		cols:     cols,
		tabs:     tabs,
		flags:    flags,
		tocPos:   -1}
}

func (v *vimDoc) fixupCodeTags(input []byte) []byte {
	r := regexp.MustCompile(`(?m)^\s*([<>])$`)
	return r.ReplaceAll(input, []byte("$1"))
}

func (v *vimDoc) buildHelpTag(text []byte) []byte {
	if v.flags&flagPascal == 0 {
		text = bytes.ToLower(text)
		text = bytes.Replace(text, []byte{' '}, []byte{'_'}, -1)
	} else {
		text = bytes.Title(text)
		text = bytes.Replace(text, []byte{' '}, []byte{}, -1)
	}

	return []byte(fmt.Sprintf("%s-%s", v.title, text))
}

func (v *vimDoc) buildChapters(h *heading) []byte {
	index := -1
	{
		for i, curr := range v.headings {
			if curr == h {
				index = i
				break
			}
		}

		if index < 0 {
			log.Fatal("heading not found")
		}
	}

	var chapters []int
	{
		level := h.level
		siblings := 1

		for i := index - 1; i >= 0; i-- {
			curr := v.headings[i]

			if curr.level == level {
				siblings++
			} else if curr.level < level {
				chapters = append(chapters, siblings)
				level = curr.level
				siblings = 1
			}
		}

		chapters = append(chapters, siblings)
	}

	var out bytes.Buffer
	for i := len(chapters) - 1; i >= 0; i-- {
		out.WriteString(strconv.Itoa(chapters[i]))
		out.WriteString(".")
	}

	return out.Bytes()
}

func (v *vimDoc) writeSplitText(out *bytes.Buffer, left, right []byte, repeat string, trim int) {
	padding := v.cols - (len(left) + len(right)) + trim
	if padding <= 0 {
		padding = 1
	}

	out.Write(left)
	out.WriteString(strings.Repeat(repeat, padding))
	out.Write(right)
	out.WriteString("\n")
}

func (v *vimDoc) writeRule(out *bytes.Buffer, repeat string) {
	out.WriteString(strings.Repeat(repeat, v.cols))
	out.WriteString("\n")
}

func (v *vimDoc) writeToc(out *bytes.Buffer) {
	for _, h := range v.headings {
		title := fmt.Sprintf("%s%s %s", strings.Repeat(" ", (h.level-1)*v.tabs), v.buildChapters(h), h.text)
		link := fmt.Sprintf("|%s|", v.buildHelpTag(h.text))
		v.writeSplitText(out, []byte(title), []byte(link), ".", 2)
	}
}

func (v *vimDoc) writeIndent(out *bytes.Buffer, text string, trim int) {
	lines := strings.Split(text, "\n")

	for index, line := range lines {
		width := v.tabs
		if width >= trim && index == 0 {
			width -= trim
		}

		if len(line) > 0 {
			out.WriteString(strings.Repeat(" ", width))
			out.WriteString(line)
			out.WriteString("\n")
		}
	}
}

// Block-level callbacks
func (v *vimDoc) BlockCode(out *bytes.Buffer, text []byte, lang string) {
	out.WriteString(">\n")
	v.writeIndent(out, string(text), 0)
	out.WriteString("<\n\n")
}

func (v *vimDoc) BlockQuote(out *bytes.Buffer, text []byte) {
	out.WriteString(">\n")
	v.writeIndent(out, string(text), 0)
	out.WriteString("<\n\n")
}

func (v *vimDoc) BlockHtml(out *bytes.Buffer, text []byte) {
	out.WriteString(">\n")
	v.writeIndent(out, string(text), 0)
	out.WriteString("<\n\n")
}

func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) {
	initPos := out.Len()
	if v.flags&flagNoRules == 0 {
		switch level {
		case 1:
			v.writeRule(out, "=")
		case 2:
			v.writeRule(out, "-")
		}
	}

	headingPos := out.Len()
	if !text() {
		out.Truncate(initPos)
		return
	}

	var temp []byte
	temp = append(temp, out.Bytes()[headingPos:]...)
	out.Truncate(headingPos)

	h := &heading{temp, level}
	v.headings = append(v.headings, h)

	tag := fmt.Sprintf("*%s*", v.buildHelpTag(h.text))
	v.writeSplitText(out, bytes.ToUpper(h.text), []byte(tag), " ", 2)
	out.WriteString("\n")
}

func (v *vimDoc) HRule(out *bytes.Buffer) {
	v.writeRule(out, "-")
}

func (v *vimDoc) List(out *bytes.Buffer, text func() bool, flags int) {
	v.lists = append(v.lists, &list{1})
	text()
	v.lists = v.lists[:len(v.lists)-1]
}

func (v *vimDoc) ListItem(out *bytes.Buffer, text []byte, flags int) {
	marker := out.Len()

	list := v.lists[len(v.lists)-1]
	if flags&blackfriday.LIST_TYPE_ORDERED == blackfriday.LIST_TYPE_ORDERED {
		out.WriteString(fmt.Sprintf("%d. ", list.index))
		list.index++
	} else {
		out.WriteString("* ")
	}

	v.writeIndent(out, string(text), out.Len()-marker)

	if flags&blackfriday.LIST_ITEM_END_OF_LIST != 0 {
		out.WriteString("\n")
	}
}

func (*vimDoc) Paragraph(out *bytes.Buffer, text func() bool) {
	marker := out.Len()

	if !text() {
		out.Truncate(marker)
		return
	}

	out.WriteString("\n\n")
}

func (*vimDoc) Table(out *bytes.Buffer, heading []byte, body []byte, columnData []int) {
	// unimplemented
	log.Println("Table is unimplemented")
}

func (*vimDoc) TableRow(out *bytes.Buffer, text []byte) {
	// unimplemented
	log.Println("TableRow is unimplemented")
}

func (*vimDoc) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {
	// unimplemented
	log.Println("TableHeaderCell is unimplemented")
}

func (*vimDoc) TableCell(out *bytes.Buffer, text []byte, flags int) {
	// unimplemented
	log.Println("TableCell is unimplemented")
}

func (*vimDoc) Footnotes(out *bytes.Buffer, text func() bool) {
	// unimplemented
	log.Println("Footnotes is unimplemented")
}

func (*vimDoc) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
	// unimplemented
	log.Println("FootnoteItem is unimplemented")
}

func (*vimDoc) TitleBlock(out *bytes.Buffer, text []byte) {
	// unimplemented
	log.Println("TitleBlock is unimplemented")
}

// Span-level callbacks
func (*vimDoc) AutoLink(out *bytes.Buffer, link []byte, kind int) {
	out.Write(link)
}

func (*vimDoc) CodeSpan(out *bytes.Buffer, text []byte) {
	r := regexp.MustCompile(`\s`)

	// vim does not correctly highlight space-delimited words in code spans
	if !r.Match(text) {
		out.WriteString("`")
		out.Write(text)
		out.WriteString("`")
	}
}

func (*vimDoc) DoubleEmphasis(out *bytes.Buffer, text []byte) {
	out.Write(text)
}

func (*vimDoc) Emphasis(out *bytes.Buffer, text []byte) {
	out.Write(text)
}

func (*vimDoc) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
	// cannot view images in vim
}

func (*vimDoc) LineBreak(out *bytes.Buffer) {
	out.WriteString("\n")
}

func (*vimDoc) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
	out.WriteString(fmt.Sprintf("%s (%s)", content, link))
}

func (*vimDoc) RawHtmlTag(out *bytes.Buffer, tag []byte) {
	// unimplemented
	log.Println("StrikeThrough is unimplemented")
}

func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) {
	out.Write(text)
}

func (*vimDoc) StrikeThrough(out *bytes.Buffer, text []byte) {
	// unimplemented
	log.Println("StrikeThrough is unimplemented")
}

func (*vimDoc) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
	// unimplemented
	log.Println("FootnoteRef is unimplemented")
}

// Low-level callbacks
func (v *vimDoc) Entity(out *bytes.Buffer, entity []byte) {
	out.Write(entity)
}

func (v *vimDoc) NormalText(out *bytes.Buffer, text []byte) {
	out.Write(text)
}

// Header and footer
func (v *vimDoc) DocumentHeader(out *bytes.Buffer) {
	if len(v.desc) > 0 {
		v.writeSplitText(out, []byte(v.filename), []byte(v.desc), " ", 0)
	} else {
		out.WriteString(v.filename)
		out.WriteString("\n")
	}

	out.WriteString("\n")
	v.tocPos = out.Len()
}

func (v *vimDoc) DocumentFooter(out *bytes.Buffer) {
	var temp bytes.Buffer

	if v.tocPos > 0 && v.flags&flagNoToc == 0 {
		temp.Write(out.Bytes()[:v.tocPos])

		v.writeRule(&temp, "=")
		title := []byte("contents")
		tag := fmt.Sprintf("*%s*", v.buildHelpTag(title))
		v.writeSplitText(&temp, bytes.ToUpper(title), []byte(tag), " ", 2)
		temp.WriteString("\n")
		v.writeToc(&temp)
		temp.WriteString("\n")

		temp.Write(out.Bytes()[v.tocPos:])
	} else {
		temp.ReadFrom(out)
	}

	out.Reset()
	out.Write(v.fixupCodeTags(temp.Bytes()))
}

func (v *vimDoc) GetFlags() int {
	return v.flags
}
<commit_msg>Small tweak<commit_after>/*
 * Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
 * Author: Alex Yatskov <alex@foosoft.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package main

import (
	"bytes"
	"fmt"
	"log"
	"path"
	"regexp"
	"strconv"
	"strings"

	"github.com/russross/blackfriday"
)

const (
	defNumCols = 80
	defTabSize = 4
)

const (
	flagNoToc = 1 << iota
	flagNoRules
	flagPascal
)

type list struct {
	index int
}

type heading struct {
	text  []byte
	level int
}

type vimDoc struct {
	filename string
	title    string
	desc     string
	cols     int
	tabs     int
	flags    int
	tocPos   int
	lists    []*list
	headings []*heading
}

func VimDocRenderer(filename, desc string, cols, tabs, flags int) blackfriday.Renderer {
	filename = path.Base(filename)
	title := filename

	if index := strings.LastIndex(filename, "."); index > -1 {
		title = filename[:index]
		if flags&flagPascal == 0 {
			title = strings.ToLower(title)
		}
	}

	return &vimDoc{
		filename: filename,
		title:    title,
		desc:     desc,
		cols:     cols,
		tabs:     tabs,
		flags:    flags,
		tocPos:   -1}
}

func (v *vimDoc) fixupCodeTags(input []byte) []byte {
	r := regexp.MustCompile(`(?m)^\s*([<>])$`)
	return r.ReplaceAll(input, []byte("$1"))
}

func (v *vimDoc) buildHelpTag(text []byte) []byte {
	if v.flags&flagPascal == 0 {
		text = bytes.ToLower(text)
		text = bytes.Replace(text, []byte{' '}, []byte{'_'}, -1)
	} else {
		text = bytes.Title(text)
		text = bytes.Replace(text, []byte{' '}, []byte{}, -1)
	}

	return []byte(fmt.Sprintf("%s-%s", v.title, text))
}

func (v *vimDoc) buildChapters(h *heading) []byte {
	index := -1
	{
		for i, curr := range v.headings {
			if curr == h {
				index = i
				break
			}
		}

		if index < 0 {
			log.Fatal("heading not found")
		}
	}

	var chapters []int
	{
		level := h.level
		siblings := 1

		for i := index - 1; i >= 0; i-- {
			curr := v.headings[i]

			if curr.level == level {
				siblings++
			} else if curr.level < level {
				chapters = append(chapters, siblings)
				level = curr.level
				siblings = 1
			}
		}

		chapters = append(chapters, siblings)
	}

	var out bytes.Buffer
	for i := len(chapters) - 1; i >= 0; i-- {
		out.WriteString(strconv.Itoa(chapters[i]))
		out.WriteString(".")
	}

	return out.Bytes()
}

func (v *vimDoc) writeSplitText(out *bytes.Buffer, left, right []byte, repeat string, trim int) {
	padding := v.cols - (len(left) + len(right)) + trim
	if padding <= 0 {
		padding = 1
	}

	out.Write(left)
	out.WriteString(strings.Repeat(repeat, padding))
	out.Write(right)
	out.WriteString("\n")
}

func (v *vimDoc) writeRule(out *bytes.Buffer, repeat string) {
	out.WriteString(strings.Repeat(repeat, v.cols))
	out.WriteString("\n")
}

func (v *vimDoc) writeToc(out *bytes.Buffer) {
	for _, h := range v.headings {
		title := fmt.Sprintf("%s%s %s", strings.Repeat(" ", (h.level-1)*v.tabs), v.buildChapters(h), h.text)
		link := fmt.Sprintf("|%s|", v.buildHelpTag(h.text))
		v.writeSplitText(out, []byte(title), []byte(link), ".", 2)
	}
}

func (v *vimDoc) writeIndent(out *bytes.Buffer, text string, trim int) {
	lines := strings.Split(text, "\n")

	for index, line := range lines {
		width := v.tabs
		if width >= trim && index == 0 {
			width -= trim
		}

		if len(line) > 0 {
			out.WriteString(strings.Repeat(" ", width))
			out.WriteString(line)
			out.WriteString("\n")
		}
	}
}

// Block-level callbacks
func (v *vimDoc) BlockCode(out *bytes.Buffer, text []byte, lang string) {
	out.WriteString(">\n")
	v.writeIndent(out, string(text), 0)
	out.WriteString("<\n\n")
}

func (v *vimDoc) BlockQuote(out *bytes.Buffer, text []byte) {
	out.WriteString(">\n")
	v.writeIndent(out, string(text), 0)
	out.WriteString("<\n\n")
}

func (v *vimDoc) BlockHtml(out *bytes.Buffer, text []byte) {
	out.WriteString(">\n")
	v.writeIndent(out, string(text), 0)
	out.WriteString("<\n\n")
}

func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) {
	initPos := out.Len()
	if v.flags&flagNoRules == 0 {
		switch level {
		case 1:
			v.writeRule(out, "=")
		case 2:
			v.writeRule(out, "-")
		}
	}

	headingPos := out.Len()
	if !text() {
		out.Truncate(initPos)
		return
	}

	var temp []byte
	temp = append(temp, out.Bytes()[headingPos:]...)
	out.Truncate(headingPos)

	h := &heading{temp, level}
	v.headings = append(v.headings, h)

	tag := fmt.Sprintf("*%s*", v.buildHelpTag(h.text))
	v.writeSplitText(out, bytes.ToUpper(h.text), []byte(tag), " ", 2)
	out.WriteString("\n")
}

func (v *vimDoc) HRule(out *bytes.Buffer) {
	v.writeRule(out, "-")
}

func (v *vimDoc) List(out *bytes.Buffer, text func() bool, flags int) {
	v.lists = append(v.lists, &list{1})
	text()
	v.lists = v.lists[:len(v.lists)-1]
}

func (v *vimDoc) ListItem(out *bytes.Buffer, text []byte, flags int) {
	marker := out.Len()

	list := v.lists[len(v.lists)-1]
	if flags&blackfriday.LIST_TYPE_ORDERED == blackfriday.LIST_TYPE_ORDERED {
		out.WriteString(fmt.Sprintf("%d. ", list.index))
		list.index++
	} else {
		out.WriteString("* ")
	}

	v.writeIndent(out, string(text), out.Len()-marker)

	if flags&blackfriday.LIST_ITEM_END_OF_LIST != 0 {
		out.WriteString("\n")
	}
}

func (*vimDoc) Paragraph(out *bytes.Buffer, text func() bool) {
	marker := out.Len()

	if !text() {
		out.Truncate(marker)
		return
	}

	out.WriteString("\n\n")
}

func (*vimDoc) Table(out *bytes.Buffer, heading []byte, body []byte, columnData []int) {
	// unimplemented
	log.Println("Table is unimplemented")
}

func (*vimDoc) TableRow(out *bytes.Buffer, text []byte) {
	// unimplemented
	log.Println("TableRow is unimplemented")
}

func (*vimDoc) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {
	// unimplemented
	log.Println("TableHeaderCell is unimplemented")
}

func (*vimDoc) TableCell(out *bytes.Buffer, text []byte, flags int) {
	// unimplemented
	log.Println("TableCell is unimplemented")
}

func (*vimDoc) Footnotes(out *bytes.Buffer, text func() bool) {
	// unimplemented
	log.Println("Footnotes is unimplemented")
}

func (*vimDoc) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
	// unimplemented
	log.Println("FootnoteItem is unimplemented")
}

func (*vimDoc) TitleBlock(out *bytes.Buffer, text []byte) {
	// unimplemented
	log.Println("TitleBlock is unimplemented")
}

// Span-level callbacks
func (*vimDoc) AutoLink(out *bytes.Buffer, link []byte, kind int) {
	out.Write(link)
}

func (*vimDoc) CodeSpan(out *bytes.Buffer, text []byte) {
	r := regexp.MustCompile(`\s`)

	// vim does not correctly highlight space-delimited words in code spans
	if !r.Match(text) {
		out.WriteString("`")
		out.Write(text)
		out.WriteString("`")
	}
}

func (*vimDoc) DoubleEmphasis(out *bytes.Buffer, text []byte) {
	out.Write(text)
}

func (*vimDoc) Emphasis(out *bytes.Buffer, text []byte) {
	out.Write(text)
}

func (*vimDoc) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
	// cannot view images in vim
}

func (*vimDoc) LineBreak(out *bytes.Buffer) {
	out.WriteString("\n")
}

func (*vimDoc) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
	out.WriteString(fmt.Sprintf("%s (%s)", content, link))
}

func (*vimDoc) RawHtmlTag(out *bytes.Buffer, tag []byte) {
	// unimplemented
	log.Println("StrikeThrough is unimplemented")
}

func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) {
	out.Write(text)
}

func (*vimDoc) StrikeThrough(out *bytes.Buffer, text []byte) {
	// unimplemented
	log.Println("StrikeThrough is unimplemented")
}

func (*vimDoc) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
	// unimplemented
	log.Println("FootnoteRef is unimplemented")
}

// Low-level callbacks
func (v *vimDoc) Entity(out *bytes.Buffer, entity []byte) {
	out.Write(entity)
}

func (v *vimDoc) NormalText(out *bytes.Buffer, text []byte) {
	out.Write(text)
}

// Header and footer
func (v *vimDoc) DocumentHeader(out *bytes.Buffer) {
	if len(v.desc) > 0 {
		v.writeSplitText(out, []byte(v.filename), []byte(v.desc), " ", 0)
	} else {
		out.WriteString(v.filename)
		out.WriteString("\n")
	}

	out.WriteString("\n")
	v.tocPos = out.Len()
}

func (v *vimDoc) DocumentFooter(out *bytes.Buffer) {
	var temp bytes.Buffer

	if v.tocPos > 0 && v.flags&flagNoToc == 0 {
		temp.Write(out.Bytes()[:v.tocPos])

		v.writeRule(&temp, "=")
		title := []byte("Contents")
		tag := fmt.Sprintf("*%s*", v.buildHelpTag(title))
		v.writeSplitText(&temp, bytes.ToUpper(title), []byte(tag), " ", 2)
		temp.WriteString("\n")
		v.writeToc(&temp)
		temp.WriteString("\n")

		temp.Write(out.Bytes()[v.tocPos:])
	} else {
		temp.ReadFrom(out)
	}

	out.Reset()
	out.Write(v.fixupCodeTags(temp.Bytes()))
}

func (v *vimDoc) GetFlags() int {
	return v.flags
}
<|endoftext|>


<commit_before>
package blend

func HSLtoRGB() {

}

func RGBtoHSL() {

}
<commit_msg>Added to color.go<commit_after><|endoftext|>


<commit_before>package hsm

import "container/List"
import "reflect"
import "fmt"

const (
    EventEmpty = iota
    EventInit
    EventEntry
    EventExit
    EventUser
)

type Event interface {
    Type() uint32
}

type StdEvent struct {
    type_ uint32
}

func (stdEvent *StdEvent) Type() uint32 {
    return stdEvent.type_
}

type State interface {
    ID() string

    GetSuper() *State
    SetSuper(super *State) void
    GetChildren() []*State
    AddChild(hsm *HSM, child *State) void
    GetState() *State
    setState(state *State) void

    Entry(hsm *HSM, event *Event) *State
    Exit(hsm *HSM, event *Event) *State
    Handle(hsm *HSM, event *Event) *State
}

type StateHead struct {
    super    *State
    children list.List
}

func NewStateHead(super *State) (*StateHead, error) {
    children = list.List.New()
    children.Init()
    head = &StateHead{
        super:    super,
        children: children,
    }
    return &head, nil
}

func (head *StateHead) AddChild(hsm *HSM, child *State) {
    head.children.PushBack(child)
    hsm.NodeMap[child.state.ID()] = child
}

type HSM struct {
    SourceState *State
    State       *State
    NodeMap     map[string]*State
}

func NewHSM(top *State) (*HSM, error) {
    hsm = &HSM{
        SourceState: nil,
        State:       top,
        NodeMap:     make(map[string]*State)}
    return hsm, nil
}

func (hsm *HSM) Dispatch(event *Event) {
    newState = hsm.State(event)
    HSM.State = newState
}

func TRIGGER_ENTRY(hsm *HSM, state *State, event *Event) *State {
    return node.Entry(hsm, event)
}

func TRIGGER_EXIT(hsm *HSM, state *State, event *Event) *State {
    return state.Exit(hsm, event)
}

func TRIGGER(hsm *HSM, state *State, event *Event) *State {
    return state.Handle(hsm, event)
}

func (hsm *HSM) TRAN(targetID string, event *Event) {
    target = hsm.NodeMap[targetID]
    for s := hsm.State; s != hsm.SourceState; {
        t := TRIGGER_EXIT(hsm, s, event)
        if t != nil {
            s = t
        } else {
            s = TRIGGER(hsm, s, &StdEvent{EventEmpty})
        }
    }
}

type S1 struct {
}

func NewS1() (*S1, error) {
    return &S1{}, nil
}

func (*S1) Name() string {
    return "S1"
}

func (*S1) Entry(event *Event) {
    fmt.Println("S1:Entry event=", event)
}

func (*S1) Exit(event *Event) {
    fmt.Println("S1:Exit event=", event)
}

type S11 struct {
    super S1
}

func NewS11() (*S11, error) {
    return &S11{}, nil
}

func (*S11) Name() string {
    return "S11"
}

func (*S11) Entry(event *Event) {
    fmt.Println("S11:Entry event=", event)
}

func (*S11) Exit(event *Event) {
    fmt.Println("S12:Exit event=", event)
}

type S12 struct {
    super S1
}

func NewS12() (*S12, error) {
    return &S12{}, nil
}

func (*S12) Name() string {
    return "S12"
}

func (*S12) Entry(event *Event) {
    fmt.Printnl("S12:Entry event=", event)
}

func (*S12) Exit(event *Event) {
    fmt.Println("S12:Exit event=", event)
}

type TopState struct {
}

func NewTopState() (*TopState, error) {
    return &TopState{}
}

func (*TopState) Name() string {
    return "TOP"
}

func (*TopState) Entry(event *Event) {
    TRAN("S1")
    return
}

func (*TopState) Exit(event *Event) {
    return
}

func main() {
    top, err = NewTopState()
    rootNode = NewState(nil, top)
    s1, err = NewS1()
    n1 = NewTreeNode(rootNode, s1)
    rootNode.AddChild(n1)
    s11, err = NewS11()
    n11 = NewTreeNode(n1, s11)
    n1.AddChild(n11)
    s12, err = NewS12()
    n12 = NewTreeNode(n1, s12)
    n1.AddChild(n12)

}
<commit_msg>add `Init'<commit_after>package hsm

import "container/List"
import "reflect"
import "fmt"

const (
    EventEmpty = iota
    EventInit
    EventEntry
    EventExit
    EventUser
)

type Event interface {
    Type() uint32
}

type StdEvent struct {
    type_ uint32
}

func (stdEvent *StdEvent) Type() uint32 {
    return stdEvent.type_
}

type State interface {
    ID() string

    GetSuper() *State
    SetSuper(super *State) void
    GetChildren() []*State
    AddChild(hsm *HSM, child *State) void
    GetState() *State
    setState(state *State) void

    Entry(hsm *HSM, event *Event) *State
    Exit(hsm *HSM, event *Event) *State
    Handle(hsm *HSM, event *Event) *State
}

type StateHead struct {
    super    *State
    children list.List
}

func NewStateHead(super *State) (*StateHead, error) {
    children = list.List.New()
    children.Init()
    head = &StateHead{
        super:    super,
        children: children,
    }
    return &head, nil
}

func (head *StateHead) AddChild(hsm *HSM, child *State) {
    head.children.PushBack(child)
    hsm.NodeMap[child.state.ID()] = child
}

type HSM struct {
    SourceState *State
    State       *State
    NodeMap     map[string]*State
}

func NewHSM(top *State) (*HSM, error) {
    hsm = &HSM{
        SourceState: nil,
        State:       top,
        NodeMap:     make(map[string]*State)}
    return hsm, nil
}

func (hsm *HSM) Init(event *Event) {

}

func (hsm *HSM) Dispatch(event *Event) {

    newState = hsm.State(event)
    HSM.State = newState
}

func TRIGGER_ENTRY(hsm *HSM, state *State, event *Event) *State {
    return node.Entry(hsm, event)
}

func TRIGGER_EXIT(hsm *HSM, state *State, event *Event) *State {
    return state.Exit(hsm, event)
}

func TRIGGER(hsm *HSM, state *State, event *Event) *State {
    return state.Handle(hsm, event)
}

func (hsm *HSM) TRAN(targetID string, event *Event) {
    target = hsm.NodeMap[targetID]
    for s := hsm.State; s != hsm.SourceState; {
        t := TRIGGER_EXIT(hsm, s, event)
        if t != nil {
            s = t
        } else {
            s = TRIGGER(hsm, s, &StdEvent{EventEmpty})
        }
    }
}

type S1 struct {
}

func NewS1() (*S1, error) {
    return &S1{}, nil
}

func (*S1) Name() string {
    return "S1"
}

func (*S1) Entry(event *Event) {
    fmt.Println("S1:Entry event=", event)
}

func (*S1) Exit(event *Event) {
    fmt.Println("S1:Exit event=", event)
}

type S11 struct {
    super S1
}

func NewS11() (*S11, error) {
    return &S11{}, nil
}

func (*S11) Name() string {
    return "S11"
}

func (*S11) Entry(event *Event) {
    fmt.Println("S11:Entry event=", event)
}

func (*S11) Exit(event *Event) {
    fmt.Println("S12:Exit event=", event)
}

type S12 struct {
    super S1
}

func NewS12() (*S12, error) {
    return &S12{}, nil
}

func (*S12) Name() string {
    return "S12"
}

func (*S12) Entry(event *Event) {
    fmt.Printnl("S12:Entry event=", event)
}

func (*S12) Exit(event *Event) {
    fmt.Println("S12:Exit event=", event)
}

type TopState struct {
}

func NewTopState() (*TopState, error) {
    return &TopState{}
}

func (*TopState) Name() string {
    return "TOP"
}

func (*TopState) Entry(event *Event) {
    TRAN("S1")
    return
}

func (*TopState) Exit(event *Event) {
    return
}

func main() {
    top, err = NewTopState()
    rootNode = NewState(nil, top)
    s1, err = NewS1()
    n1 = NewTreeNode(rootNode, s1)
    rootNode.AddChild(n1)
    s11, err = NewS11()
    n11 = NewTreeNode(n1, s11)
    n1.AddChild(n11)
    s12, err = NewS12()
    n12 = NewTreeNode(n1, s12)
    n1.AddChild(n12)

}
<|endoftext|>


<commit_before>package htm

import (
	"errors"
	"fmt"

	"azul3d.org/gfx.v1"
	"azul3d.org/lmath.v1"
)

type HTM struct {
	Vertices []lmath.Vec3
	Indices  [][]int
	Nodes    map[string]int
}

func NewHTM() *HTM {
	return &HTM{
		Vertices: []lmath.Vec3{
			{0, 0, 1},
			{1, 0, 0},
			{0, 1, 0},
			{-1, 0, 0},
			{0, -1, 0},
			{0, 0, -1},
		},
		Indices: [][]int{
			{1, 5, 2}, // S0
			{2, 5, 3}, // S1
			{3, 5, 4}, // S2
			{4, 5, 1}, // S3

			{1, 0, 4}, // N0
			{4, 0, 3}, // N1
			{3, 0, 2}, // N2
			{2, 0, 1}, // N3
		},
		Nodes: map[string]int{
			"S0": 0,
			"S1": 1,
			"S2": 2,
			"S3": 3,
			"N0": 4,
			"N1": 5,
			"N2": 6,
			"N3": 7,
		},
	}
}

func (h *HTM) LookupNode(x string) ([]int, error) {
	if i, ok := h.Nodes[x]; ok {
		return h.Indices[i], nil
	}
	return nil, errors.New(fmt.Sprintf("Node name '%s' does not exist.", x))
}

func (h *HTM) ConnectMidpoints(i0, i1 int) lmath.Vec3 {
	v0, v1 := h.Vertices[i0], h.Vertices[i1]
	w0, _ := v0.Add(v1).Normalized()
	return w0
}

func (h *HTM) IterLevels(x int) error {
	for i := 2; i <= x; i++ {
		for k := range h.Nodes {
			if len(k) == i {
				err := h.NextLevel(k)
				if err != nil {
					return err
				}
			}
		}
	}
	return nil
}

func (h *HTM) NextLevel(x string) error {
	n, err := h.LookupNode(x)
	if err != nil {
		return err
	}

	v0, v1, v2 := n[0], n[1], n[2]

	w0 := h.ConnectMidpoints(v1, v2)
	w1 := h.ConnectMidpoints(v0, v2)
	w2 := h.ConnectMidpoints(v0, v1)

	h.Vertices = append(h.Vertices, w0, w1, w2)

	l := len(h.Vertices)
	ind := [][]int{
		{v0, l - 1, l - 2},    // v0, w2, w1
		{v1, l - 3, l - 1},    // v1, w0, w2
		{v2, l - 2, l - 3},    // v2, w1, w0
		{l - 3, l - 2, l - 1}, // w0, w1, w2
	}

	h.Nodes[x+"0"] = len(h.Indices)
	h.Nodes[x+"1"] = len(h.Indices) + 1
	h.Nodes[x+"2"] = len(h.Indices) + 2
	h.Nodes[x+"3"] = len(h.Indices) + 3

	h.Indices = append(h.Indices, ind...)

	return nil
}

func (h *HTM) ConvertVertices() []gfx.Vec3 {
	var vertices []gfx.Vec3
	for _, a := range h.Vertices {
		vertices = append(vertices, gfx.ConvertVec3(a))
	}
	return vertices
}

func (h *HTM) ConvertIndices() []uint32 {
	var indices []uint32
	for _, a := range h.Indices {
		for _, b := range a {
			indices = append(indices, uint32(b))
		}
	}
	return indices
}

func (h *HTM) NewObject() *gfx.Object {
	o := gfx.NewObject()
	m := gfx.NewMesh()
	m.Vertices = h.ConvertVertices()
	m.Indices = h.ConvertIndices()
	o.Meshes = []*gfx.Mesh{m}
	return o
}

func (h *HTM) PPrint() {
	for _, v := range h.Vertices {
		fmt.Println(v)
	}
	for _, n := range h.Indices {
		fmt.Println(n)
	}
}
<commit_msg>proper impl<commit_after>package htm

import (
	"azul3d.org/lmath.v1"
)

type Tree struct {
	Name     string
	Indices  [3]int
	Vertices *[]lmath.Vec3

	T0 *Tree
	T1 *Tree
	T2 *Tree
	T3 *Tree
}

func NewTree(name string, verts *[]lmath.Vec3, i0, i1, i2 int) *Tree {
	return &Tree{
		Name:     name,
		Indices:  [3]int{i0, i1, i2},
		Vertices: verts,
	}
}

func (t *Tree) SubDivide(level int) {
	if len(t.Name) > level {
		return
	}

	i0, i1, i2 := t.Indices[0], t.Indices[1], t.Indices[2]
	v0, v1, v2 := (*t.Vertices)[i0], (*t.Vertices)[i1], (*t.Vertices)[i2]

	w0, _ := v1.Add(v2).Normalized()
	w1, _ := v0.Add(v2).Normalized()
	w2, _ := v0.Add(v1).Normalized()

	*t.Vertices = append(*t.Vertices, w0, w1, w2)

	l := len(*t.Vertices)

	t.T0 = NewTree(t.Name+"0", t.Vertices, i0, l-1, l-2)  // v0, w2, w1
	t.T1 = NewTree(t.Name+"1", t.Vertices, i1, l-3, l-1)  // v1, w0, w2
	t.T2 = NewTree(t.Name+"2", t.Vertices, i2, l-2, l-3)  // v2, w1, w0
	t.T3 = NewTree(t.Name+"3", t.Vertices, l-3, l-2, l-1) // w0, w1, w2

	t.T0.SubDivide(level)
	t.T1.SubDivide(level)
	t.T2.SubDivide(level)
	t.T3.SubDivide(level)
}

func (t *Tree) CollectIndices(indices *[]uint32) {
	if t.T0 == nil {
		*indices = append(*indices, uint32(t.Indices[0]), uint32(t.Indices[1]), uint32(t.Indices[2]))
	} else {
		t.T0.CollectIndices(indices)
		t.T1.CollectIndices(indices)
		t.T2.CollectIndices(indices)
		t.T3.CollectIndices(indices)
	}
}

type HTM struct {
	Vertices *[]lmath.Vec3

	S0, S1, S2, S3 *Tree
	N0, N1, N2, N3 *Tree
}

func New() *HTM {
	verts := []lmath.Vec3{
		{0, 0, 1},
		{1, 0, 0},
		{0, 1, 0},
		{-1, 0, 0},
		{0, -1, 0},
		{0, 0, -1},
	}
	return &HTM{
		Vertices: &verts,

		S0: NewTree("S0", &verts, 1, 5, 2),
		S1: NewTree("S1", &verts, 2, 5, 3),
		S2: NewTree("S2", &verts, 3, 5, 4),
		S3: NewTree("S3", &verts, 4, 5, 1),
		N0: NewTree("N0", &verts, 1, 0, 4),
		N1: NewTree("N1", &verts, 4, 0, 3),
		N2: NewTree("N2", &verts, 3, 0, 2),
		N3: NewTree("N3", &verts, 2, 0, 1),
	}
}

func (h *HTM) SubDivide(level int) {
	h.S0.SubDivide(level)
	h.S1.SubDivide(level)
	h.S2.SubDivide(level)
	h.S3.SubDivide(level)
	h.N0.SubDivide(level)
	h.N1.SubDivide(level)
	h.N2.SubDivide(level)
	h.N3.SubDivide(level)
}

func (h *HTM) Indices() []uint32 {
	var indices []uint32
	h.S0.CollectIndices(&indices)
	h.S1.CollectIndices(&indices)
	h.S2.CollectIndices(&indices)
	h.S3.CollectIndices(&indices)
	h.N0.CollectIndices(&indices)
	h.N1.CollectIndices(&indices)
	h.N2.CollectIndices(&indices)
	h.N3.CollectIndices(&indices)
	return indices
}
<|endoftext|>


<commit_before>/*
Copyright 2019 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package addons

import (
	"fmt"
	"path"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/golang/glog"
	"github.com/pkg/errors"
	"github.com/spf13/viper"

	"k8s.io/minikube/pkg/drivers/kic/oci"
	"k8s.io/minikube/pkg/kapi"
	"k8s.io/minikube/pkg/minikube/assets"
	"k8s.io/minikube/pkg/minikube/command"
	"k8s.io/minikube/pkg/minikube/config"
	"k8s.io/minikube/pkg/minikube/constants"
	"k8s.io/minikube/pkg/minikube/driver"
	"k8s.io/minikube/pkg/minikube/exit"
	"k8s.io/minikube/pkg/minikube/machine"
	"k8s.io/minikube/pkg/minikube/out"
	"k8s.io/minikube/pkg/minikube/out/register"
	"k8s.io/minikube/pkg/minikube/storageclass"
	"k8s.io/minikube/pkg/util/retry"
)

// defaultStorageClassProvisioner is the name of the default storage class provisioner
const defaultStorageClassProvisioner = "standard"

// RunCallbacks runs all actions associated to an addon, but does not set it (thread-safe)
func RunCallbacks(cc *config.ClusterConfig, name string, value string) error {
	glog.Infof("Setting %s=%s in profile %q", name, value, cc.Name)
	a, valid := isAddonValid(name)
	if !valid {
		return errors.Errorf("%s is not a valid addon", name)
	}

	// Run any additional validations for this property
	if err := run(cc, name, value, a.validations); err != nil {
		return errors.Wrap(err, "running validations")
	}

	// Run any callbacks for this property
	if err := run(cc, name, value, a.callbacks); err != nil {
		return errors.Wrap(err, "running callbacks")
	}
	return nil
}

// Set sets a value in the config (not threadsafe)
func Set(cc *config.ClusterConfig, name string, value string) error {
	a, valid := isAddonValid(name)
	if !valid {
		return errors.Errorf("%s is not a valid addon", name)
	}
	return a.set(cc, name, value)
}

// SetAndSave sets a value and saves the config
func SetAndSave(profile string, name string, value string) error {
	cc, err := config.Load(profile)
	if err != nil {
		return errors.Wrap(err, "loading profile")
	}

	if err := RunCallbacks(cc, name, value); err != nil {
		return errors.Wrap(err, "run callbacks")
	}

	if err := Set(cc, name, value); err != nil {
		return errors.Wrap(err, "set")
	}

	glog.Infof("Writing out %q config to set %s=%v...", profile, name, value)
	return config.Write(profile, cc)
}

// Runs all the validation or callback functions and collects errors
func run(cc *config.ClusterConfig, name string, value string, fns []setFn) error {
	var errors []error
	for _, fn := range fns {
		err := fn(cc, name, value)
		if err != nil {
			errors = append(errors, err)
		}
	}
	if len(errors) > 0 {
		return fmt.Errorf("%v", errors)
	}
	return nil
}

// SetBool sets a bool value in the config (not threadsafe)
func SetBool(cc *config.ClusterConfig, name string, val string) error {
	b, err := strconv.ParseBool(val)
	if err != nil {
		return err
	}
	if cc.Addons == nil {
		cc.Addons = map[string]bool{}
	}
	cc.Addons[name] = b
	return nil
}

// enableOrDisableAddon updates addon status executing any commands necessary
func enableOrDisableAddon(cc *config.ClusterConfig, name string, val string) error {
	glog.Infof("Setting addon %s=%s in %q", name, val, cc.Name)
	enable, err := strconv.ParseBool(val)
	if err != nil {
		return errors.Wrapf(err, "parsing bool: %s", name)
	}
	addon := assets.Addons[name]

	// check addon status before enabling/disabling it
	if isAddonAlreadySet(cc, addon, enable) {
		glog.Warningf("addon %s should already be in state %v", name, val)
		if !enable {
			return nil
		}
	}

	// to match both ingress and ingress-dns adons
	if strings.HasPrefix(name, "ingress") && enable && driver.IsKIC(cc.Driver) && runtime.GOOS != "linux" {
		exit.UsageT(`Due to {{.driver_name}} networking limitations on {{.os_name}}, {{.addon_name}} addon is not supported for this driver.
Alternatively to use this addon you can use a vm-based driver:

	'minikube start --vm=true'

To track the update on this work in progress feature please check:
https://github.com/kubernetes/minikube/issues/7332`, out.V{"driver_name": cc.Driver, "os_name": runtime.GOOS, "addon_name": name})
	}

	if strings.HasPrefix(name, "istio") && enable {
		minMem := 8192
		minCPUs := 4
		if cc.Memory < minMem {
			out.WarningT("Istio needs {{.minMem}}MB of memory -- your configuration only allocates {{.memory}}MB", out.V{"minMem": minMem, "memory": cc.Memory})
		}
		if cc.CPUs < minCPUs {
			out.WarningT("Istio needs {{.minCPUs}} CPUs -- your configuration only allocates {{.cpus}} CPUs", out.V{"minCPUs": minCPUs, "cpus": cc.CPUs})
		}
	}

	// TODO(r2d4): config package should not reference API, pull this out
	api, err := machine.NewAPIClient()
	if err != nil {
		return errors.Wrap(err, "machine client")
	}
	defer api.Close()

	cp, err := config.PrimaryControlPlane(cc)
	if err != nil {
		exit.WithError("Error getting primary control plane", err)
	}

	mName := driver.MachineName(*cc, cp)
	host, err := machine.LoadHost(api, mName)
	if err != nil || !machine.IsRunning(api, mName) {
		glog.Warningf("%q is not running, setting %s=%v and skipping enablement (err=%v)", mName, addon.Name(), enable, err)
		return nil
	}

	if name == "registry" {
		if driver.NeedsPortForward(cc.Driver) {
			port, err := oci.ForwardedPort(cc.Driver, cc.Name, constants.RegistryAddonPort)
			if err != nil {
				return errors.Wrap(err, "registry port")
			}
			out.T(out.Tip, `Registry addon on with {{.driver}} uses {{.port}} please use that instead of default 5000`, out.V{"driver": cc.Driver, "port": port})
			out.T(out.Documentation, `For more information see: https://minikube.sigs.k8s.io/docs/drivers/{{.driver}}`, out.V{"driver": cc.Driver})
		}
	}

	cmd, err := machine.CommandRunner(host)
	if err != nil {
		return errors.Wrap(err, "command runner")
	}

	data := assets.GenerateTemplateData(cc.KubernetesConfig)
	return enableOrDisableAddonInternal(cc, addon, cmd, data, enable)
}

func isAddonAlreadySet(cc *config.ClusterConfig, addon *assets.Addon, enable bool) bool {
	enabled := addon.IsEnabled(cc)
	if enabled && enable {
		return true
	}

	if !enabled && !enable {
		return true
	}

	return false
}

func enableOrDisableAddonInternal(cc *config.ClusterConfig, addon *assets.Addon, cmd command.Runner, data interface{}, enable bool) error {
	deployFiles := []string{}

	for _, addon := range addon.Assets {
		var f assets.CopyableFile
		var err error
		if addon.IsTemplate() {
			f, err = addon.Evaluate(data)
			if err != nil {
				return errors.Wrapf(err, "evaluate bundled addon %s asset", addon.GetSourcePath())
			}

		} else {
			f = addon
		}
		fPath := path.Join(f.GetTargetDir(), f.GetTargetName())

		if enable {
			glog.Infof("installing %s", fPath)
			if err := cmd.Copy(f); err != nil {
				return err
			}
		} else {
			glog.Infof("Removing %+v", fPath)
			defer func() {
				if err := cmd.Remove(f); err != nil {
					glog.Warningf("error removing %s; addon should still be disabled as expected", fPath)
				}
			}()
		}
		if strings.HasSuffix(fPath, ".yaml") {
			deployFiles = append(deployFiles, fPath)
		}
	}

	command := kubectlCommand(cc, deployFiles, enable)

	// Retry, because sometimes we race against an apiserver restart
	apply := func() error {
		_, err := cmd.RunCmd(command)
		if err != nil {
			glog.Warningf("apply failed, will retry: %v", err)
		}
		return err
	}

	return retry.Expo(apply, 250*time.Millisecond, 2*time.Minute)
}

// enableOrDisableStorageClasses enables or disables storage classes
func enableOrDisableStorageClasses(cc *config.ClusterConfig, name string, val string) error {
	glog.Infof("enableOrDisableStorageClasses %s=%v on %q", name, val, cc.Name)
	enable, err := strconv.ParseBool(val)
	if err != nil {
		return errors.Wrap(err, "Error parsing boolean")
	}

	class := defaultStorageClassProvisioner
	if name == "storage-provisioner-gluster" {
		class = "glusterfile"
	}

	api, err := machine.NewAPIClient()
	if err != nil {
		return errors.Wrap(err, "machine client")
	}
	defer api.Close()

	cp, err := config.PrimaryControlPlane(cc)
	if err != nil {
		return errors.Wrap(err, "getting control plane")
	}
	if !machine.IsRunning(api, driver.MachineName(*cc, cp)) {
		glog.Warningf("%q is not running, writing %s=%v to disk and skipping enablement", driver.MachineName(*cc, cp), name, val)
		return enableOrDisableAddon(cc, name, val)
	}

	storagev1, err := storageclass.GetStoragev1(cc.Name)
	if err != nil {
		return errors.Wrapf(err, "Error getting storagev1 interface %v ", err)
	}

	if enable {
		// Only StorageClass for 'name' should be marked as default
		err = storageclass.SetDefaultStorageClass(storagev1, class)
		if err != nil {
			return errors.Wrapf(err, "Error making %s the default storage class", class)
		}
	} else {
		// Unset the StorageClass as default
		err := storageclass.DisableDefaultStorageClass(storagev1, class)
		if err != nil {
			return errors.Wrapf(err, "Error disabling %s as the default storage class", class)
		}
	}

	return enableOrDisableAddon(cc, name, val)
}

func verifyAddonStatus(cc *config.ClusterConfig, name string, val string) error {
	glog.Infof("Verifying addon %s=%s in %q", name, val, cc.Name)
	enable, err := strconv.ParseBool(val)
	if err != nil {
		return errors.Wrapf(err, "parsing bool: %s", name)
	}

	label, ok := addonPodLabels[name]
	if ok && enable {
		out.T(out.HealthCheck, "Verifying {{.addon_name}} addon...", out.V{"addon_name": name})
		client, err := kapi.Client(viper.GetString(config.ProfileName))
		if err != nil {
			return errors.Wrapf(err, "get kube-client to validate %s addon: %v", name, err)
		}

		err = kapi.WaitForPods(client, "kube-system", label, time.Minute*3)
		if err != nil {
			return errors.Wrapf(err, "verifying %s addon pods : %v", name, err)
		}

	}
	return nil
}

// Start enables the default addons for a profile, plus any additional
func Start(wg *sync.WaitGroup, cc *config.ClusterConfig, toEnable map[string]bool, additional []string) {
	wg.Add(1)
	defer wg.Done()

	start := time.Now()
	glog.Infof("enableAddons start: toEnable=%v, additional=%s", toEnable, additional)
	defer func() {
		glog.Infof("enableAddons completed in %s", time.Since(start))
	}()

	// Get the default values of any addons not saved to our config
	for name, a := range assets.Addons {
		defaultVal := a.IsEnabled(cc)

		_, exists := toEnable[name]
		if !exists {
			toEnable[name] = defaultVal
		}
	}

	// Apply new addons
	for _, name := range additional {
		// replace heapster as metrics-server because heapster is deprecated
		if name == "heapster" {
			name = "metrics-server"
		}
		// if the specified addon doesn't exist, skip enabling
		_, e := isAddonValid(name)
		if e {
			toEnable[name] = true
		}
	}

	toEnableList := []string{}
	for k, v := range toEnable {
		if v {
			toEnableList = append(toEnableList, k)
		}
	}
	sort.Strings(toEnableList)

	var awg sync.WaitGroup

	defer func() { // making it show after verifications( not perfect till #7613 is closed)
		register.Reg.SetStep(register.EnablingAddons)
		out.T(out.AddonEnable, "Enabled addons: {{.addons}}", out.V{"addons": strings.Join(toEnableList, ", ")})
	}()
	for _, a := range toEnableList {
		awg.Add(1)
		go func(name string) {
			err := RunCallbacks(cc, name, "true")
			if err != nil {
				out.WarningT("Enabling '{{.name}}' returned an error: {{.error}}", out.V{"name": name, "error": err})
			}
			awg.Done()
		}(a)
	}

	// Wait until all of the addons are enabled before updating the config (not thread safe)
	awg.Wait()
	for _, a := range toEnableList {
		if err := Set(cc, a, "true"); err != nil {
			glog.Errorf("store failed: %v", err)
		}
	}
}
<commit_msg>(Addons) Create a new command in every retry<commit_after>/*
Copyright 2019 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package addons

import (
	"fmt"
	"path"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/golang/glog"
	"github.com/pkg/errors"
	"github.com/spf13/viper"

	"k8s.io/minikube/pkg/drivers/kic/oci"
	"k8s.io/minikube/pkg/kapi"
	"k8s.io/minikube/pkg/minikube/assets"
	"k8s.io/minikube/pkg/minikube/command"
	"k8s.io/minikube/pkg/minikube/config"
	"k8s.io/minikube/pkg/minikube/constants"
	"k8s.io/minikube/pkg/minikube/driver"
	"k8s.io/minikube/pkg/minikube/exit"
	"k8s.io/minikube/pkg/minikube/machine"
	"k8s.io/minikube/pkg/minikube/out"
	"k8s.io/minikube/pkg/minikube/out/register"
	"k8s.io/minikube/pkg/minikube/storageclass"
	"k8s.io/minikube/pkg/util/retry"
)

// defaultStorageClassProvisioner is the name of the default storage class provisioner
const defaultStorageClassProvisioner = "standard"

// RunCallbacks runs all actions associated to an addon, but does not set it (thread-safe)
func RunCallbacks(cc *config.ClusterConfig, name string, value string) error {
	glog.Infof("Setting %s=%s in profile %q", name, value, cc.Name)
	a, valid := isAddonValid(name)
	if !valid {
		return errors.Errorf("%s is not a valid addon", name)
	}

	// Run any additional validations for this property
	if err := run(cc, name, value, a.validations); err != nil {
		return errors.Wrap(err, "running validations")
	}

	// Run any callbacks for this property
	if err := run(cc, name, value, a.callbacks); err != nil {
		return errors.Wrap(err, "running callbacks")
	}
	return nil
}

// Set sets a value in the config (not threadsafe)
func Set(cc *config.ClusterConfig, name string, value string) error {
	a, valid := isAddonValid(name)
	if !valid {
		return errors.Errorf("%s is not a valid addon", name)
	}
	return a.set(cc, name, value)
}

// SetAndSave sets a value and saves the config
func SetAndSave(profile string, name string, value string) error {
	cc, err := config.Load(profile)
	if err != nil {
		return errors.Wrap(err, "loading profile")
	}

	if err := RunCallbacks(cc, name, value); err != nil {
		return errors.Wrap(err, "run callbacks")
	}

	if err := Set(cc, name, value); err != nil {
		return errors.Wrap(err, "set")
	}

	glog.Infof("Writing out %q config to set %s=%v...", profile, name, value)
	return config.Write(profile, cc)
}

// Runs all the validation or callback functions and collects errors
func run(cc *config.ClusterConfig, name string, value string, fns []setFn) error {
	var errors []error
	for _, fn := range fns {
		err := fn(cc, name, value)
		if err != nil {
			errors = append(errors, err)
		}
	}
	if len(errors) > 0 {
		return fmt.Errorf("%v", errors)
	}
	return nil
}

// SetBool sets a bool value in the config (not threadsafe)
func SetBool(cc *config.ClusterConfig, name string, val string) error {
	b, err := strconv.ParseBool(val)
	if err != nil {
		return err
	}
	if cc.Addons == nil {
		cc.Addons = map[string]bool{}
	}
	cc.Addons[name] = b
	return nil
}

// enableOrDisableAddon updates addon status executing any commands necessary
func enableOrDisableAddon(cc *config.ClusterConfig, name string, val string) error {
	glog.Infof("Setting addon %s=%s in %q", name, val, cc.Name)
	enable, err := strconv.ParseBool(val)
	if err != nil {
		return errors.Wrapf(err, "parsing bool: %s", name)
	}
	addon := assets.Addons[name]

	// check addon status before enabling/disabling it
	if isAddonAlreadySet(cc, addon, enable) {
		glog.Warningf("addon %s should already be in state %v", name, val)
		if !enable {
			return nil
		}
	}

	// to match both ingress and ingress-dns adons
	if strings.HasPrefix(name, "ingress") && enable && driver.IsKIC(cc.Driver) && runtime.GOOS != "linux" {
		exit.UsageT(`Due to {{.driver_name}} networking limitations on {{.os_name}}, {{.addon_name}} addon is not supported for this driver.
Alternatively to use this addon you can use a vm-based driver:

	'minikube start --vm=true'

To track the update on this work in progress feature please check:
https://github.com/kubernetes/minikube/issues/7332`, out.V{"driver_name": cc.Driver, "os_name": runtime.GOOS, "addon_name": name})
	}

	if strings.HasPrefix(name, "istio") && enable {
		minMem := 8192
		minCPUs := 4
		if cc.Memory < minMem {
			out.WarningT("Istio needs {{.minMem}}MB of memory -- your configuration only allocates {{.memory}}MB", out.V{"minMem": minMem, "memory": cc.Memory})
		}
		if cc.CPUs < minCPUs {
			out.WarningT("Istio needs {{.minCPUs}} CPUs -- your configuration only allocates {{.cpus}} CPUs", out.V{"minCPUs": minCPUs, "cpus": cc.CPUs})
		}
	}

	// TODO(r2d4): config package should not reference API, pull this out
	api, err := machine.NewAPIClient()
	if err != nil {
		return errors.Wrap(err, "machine client")
	}
	defer api.Close()

	cp, err := config.PrimaryControlPlane(cc)
	if err != nil {
		exit.WithError("Error getting primary control plane", err)
	}

	mName := driver.MachineName(*cc, cp)
	host, err := machine.LoadHost(api, mName)
	if err != nil || !machine.IsRunning(api, mName) {
		glog.Warningf("%q is not running, setting %s=%v and skipping enablement (err=%v)", mName, addon.Name(), enable, err)
		return nil
	}

	if name == "registry" {
		if driver.NeedsPortForward(cc.Driver) {
			port, err := oci.ForwardedPort(cc.Driver, cc.Name, constants.RegistryAddonPort)
			if err != nil {
				return errors.Wrap(err, "registry port")
			}
			out.T(out.Tip, `Registry addon on with {{.driver}} uses {{.port}} please use that instead of default 5000`, out.V{"driver": cc.Driver, "port": port})
			out.T(out.Documentation, `For more information see: https://minikube.sigs.k8s.io/docs/drivers/{{.driver}}`, out.V{"driver": cc.Driver})
		}
	}

	cmd, err := machine.CommandRunner(host)
	if err != nil {
		return errors.Wrap(err, "command runner")
	}

	data := assets.GenerateTemplateData(cc.KubernetesConfig)
	return enableOrDisableAddonInternal(cc, addon, cmd, data, enable)
}

func isAddonAlreadySet(cc *config.ClusterConfig, addon *assets.Addon, enable bool) bool {
	enabled := addon.IsEnabled(cc)
	if enabled && enable {
		return true
	}

	if !enabled && !enable {
		return true
	}

	return false
}

func enableOrDisableAddonInternal(cc *config.ClusterConfig, addon *assets.Addon, cmd command.Runner, data interface{}, enable bool) error {
	deployFiles := []string{}

	for _, addon := range addon.Assets {
		var f assets.CopyableFile
		var err error
		if addon.IsTemplate() {
			f, err = addon.Evaluate(data)
			if err != nil {
				return errors.Wrapf(err, "evaluate bundled addon %s asset", addon.GetSourcePath())
			}

		} else {
			f = addon
		}
		fPath := path.Join(f.GetTargetDir(), f.GetTargetName())

		if enable {
			glog.Infof("installing %s", fPath)
			if err := cmd.Copy(f); err != nil {
				return err
			}
		} else {
			glog.Infof("Removing %+v", fPath)
			defer func() {
				if err := cmd.Remove(f); err != nil {
					glog.Warningf("error removing %s; addon should still be disabled as expected", fPath)
				}
			}()
		}
		if strings.HasSuffix(fPath, ".yaml") {
			deployFiles = append(deployFiles, fPath)
		}
	}

	// Retry, because sometimes we race against an apiserver restart
	apply := func() error {
		_, err := cmd.RunCmd(kubectlCommand(cc, deployFiles, enable))
		if err != nil {
			glog.Warningf("apply failed, will retry: %v", err)
		}
		return err
	}

	return retry.Expo(apply, 250*time.Millisecond, 2*time.Minute)
}

// enableOrDisableStorageClasses enables or disables storage classes
func enableOrDisableStorageClasses(cc *config.ClusterConfig, name string, val string) error {
	glog.Infof("enableOrDisableStorageClasses %s=%v on %q", name, val, cc.Name)
	enable, err := strconv.ParseBool(val)
	if err != nil {
		return errors.Wrap(err, "Error parsing boolean")
	}

	class := defaultStorageClassProvisioner
	if name == "storage-provisioner-gluster" {
		class = "glusterfile"
	}

	api, err := machine.NewAPIClient()
	if err != nil {
		return errors.Wrap(err, "machine client")
	}
	defer api.Close()

	cp, err := config.PrimaryControlPlane(cc)
	if err != nil {
		return errors.Wrap(err, "getting control plane")
	}
	if !machine.IsRunning(api, driver.MachineName(*cc, cp)) {
		glog.Warningf("%q is not running, writing %s=%v to disk and skipping enablement", driver.MachineName(*cc, cp), name, val)
		return enableOrDisableAddon(cc, name, val)
	}

	storagev1, err := storageclass.GetStoragev1(cc.Name)
	if err != nil {
		return errors.Wrapf(err, "Error getting storagev1 interface %v ", err)
	}

	if enable {
		// Only StorageClass for 'name' should be marked as default
		err = storageclass.SetDefaultStorageClass(storagev1, class)
		if err != nil {
			return errors.Wrapf(err, "Error making %s the default storage class", class)
		}
	} else {
		// Unset the StorageClass as default
		err := storageclass.DisableDefaultStorageClass(storagev1, class)
		if err != nil {
			return errors.Wrapf(err, "Error disabling %s as the default storage class", class)
		}
	}

	return enableOrDisableAddon(cc, name, val)
}

func verifyAddonStatus(cc *config.ClusterConfig, name string, val string) error {
	glog.Infof("Verifying addon %s=%s in %q", name, val, cc.Name)
	enable, err := strconv.ParseBool(val)
	if err != nil {
		return errors.Wrapf(err, "parsing bool: %s", name)
	}

	label, ok := addonPodLabels[name]
	if ok && enable {
		out.T(out.HealthCheck, "Verifying {{.addon_name}} addon...", out.V{"addon_name": name})
		client, err := kapi.Client(viper.GetString(config.ProfileName))
		if err != nil {
			return errors.Wrapf(err, "get kube-client to validate %s addon: %v", name, err)
		}

		err = kapi.WaitForPods(client, "kube-system", label, time.Minute*3)
		if err != nil {
			return errors.Wrapf(err, "verifying %s addon pods : %v", name, err)
		}

	}
	return nil
}

// Start enables the default addons for a profile, plus any additional
func Start(wg *sync.WaitGroup, cc *config.ClusterConfig, toEnable map[string]bool, additional []string) {
	wg.Add(1)
	defer wg.Done()

	start := time.Now()
	glog.Infof("enableAddons start: toEnable=%v, additional=%s", toEnable, additional)
	defer func() {
		glog.Infof("enableAddons completed in %s", time.Since(start))
	}()

	// Get the default values of any addons not saved to our config
	for name, a := range assets.Addons {
		defaultVal := a.IsEnabled(cc)

		_, exists := toEnable[name]
		if !exists {
			toEnable[name] = defaultVal
		}
	}

	// Apply new addons
	for _, name := range additional {
		// replace heapster as metrics-server because heapster is deprecated
		if name == "heapster" {
			name = "metrics-server"
		}
		// if the specified addon doesn't exist, skip enabling
		_, e := isAddonValid(name)
		if e {
			toEnable[name] = true
		}
	}

	toEnableList := []string{}
	for k, v := range toEnable {
		if v {
			toEnableList = append(toEnableList, k)
		}
	}
	sort.Strings(toEnableList)

	var awg sync.WaitGroup

	defer func() { // making it show after verifications( not perfect till #7613 is closed)
		register.Reg.SetStep(register.EnablingAddons)
		out.T(out.AddonEnable, "Enabled addons: {{.addons}}", out.V{"addons": strings.Join(toEnableList, ", ")})
	}()
	for _, a := range toEnableList {
		awg.Add(1)
		go func(name string) {
			err := RunCallbacks(cc, name, "true")
			if err != nil {
				out.WarningT("Enabling '{{.name}}' returned an error: {{.error}}", out.V{"name": name, "error": err})
			}
			awg.Done()
		}(a)
	}

	// Wait until all of the addons are enabled before updating the config (not thread safe)
	awg.Wait()
	for _, a := range toEnableList {
		if err := Set(cc, a, "true"); err != nil {
			glog.Errorf("store failed: %v", err)
		}
	}
}
<|endoftext|>