~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/btree_gist/btree_cash.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "btree_gist.h"
 
2
#include "btree_utils_num.h"
 
3
#include "utils/cash.h"
 
4
 
 
5
typedef struct
 
6
{
 
7
        Cash            lower;
 
8
        Cash            upper;
 
9
}       cashKEY;
 
10
 
 
11
/*
 
12
** Cash ops
 
13
*/
 
14
PG_FUNCTION_INFO_V1(gbt_cash_compress);
 
15
PG_FUNCTION_INFO_V1(gbt_cash_union);
 
16
PG_FUNCTION_INFO_V1(gbt_cash_picksplit);
 
17
PG_FUNCTION_INFO_V1(gbt_cash_consistent);
 
18
PG_FUNCTION_INFO_V1(gbt_cash_penalty);
 
19
PG_FUNCTION_INFO_V1(gbt_cash_same);
 
20
 
 
21
Datum           gbt_cash_compress(PG_FUNCTION_ARGS);
 
22
Datum           gbt_cash_union(PG_FUNCTION_ARGS);
 
23
Datum           gbt_cash_picksplit(PG_FUNCTION_ARGS);
 
24
Datum           gbt_cash_consistent(PG_FUNCTION_ARGS);
 
25
Datum           gbt_cash_penalty(PG_FUNCTION_ARGS);
 
26
Datum           gbt_cash_same(PG_FUNCTION_ARGS);
 
27
 
 
28
static bool
 
29
gbt_cashgt(const void *a, const void *b)
 
30
{
 
31
        return (*((Cash *) a) > *((Cash *) b));
 
32
}
 
33
static bool
 
34
gbt_cashge(const void *a, const void *b)
 
35
{
 
36
        return (*((Cash *) a) >= *((Cash *) b));
 
37
}
 
38
static bool
 
39
gbt_casheq(const void *a, const void *b)
 
40
{
 
41
        return (*((Cash *) a) == *((Cash *) b));
 
42
}
 
43
static bool
 
44
gbt_cashle(const void *a, const void *b)
 
45
{
 
46
        return (*((Cash *) a) <= *((Cash *) b));
 
47
}
 
48
static bool
 
49
gbt_cashlt(const void *a, const void *b)
 
50
{
 
51
        return (*((Cash *) a) < *((Cash *) b));
 
52
}
 
53
 
 
54
static int
 
55
gbt_cashkey_cmp(const void *a, const void *b)
 
56
{
 
57
 
 
58
        if (*(Cash *) &(((Nsrt *) a)->t[0]) > *(Cash *) &(((Nsrt *) b)->t[0]))
 
59
                return 1;
 
60
        else if (*(Cash *) &(((Nsrt *) a)->t[0]) < *(Cash *) &(((Nsrt *) b)->t[0]))
 
61
                return -1;
 
62
        return 0;
 
63
 
 
64
}
 
65
 
 
66
 
 
67
static const gbtree_ninfo tinfo =
 
68
{
 
69
        gbt_t_cash,
 
70
        sizeof(Cash),
 
71
        gbt_cashgt,
 
72
        gbt_cashge,
 
73
        gbt_casheq,
 
74
        gbt_cashle,
 
75
        gbt_cashlt,
 
76
        gbt_cashkey_cmp
 
77
};
 
78
 
 
79
 
 
80
/**************************************************
 
81
 * Cash ops
 
82
 **************************************************/
 
83
 
 
84
 
 
85
Datum
 
86
gbt_cash_compress(PG_FUNCTION_ARGS)
 
87
{
 
88
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
89
        GISTENTRY  *retval = NULL;
 
90
 
 
91
        PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
 
92
}
 
93
 
 
94
 
 
95
Datum
 
96
gbt_cash_consistent(PG_FUNCTION_ARGS)
 
97
{
 
98
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
99
        Cash            query = (*((Cash *) PG_GETARG_POINTER(1)));
 
100
        cashKEY    *kkk = (cashKEY *) DatumGetPointer(entry->key);
 
101
        GBT_NUMKEY_R key;
 
102
        StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
 
103
 
 
104
        key.lower = (GBT_NUMKEY *) & kkk->lower;
 
105
        key.upper = (GBT_NUMKEY *) & kkk->upper;
 
106
 
 
107
        PG_RETURN_BOOL(
 
108
                                   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
 
109
                );
 
110
}
 
111
 
 
112
 
 
113
Datum
 
114
gbt_cash_union(PG_FUNCTION_ARGS)
 
115
{
 
116
        GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
 
117
        void       *out = palloc(sizeof(cashKEY));
 
118
 
 
119
        *(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
 
120
        PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
 
121
}
 
122
 
 
123
 
 
124
Datum
 
125
gbt_cash_penalty(PG_FUNCTION_ARGS)
 
126
{
 
127
        cashKEY    *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
 
128
        cashKEY    *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
 
129
        float      *result = (float *) PG_GETARG_POINTER(2);
 
130
 
 
131
        Cash            res;
 
132
 
 
133
        *result = 0.0;
 
134
 
 
135
        penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
 
136
 
 
137
        if (res > 0)
 
138
        {
 
139
                *result += FLT_MIN;
 
140
                *result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
 
141
                *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
 
142
        }
 
143
 
 
144
        PG_RETURN_POINTER(result);
 
145
 
 
146
}
 
147
 
 
148
Datum
 
149
gbt_cash_picksplit(PG_FUNCTION_ARGS)
 
150
{
 
151
        PG_RETURN_POINTER(gbt_num_picksplit(
 
152
                                                                (GistEntryVector *) PG_GETARG_POINTER(0),
 
153
                                                                  (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
 
154
                                                                                &tinfo
 
155
                                                                                ));
 
156
}
 
157
 
 
158
Datum
 
159
gbt_cash_same(PG_FUNCTION_ARGS)
 
160
{
 
161
        cashKEY    *b1 = (cashKEY *) PG_GETARG_POINTER(0);
 
162
        cashKEY    *b2 = (cashKEY *) PG_GETARG_POINTER(1);
 
163
        bool       *result = (bool *) PG_GETARG_POINTER(2);
 
164
 
 
165
        *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
 
166
        PG_RETURN_POINTER(result);
 
167
}