/branches/SN-NG4.2-db_4.8.30_import/db4/csharp/BTreeCursor.cs

https://gitlab.com/OpenSourceMirror/sourcenav · C# · 295 lines · 74 code · 9 blank · 212 comment · 0 complexity · ce9cbc82f9931b082a40461d5c9027ba MD5 · raw file

  1. /*-
  2. * See the file LICENSE for redistribution information.
  3. *
  4. * Copyright (c) 2009 Oracle. All rights reserved.
  5. *
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using BerkeleyDB.Internal;
  11. namespace BerkeleyDB {
  12. /// <summary>
  13. /// A class for traversing the records of a <see cref="BTreeDatabase"/>
  14. /// </summary>
  15. public class BTreeCursor : Cursor {
  16. internal BTreeCursor(DBC dbc, uint pagesize)
  17. : base(dbc, DatabaseType.BTREE, pagesize) { }
  18. internal BTreeCursor(DBC dbc, uint pagesize, CachePriority p)
  19. : base(dbc, DatabaseType.BTREE, pagesize, p) { }
  20. /// <summary>
  21. /// Create a new cursor that uses the same transaction and locker ID as
  22. /// the original cursor.
  23. /// </summary>
  24. /// <remarks>
  25. /// This is useful when an application is using locking and requires two
  26. /// or more cursors in the same thread of control.
  27. /// </remarks>
  28. /// <param name="keepPosition">
  29. /// If true, the newly created cursor is initialized to refer to the
  30. /// same position in the database as the original cursor (if any) and
  31. /// hold the same locks (if any). If false, or the original cursor does
  32. /// not hold a database position and locks, the created cursor is
  33. /// uninitialized and will behave like a cursor newly created by
  34. /// <see cref="BTreeDatabase.Cursor"/>.</param>
  35. /// <returns>A newly created cursor</returns>
  36. public new BTreeCursor Duplicate(bool keepPosition) {
  37. return new BTreeCursor(
  38. dbc.dup(keepPosition ? DbConstants.DB_POSITION : 0), pgsz);
  39. }
  40. /// <summary>
  41. /// Position the cursor at a specific key/data pair in the database, and
  42. /// store the key/data pair in <see cref="Cursor.Current"/>.
  43. /// </summary>
  44. /// <param name="recno">
  45. /// The specific numbered record of the database at which to position
  46. /// the cursor.
  47. /// </param>
  48. /// <returns>
  49. /// True if the cursor was positioned successfully, false otherwise.
  50. /// </returns>
  51. public bool Move(uint recno) { return Move(recno, null); }
  52. /// <summary>
  53. /// Position the cursor at a specific key/data pair in the database, and
  54. /// store the key/data pair in <see cref="Cursor.Current"/>.
  55. /// </summary>
  56. /// <param name="recno">
  57. /// The specific numbered record of the database at which to position
  58. /// the cursor.
  59. /// </param>
  60. /// <param name="info">The locking behavior to use</param>
  61. /// <returns>
  62. /// True if the cursor was positioned successfully, false otherwise.
  63. /// </returns>
  64. public bool Move(uint recno, LockingInfo info) {
  65. DatabaseEntry key = new DatabaseEntry();
  66. key.Data = BitConverter.GetBytes(recno);
  67. DatabaseEntry data = new DatabaseEntry();
  68. return base.Get(key, data, DbConstants.DB_SET_RECNO, info);
  69. }
  70. /// <summary>
  71. /// Position the cursor at a specific key/data pair in the database, and
  72. /// store the key/data pair and as many duplicate data items that can
  73. /// fit in a buffer the size of one database page in
  74. /// <see cref="Cursor.CurrentMultiple"/>.
  75. /// </summary>
  76. /// <param name="recno">
  77. /// The specific numbered record of the database at which to position
  78. /// the cursor.
  79. /// </param>
  80. /// <returns>
  81. /// True if the cursor was positioned successfully, false otherwise.
  82. /// </returns>
  83. public bool MoveMultiple(uint recno) {
  84. return MoveMultiple(recno, (int)pgsz, null);
  85. }
  86. /// <summary>
  87. /// Position the cursor at a specific key/data pair in the database, and
  88. /// store the key/data pair and as many duplicate data items that can
  89. /// fit in a buffer the size of one database page in
  90. /// <see cref="Cursor.CurrentMultiple"/>.
  91. /// </summary>
  92. /// <param name="recno">
  93. /// The specific numbered record of the database at which to position
  94. /// the cursor.
  95. /// </param>
  96. /// <param name="BufferSize">
  97. /// The size of a buffer to fill with duplicate data items. Must be at
  98. /// least the page size of the underlying database and be a multiple of
  99. /// 1024.
  100. /// </param>
  101. /// <returns>
  102. /// True if the cursor was positioned successfully, false otherwise.
  103. /// </returns>
  104. public bool MoveMultiple(uint recno, int BufferSize) {
  105. return MoveMultiple(recno, BufferSize, null);
  106. }
  107. /// <summary>
  108. /// Position the cursor at a specific key/data pair in the database, and
  109. /// store the key/data pair and as many duplicate data items that can
  110. /// fit in a buffer the size of one database page in
  111. /// <see cref="Cursor.CurrentMultiple"/>.
  112. /// </summary>
  113. /// <param name="recno">
  114. /// The specific numbered record of the database at which to position
  115. /// the cursor.
  116. /// </param>
  117. /// <param name="info">The locking behavior to use</param>
  118. /// <returns>
  119. /// True if the cursor was positioned successfully, false otherwise.
  120. /// </returns>
  121. public bool MoveMultiple(uint recno, LockingInfo info) {
  122. return MoveMultiple(recno, (int)pgsz, info);
  123. }
  124. /// <summary>
  125. /// Position the cursor at a specific key/data pair in the database, and
  126. /// store the key/data pair and as many duplicate data items that can
  127. /// fit in a buffer the size of one database page in
  128. /// <see cref="Cursor.CurrentMultiple"/>.
  129. /// </summary>
  130. /// <param name="recno">
  131. /// The specific numbered record of the database at which to position
  132. /// the cursor.
  133. /// </param>
  134. /// <param name="BufferSize">
  135. /// The size of a buffer to fill with duplicate data items. Must be at
  136. /// least the page size of the underlying database and be a multiple of
  137. /// 1024.
  138. /// </param>
  139. /// <param name="info">The locking behavior to use</param>
  140. /// <returns>
  141. /// True if the cursor was positioned successfully, false otherwise.
  142. /// </returns>
  143. public bool MoveMultiple(uint recno, int BufferSize, LockingInfo info) {
  144. DatabaseEntry key = new DatabaseEntry();
  145. key.Data = BitConverter.GetBytes(recno);
  146. DatabaseEntry data = new DatabaseEntry();
  147. return base.GetMultiple(
  148. key, data, BufferSize, DbConstants.DB_SET_RECNO, info, false);
  149. }
  150. /// <summary>
  151. /// Position the cursor at a specific key/data pair in the database, and
  152. /// store the key/data pair and as many ensuing key/data pairs that can
  153. /// fit in a buffer the size of one database page in
  154. /// <see cref="Cursor.CurrentMultipleKey"/>.
  155. /// </summary>
  156. /// <param name="recno">
  157. /// The specific numbered record of the database at which to position
  158. /// the cursor.
  159. /// </param>
  160. /// <returns>
  161. /// True if the cursor was positioned successfully, false otherwise.
  162. /// </returns>
  163. public bool MoveMultipleKey(uint recno) {
  164. return MoveMultipleKey(recno, (int)pgsz, null);
  165. }
  166. /// <summary>
  167. /// Position the cursor at a specific key/data pair in the database, and
  168. /// store the key/data pair and as many ensuing key/data pairs that can
  169. /// fit in a buffer the size of one database page in
  170. /// <see cref="Cursor.CurrentMultipleKey"/>.
  171. /// </summary>
  172. /// <param name="recno">
  173. /// The specific numbered record of the database at which to position
  174. /// the cursor.
  175. /// </param>
  176. /// <param name="BufferSize">
  177. /// The size of a buffer to fill with key/data pairs. Must be at least
  178. /// the page size of the underlying database and be a multiple of 1024.
  179. /// </param>
  180. /// <returns>
  181. /// True if the cursor was positioned successfully, false otherwise.
  182. /// </returns>
  183. public bool MoveMultipleKey(uint recno, int BufferSize) {
  184. return MoveMultipleKey(recno, BufferSize, null);
  185. }
  186. /// <summary>
  187. /// Position the cursor at a specific key/data pair in the database, and
  188. /// store the key/data pair and as many ensuing key/data pairs that can
  189. /// fit in a buffer the size of one database page in
  190. /// <see cref="Cursor.CurrentMultipleKey"/>.
  191. /// </summary>
  192. /// <param name="recno">
  193. /// The specific numbered record of the database at which to position
  194. /// the cursor.
  195. /// </param>
  196. /// <param name="info">The locking behavior to use</param>
  197. /// <returns>
  198. /// True if the cursor was positioned successfully, false otherwise.
  199. /// </returns>
  200. public bool MoveMultipleKey(uint recno, LockingInfo info) {
  201. return MoveMultipleKey(recno, (int)pgsz, info);
  202. }
  203. /// <summary>
  204. /// Position the cursor at a specific key/data pair in the database, and
  205. /// store the key/data pair and as many ensuing key/data pairs that can
  206. /// fit in a buffer the size of one database page in
  207. /// <see cref="Cursor.CurrentMultipleKey"/>.
  208. /// </summary>
  209. /// <param name="recno">
  210. /// The specific numbered record of the database at which to position
  211. /// the cursor.
  212. /// </param>
  213. /// <param name="BufferSize">
  214. /// The size of a buffer to fill with key/data pairs. Must be at least
  215. /// the page size of the underlying database and be a multiple of 1024.
  216. /// </param>
  217. /// <param name="info">The locking behavior to use</param>
  218. /// <returns>
  219. /// True if the cursor was positioned successfully, false otherwise.
  220. /// </returns>
  221. public bool MoveMultipleKey(
  222. uint recno, int BufferSize, LockingInfo info) {
  223. DatabaseEntry key = new DatabaseEntry();
  224. key.Data = BitConverter.GetBytes(recno);
  225. DatabaseEntry data = new DatabaseEntry();
  226. return base.GetMultiple(
  227. key, data, BufferSize, DbConstants.DB_SET_RECNO, info, true);
  228. }
  229. /// <summary>
  230. /// Return the record number associated with the cursor's current
  231. /// position.
  232. /// </summary>
  233. /// <returns>The record number associated with the cursor.</returns>
  234. public uint Recno() { return Recno(null); }
  235. /// <summary>
  236. /// Return the record number associated with the cursor's current
  237. /// position.
  238. /// </summary>
  239. /// <param name="info">The locking behavior to use</param>
  240. /// <returns>The record number associated with the cursor.</returns>
  241. public uint Recno(LockingInfo info) {
  242. DatabaseEntry key = new DatabaseEntry();
  243. DatabaseEntry data = new DatabaseEntry();
  244. base.Get(key, data, DbConstants.DB_GET_RECNO, info);
  245. return BitConverter.ToUInt32(base.Current.Value.Data, 0);
  246. }
  247. /// <summary>
  248. /// Insert the data element as a duplicate element of the key to which
  249. /// the cursor refers.
  250. /// </summary>
  251. /// <param name="data">The data element to insert</param>
  252. /// <param name="loc">
  253. /// Specify whether to insert the data item immediately before or
  254. /// immediately after the cursor's current position.
  255. /// </param>
  256. public new void Insert(DatabaseEntry data, InsertLocation loc) {
  257. base.Insert(data, loc);
  258. }
  259. /// <summary>
  260. /// Insert the specified key/data pair into the database, unless a
  261. /// key/data pair comparing equally to it already exists in the
  262. /// database.
  263. /// </summary>
  264. /// <param name="pair">The key/data pair to be inserted</param>
  265. /// <exception cref="KeyExistException">
  266. /// Thrown if a matching key/data pair already exists in the database.
  267. /// </exception>
  268. public new void AddUnique(
  269. KeyValuePair<DatabaseEntry, DatabaseEntry> pair) {
  270. base.AddUnique(pair);
  271. }
  272. /// <summary>
  273. /// Insert the specified key/data pair into the database.
  274. /// </summary>
  275. /// <param name="pair">The key/data pair to be inserted</param>
  276. /// <param name="loc">
  277. /// If the key already exists in the database and no duplicate sort
  278. /// function has been specified, specify whether the inserted data item
  279. /// is added as the first or the last of the data items for that key.
  280. /// </param>
  281. public new void Add(KeyValuePair<DatabaseEntry, DatabaseEntry> pair,
  282. InsertLocation loc) {
  283. base.Add(pair, loc);
  284. }
  285. }
  286. }