Skip to content

Commit a802759

Browse files
committed
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.
1 parent 477a511 commit a802759

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

session/db.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ func initDB(filepath string, firstInit bool) (*bbolt.DB, error) {
111111
}
112112

113113
_, err = sessionBkt.CreateBucketIfNotExists(idIndexKey)
114+
if err != nil {
115+
return err
116+
}
117+
118+
_, err = sessionBkt.CreateBucketIfNotExists(groupIDIndexKey)
114119

115120
return err
116121
})

session/store.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ var (
1818
// The session bucket has the following structure:
1919
// session -> <key> -> <serialised session>
2020
// -> id-index -> <session-id> -> key -> <session key>
21+
// -> group -> <group-ID>
22+
// -> group-id-index -> <group-id> -> session-id -> sequence -> <session-id>
2123
sessionBucketKey = []byte("session")
2224

2325
// idIndexKey is the key used to define the id-index sub-bucket within
@@ -30,6 +32,20 @@ var (
3032
// session ID.
3133
sessionKeyKey = []byte("key")
3234

35+
// groupIDKey is the key used within the id-index bucket to store the
36+
// group ID associated with the given session ID.
37+
groupIDKey = []byte("group")
38+
39+
// groupIDIndexKey is the key used to define the group-id-index
40+
// sub-bucket within the main session bucket. This bucket will be used
41+
// to store the mapping from group ID to various other fields.
42+
groupIDIndexKey = []byte("group-id-index")
43+
44+
// sessionIDKey is a key used in the group-id-index under a sub-bucket
45+
// defined by a specific group ID. It will be used to store the session
46+
// IDs associated with the given group ID.
47+
sessionIDKey = []byte("session-id")
48+
3349
// ErrSessionNotFound is an error returned when we attempt to retrieve
3450
// information about a session but it is not found.
3551
ErrSessionNotFound = errors.New("session not found")
@@ -87,6 +103,14 @@ func (db *DB) CreateSession(session *Session) error {
87103
return err
88104
}
89105

106+
// Add the mapping from session ID to group ID and vice versa.
107+
err = addIDToGroupIDPair(
108+
sessionBucket, session.ID, session.GroupID,
109+
)
110+
if err != nil {
111+
return err
112+
}
113+
90114
return sessionBucket.Put(sessionKey, buf.Bytes())
91115
})
92116
}
@@ -358,3 +382,50 @@ func getKeyForID(sessionBkt *bbolt.Bucket, id ID) ([]byte, error) {
358382

359383
return sessionKeyBytes, nil
360384
}
385+
386+
// addIDToGroupIDPair inserts the mapping from session ID to group ID into the
387+
// id-index bucket and also inserts the mapping from group ID to session ID into
388+
// the group-id-index bucket.
389+
func addIDToGroupIDPair(sessionBkt *bbolt.Bucket, id, groupID ID) error {
390+
// First we will add the mapping from session ID to group ID.
391+
idIndexBkt := sessionBkt.Bucket(idIndexKey)
392+
if idIndexBkt == nil {
393+
return ErrDBInitErr
394+
}
395+
396+
idBkt, err := idIndexBkt.CreateBucketIfNotExists(id[:])
397+
if err != nil {
398+
return err
399+
}
400+
401+
err = idBkt.Put(groupIDKey, groupID[:])
402+
if err != nil {
403+
return err
404+
}
405+
406+
// Now we add the mapping from group ID to session.
407+
groupIdIndexBkt := sessionBkt.Bucket(groupIDIndexKey)
408+
if groupIdIndexBkt == nil {
409+
return ErrDBInitErr
410+
}
411+
412+
groupBkt, err := groupIdIndexBkt.CreateBucketIfNotExists(groupID[:])
413+
if err != nil {
414+
return err
415+
}
416+
417+
sessionIDsBkt, err := groupBkt.CreateBucketIfNotExists(sessionIDKey)
418+
if err != nil {
419+
return err
420+
}
421+
422+
nextSeq, err := sessionIDsBkt.NextSequence()
423+
if err != nil {
424+
return err
425+
}
426+
427+
var seqNoBytes [8]byte
428+
byteOrder.PutUint64(seqNoBytes[:], nextSeq)
429+
430+
return sessionIDsBkt.Put(seqNoBytes[:], id[:])
431+
}

0 commit comments

Comments
 (0)