fix markup
[smlar.git] / smlar.h
1 #ifndef _SMLAR_H_
2 #define _SMLAR_H_
3
4 #include "postgres.h"
5 #include "utils/array.h"
6 #include "access/tupdesc.h"
7 #include "catalog/pg_collation.h"
8
9 typedef struct ProcTypeInfoData *ProcTypeInfo;
10
11 typedef struct ProcTypeInfoData {
12         Oid                             typid;
13         Oid                             hashFuncOid;
14         Oid                             cmpFuncOid;
15         int16                   typlen;
16         bool                    typbyval;
17         char                    typalign;
18
19         /* support of composite type */
20         char                    typtype;
21         TupleDesc               tupDesc;
22
23         /*
24          * Following member can become invalid,
25          * so fill it just before using
26          */
27         bool                    hashFuncInited;
28         FmgrInfo                hashFunc;
29         bool                    cmpFuncInited;
30         FmgrInfo                cmpFunc;
31 } ProcTypeInfoData;
32
33 ProcTypeInfo findProcs(Oid typid);
34 void getFmgrInfoHash(ProcTypeInfo info);
35 void getFmgrInfoCmp(ProcTypeInfo info);
36
37 #define NDIM 1
38 /* reject arrays we can't handle; but allow a NULL or empty array */
39 #define CHECKARRVALID(x) \
40         do { \
41                 if (x) { \
42                         if (ARR_NDIM(x) != NDIM && ARR_NDIM(x) != 0) \
43                                 ereport(ERROR, \
44                                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), \
45                                                  errmsg("array must be one-dimensional"))); \
46                         if (ARR_HASNULL(x)) \
47                                 ereport(ERROR, \
48                                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), \
49                                                  errmsg("array must not contain nulls"))); \
50                 } \
51         } while(0)
52
53 #define ARRISVOID(x)  ((x) == NULL || ARRNELEMS(x) == 0)
54 #define ARRNELEMS(x)  ArrayGetNItems(ARR_NDIM(x), ARR_DIMS(x))
55
56
57 typedef struct SimpleArray {
58         Datum              *elems;
59         double             *df;  /* frequency in current doc */
60         uint32             *hash;
61         int                             nelems;
62         ProcTypeInfo    info;
63 } SimpleArray;
64
65 SimpleArray     * Array2SimpleArray(ProcTypeInfo info, ArrayType *a);
66 SimpleArray     * Array2SimpleArrayS(ProcTypeInfo info, ArrayType *a);
67 SimpleArray     * Array2SimpleArrayU(ProcTypeInfo info, ArrayType *a, void *cache);
68 void allocateHash(void *cache, SimpleArray *a);
69
70 /*
71  * GUC vars
72  */
73 double GetSmlarLimit(void);
74 const char* GetSmlarTable(void);
75 bool GetSmlarUsePersistent(void);
76 double getOneAdd(void);
77 int getTFMethod(void);
78 int getSmlType(void); 
79 /*
80  * GiST
81  */
82
83 #define SmlarOverlapStrategy            1
84 #define SmlarSimilarityStrategy         2
85
86 struct SmlSign;
87 struct SmlSign* Array2HashedArray(ProcTypeInfo info, ArrayType *a);
88 /*
89  * Cache subsystem
90  */
91 void*   SearchArrayCache( void *cache, MemoryContext ctx, Datum a, ArrayType **da, SimpleArray **sa,  struct SmlSign  **ss );
92
93 typedef struct StatElem {
94         Datum           datum;
95         double          idf; /*  log(d/df) */
96 } StatElem;
97
98 typedef struct HashedElem {
99         uint32          hash;
100         double          idfMin;
101         double          idfMax;
102 } HashedElem;
103
104 typedef struct SignedElem {
105         double          idfMin;
106         double          idfMax;
107 } SignedElem;
108
109 typedef struct StatCache {
110         StatElem                *elems;
111         int                             nelems;
112         int64_t                 ndoc;
113         HashedElem              *helems;
114         int                             nhelems;
115         SignedElem              *selems;
116         ProcTypeInfo    info;
117 } StatCache;
118
119 StatCache *initStatCache(MemoryContext ctx);
120 void getHashStatCache(StatCache *stat, MemoryContext ctx, size_t n);
121
122 void    resetStatCache(void);
123 StatElem  *findStat(StatCache *stat, Datum query, StatElem *low);
124 StatElem  *fingArrayStat(void *cache, Oid typoid, Datum query, StatElem *low);
125 StatCache *getStat(void *cache, size_t n);
126
127 /*
128  * Formula's type of similarity
129  */
130 #define ST_COSINE       1
131 #define ST_TFIDF        2
132 #define ST_OVERLAP      3
133 /*
134  * TF methods
135  */
136 #define TF_N            1
137 #define TF_LOG          2
138 #define TF_CONST        3
139
140 #define FCall2(f, x1, x2)   FunctionCall2Coll((f), C_COLLATION_OID, (x1), (x2))
141
142 #endif