/Raven.Database/Storage/Esent/SchemaUpdates/Updates/From37To38.cs

https://github.com/samueldjack/ravendb · C# · 282 lines · 242 code · 35 blank · 5 comment · 0 complexity · b4933ad23d80831e582b7ff769c2600f MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="From37To38.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using Microsoft.Isam.Esent.Interop;
  7. using Raven.Database.Impl;
  8. namespace Raven.Storage.Esent.SchemaUpdates.Updates
  9. {
  10. public class From37To38 : ISchemaUpdate
  11. {
  12. public string FromSchemaVersion { get { return "3.7"; } }
  13. public void Init(IUuidGenerator generator)
  14. {
  15. }
  16. public void Update(Session session, JET_DBID dbid)
  17. {
  18. Api.JetDeleteTable(session, dbid, "mapped_results"); // just kill the old table, we won't use the data anyway
  19. CreateMapResultsTable(session, dbid);
  20. CreateReduceResultsTable(session, dbid);
  21. CreateScheduledReductionsTable(session, dbid);
  22. SchemaCreator.UpdateVersion(session, dbid, "3.8");
  23. }
  24. private void CreateScheduledReductionsTable(Session session, JET_DBID dbid)
  25. {
  26. JET_TABLEID tableid;
  27. Api.JetCreateTable(session, dbid, "scheduled_reductions", 1, 80, out tableid);
  28. JET_COLUMNID columnid;
  29. Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
  30. {
  31. coltyp = JET_coltyp.Long,
  32. grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
  33. }, null, 0, out columnid);
  34. Api.JetAddColumn(session, tableid, "view", new JET_COLUMNDEF
  35. {
  36. cbMax = 2048,
  37. coltyp = JET_coltyp.LongText,
  38. cp = JET_CP.Unicode,
  39. grbit = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
  40. }, null, 0, out columnid);
  41. Api.JetAddColumn(session, tableid, "reduce_key", new JET_COLUMNDEF
  42. {
  43. coltyp = JET_coltyp.LongText,
  44. cp = JET_CP.Unicode,
  45. grbit = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
  46. }, null, 0, out columnid);
  47. Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
  48. {
  49. cbMax = 16,
  50. coltyp = JET_coltyp.Binary,
  51. grbit = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
  52. }, null, 0, out columnid);
  53. Api.JetAddColumn(session, tableid, "timestamp", new JET_COLUMNDEF
  54. {
  55. coltyp = JET_coltyp.DateTime,
  56. grbit = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
  57. }, null, 0, out columnid);
  58. Api.JetAddColumn(session, tableid, "bucket", new JET_COLUMNDEF
  59. {
  60. coltyp = JET_coltyp.Long,
  61. grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
  62. }, null, 0, out columnid);
  63. Api.JetAddColumn(session, tableid, "level", new JET_COLUMNDEF
  64. {
  65. coltyp = JET_coltyp.Long,
  66. grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
  67. }, null, 0, out columnid);
  68. SchemaCreator.CreateIndexes(session, tableid,
  69. new JET_INDEXCREATE
  70. {
  71. szIndexName = "by_id",
  72. szKey = "+id\0\0",
  73. grbit = CreateIndexGrbit.IndexPrimary
  74. },
  75. new JET_INDEXCREATE
  76. {
  77. szIndexName = "by_view",
  78. szKey = "+view\0\0",
  79. grbit = CreateIndexGrbit.IndexDisallowNull
  80. },
  81. new JET_INDEXCREATE
  82. {
  83. szIndexName = "by_view_level_reduce_key_and_bucket",
  84. szKey = "+view\0+level\0+reduce_key\0+bucket\0\0",
  85. });
  86. }
  87. private void CreateMapResultsTable(Session session, JET_DBID dbid)
  88. {
  89. JET_TABLEID tableid;
  90. Api.JetCreateTable(session, dbid, "mapped_results", 1, 80, out tableid);
  91. JET_COLUMNID columnid;
  92. Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
  93. {
  94. coltyp = JET_coltyp.Long,
  95. grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
  96. }, null, 0, out columnid);
  97. Api.JetAddColumn(session, tableid, "view", new JET_COLUMNDEF
  98. {
  99. cbMax = 2048,
  100. coltyp = JET_coltyp.LongText,
  101. cp = JET_CP.Unicode,
  102. grbit = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
  103. }, null, 0, out columnid);
  104. Api.JetAddColumn(session, tableid, "document_key", new JET_COLUMNDEF
  105. {
  106. cbMax = 2048,
  107. coltyp = JET_coltyp.LongText,
  108. cp = JET_CP.Unicode,
  109. grbit = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
  110. }, null, 0, out columnid);
  111. Api.JetAddColumn(session, tableid, "reduce_key", new JET_COLUMNDEF
  112. {
  113. coltyp = JET_coltyp.LongText,
  114. cp = JET_CP.Unicode,
  115. grbit = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
  116. }, null, 0, out columnid);
  117. Api.JetAddColumn(session, tableid, "data", new JET_COLUMNDEF
  118. {
  119. coltyp = JET_coltyp.LongBinary,
  120. grbit = ColumndefGrbit.ColumnTagged
  121. }, null, 0, out columnid);
  122. Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
  123. {
  124. cbMax = 16,
  125. coltyp = JET_coltyp.Binary,
  126. grbit = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
  127. }, null, 0, out columnid);
  128. Api.JetAddColumn(session, tableid, "timestamp", new JET_COLUMNDEF
  129. {
  130. coltyp = JET_coltyp.DateTime,
  131. grbit = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
  132. }, null, 0, out columnid);
  133. Api.JetAddColumn(session, tableid, "bucket", new JET_COLUMNDEF
  134. {
  135. coltyp = JET_coltyp.Long,
  136. grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
  137. }, null, 0, out columnid);
  138. SchemaCreator.CreateIndexes(session, tableid,
  139. new JET_INDEXCREATE
  140. {
  141. szIndexName = "by_id",
  142. szKey = "+id\0\0",
  143. grbit = CreateIndexGrbit.IndexPrimary
  144. },
  145. new JET_INDEXCREATE
  146. {
  147. szIndexName = "by_view_and_doc_key",
  148. szKey = "+view\0+document_key\0\0",
  149. grbit = CreateIndexGrbit.IndexDisallowNull
  150. },
  151. new JET_INDEXCREATE
  152. {
  153. szIndexName = "by_view",
  154. szKey = "+view\0\0",
  155. grbit = CreateIndexGrbit.IndexDisallowNull
  156. },
  157. new JET_INDEXCREATE
  158. {
  159. szIndexName = "by_view_and_etag",
  160. szKey = "+view\0-etag\0\0",
  161. },
  162. new JET_INDEXCREATE
  163. {
  164. szIndexName = "by_view_reduce_key_and_bucket",
  165. szKey = "+view\0+reduce_key\0+bucket\0\0",
  166. });
  167. }
  168. private void CreateReduceResultsTable(Session session, JET_DBID dbid)
  169. {
  170. JET_TABLEID tableid;
  171. Api.JetCreateTable(session, dbid, "reduce_results", 1, 80, out tableid);
  172. JET_COLUMNID columnid;
  173. Api.JetAddColumn(session, tableid, "id", new JET_COLUMNDEF
  174. {
  175. coltyp = JET_coltyp.Long,
  176. grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnAutoincrement | ColumndefGrbit.ColumnNotNULL
  177. }, null, 0, out columnid);
  178. Api.JetAddColumn(session, tableid, "view", new JET_COLUMNDEF
  179. {
  180. cbMax = 2048,
  181. coltyp = JET_coltyp.LongText,
  182. cp = JET_CP.Unicode,
  183. grbit = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
  184. }, null, 0, out columnid);
  185. Api.JetAddColumn(session, tableid, "reduce_key", new JET_COLUMNDEF
  186. {
  187. coltyp = JET_coltyp.LongText,
  188. cp = JET_CP.Unicode,
  189. grbit = SchemaCreator.ColumnNotNullIfOnHigherThanWindowsXp()
  190. }, null, 0, out columnid);
  191. Api.JetAddColumn(session, tableid, "data", new JET_COLUMNDEF
  192. {
  193. coltyp = JET_coltyp.LongBinary,
  194. grbit = ColumndefGrbit.ColumnTagged
  195. }, null, 0, out columnid);
  196. Api.JetAddColumn(session, tableid, "etag", new JET_COLUMNDEF
  197. {
  198. cbMax = 16,
  199. coltyp = JET_coltyp.Binary,
  200. grbit = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
  201. }, null, 0, out columnid);
  202. Api.JetAddColumn(session, tableid, "timestamp", new JET_COLUMNDEF
  203. {
  204. coltyp = JET_coltyp.DateTime,
  205. grbit = ColumndefGrbit.ColumnNotNULL | ColumndefGrbit.ColumnFixed
  206. }, null, 0, out columnid);
  207. Api.JetAddColumn(session, tableid, "bucket", new JET_COLUMNDEF
  208. {
  209. coltyp = JET_coltyp.Long,
  210. grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
  211. }, null, 0, out columnid);
  212. Api.JetAddColumn(session, tableid, "source_bucket", new JET_COLUMNDEF
  213. {
  214. coltyp = JET_coltyp.Long,
  215. grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
  216. }, null, 0, out columnid);
  217. Api.JetAddColumn(session, tableid, "level", new JET_COLUMNDEF
  218. {
  219. coltyp = JET_coltyp.Long,
  220. grbit = ColumndefGrbit.ColumnFixed | ColumndefGrbit.ColumnNotNULL
  221. }, null, 0, out columnid);
  222. SchemaCreator.CreateIndexes(session, tableid,
  223. new JET_INDEXCREATE
  224. {
  225. szIndexName = "by_id",
  226. szKey = "+id\0\0",
  227. grbit = CreateIndexGrbit.IndexPrimary
  228. },
  229. new JET_INDEXCREATE
  230. {
  231. szIndexName = "by_view",
  232. szKey = "+view\0\0",
  233. grbit = CreateIndexGrbit.IndexDisallowNull
  234. },
  235. new JET_INDEXCREATE
  236. {
  237. szIndexName = "by_view_level_reduce_key_and_bucket",
  238. szKey = "+view\0+level\0+reduce_key\0+bucket\0\0",
  239. },
  240. new JET_INDEXCREATE
  241. {
  242. szIndexName = "by_view_level_reduce_key_and_source_bucket",
  243. szKey = "+view\0+level\0+reduce_key\0+source_bucket\0\0",
  244. });
  245. }
  246. }
  247. }