|
| 1 | +package bsoncore |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + |
| 7 | + "github.com/mongodb/mongo-go-driver/bson/bsontype" |
| 8 | +) |
| 9 | + |
| 10 | +// DocumentSequenceStyle is used to represent how a document sequence is laid out in a slice of |
| 11 | +// bytes. |
| 12 | +type DocumentSequenceStyle uint32 |
| 13 | + |
| 14 | +// These constants are the valid styles for a DocumentSequence. |
| 15 | +const ( |
| 16 | + _ DocumentSequenceStyle = iota |
| 17 | + SequenceStyle |
| 18 | + ArrayStyle |
| 19 | +) |
| 20 | + |
| 21 | +// DocumentSequence represents a sequence of documents. The Style field indicates how the documents |
| 22 | +// are laid out inside of the Data field. |
| 23 | +type DocumentSequence struct { |
| 24 | + Style DocumentSequenceStyle |
| 25 | + Data []byte |
| 26 | + Pos int |
| 27 | +} |
| 28 | + |
| 29 | +// ErrCorruptedDocument is returned when a full document couldn't be read from the sequence. |
| 30 | +var ErrCorruptedDocument = errors.New("invalid DocumentSequence: corrupted document") |
| 31 | + |
| 32 | +// ErrNonDocument is returned when a DocumentSequence contains a non-document BSON value. |
| 33 | +var ErrNonDocument = errors.New("invalid DocumentSequence: a non-document value was found in sequence") |
| 34 | + |
| 35 | +// ErrInvalidDocumentSequenceStyle is returned when an unknown DocumentSequenceStyle is set on a |
| 36 | +// DocumentSequence. |
| 37 | +var ErrInvalidDocumentSequenceStyle = errors.New("invalid DocumentSequenceStyle") |
| 38 | + |
| 39 | +// DocumentCount returns the number of documents in the sequence. |
| 40 | +func (ds *DocumentSequence) DocumentCount() int { |
| 41 | + if ds == nil { |
| 42 | + return 0 |
| 43 | + } |
| 44 | + switch ds.Style { |
| 45 | + case SequenceStyle: |
| 46 | + var count int |
| 47 | + var ok bool |
| 48 | + rem := ds.Data |
| 49 | + for len(rem) > 0 { |
| 50 | + _, rem, ok = ReadDocument(rem) |
| 51 | + if !ok { |
| 52 | + return 0 |
| 53 | + } |
| 54 | + count++ |
| 55 | + } |
| 56 | + return count |
| 57 | + case ArrayStyle: |
| 58 | + _, rem, ok := ReadLength(ds.Data) |
| 59 | + if !ok { |
| 60 | + return 0 |
| 61 | + } |
| 62 | + |
| 63 | + var count int |
| 64 | + for len(rem) > 1 { |
| 65 | + _, rem, ok = ReadElement(rem) |
| 66 | + if !ok { |
| 67 | + return 0 |
| 68 | + } |
| 69 | + count++ |
| 70 | + } |
| 71 | + return count |
| 72 | + default: |
| 73 | + return 0 |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +//ResetIterator resets the iteration point for the Next method to the beginning of the document |
| 78 | +//sequence. |
| 79 | +func (ds *DocumentSequence) ResetIterator() { |
| 80 | + if ds == nil { |
| 81 | + return |
| 82 | + } |
| 83 | + ds.Pos = 0 |
| 84 | +} |
| 85 | + |
| 86 | +// documents returns a slice of the documents. If nil either the Data field is also nil or could not |
| 87 | +// be properly read. |
| 88 | +func (ds *DocumentSequence) documents() ([]Document, error) { |
| 89 | + if ds == nil { |
| 90 | + return nil, nil |
| 91 | + } |
| 92 | + switch ds.Style { |
| 93 | + case SequenceStyle: |
| 94 | + rem := ds.Data |
| 95 | + var docs []Document |
| 96 | + var doc Document |
| 97 | + var ok bool |
| 98 | + for { |
| 99 | + doc, rem, ok = ReadDocument(rem) |
| 100 | + if !ok { |
| 101 | + if len(rem) == 0 { |
| 102 | + break |
| 103 | + } |
| 104 | + return nil, ErrCorruptedDocument |
| 105 | + } |
| 106 | + docs = append(docs, doc) |
| 107 | + } |
| 108 | + return docs, nil |
| 109 | + case ArrayStyle: |
| 110 | + if len(ds.Data) == 0 { |
| 111 | + return nil, nil |
| 112 | + } |
| 113 | + vals, err := Document(ds.Data).Values() |
| 114 | + if err != nil { |
| 115 | + return nil, ErrCorruptedDocument |
| 116 | + } |
| 117 | + docs := make([]Document, 0, len(vals)) |
| 118 | + for _, v := range vals { |
| 119 | + if v.Type != bsontype.EmbeddedDocument { |
| 120 | + return nil, ErrNonDocument |
| 121 | + } |
| 122 | + docs = append(docs, v.Data) |
| 123 | + } |
| 124 | + return docs, nil |
| 125 | + default: |
| 126 | + return nil, ErrInvalidDocumentSequenceStyle |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// Next retrieves the next document from this sequence and returns it. This method will return |
| 131 | +// io.EOF when it has reached the end of the sequence. |
| 132 | +func (ds *DocumentSequence) Next() (Document, error) { |
| 133 | + if ds == nil || ds.Pos >= len(ds.Data) { |
| 134 | + return nil, io.EOF |
| 135 | + } |
| 136 | + switch ds.Style { |
| 137 | + case SequenceStyle: |
| 138 | + doc, _, ok := ReadDocument(ds.Data[ds.Pos:]) |
| 139 | + if !ok { |
| 140 | + return nil, ErrCorruptedDocument |
| 141 | + } |
| 142 | + ds.Pos += len(doc) |
| 143 | + return doc, nil |
| 144 | + case ArrayStyle: |
| 145 | + if ds.Pos < 4 { |
| 146 | + if len(ds.Data) < 4 { |
| 147 | + return nil, ErrCorruptedDocument |
| 148 | + } |
| 149 | + ds.Pos = 4 // Skip the length of the document |
| 150 | + } |
| 151 | + if len(ds.Data[ds.Pos:]) == 1 && ds.Data[ds.Pos] == 0x00 { |
| 152 | + return nil, io.EOF // At the end of the document |
| 153 | + } |
| 154 | + elem, _, ok := ReadElement(ds.Data[ds.Pos:]) |
| 155 | + if !ok { |
| 156 | + return nil, ErrCorruptedDocument |
| 157 | + } |
| 158 | + ds.Pos += len(elem) |
| 159 | + val := elem.Value() |
| 160 | + if val.Type != bsontype.EmbeddedDocument { |
| 161 | + return nil, ErrNonDocument |
| 162 | + } |
| 163 | + return val.Data, nil |
| 164 | + default: |
| 165 | + return nil, ErrInvalidDocumentSequenceStyle |
| 166 | + } |
| 167 | +} |
0 commit comments