Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jnmoyne committed Sep 15, 2022
1 parent a51d3ea commit 28d0c3a
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions jsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ type apiPaged struct {
// apiPagedRequest includes parameters allowing specific pages to be requested
// from APIs responding with apiPaged.
type apiPagedRequest struct {
Offset int `json:"offset"`
Offset int `json:"offset,omitempty"`
}

// AccountInfo contains info about the JetStream usage from the current account.
Expand Down Expand Up @@ -708,19 +708,22 @@ func (js *js) StreamInfo(stream string, opts ...JSOpt) (*StreamInfo, error) {
}

var i int
var subjectMessagesMap map[string]uint64 = nil
var subjectMessagesMap map[string]uint64
var req []byte
var requestPayload bool

siOpts := StreamInfoRequest{}
var siOpts StreamInfoRequest
if o.streamInfoOpts != nil {
siOpts.DeletedDetails = o.streamInfoOpts.DeletedDetails
siOpts.SubjectsFilter = o.streamInfoOpts.SubjectsFilter
requestPayload = true
siOpts = *o.streamInfoOpts
}

for {
siOpts.Offset = i
if req, err = json.Marshal(&siOpts); err != nil {
return nil, err
if requestPayload {
siOpts.Offset = i
if req, err = json.Marshal(&siOpts); err != nil {
return nil, err
}
}

siSubj := js.apiSubj(fmt.Sprintf(apiStreamInfoT, stream))
Expand All @@ -731,35 +734,40 @@ func (js *js) StreamInfo(stream string, opts ...JSOpt) (*StreamInfo, error) {
}

var resp streamInfoResponse

if err := json.Unmarshal(r.Data, &resp); err != nil {
return nil, err
}

if resp.Error != nil {
if errors.Is(resp.Error, ErrStreamNotFound) {
return nil, ErrStreamNotFound
}
return nil, resp.Error
}

// for backwards compatibility
var total int
// for backwards compatibility
if resp.Total != 0 {
total = resp.Total
} else {
total = len(resp.State.Subjects)
}

if subjectMessagesMap == nil {
subjectMessagesMap = make(map[string]uint64, total)
}
if requestPayload && len(resp.StreamInfo.State.Subjects) > 0 {
if subjectMessagesMap == nil {
subjectMessagesMap = make(map[string]uint64, total)
}

for k, j := range resp.State.Subjects {
subjectMessagesMap[k] = j
i++
for k, j := range resp.State.Subjects {
subjectMessagesMap[k] = j
i++
}
}
if i == total {
resp.StreamInfo.State.Subjects = subjectMessagesMap

if i >= total {
if requestPayload {
resp.StreamInfo.State.Subjects = subjectMessagesMap
}
return resp.StreamInfo, nil
}
}
Expand Down

0 comments on commit 28d0c3a

Please sign in to comment.