1/*-
2 * Copyright 1997, 1998, 2001 John-Mark Gurney.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#pragma once
29
30#include "ai_types.h"
31
32struct btree;
33struct btreenode;
34
35#define VOIDSIZE 8 /* force to 8, otherwise UU would not work on 32bit */
36#define U160SIZE AS_DIGEST_KEY_SZ
37
38typedef struct btree_specification { /* size 9B */
39 unsigned char ktype; /* [STRING,INT,FLOAT,LONG]--------------------| */
40 unsigned char btype; /* [data,index,node] | */
41 unsigned char ksize; /* UU&INDEX(8), UL&LU(12), LL(16) | */
42 unsigned int bflag; /* [OTHER_BT + BTFLAG_*_INDEX] | */
43 unsigned short num; /*--------------------------------------------| */
44} __attribute__ ((packed)) bts_t;
45
46typedef void * bt_data_t;
47typedef int (*bt_cmp_t)(bt_data_t k1, bt_data_t k2);
48
49// CONSTRUCTOR CONSTRUCTOR CONSTRUCTOR CONSTRUCTOR CONSTRUCTOR CONSTRUCTOR
50struct btree *bt_create(bt_cmp_t cmp, bts_t *s, char dirty);
51
52// CRUD CRUD CRUD CRUD CRUD CRUD CRUD CRUD CRUD CRUD CRUD CRUD CRUD CRUD CRUD
53typedef struct data_with_dirt_t {
54 bt_data_t k; // the data
55 uint32 dr; // dirty-right
56} dwd_t;
57bool bt_insert (struct btree *btr, bt_data_t k, uint32 dr);
58dwd_t bt_delete (struct btree *btr, bt_data_t k, bool leafd);
59
60// OPERATORS OPERATORS OPERATORS OPERATORS OPERATORS OPERATORS OPERATORS
61bt_data_t bt_max (struct btree *btr);
62bt_data_t bt_min (struct btree *btr);
63bt_data_t bt_find (struct btree *btr, bt_data_t k, ai_obj *akey);
64
65// DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY
66struct btreenode *addDStoBTN(struct btree *btr, struct btreenode *x,
67 struct btreenode *p, int pi, char dirty);
68
69uint32 getDR (struct btree *btr, struct btreenode *x, int i);
70bool bt_exist (struct btree *btr, bt_data_t k, ai_obj *akey);
71
72typedef struct data_with_miss_t {
73 bt_data_t k; // the data
74 bool miss;
75 struct btreenode *x; // NOTE: used for DELETE an EVICTed row
76 int i; // NOTE: used for DELETE an EVICTed row
77 struct btreenode *p; // NOTE: used for DELETE an EVICTed row
78 int pi; // NOTE: used for DELETE an EVICTed row
79} dwm_t;
80
81struct ai_obj;
82dwm_t findnodekey(struct btree *btr, struct btreenode *x, bt_data_t k, ai_obj *akey);
83
84// ITERATOR ITERATOR ITERATOR ITERATOR ITERATOR ITERATOR ITERATOR ITERATOR
85struct btIterator;
86int bt_init_iterator(struct btree *br, bt_data_t k, struct btIterator *iter, ai_obj *alow);
87void bt_destroy (struct btree *btr);
88