couch_db.hrl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. % Licensed under the Apache License, Version 2.0 (the "License"); you may not
  2. % use this file except in compliance with the License. You may obtain a copy of
  3. % the License at
  4. %
  5. % http://www.apache.org/licenses/LICENSE-2.0
  6. %
  7. % Unless required by applicable law or agreed to in writing, software
  8. % distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. % WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. % License for the specific language governing permissions and limitations under
  11. % the License.
  12. -define(LOCAL_DOC_PREFIX, "_local/").
  13. -define(DESIGN_DOC_PREFIX0, "_design").
  14. -define(DESIGN_DOC_PREFIX, "_design/").
  15. -define(DEFAULT_COMPRESSION, snappy).
  16. -define(MIN_STR, <<"">>).
  17. -define(MAX_STR, <<255>>). % illegal utf string
  18. % the lowest possible database sequence number
  19. -define(LOWEST_SEQ, 0).
  20. -define(REWRITE_COUNT, couch_rewrite_count).
  21. -define(JSON_ENCODE(V), ejson:encode(V)).
  22. -define(JSON_DECODE(V), ejson:decode(V)).
  23. -define(b2l(V), binary_to_list(V)).
  24. -define(l2b(V), list_to_binary(V)).
  25. -define(term_to_bin(T), term_to_binary(T, [{minor_version, 1}])).
  26. -define(term_size(T),
  27. try
  28. erlang:external_size(T)
  29. catch _:_ ->
  30. byte_size(?term_to_bin(T))
  31. end).
  32. -define(DEFAULT_ATTACHMENT_CONTENT_TYPE, <<"application/octet-stream">>).
  33. -define(LOG_DEBUG(Format, Args),
  34. case couch_log:debug_on(?MODULE) of
  35. true ->
  36. couch_log:debug(Format, Args);
  37. false -> ok
  38. end).
  39. -define(LOG_INFO(Format, Args),
  40. case couch_log:info_on(?MODULE) of
  41. true ->
  42. couch_log:info(Format, Args);
  43. false -> ok
  44. end).
  45. -define(LOG_WARN(Format, Args),
  46. case couch_log:warn_on(?MODULE) of
  47. true ->
  48. couch_log:warn(Format, Args);
  49. false -> ok
  50. end).
  51. -define(LOG_ERROR(Format, Args), couch_log:error(Format, Args)).
  52. % Tree::term() is really a tree(), but we don't want to require R13B04 yet
  53. -type branch() :: {Key::term(), Value::term(), Tree::term()}.
  54. -type path() :: {Start::pos_integer(), branch()}.
  55. -type tree() :: [branch()]. % sorted by key
  56. -record(rev_info,
  57. {
  58. rev,
  59. seq = 0,
  60. deleted = false,
  61. body_sp = nil % stream pointer
  62. }).
  63. -record(doc_info,
  64. {
  65. id = <<"">>,
  66. high_seq = 0,
  67. revs = [] % rev_info
  68. }).
  69. -record(full_doc_info,
  70. {id = <<"">>,
  71. update_seq = 0,
  72. deleted = false,
  73. rev_tree = [],
  74. leafs_size = 0
  75. }).
  76. -record(httpd,
  77. {mochi_req,
  78. peer,
  79. method,
  80. requested_path_parts,
  81. path_parts,
  82. db_url_handlers,
  83. user_ctx,
  84. req_body = undefined,
  85. design_url_handlers,
  86. auth,
  87. default_fun,
  88. url_handlers
  89. }).
  90. -record(doc,
  91. {
  92. id = <<"">>,
  93. revs = {0, []},
  94. % the json body object.
  95. body = {[]},
  96. atts = [], % attachments
  97. deleted = false,
  98. % key/value tuple of meta information, provided when using special options:
  99. % couch_db:open_doc(Db, Id, Options).
  100. meta = []
  101. }).
  102. -record(att,
  103. {
  104. name,
  105. type,
  106. att_len,
  107. disk_len, % length of the attachment in its identity form
  108. % (that is, without a content encoding applied to it)
  109. % differs from att_len when encoding /= identity
  110. md5= <<>>,
  111. revpos=0,
  112. data,
  113. encoding=identity % currently supported values are:
  114. % identity, gzip
  115. % additional values to support in the future:
  116. % deflate, compress
  117. }).
  118. -record(user_ctx,
  119. {
  120. name=null,
  121. roles=[],
  122. handler
  123. }).
  124. % This should be updated anytime a header change happens that requires more
  125. % than filling in new defaults.
  126. %
  127. % As long the changes are limited to new header fields (with inline
  128. % defaults) added to the end of the record, then there is no need to increment
  129. % the disk revision number.
  130. %
  131. % if the disk revision is incremented, then new upgrade logic will need to be
  132. % added to couch_db_updater:init_db.
  133. -define(LATEST_DISK_VERSION, 6).
  134. -record(db_header,
  135. {disk_version = ?LATEST_DISK_VERSION,
  136. update_seq = 0,
  137. unused = 0,
  138. fulldocinfo_by_id_btree_state = nil,
  139. docinfo_by_seq_btree_state = nil,
  140. local_docs_btree_state = nil,
  141. purge_seq = 0,
  142. purged_docs = nil,
  143. security_ptr = nil,
  144. revs_limit = 1000
  145. }).
  146. -record(db,
  147. {main_pid = nil,
  148. update_pid = nil,
  149. compactor_pid = nil,
  150. instance_start_time, % number of microsecs since jan 1 1970 as a binary string
  151. fd,
  152. updater_fd,
  153. fd_ref_counter,
  154. header = #db_header{},
  155. committed_update_seq,
  156. fulldocinfo_by_id_btree,
  157. docinfo_by_seq_btree,
  158. local_docs_btree,
  159. update_seq,
  160. name,
  161. filepath,
  162. validate_doc_funs = [],
  163. security = [],
  164. security_ptr = nil,
  165. user_ctx = #user_ctx{},
  166. waiting_delayed_commit = nil,
  167. revs_limit = 1000,
  168. fsync_options = [],
  169. options = [],
  170. compression,
  171. before_doc_update = nil, % nil | fun(Doc, Db) -> NewDoc
  172. after_doc_read = nil % nil | fun(Doc, Db) -> NewDoc
  173. }).
  174. -record(view_query_args, {
  175. start_key,
  176. end_key,
  177. start_docid = ?MIN_STR,
  178. end_docid = ?MAX_STR,
  179. direction = fwd,
  180. inclusive_end=true, % aka a closed-interval
  181. limit = 10000000000, % Huge number to simplify logic
  182. skip = 0,
  183. group_level = 0,
  184. view_type = nil,
  185. include_docs = false,
  186. conflicts = false,
  187. stale = false,
  188. multi_get = false,
  189. callback = nil,
  190. list = nil
  191. }).
  192. -record(view_fold_helper_funs, {
  193. reduce_count,
  194. passed_end,
  195. start_response,
  196. send_row
  197. }).
  198. -record(reduce_fold_helper_funs, {
  199. start_response,
  200. send_row
  201. }).
  202. -record(extern_resp_args, {
  203. code = 200,
  204. stop = false,
  205. data = <<>>,
  206. ctype = "application/json",
  207. headers = [],
  208. json = nil
  209. }).
  210. -record(index_header,
  211. {seq=0,
  212. purge_seq=0,
  213. id_btree_state=nil,
  214. view_states=nil
  215. }).
  216. % small value used in revision trees to indicate the revision isn't stored
  217. -define(REV_MISSING, []).
  218. -record(changes_args, {
  219. feed = "normal",
  220. dir = fwd,
  221. since = 0,
  222. limit = 1000000000000000,
  223. style = main_only,
  224. heartbeat,
  225. timeout,
  226. filter = "",
  227. filter_fun,
  228. filter_args = [],
  229. include_docs = false,
  230. conflicts = false,
  231. db_open_options = []
  232. }).
  233. -record(btree, {
  234. fd,
  235. root,
  236. extract_kv = fun({_Key, _Value} = KV) -> KV end,
  237. assemble_kv = fun(Key, Value) -> {Key, Value} end,
  238. less = fun(A, B) -> A < B end,
  239. reduce = nil,
  240. compression = ?DEFAULT_COMPRESSION
  241. }).