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
13
14% The value a key can have. For the vtree that is either a number or
15% (UTF-8) string. `nil` is used as a wildcard in queries, to match open or
16% full ranges.
17-type keyval() :: number() | string() | nil.
18
19% The multidimensional bounding box
20-type mbb() :: [{Min :: keyval(), Max :: keyval()}].
21
22-type geom_type() :: 'Point' | 'LineString' | 'Polygon' | 'MultiPoint' |
23                     'MultiLineString' | 'MultiPolygon' | 'GeometryCollection'.
24-type geom_coords() :: [number()] | [geom_coords()].
25-type geom() :: {Type :: geom_type(), Coordinates :: geom_coords()}.
26
27% No idea what the json type will be yet
28-type json() :: any().
29
30-type kp_value() :: {PointerNode :: non_neg_integer(),
31                     TreeSize :: non_neg_integer(), Reduce :: any()}.
32-type kv_value() :: {DocId :: binary(), Geometry :: geom(), Body :: json()}.
33
34-type candidate() :: {[split_node()], [split_node()]}.
35
36% The less function compares two values and returns true if the former is
37% less than the latter
38-type lessfun() :: fun((keyval(), keyval()) -> boolean()).
39
40
41-define(KP_NODE, 0).
42-define(KV_NODE, 1).
43-define(ext_size(T), erlang:external_size(T, [{minor_version, 1}])).
44
45
46-record(kv_node, {
47          key = [] :: mbb(),
48          docid = nil :: binary() | nil ,
49          geometry = <<>> :: binary() | {dups, [binary()]},
50          body = nil :: binary() | nil | {dups, [binary()]},
51          % The body and the geometry are stored on disk early. Store their
52          % size here. A value of -1 means that the `geometry` and the `body`
53          % property are pointers, but the size is not known. A value of 0
54          % means that the size wasn't set yet and the `geometry` and `body`
55          % contain the actual values
56          size = 0 :: integer(),
57          partition = 0 :: non_neg_integer()
58}).
59
60-record(kp_node, {
61          % Root nodes don't have a key
62          key = [] :: mbb() | nil,
63          childpointer = 0 :: non_neg_integer(),
64          treesize = 0 :: non_neg_integer(),
65          mbb_orig = [] :: mbb()
66}).
67
68-record(vtree, {
69          fd = nil :: file:io_device() | nil,
70          % The root node of the tree
71          %root = nil ::kp_value() | kv_value()
72          root = nil :: #kp_node{} | nil,
73          less = fun(A, B) -> A < B end,
74          reduce = nil :: any(),
75          % `kp_chunk_threshold` and `kv_chunk_threshold` are normally set by
76          % vtree_state (which is part of the Couchbase/Apache CouchDB API
77          % implementation)
78          kp_chunk_threshold = nil :: number() | nil,
79          kv_chunk_threshold = nil :: number() | nil,
80          % The value of the minimum fill rate of a node should always be
81          % below 0.5.
82          min_fill_rate = 0.4 :: number()
83}).
84
85%% The node format for the splits. It contains the MBB and in case of a:
86%%  1. KV node: the pointer to the node in the file
87%%  2. KP node: a list of pointers to its children
88%%  3. abused: any value
89-type split_node() :: {Mbb :: mbb(), KvOrKpv :: #kv_node{} | #kp_node{} | any()}.
90