BsonDocumentReader.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.IO;
  17. namespace MongoDB.Bson.IO
  18. {
  19. /// <summary>
  20. /// Represents a BSON reader for a BsonDocument.
  21. /// </summary>
  22. public class BsonDocumentReader : BsonReader
  23. {
  24. // private fields
  25. private BsonDocumentReaderContext _context;
  26. private BsonValue _currentValue;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the BsonDocumentReader class.
  30. /// </summary>
  31. /// <param name="document">A BsonDocument.</param>
  32. /// <param name="settings">The reader settings.</param>
  33. public BsonDocumentReader(BsonDocument document, BsonDocumentReaderSettings settings)
  34. : base(settings)
  35. {
  36. if (document == null)
  37. {
  38. throw new ArgumentNullException("document");
  39. }
  40. _context = new BsonDocumentReaderContext(null, ContextType.TopLevel, document);
  41. _currentValue = document;
  42. }
  43. // public methods
  44. /// <summary>
  45. /// Closes the reader.
  46. /// </summary>
  47. public override void Close()
  48. {
  49. // Close can be called on Disposed objects
  50. if (State != BsonReaderState.Closed)
  51. {
  52. State = BsonReaderState.Closed;
  53. }
  54. }
  55. /// <summary>
  56. /// Gets a bookmark to the reader's current position and state.
  57. /// </summary>
  58. /// <returns>A bookmark.</returns>
  59. public override BsonReaderBookmark GetBookmark()
  60. {
  61. return new BsonDocumentReaderBookmark(State, CurrentBsonType, CurrentName, _context, _currentValue);
  62. }
  63. /// <summary>
  64. /// Reads BSON binary data from the reader.
  65. /// </summary>
  66. /// <returns>A BsonBinaryData.</returns>
  67. public override BsonBinaryData ReadBinaryData()
  68. {
  69. if (Disposed) { ThrowObjectDisposedException(); }
  70. VerifyBsonType("ReadBinaryData", BsonType.Binary);
  71. State = GetNextState();
  72. return _currentValue.AsBsonBinaryData;
  73. }
  74. /// <summary>
  75. /// Reads a BSON boolean from the reader.
  76. /// </summary>
  77. /// <returns>A Boolean.</returns>
  78. public override bool ReadBoolean()
  79. {
  80. if (Disposed) { ThrowObjectDisposedException(); }
  81. VerifyBsonType("ReadBoolean", BsonType.Boolean);
  82. State = GetNextState();
  83. return _currentValue.AsBoolean;
  84. }
  85. /// <summary>
  86. /// Reads a BsonType from the reader.
  87. /// </summary>
  88. /// <typeparam name="TValue">The type of the BsonTrie values.</typeparam>
  89. /// <param name="bsonTrie">An optional trie to search for a value that matches the next element name.</param>
  90. /// <param name="found">Set to true if a matching value was found in the trie.</param>
  91. /// <param name="value">Set to the matching value found in the trie or null if no matching value was found.</param>
  92. /// <returns>A BsonType.</returns>
  93. public override BsonType ReadBsonType<TValue>(BsonTrie<TValue> bsonTrie, out bool found, out TValue value)
  94. {
  95. if (Disposed) { ThrowObjectDisposedException(); }
  96. found = false;
  97. value = default(TValue);
  98. if (State == BsonReaderState.Initial || State == BsonReaderState.ScopeDocument)
  99. {
  100. // there is an implied type of Document for the top level and for scope documents
  101. CurrentBsonType = BsonType.Document;
  102. State = BsonReaderState.Value;
  103. return CurrentBsonType;
  104. }
  105. if (State != BsonReaderState.Type)
  106. {
  107. ThrowInvalidState("ReadBsonType", BsonReaderState.Type);
  108. }
  109. switch (_context.ContextType)
  110. {
  111. case ContextType.Array:
  112. _currentValue = _context.GetNextValue();
  113. if (_currentValue == null)
  114. {
  115. State = BsonReaderState.EndOfArray;
  116. return BsonType.EndOfDocument;
  117. }
  118. State = BsonReaderState.Value;
  119. break;
  120. case ContextType.Document:
  121. var currentElement = _context.GetNextElement();
  122. if (currentElement == null)
  123. {
  124. State = BsonReaderState.EndOfDocument;
  125. return BsonType.EndOfDocument;
  126. }
  127. if (bsonTrie != null)
  128. {
  129. found = bsonTrie.TryGetValue(currentElement.Name, out value);
  130. }
  131. CurrentName = currentElement.Name;
  132. _currentValue = currentElement.Value;
  133. State = BsonReaderState.Name;
  134. break;
  135. default:
  136. throw new BsonInternalException("Invalid ContextType.");
  137. }
  138. CurrentBsonType = _currentValue.BsonType;
  139. return CurrentBsonType;
  140. }
  141. /// <summary>
  142. /// Reads BSON binary data from the reader.
  143. /// </summary>
  144. /// <returns>A byte array.</returns>
  145. #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
  146. public override byte[] ReadBytes()
  147. {
  148. if (Disposed) { ThrowObjectDisposedException(); }
  149. VerifyBsonType("ReadBytes", BsonType.Binary);
  150. State = GetNextState();
  151. var binaryData = _currentValue.AsBsonBinaryData;
  152. var subType = binaryData.SubType;
  153. if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary)
  154. {
  155. var message = string.Format("ReadBytes requires the binary sub type to be Binary, not {2}.", subType);
  156. throw new Exception(message);
  157. }
  158. return binaryData.Bytes;
  159. }
  160. #pragma warning restore 618
  161. /// <summary>
  162. /// Reads a BSON DateTime from the reader.
  163. /// </summary>
  164. /// <returns>The number of milliseconds since the Unix epoch.</returns>
  165. public override long ReadDateTime()
  166. {
  167. if (Disposed) { ThrowObjectDisposedException(); }
  168. VerifyBsonType("ReadDateTime", BsonType.DateTime);
  169. State = GetNextState();
  170. return _currentValue.AsBsonDateTime.MillisecondsSinceEpoch;
  171. }
  172. /// <summary>
  173. /// Reads a BSON Double from the reader.
  174. /// </summary>
  175. /// <returns>A Double.</returns>
  176. public override double ReadDouble()
  177. {
  178. if (Disposed) { ThrowObjectDisposedException(); }
  179. VerifyBsonType("ReadDouble", BsonType.Double);
  180. State = GetNextState();
  181. return _currentValue.AsDouble;
  182. }
  183. /// <summary>
  184. /// Reads the end of a BSON array from the reader.
  185. /// </summary>
  186. public override void ReadEndArray()
  187. {
  188. if (Disposed) { ThrowObjectDisposedException(); }
  189. if (_context.ContextType != ContextType.Array)
  190. {
  191. ThrowInvalidContextType("ReadEndArray", _context.ContextType, ContextType.Array);
  192. }
  193. if (State == BsonReaderState.Type)
  194. {
  195. ReadBsonType(); // will set state to EndOfArray if at end of array
  196. }
  197. if (State != BsonReaderState.EndOfArray)
  198. {
  199. ThrowInvalidState("ReadEndArray", BsonReaderState.EndOfArray);
  200. }
  201. _context = _context.PopContext();
  202. switch (_context.ContextType)
  203. {
  204. case ContextType.Array: State = BsonReaderState.Type; break;
  205. case ContextType.Document: State = BsonReaderState.Type; break;
  206. case ContextType.TopLevel: State = BsonReaderState.Done; break;
  207. default: throw new BsonInternalException("Unexpected ContextType.");
  208. }
  209. }
  210. /// <summary>
  211. /// Reads the end of a BSON document from the reader.
  212. /// </summary>
  213. public override void ReadEndDocument()
  214. {
  215. if (Disposed) { ThrowObjectDisposedException(); }
  216. if (_context.ContextType != ContextType.Document && _context.ContextType != ContextType.ScopeDocument)
  217. {
  218. ThrowInvalidContextType("ReadEndDocument", _context.ContextType, ContextType.Document, ContextType.ScopeDocument);
  219. }
  220. if (State == BsonReaderState.Type)
  221. {
  222. ReadBsonType(); // will set state to EndOfDocument if at end of document
  223. }
  224. if (State != BsonReaderState.EndOfDocument)
  225. {
  226. ThrowInvalidState("ReadEndDocument", BsonReaderState.EndOfDocument);
  227. }
  228. _context = _context.PopContext();
  229. switch (_context.ContextType)
  230. {
  231. case ContextType.Array: State = BsonReaderState.Type; break;
  232. case ContextType.Document: State = BsonReaderState.Type; break;
  233. case ContextType.TopLevel: State = BsonReaderState.Done; break;
  234. default: throw new BsonInternalException("Unexpected ContextType.");
  235. }
  236. }
  237. /// <summary>
  238. /// Reads a BSON Int32 from the reader.
  239. /// </summary>
  240. /// <returns>An Int32.</returns>
  241. public override int ReadInt32()
  242. {
  243. if (Disposed) { ThrowObjectDisposedException(); }
  244. VerifyBsonType("ReadInt32", BsonType.Int32);
  245. State = GetNextState();
  246. return _currentValue.AsInt32;
  247. }
  248. /// <summary>
  249. /// Reads a BSON Int64 from the reader.
  250. /// </summary>
  251. /// <returns>An Int64.</returns>
  252. public override long ReadInt64()
  253. {
  254. if (Disposed) { ThrowObjectDisposedException(); }
  255. VerifyBsonType("ReadInt64", BsonType.Int64);
  256. State = GetNextState();
  257. return _currentValue.AsInt64;
  258. }
  259. /// <summary>
  260. /// Reads a BSON JavaScript from the reader.
  261. /// </summary>
  262. /// <returns>A string.</returns>
  263. public override string ReadJavaScript()
  264. {
  265. if (Disposed) { ThrowObjectDisposedException(); }
  266. VerifyBsonType("ReadJavaScript", BsonType.JavaScript);
  267. State = GetNextState();
  268. return _currentValue.AsBsonJavaScript.Code;
  269. }
  270. /// <summary>
  271. /// Reads a BSON JavaScript with scope from the reader (call ReadStartDocument next to read the scope).
  272. /// </summary>
  273. /// <returns>A string.</returns>
  274. public override string ReadJavaScriptWithScope()
  275. {
  276. if (Disposed) { ThrowObjectDisposedException(); }
  277. VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);
  278. State = BsonReaderState.ScopeDocument;
  279. return _currentValue.AsBsonJavaScriptWithScope.Code;
  280. }
  281. /// <summary>
  282. /// Reads a BSON MaxKey from the reader.
  283. /// </summary>
  284. public override void ReadMaxKey()
  285. {
  286. if (Disposed) { ThrowObjectDisposedException(); }
  287. VerifyBsonType("ReadMaxKey", BsonType.MaxKey);
  288. State = GetNextState();
  289. }
  290. /// <summary>
  291. /// Reads a BSON MinKey from the reader.
  292. /// </summary>
  293. public override void ReadMinKey()
  294. {
  295. if (Disposed) { ThrowObjectDisposedException(); }
  296. VerifyBsonType("ReadMinKey", BsonType.MinKey);
  297. State = GetNextState();
  298. }
  299. /// <summary>
  300. /// Reads a BSON null from the reader.
  301. /// </summary>
  302. public override void ReadNull()
  303. {
  304. if (Disposed) { ThrowObjectDisposedException(); }
  305. VerifyBsonType("ReadNull", BsonType.Null);
  306. State = GetNextState();
  307. }
  308. /// <summary>
  309. /// Reads a BSON ObjectId from the reader.
  310. /// </summary>
  311. /// <returns>An ObjectId.</returns>
  312. public override ObjectId ReadObjectId()
  313. {
  314. if (Disposed) { ThrowObjectDisposedException(); }
  315. VerifyBsonType("ReadObjectId", BsonType.ObjectId);
  316. State = GetNextState();
  317. return _currentValue.AsObjectId;
  318. }
  319. /// <summary>
  320. /// Reads a BSON regular expression from the reader.
  321. /// </summary>
  322. /// <returns>A BsonRegularExpression.</returns>
  323. public override BsonRegularExpression ReadRegularExpression()
  324. {
  325. if (Disposed) { ThrowObjectDisposedException(); }
  326. VerifyBsonType("ReadRegularExpression", BsonType.RegularExpression);
  327. State = GetNextState();
  328. return _currentValue.AsBsonRegularExpression;
  329. }
  330. /// <summary>
  331. /// Reads the start of a BSON array.
  332. /// </summary>
  333. public override void ReadStartArray()
  334. {
  335. if (Disposed) { ThrowObjectDisposedException(); }
  336. VerifyBsonType("ReadStartArray", BsonType.Array);
  337. var array = _currentValue.AsBsonArray;
  338. _context = new BsonDocumentReaderContext(_context, ContextType.Array, array);
  339. State = BsonReaderState.Type;
  340. }
  341. /// <summary>
  342. /// Reads the start of a BSON document.
  343. /// </summary>
  344. public override void ReadStartDocument()
  345. {
  346. if (Disposed) { ThrowObjectDisposedException(); }
  347. VerifyBsonType("ReadStartDocument", BsonType.Document);
  348. BsonDocument document;
  349. var script = _currentValue as BsonJavaScriptWithScope;
  350. if (script != null)
  351. {
  352. document = script.Scope;
  353. }
  354. else
  355. {
  356. document = _currentValue.AsBsonDocument;
  357. }
  358. _context = new BsonDocumentReaderContext(_context, ContextType.Document, document);
  359. State = BsonReaderState.Type;
  360. }
  361. /// <summary>
  362. /// Reads a BSON string from the reader.
  363. /// </summary>
  364. /// <returns>A String.</returns>
  365. public override string ReadString()
  366. {
  367. if (Disposed) { ThrowObjectDisposedException(); }
  368. VerifyBsonType("ReadString", BsonType.String);
  369. State = GetNextState();
  370. return _currentValue.AsString;
  371. }
  372. /// <summary>
  373. /// Reads a BSON symbol from the reader.
  374. /// </summary>
  375. /// <returns>A string.</returns>
  376. public override string ReadSymbol()
  377. {
  378. if (Disposed) { ThrowObjectDisposedException(); }
  379. VerifyBsonType("ReadSymbol", BsonType.Symbol);
  380. State = GetNextState();
  381. return _currentValue.AsBsonSymbol.Name;
  382. }
  383. /// <summary>
  384. /// Reads a BSON timestamp from the reader.
  385. /// </summary>
  386. /// <returns>The combined timestamp/increment.</returns>
  387. public override long ReadTimestamp()
  388. {
  389. if (Disposed) { ThrowObjectDisposedException(); }
  390. VerifyBsonType("ReadTimestamp", BsonType.Timestamp);
  391. State = GetNextState();
  392. return _currentValue.AsBsonTimestamp.Value;
  393. }
  394. /// <summary>
  395. /// Reads a BSON undefined from the reader.
  396. /// </summary>
  397. public override void ReadUndefined()
  398. {
  399. if (Disposed) { ThrowObjectDisposedException(); }
  400. VerifyBsonType("ReadUndefined", BsonType.Undefined);
  401. State = GetNextState();
  402. }
  403. /// <summary>
  404. /// Returns the reader to previously bookmarked position and state.
  405. /// </summary>
  406. /// <param name="bookmark">The bookmark.</param>
  407. public override void ReturnToBookmark(BsonReaderBookmark bookmark)
  408. {
  409. var documentReaderBookmark = (BsonDocumentReaderBookmark)bookmark;
  410. State = documentReaderBookmark.State;
  411. CurrentBsonType = documentReaderBookmark.CurrentBsonType;
  412. CurrentName = documentReaderBookmark.CurrentName;
  413. _context = documentReaderBookmark.CloneContext();
  414. _currentValue = documentReaderBookmark.CurrentValue;
  415. }
  416. /// <summary>
  417. /// Skips the name (reader must be positioned on a name).
  418. /// </summary>
  419. public override void SkipName()
  420. {
  421. if (Disposed) { ThrowObjectDisposedException(); }
  422. if (State != BsonReaderState.Name)
  423. {
  424. ThrowInvalidState("SkipName", BsonReaderState.Name);
  425. }
  426. State = BsonReaderState.Value;
  427. }
  428. /// <summary>
  429. /// Skips the value (reader must be positioned on a value).
  430. /// </summary>
  431. public override void SkipValue()
  432. {
  433. if (Disposed) { ThrowObjectDisposedException(); }
  434. if (State != BsonReaderState.Value)
  435. {
  436. ThrowInvalidState("SkipValue", BsonReaderState.Value);
  437. }
  438. State = BsonReaderState.Type;
  439. }
  440. // protected methods
  441. /// <summary>
  442. /// Disposes of any resources used by the reader.
  443. /// </summary>
  444. /// <param name="disposing">True if called from Dispose.</param>
  445. protected override void Dispose(bool disposing)
  446. {
  447. if (disposing)
  448. {
  449. try
  450. {
  451. Close();
  452. }
  453. catch { } // ignore exceptions
  454. }
  455. base.Dispose(disposing);
  456. }
  457. // private methods
  458. private BsonReaderState GetNextState()
  459. {
  460. switch (_context.ContextType)
  461. {
  462. case ContextType.Array:
  463. case ContextType.Document:
  464. return BsonReaderState.Type;
  465. case ContextType.TopLevel:
  466. return BsonReaderState.Done;
  467. default:
  468. throw new BsonInternalException("Unexpected ContextType.");
  469. }
  470. }
  471. }
  472. }