From db142d97ecaf18b452342743e60155bf2fa5fe53 Mon Sep 17 00:00:00 2001 From: sawka Date: Tue, 20 Sep 2022 17:01:25 -0700 Subject: [PATCH] add linenum to line, nextlinenum to window --- db/migrations/000001_init.up.sql | 6 +++- db/schema.sql | 5 +++- pkg/sstore/dbops.go | 31 +++++++++++++------- pkg/sstore/sstore.go | 50 ++++++++++++++++++-------------- 4 files changed, 59 insertions(+), 33 deletions(-) diff --git a/db/migrations/000001_init.up.sql b/db/migrations/000001_init.up.sql index c58399aa..5e8ef70a 100644 --- a/db/migrations/000001_init.up.sql +++ b/db/migrations/000001_init.up.sql @@ -22,6 +22,7 @@ CREATE TABLE window ( curremoteownerid varchar(36) NOT NULL, curremoteid varchar(36) NOT NULL, curremotename varchar(50) NOT NULL, + nextlinenum int NOT NULL, winopts json NOT NULL, ownerid varchar(36) NOT NULL, sharemode varchar(12) NOT NULL, @@ -63,10 +64,13 @@ CREATE TABLE remote_instance ( CREATE TABLE line ( sessionid varchar(36) NOT NULL, windowid varchar(36) NOT NULL, - lineid varchar(36) NOT NULL, userid varchar(36) NOT NULL, + lineid varchar(36) NOT NULL, ts bigint NOT NULL, + linenum int NOT NULL, + linenumtemp boolean NOT NULL, linetype varchar(10) NOT NULL, + linelocal boolean NOT NULL, text text NOT NULL, cmdid varchar(36) NOT NULL, ephemeral boolean NOT NULL, diff --git a/db/schema.sql b/db/schema.sql index 9e71be1c..a7054dd2 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -59,10 +59,13 @@ CREATE TABLE remote_instance ( CREATE TABLE line ( sessionid varchar(36) NOT NULL, windowid varchar(36) NOT NULL, - lineid varchar(36) NOT NULL, userid varchar(36) NOT NULL, + lineid varchar(36) NOT NULL, ts bigint NOT NULL, + linenum int NOT NULL, + linenumtemp boolean NOT NULL, linetype varchar(10) NOT NULL, + linelocal boolean NOT NULL, text text NOT NULL, cmdid varchar(36) NOT NULL, ephemeral boolean NOT NULL, diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 9c73d7ee..d19f7734 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -512,16 +512,17 @@ func GetScreenById(ctx context.Context, sessionId string, screenId string) (*Scr func txCreateWindow(tx *TxWrap, sessionId string, curRemote RemotePtrType) string { w := &WindowType{ - SessionId: sessionId, - WindowId: uuid.New().String(), - CurRemote: curRemote, - WinOpts: WindowOptsType{}, - ShareMode: ShareModeLocal, - ShareOpts: WindowShareOptsType{}, + SessionId: sessionId, + WindowId: uuid.New().String(), + CurRemote: curRemote, + NextLineNum: 1, + WinOpts: WindowOptsType{}, + ShareMode: ShareModeLocal, + ShareOpts: WindowShareOptsType{}, } wmap := w.ToMap() - query := `INSERT INTO window ( sessionid, windowid, curremoteownerid, curremoteid, curremotename, winopts, ownerid, sharemode, shareopts) - VALUES (:sessionid,:windowid,:curremoteownerid,:curremoteid,:curremotename,:winopts,:ownerid,:sharemode,:shareopts)` + query := `INSERT INTO window ( sessionid, windowid, curremoteownerid, curremoteid, curremotename, nextlinenum, winopts, ownerid, sharemode, shareopts) + VALUES (:sessionid,:windowid,:curremoteownerid,:curremoteid,:curremotename,:nextlinenum,:winopts,:ownerid,:sharemode,:shareopts)` tx.NamedExecWrap(query, wmap) return w.WindowId } @@ -533,6 +534,9 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error { if line.LineId == "" { return fmt.Errorf("line must have lineid set") } + if line.LineNum != 0 { + return fmt.Errorf("line should not hage linenum set") + } return WithTx(ctx, func(tx *TxWrap) error { var windowId string query := `SELECT windowid FROM window WHERE sessionid = ? AND windowid = ?` @@ -540,9 +544,14 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error { if !hasWindow { return fmt.Errorf("window not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId) } - query = `INSERT INTO line ( sessionid, windowid, lineid, ts, userid, linetype, text, cmdid, ephemeral) - VALUES (:sessionid,:windowid,:lineid,:ts,:userid,:linetype,:text,:cmdid,:ephemeral)` + query = `SELECT nextlinenum FROM window WHERE sessionid = ? AND windowid = ?` + nextLineNum := tx.GetInt(query, line.SessionId, line.WindowId) + line.LineNum = int64(nextLineNum) + query = `INSERT INTO line ( sessionid, windowid, userid, lineid, ts, linenum, linenumtemp, linelocal, linetype, text, cmdid, ephemeral) + VALUES (:sessionid,:windowid,:userid,:lineid,:ts,:linenum,:linenumtemp,:linelocal,:linetype,:text,:cmdid,:ephemeral)` tx.NamedExecWrap(query, line) + query = `UPDATE window SET nextlinenum = ? WHERE sessionid = ? AND windowid = ?` + tx.ExecWrap(query, nextLineNum+1, line.SessionId, line.WindowId) if cmd != nil { cmdMap := cmd.ToMap() query = ` @@ -876,6 +885,8 @@ func ClearWindow(ctx context.Context, sessionId string, windowId string) (*Model lineIds = tx.SelectStrings(query, sessionId, windowId) query = `DELETE FROM line WHERE sessionid = ? AND windowid = ?` tx.ExecWrap(query, sessionId, windowId) + query = `UPDATE window SET nextlinenum = 1 WHERE sessionid = ? AND windowid = ?` + tx.ExecWrap(query, sessionId, windowId) return nil }) if txErr != nil { diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index d1b07301..95ace511 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -197,15 +197,16 @@ func (r RemotePtrType) MakeFullRemoteRef() string { } type WindowType struct { - SessionId string `json:"sessionid"` - WindowId string `json:"windowid"` - CurRemote RemotePtrType `json:"curremote"` - WinOpts WindowOptsType `json:"winopts"` - OwnerId string `json:"ownerid"` - ShareMode string `json:"sharemode"` - ShareOpts WindowShareOptsType `json:"shareopts"` - Lines []*LineType `json:"lines"` - Cmds []*CmdType `json:"cmds"` + SessionId string `json:"sessionid"` + WindowId string `json:"windowid"` + CurRemote RemotePtrType `json:"curremote"` + WinOpts WindowOptsType `json:"winopts"` + OwnerId string `json:"ownerid"` + NextLineNum int64 `json:"nextlinenum"` + ShareMode string `json:"sharemode"` + ShareOpts WindowShareOptsType `json:"shareopts"` + Lines []*LineType `json:"lines"` + Cmds []*CmdType `json:"cmds"` // only for updates Remove bool `json:"remove,omitempty"` @@ -218,6 +219,7 @@ func (w *WindowType) ToMap() map[string]interface{} { rtn["curremoteownerid"] = w.CurRemote.OwnerId rtn["curremoteid"] = w.CurRemote.RemoteId rtn["curremotename"] = w.CurRemote.Name + rtn["nextlinenum"] = w.NextLineNum rtn["winopts"] = quickJson(w.WinOpts) rtn["ownerid"] = w.OwnerId rtn["sharemode"] = w.ShareMode @@ -235,6 +237,7 @@ func WindowFromMap(m map[string]interface{}) *WindowType { quickSetStr(&w.CurRemote.OwnerId, m, "curremoteownerid") quickSetStr(&w.CurRemote.RemoteId, m, "curremoteid") quickSetStr(&w.CurRemote.Name, m, "curremotename") + quickSetInt64(&w.NextLineNum, m, "nextlinenum") quickSetJson(&w.WinOpts, m, "winopts") quickSetStr(&w.OwnerId, m, "ownerid") quickSetStr(&w.ShareMode, m, "sharemode") @@ -416,16 +419,19 @@ type RemoteInstance struct { } type LineType struct { - SessionId string `json:"sessionid"` - WindowId string `json:"windowid"` - LineId string `json:"lineid"` - Ts int64 `json:"ts"` - UserId string `json:"userid"` - LineType string `json:"linetype"` - Text string `json:"text,omitempty"` - CmdId string `json:"cmdid,omitempty"` - Ephemeral bool `json:"ephemeral,omitempty"` - Remove bool `json:"remove,omitempty"` + SessionId string `json:"sessionid"` + WindowId string `json:"windowid"` + UserId string `json:"userid"` + LineId string `json:"lineid"` + Ts int64 `json:"ts"` + LineNum int64 `json:"linenum"` + LineNumTemp bool `json:"linenumtemp,omitempty"` + LineLocal bool `json:"linelocal"` + LineType string `json:"linetype"` + Text string `json:"text,omitempty"` + CmdId string `json:"cmdid,omitempty"` + Ephemeral bool `json:"ephemeral,omitempty"` + Remove bool `json:"remove,omitempty"` } type SSHOpts struct { @@ -574,9 +580,10 @@ func makeNewLineCmd(sessionId string, windowId string, userId string, cmdId stri rtn := &LineType{} rtn.SessionId = sessionId rtn.WindowId = windowId + rtn.UserId = userId rtn.LineId = uuid.New().String() rtn.Ts = time.Now().UnixMilli() - rtn.UserId = userId + rtn.LineLocal = true rtn.LineType = LineTypeCmd rtn.CmdId = cmdId return rtn @@ -586,9 +593,10 @@ func makeNewLineText(sessionId string, windowId string, userId string, text stri rtn := &LineType{} rtn.SessionId = sessionId rtn.WindowId = windowId + rtn.UserId = userId rtn.LineId = uuid.New().String() rtn.Ts = time.Now().UnixMilli() - rtn.UserId = userId + rtn.LineLocal = true rtn.LineType = LineTypeText rtn.Text = text return rtn