scf begin

This commit is contained in:
DarkGopher 2025-06-27 18:48:26 +02:00
parent efcfce5197
commit 6d0dd67a7d
2 changed files with 78 additions and 0 deletions

58
storage/hybrid/onlydoc.go Normal file
View file

@ -0,0 +1,58 @@
package hybrid
// Provider interace implement lifecycle for one sessions
type Provider interface {
//set additional params for provider ex: sql db connection, filesystem path .. etc.
SetParams(params any) error
//create new session using sid value 1) in meory 2) into WAL file create new session op.
Init(sid string) (Session, error)
//read and return existing session by id or if not exist create new session 1) from
//memory WAL file no need modify i no call Init from Load...
Load(sid string) (Session, error)
//destroy remove session with sid from storage if exist 1) in memory 2) in WAL ile
//add destroy operation
Destroy(sid string) error
//regenerate id change old sid to newsid and preserve existing session data 1) in memory
//and 2) add change id op. into WAL file
ChangeID(oldsid, newsid string) (err error)
//Exists return true if session with sid exist
Exists(sid string) bool
//gc remove all outdated sessions 1) in memory and 2) add every destroy op. into WAL
GC(maxlifetime int64)
}
// Session interface implement storage for one session and have maxLifetime and lastAccessTime
/*
Session change WAL file (SCF) file format:
| OP. NUM. | SID | DATA (binary stream) | LEN |
OP. NUM. : uint8
0 - create new session
1 - destroy session
2 - change session ID
3 - modify (create, update and delete) session key/val data
SID:
- 32 bit. session ID
DATA:
0: --no data--
1: --no-data--
2: new SID
3:
create: key([]byte)|val([]byte)
delete: key([]byte)
update: key([]byte)|val([]byte)
*/
type Session interface {
//set session value and update last access time 1) in memory and 2) add Set key op. into WAL
Set(key, value any) error
//get session value and update last access time 1) from memory and no need any WAL modiication
Get(key any) (v any, err error)
//delete session value 1) in memory and 2) add delete key op. into WAL
Delete(key any) error
//get session id 1) from memory and no need mod. WAL
SessionID() string
}

20
storage/hybrid/scf/scf.go Normal file
View file

@ -0,0 +1,20 @@
// Package scf provide Session Changes Log file
package scf
import (
"fmt"
"os"
)
type Scf struct {
fd *os.File
}
// NewScf open or create if not exists Session Changes Log file
func NewScf(fpath string) (scf *Scf, err error) {
var fd *os.File
if fd, err = os.Create(fpath); err != nil {
return nil, fmt.Errorf("can not open(create) SCF file: %s err: %v", fpath, err)
}
return &Scf{fd}, nil
}