mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-13 12:33:36 +02:00
session: add session ID to group ID indexes
This commit adds two new indexes to the session store. One from session ID to group ID and one from groupID to a set of session IDs. This commit only adds the new logic and starts writing the the new indexes from the `StoreSession` method. The next commit will add a migration to back-fill the new indexes.
This commit is contained in:
parent
477a5115dd
commit
a802759f4e
2 changed files with 76 additions and 0 deletions
|
|
@ -111,6 +111,11 @@ func initDB(filepath string, firstInit bool) (*bbolt.DB, error) {
|
|||
}
|
||||
|
||||
_, err = sessionBkt.CreateBucketIfNotExists(idIndexKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = sessionBkt.CreateBucketIfNotExists(groupIDIndexKey)
|
||||
|
||||
return err
|
||||
})
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ var (
|
|||
// The session bucket has the following structure:
|
||||
// session -> <key> -> <serialised session>
|
||||
// -> id-index -> <session-id> -> key -> <session key>
|
||||
// -> group -> <group-ID>
|
||||
// -> group-id-index -> <group-id> -> session-id -> sequence -> <session-id>
|
||||
sessionBucketKey = []byte("session")
|
||||
|
||||
// idIndexKey is the key used to define the id-index sub-bucket within
|
||||
|
|
@ -30,6 +32,20 @@ var (
|
|||
// session ID.
|
||||
sessionKeyKey = []byte("key")
|
||||
|
||||
// groupIDKey is the key used within the id-index bucket to store the
|
||||
// group ID associated with the given session ID.
|
||||
groupIDKey = []byte("group")
|
||||
|
||||
// groupIDIndexKey is the key used to define the group-id-index
|
||||
// sub-bucket within the main session bucket. This bucket will be used
|
||||
// to store the mapping from group ID to various other fields.
|
||||
groupIDIndexKey = []byte("group-id-index")
|
||||
|
||||
// sessionIDKey is a key used in the group-id-index under a sub-bucket
|
||||
// defined by a specific group ID. It will be used to store the session
|
||||
// IDs associated with the given group ID.
|
||||
sessionIDKey = []byte("session-id")
|
||||
|
||||
// ErrSessionNotFound is an error returned when we attempt to retrieve
|
||||
// information about a session but it is not found.
|
||||
ErrSessionNotFound = errors.New("session not found")
|
||||
|
|
@ -87,6 +103,14 @@ func (db *DB) CreateSession(session *Session) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Add the mapping from session ID to group ID and vice versa.
|
||||
err = addIDToGroupIDPair(
|
||||
sessionBucket, session.ID, session.GroupID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return sessionBucket.Put(sessionKey, buf.Bytes())
|
||||
})
|
||||
}
|
||||
|
|
@ -358,3 +382,50 @@ func getKeyForID(sessionBkt *bbolt.Bucket, id ID) ([]byte, error) {
|
|||
|
||||
return sessionKeyBytes, nil
|
||||
}
|
||||
|
||||
// addIDToGroupIDPair inserts the mapping from session ID to group ID into the
|
||||
// id-index bucket and also inserts the mapping from group ID to session ID into
|
||||
// the group-id-index bucket.
|
||||
func addIDToGroupIDPair(sessionBkt *bbolt.Bucket, id, groupID ID) error {
|
||||
// First we will add the mapping from session ID to group ID.
|
||||
idIndexBkt := sessionBkt.Bucket(idIndexKey)
|
||||
if idIndexBkt == nil {
|
||||
return ErrDBInitErr
|
||||
}
|
||||
|
||||
idBkt, err := idIndexBkt.CreateBucketIfNotExists(id[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = idBkt.Put(groupIDKey, groupID[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Now we add the mapping from group ID to session.
|
||||
groupIdIndexBkt := sessionBkt.Bucket(groupIDIndexKey)
|
||||
if groupIdIndexBkt == nil {
|
||||
return ErrDBInitErr
|
||||
}
|
||||
|
||||
groupBkt, err := groupIdIndexBkt.CreateBucketIfNotExists(groupID[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sessionIDsBkt, err := groupBkt.CreateBucketIfNotExists(sessionIDKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
nextSeq, err := sessionIDsBkt.NextSequence()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var seqNoBytes [8]byte
|
||||
byteOrder.PutUint64(seqNoBytes[:], nextSeq)
|
||||
|
||||
return sessionIDsBkt.Put(seqNoBytes[:], id[:])
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue