~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/btree_gist/btree_int4.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
 
 
4
typedef struct int32key
 
5
{
 
6
        int32           lower;
 
7
        int32           upper;
 
8
}       int32KEY;
 
9
 
 
10
/*
 
11
** int32 ops
 
12
*/
 
13
PG_FUNCTION_INFO_V1(gbt_int4_compress);
 
14
PG_FUNCTION_INFO_V1(gbt_int4_union);
 
15
PG_FUNCTION_INFO_V1(gbt_int4_picksplit);
 
16
PG_FUNCTION_INFO_V1(gbt_int4_consistent);
 
17
PG_FUNCTION_INFO_V1(gbt_int4_penalty);
 
18
PG_FUNCTION_INFO_V1(gbt_int4_same);
 
19
 
 
20
Datum           gbt_int4_compress(PG_FUNCTION_ARGS);
 
21
Datum           gbt_int4_union(PG_FUNCTION_ARGS);
 
22
Datum           gbt_int4_picksplit(PG_FUNCTION_ARGS);
 
23
Datum           gbt_int4_consistent(PG_FUNCTION_ARGS);
 
24
Datum           gbt_int4_penalty(PG_FUNCTION_ARGS);
 
25
Datum           gbt_int4_same(PG_FUNCTION_ARGS);
 
26
 
 
27
 
 
28
static bool
 
29
gbt_int4gt(const void *a, const void *b)
 
30
{
 
31
        return (*((int32 *) a) > *((int32 *) b));
 
32
}
 
33
static bool
 
34
gbt_int4ge(const void *a, const void *b)
 
35
{
 
36
        return (*((int32 *) a) >= *((int32 *) b));
 
37
}
 
38
static bool
 
39
gbt_int4eq(const void *a, const void *b)
 
40
{
 
41
        return (*((int32 *) a) == *((int32 *) b));
 
42
}
 
43
static bool
 
44
gbt_int4le(const void *a, const void *b)
 
45
{
 
46
        return (*((int32 *) a) <= *((int32 *) b));
 
47
}
 
48
static bool
 
49
gbt_int4lt(const void *a, const void *b)
 
50
{
 
51
        return (*((int32 *) a) < *((int32 *) b));
 
52
}
 
53
 
 
54
static int
 
55
gbt_int4key_cmp(const void *a, const void *b)
 
56
{
 
57
 
 
58
        if (*(int32 *) &(((Nsrt *) a)->t[0]) > *(int32 *) &(((Nsrt *) b)->t[0]))
 
59
                return 1;
 
60
        else if (*(int32 *) &(((Nsrt *) a)->t[0]) < *(int32 *) &(((Nsrt *) b)->t[0]))
 
61
                return -1;
 
62
        return 0;
 
63
 
 
64
}
 
65
 
 
66
 
 
67
static const gbtree_ninfo tinfo =
 
68
{
 
69
        gbt_t_int4,
 
70
        sizeof(int32),
 
71
        gbt_int4gt,
 
72
        gbt_int4ge,
 
73
        gbt_int4eq,
 
74
        gbt_int4le,
 
75
        gbt_int4lt,
 
76
        gbt_int4key_cmp
 
77
};
 
78
 
 
79
 
 
80
/**************************************************
 
81
 * int32 ops
 
82
 **************************************************/
 
83
 
 
84
 
 
85
Datum
 
86
gbt_int4_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_int4_consistent(PG_FUNCTION_ARGS)
 
97
{
 
98
 
 
99
        GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
 
100
        int32           query = PG_GETARG_INT32(1);
 
101
        int32KEY   *kkk = (int32KEY *) DatumGetPointer(entry->key);
 
102
        GBT_NUMKEY_R key;
 
103
        StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
 
104
 
 
105
        key.lower = (GBT_NUMKEY *) & kkk->lower;
 
106
        key.upper = (GBT_NUMKEY *) & kkk->upper;
 
107
 
 
108
        PG_RETURN_BOOL(
 
109
                                   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
 
110
                );
 
111
}
 
112
 
 
113
 
 
114
Datum
 
115
gbt_int4_union(PG_FUNCTION_ARGS)
 
116
{
 
117
        GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
 
118
        void       *out = palloc(sizeof(int32KEY));
 
119
 
 
120
        *(int *) PG_GETARG_POINTER(1) = sizeof(int32KEY);
 
121
        PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
 
122
}
 
123
 
 
124
 
 
125
Datum
 
126
gbt_int4_penalty(PG_FUNCTION_ARGS)
 
127
{
 
128
        int32KEY   *origentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
 
129
        int32KEY   *newentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
 
130
        float      *result = (float *) PG_GETARG_POINTER(2);
 
131
        int4            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
Datum
 
148
gbt_int4_picksplit(PG_FUNCTION_ARGS)
 
149
{
 
150
        PG_RETURN_POINTER(gbt_num_picksplit(
 
151
                                                                (GistEntryVector *) PG_GETARG_POINTER(0),
 
152
                                                                  (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
 
153
                                                                                &tinfo
 
154
                                                                                ));
 
155
}
 
156
 
 
157
Datum
 
158
gbt_int4_same(PG_FUNCTION_ARGS)
 
159
{
 
160
        int32KEY   *b1 = (int32KEY *) PG_GETARG_POINTER(0);
 
161
        int32KEY   *b2 = (int32KEY *) PG_GETARG_POINTER(1);
 
162
        bool       *result = (bool *) PG_GETARG_POINTER(2);
 
163
 
 
164
        *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
 
165
        PG_RETURN_POINTER(result);
 
166
}