|
18 | 18 | // The session bucket has the following structure:
|
19 | 19 | // session -> <key> -> <serialised session>
|
20 | 20 | // -> id-index -> <session-id> -> key -> <session key>
|
| 21 | + // -> group -> <group-ID> |
| 22 | + // -> group-id-index -> <group-id> -> session-id -> sequence -> <session-id> |
21 | 23 | sessionBucketKey = []byte("session")
|
22 | 24 |
|
23 | 25 | // idIndexKey is the key used to define the id-index sub-bucket within
|
|
30 | 32 | // session ID.
|
31 | 33 | sessionKeyKey = []byte("key")
|
32 | 34 |
|
| 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 | + |
33 | 49 | // ErrSessionNotFound is an error returned when we attempt to retrieve
|
34 | 50 | // information about a session but it is not found.
|
35 | 51 | ErrSessionNotFound = errors.New("session not found")
|
@@ -87,6 +103,14 @@ func (db *DB) CreateSession(session *Session) error {
|
87 | 103 | return err
|
88 | 104 | }
|
89 | 105 |
|
| 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 | + |
90 | 114 | return sessionBucket.Put(sessionKey, buf.Bytes())
|
91 | 115 | })
|
92 | 116 | }
|
@@ -358,3 +382,50 @@ func getKeyForID(sessionBkt *bbolt.Bucket, id ID) ([]byte, error) {
|
358 | 382 |
|
359 | 383 | return sessionKeyBytes, nil
|
360 | 384 | }
|
| 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