1cedead69SVolker Mische% Licensed under the Apache License, Version 2.0 (the "License"); you may not
2cedead69SVolker Mische% use this file except in compliance with the License. You may obtain a copy of
3cedead69SVolker Mische% the License at
4cedead69SVolker Mische%
5cedead69SVolker Mische%   http://www.apache.org/licenses/LICENSE-2.0
6cedead69SVolker Mische%
7cedead69SVolker Mische% Unless required by applicable law or agreed to in writing, software
8cedead69SVolker Mische% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9cedead69SVolker Mische% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10cedead69SVolker Mische% License for the specific language governing permissions and limitations under
11cedead69SVolker Mische% the License.
12cedead69SVolker Mische
13cedead69SVolker Mische
14cedead69SVolker Mische% The value a key can have. For the vtree that is either a number or
15775d48c3SVolker Mische% (UTF-8) string. `nil` is used as a wildcard in queries, to match open or
16775d48c3SVolker Mische% full ranges.
17775d48c3SVolker Mische-type keyval() :: number() | string() | nil.
18cedead69SVolker Mische
19cedead69SVolker Mische% The multidimensional bounding box
20cedead69SVolker Mische-type mbb() :: [{Min :: keyval(), Max :: keyval()}].
2184438162SVolker Mische
2284438162SVolker Mische-type geom_type() :: 'Point' | 'LineString' | 'Polygon' | 'MultiPoint' |
2384438162SVolker Mische                     'MultiLineString' | 'MultiPolygon' | 'GeometryCollection'.
2484438162SVolker Mische-type geom_coords() :: [number()] | [geom_coords()].
2584438162SVolker Mische-type geom() :: {Type :: geom_type(), Coordinates :: geom_coords()}.
2684438162SVolker Mische
2784438162SVolker Mische% No idea what the json type will be yet
2884438162SVolker Mische-type json() :: any().
2984438162SVolker Mische
3003565c35SVolker Mische-type kp_value() :: {PointerNode :: non_neg_integer(),
3184438162SVolker Mische                     TreeSize :: non_neg_integer(), Reduce :: any()}.
3284438162SVolker Mische-type kv_value() :: {DocId :: binary(), Geometry :: geom(), Body :: json()}.
33cedead69SVolker Mische
34cedead69SVolker Mische-type candidate() :: {[split_node()], [split_node()]}.
35cedead69SVolker Mische
36cedead69SVolker Mische% The less function compares two values and returns true if the former is
37cedead69SVolker Mische% less than the latter
38cedead69SVolker Mische-type lessfun() :: fun((keyval(), keyval()) -> boolean()).
3984438162SVolker Mische
4084438162SVolker Mische
4184438162SVolker Mische-define(KP_NODE, 0).
4284438162SVolker Mische-define(KV_NODE, 1).
4379503af1SVolker Mische-define(ext_size(T), erlang:external_size(T, [{minor_version, 1}])).
4484438162SVolker Mische
4584438162SVolker Mische
4684438162SVolker Mische-record(kv_node, {
4784438162SVolker Mische          key = [] :: mbb(),
4884438162SVolker Mische          docid = nil :: binary() | nil ,
49532a0043SVolker Mische          geometry = <<>> :: binary() | {dups, [binary()]},
50532a0043SVolker Mische          body = nil :: binary() | nil | {dups, [binary()]},
51f6402621SVolker Mische          % The body and the geometry are stored on disk early. Store their
52f6402621SVolker Mische          % size here. A value of -1 means that the `geometry` and the `body`
53f6402621SVolker Mische          % property are pointers, but the size is not known. A value of 0
54f6402621SVolker Mische          % means that the size wasn't set yet and the `geometry` and `body`
55f6402621SVolker Mische          % contain the actual values
566cd8cae1SVolker Mische          size = 0 :: integer(),
576cd8cae1SVolker Mische          partition = 0 :: non_neg_integer()
5884438162SVolker Mische}).
5984438162SVolker Mische
6084438162SVolker Mische-record(kp_node, {
61618ea5a4SVolker Mische          % Root nodes don't have a key
62618ea5a4SVolker Mische          key = [] :: mbb() | nil,
63618ea5a4SVolker Mische          childpointer = 0 :: non_neg_integer(),
6484438162SVolker Mische          treesize = 0 :: non_neg_integer(),
652b13ff38SVolker Mische          mbb_orig = [] :: mbb()
6684438162SVolker Mische}).
6784438162SVolker Mische
6884438162SVolker Mische-record(vtree, {
6984438162SVolker Mische          fd = nil :: file:io_device() | nil,
7084438162SVolker Mische          % The root node of the tree
7184438162SVolker Mische          %root = nil ::kp_value() | kv_value()
7284438162SVolker Mische          root = nil :: #kp_node{} | nil,
7384438162SVolker Mische          less = fun(A, B) -> A < B end,
7484438162SVolker Mische          reduce = nil :: any(),
75283da2dcSVolker Mische          % `kp_chunk_threshold` and `kv_chunk_threshold` are normally set by
76283da2dcSVolker Mische          % vtree_state (which is part of the Couchbase/Apache CouchDB API
77283da2dcSVolker Mische          % implementation)
78283da2dcSVolker Mische          kp_chunk_threshold = nil :: number() | nil,
79283da2dcSVolker Mische          kv_chunk_threshold = nil :: number() | nil,
80283da2dcSVolker Mische          % The value of the minimum fill rate of a node should always be
81283da2dcSVolker Mische          % below 0.5.
82283da2dcSVolker Mische          min_fill_rate = 0.4 :: number()
8384438162SVolker Mische}).
84283da2dcSVolker Mische
8503565c35SVolker Mische%% The node format for the splits. It contains the MBB and in case of a:
8603565c35SVolker Mische%%  1. KV node: the pointer to the node in the file
8703565c35SVolker Mische%%  2. KP node: a list of pointers to its children
8803565c35SVolker Mische%%  3. abused: any value
89283da2dcSVolker Mische-type split_node() :: {Mbb :: mbb(), KvOrKpv :: #kv_node{} | #kp_node{} | any()}.