File: std_pg_hash.c

package info (click to toggle)
postgis 2.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 75,792 kB
  • sloc: ansic: 139,314; sql: 136,281; xml: 48,954; sh: 4,906; perl: 4,509; makefile: 2,897; python: 1,198; yacc: 441; cpp: 305; lex: 132
file content (915 lines) | stat: -rw-r--r-- 25,060 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915


/* PostgreSQL headers */
#include "postgres.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "utils/memutils.h"
#include "executor/spi.h"
#include "access/hash.h"
#include "utils/hsearch.h"
#include "funcapi.h"
#include "catalog/pg_type.h"

/* standardizer headers */
#undef DEBUG
//#define DEBUG 1
#include "../../postgis_config.h"
#include "pagc_api.h"
#include "pagc_std_api.h"
#include "std_pg_hash.h"

/* C headers */
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#ifdef DEBUG
#define SET_TIME(a) gettimeofday(&(a), NULL)
#define ELAPSED_T(a,b) \
    elapsed = (b.tv_sec - a.tv_sec)*1000.0; \
    elapsed += (b.tv_usec - a.tv_usec)/1000.0;
#else
#define SET_TIME(a) do { ; } while (0)
#define ELAPSED_T(a,b) do { ; } while (0)
#endif

#define MAX_RULE_LENGTH 128
#define TUPLIMIT 1000

#define STD_CACHE_ITEMS 4
#define STD_BACKEND_HASH_SIZE 16

static HTAB* StdHash = NULL;


typedef struct
{
    char *lextab;
    char *gaztab;
    char *rultab;
    STANDARDIZER *std;
    MemoryContext std_mcxt;
}
StdCacheItem;

typedef struct
{
    StdCacheItem StdCache[STD_CACHE_ITEMS];
    int NextSlot;
    MemoryContext StdCacheContext;
}
StdPortalCache;

typedef struct
{
    MemoryContext context;
    STANDARDIZER *std;
}
StdHashEntry;

typedef struct lex_columns
{
    int seq;
    int word;
    int stdword;
    int token;
} lex_columns_t;

typedef struct rules_columns
{
    int rule;
} rules_columns_t;



/* Memory context hash table function prototypes */
uint32 mcxt_ptr_hash_std(const void *key, Size keysize);
static void CreateStdHash(void);
static void AddStdHashEntry(MemoryContext mcxt, STANDARDIZER *std);
static StdHashEntry *GetStdHashEntry(MemoryContext mcxt);
static void DeleteStdHashEntry(MemoryContext mcxt);


static bool IsInStdPortalCache(StdPortalCache *STDCache,  char *lextab, char *gaztab, char *rultab);
static STANDARDIZER *GetStdFromPortalCache(StdPortalCache *STDCache,  char *lextab, char *gaztab, char *rultab);
static void AddToStdPortalCache(StdPortalCache *STDCache, char *lextab, char *gaztab, char *rultab);
static StdPortalCache *GetStdPortalCache(FunctionCallInfo fcinfo);


/* standardizer api functions */

static STANDARDIZER *CreateStd(char *lextab, char *gaztab, char *rultab);
static int parse_rule(char *buf, int *rule);
static int fetch_lex_columns(SPITupleTable *tuptable, lex_columns_t *lex_cols);
static int tableNameOk(char *t);
static int load_lex(LEXICON *lex, char *tabname);
static int fetch_rules_columns(SPITupleTable *tuptable, rules_columns_t *rules_cols);
static int load_rules(RULES *rules, char *tabname);





static void
#if POSTGIS_PGSQL_VERSION < 96

StdCacheDelete(MemoryContext context)
{
#else
StdCacheDelete(void *ptr)
{
	MemoryContext context = (MemoryContext)ptr;
#endif
    StdHashEntry *she;

    DBG("Enter: StdCacheDelete");
    /* lookup the hash entry in the global hash table
       so we can free it */
    she = GetStdHashEntry(context);

    if (!she)
        elog(ERROR, "StdCacheDelete: Trying to delete non-existant hash entry object with MemoryContext key (%p)", (void *)context);

    DBG("deleting std object (%p) with MemoryContext key (%p)", she->std, context);

    if (she->std)
        std_free(she->std);

    DeleteStdHashEntry(context);
}

#if POSTGIS_PGSQL_VERSION < 96

static void
StdCacheInit(MemoryContext context)
{
    /* NOP - initialized when first used. */
}


static void
StdCacheReset(MemoryContext context)
{
    // NOP - Seems to be a required function
}

static bool
StdCacheIsEmpty(MemoryContext context)
{
    // always return false - another required function
    return FALSE;
}

static void
StdCacheStats(MemoryContext context, int level)
{
    // another required function
    fprintf(stderr, "%s: STANDARDIZER context\n", context->name);
}

#ifdef MEMORY_CONTEXT_CHECKING
static void
StdCacheCheck(MemoryContext context)
{
    // NOP - another required function
}
#endif

/* Memory context definition must match the current version of PostgreSQL */
static MemoryContextMethods StdCacheContextMethods =
{
    NULL,
    NULL,
    NULL,
    StdCacheInit,
    StdCacheReset,
    StdCacheDelete,
    NULL,
    StdCacheIsEmpty,
    StdCacheStats
#ifdef MEMORY_CONTEXT_CHECKING
    , StdCacheCheck
#endif
};

#endif /* POSTGIS_PGSQL_VERSION < 96 */


uint32
mcxt_ptr_hash_std(const void *key, Size keysize)
{
    uint32 hashval;
    hashval = DatumGetUInt32(hash_any(key, keysize));
    return hashval;
}


static void
CreateStdHash(void)
{
    HASHCTL ctl;

    ctl.keysize = sizeof(MemoryContext);
    ctl.entrysize = sizeof(StdHashEntry);
    ctl.hash = mcxt_ptr_hash_std;

    StdHash = hash_create("PAGC Address Standardizer Backend MemoryContext Hash", STD_BACKEND_HASH_SIZE, &ctl, (HASH_ELEM | HASH_FUNCTION));
    DBG("CreateStdHash: created StdHash (%p)", StdHash);
}


static void
AddStdHashEntry(MemoryContext mcxt, STANDARDIZER *std)
{
    bool found;
    void **key;
    StdHashEntry *he;

    DBG("Enter: AddStdHashEntry(mcxt=%p, std=%p)", mcxt, std);
    /* The hash key is the MemoryContext pointer */
    key = (void *)&mcxt;

    he = (StdHashEntry *) hash_search(StdHash, key, HASH_ENTER, &found);
    DBG("AddStdHashEntry: he=%p, found=%d", he, found);
    if (!found) {
        DBG("&he->context=%p", &he->context);
        he->context = mcxt;
        DBG("&he->std=%p", &he->std);
        he->std = std;
        DBG("Leaving AddStdHashEntry");
    }
    else {
        elog(ERROR, "AddStdHashEntry: This memory context is already in use! (%p)", (void *)mcxt);
    }
}

static StdHashEntry *
GetStdHashEntry(MemoryContext mcxt)
{
    void **key;
    StdHashEntry *he;

    DBG("Enter: GetStdHashEntry");
    key = (void *)&mcxt;
    he = (StdHashEntry *) hash_search(StdHash, key, HASH_FIND, NULL);
    return he;
}


static void
DeleteStdHashEntry(MemoryContext mcxt)
{
    void **key;
    StdHashEntry *he;

    DBG("Enter: DeleteStdHashEntry");
    key = (void *)&mcxt;
    he = (StdHashEntry *) hash_search(StdHash, key, HASH_REMOVE, NULL);
    if (!he)
        elog(ERROR, "DeleteStdHashEntry: There was an error removing the STD object from this MemoryContext (%p)", (void *)mcxt);

    he->std = NULL;
}


/* public api */
bool
IsInStdCache(StdCache STDCache, char *lextab, char *gaztab, char *rultab) {
    return IsInStdPortalCache((StdPortalCache *) STDCache, lextab, gaztab, rultab);
}


static bool
IsInStdPortalCache(StdPortalCache *STDCache,  char *lextab, char *gaztab, char *rultab)
{
    int i;

    DBG("Enter: IsInStdPortalCache");
    for (i=0; i<STD_CACHE_ITEMS; i++) {
        StdCacheItem *ci = &STDCache->StdCache[i];
        if (ci->lextab && !strcmp(ci->lextab, lextab) &&
            ci->lextab && !strcmp(ci->gaztab, gaztab) &&
            ci->lextab && !strcmp(ci->rultab, rultab))
                return TRUE;
    }

    return FALSE;
}


/* public api */
STANDARDIZER *
GetStdFromStdCache(StdCache STDCache,  char *lextab, char *gaztab, char *rultab) {
    return GetStdFromPortalCache((StdPortalCache *) STDCache, lextab, gaztab, rultab);
}


static STANDARDIZER *
GetStdFromPortalCache(StdPortalCache *STDCache,  char *lextab, char *gaztab, char *rultab)
{
    int i;

    DBG("Enter: GetStdFromPortalCache");
    for (i=0; i<STD_CACHE_ITEMS; i++) {
        StdCacheItem *ci = &STDCache->StdCache[i];
        if (ci->lextab && !strcmp(ci->lextab, lextab) &&
            ci->lextab && !strcmp(ci->gaztab, gaztab) &&
            ci->lextab && !strcmp(ci->rultab, rultab))
                return STDCache->StdCache[i].std;
    }

    return NULL;
}


static void
DeleteNextSlotFromStdCache(StdPortalCache *STDCache)
{
    MemoryContext old_context;

    DBG("Enter: DeleteNextSlotFromStdCache");
    if (STDCache->StdCache[STDCache->NextSlot].std != NULL) {
        StdCacheItem *ce = &STDCache->StdCache[STDCache->NextSlot];
        DBG("Removing STD cache entry ('%s', '%s', '%s') index %d", ce->lextab, ce->gaztab, ce->rultab, STDCache->NextSlot);

        /* zero out the entries and free the memory context
           We will get a callback to free the std object.
        */
        old_context = MemoryContextSwitchTo(STDCache->StdCacheContext);
        MemoryContextDelete(ce->std_mcxt);
        pfree(ce->lextab);
        ce->lextab = NULL;
        pfree(ce->gaztab);
        ce->gaztab = NULL;
        pfree(ce->rultab);
        ce->rultab = NULL;
        ce->std    = NULL;
        MemoryContextSwitchTo(old_context);
    }
}


/* public api */
void
AddToStdCache(StdCache cache, char *lextab, char *gaztab, char *rultab) {
    AddToStdPortalCache((StdPortalCache *) cache, lextab, gaztab, rultab);
}


static void
AddToStdPortalCache(StdPortalCache *STDCache, char *lextab, char *gaztab, char *rultab)
{
    MemoryContext STDMemoryContext;
    MemoryContext old_context;
    STANDARDIZER *std = NULL;
#if POSTGIS_PGSQL_VERSION >= 96
		MemoryContextCallback *callback;
#endif

    DBG("Enter: AddToStdPortalCache");
    std = CreateStd(lextab, gaztab, rultab);
    if (!std)
        elog(ERROR,
            "AddToStdPortalCache: could not create address standardizer for '%s', '%s', '%s'", lextab, gaztab, rultab);

    /* if the NextSlot in the cache is used, then delete it */
    if (STDCache->StdCache[STDCache->NextSlot].std != NULL) {
#ifdef DEBUG
        StdCacheItem *ce = &STDCache->StdCache[STDCache->NextSlot];
        DBG("Removing item from STD cache ('%s', '%s', '%s') index %d", ce->lextab, ce->gaztab, ce->rultab, STDCache->NextSlot);
#endif
        DeleteNextSlotFromStdCache(STDCache);
    }

    DBG("Adding item to STD cache ('%s', '%s', '%s') index %d", lextab, gaztab, rultab, STDCache->NextSlot);


#if POSTGIS_PGSQL_VERSION < 96
    STDMemoryContext = MemoryContextCreate(T_AllocSetContext, 8192,
                                           &StdCacheContextMethods,
                                           STDCache->StdCacheContext,
                                           "PAGC STD Memory Context");
#else
	STDMemoryContext =  AllocSetContextCreate(STDCache->StdCacheContext,
	                                          "PAGC STD Memory Context",
	                                          ALLOCSET_SMALL_SIZES);

	/* PgSQL comments suggest allocating callback in the context */
	/* being managed, so that the callback object gets cleaned along with */
	/* the context */
	callback = MemoryContextAlloc(STDMemoryContext, sizeof(MemoryContextCallback));
	callback->arg = (void*)(STDMemoryContext);
	callback->func = StdCacheDelete;
	MemoryContextRegisterResetCallback(STDMemoryContext, callback);
#endif



    /* Create the backend hash if it doesn't already exist */
    DBG("Check if StdHash exists (%p)", StdHash);
    if (!StdHash)
        CreateStdHash();

    /*
     * Add the MemoryContext to the backend hash so we can
     * clean up upon portal shutdown
     */
    DBG("Adding standardizer obj (%p) to hash table with MemoryContext key (%p)", std, STDMemoryContext);

    AddStdHashEntry(STDMemoryContext, std);

    /* change memory contexts so the pstrdup are allocated in the
     * context of this cache item. They will be freed when the
     * cache item is deleted.
    */
    DBG("AddToStdPortalCache: changing memory context to %p", STDCache->StdCacheContext);
    old_context = MemoryContextSwitchTo(STDCache->StdCacheContext);
    DBG("  old_context= %p", old_context);
    STDCache->StdCache[STDCache->NextSlot].lextab = pstrdup(lextab);
    DBG("  pstrdup(lextab) completed");
    STDCache->StdCache[STDCache->NextSlot].gaztab = pstrdup(gaztab);
    DBG("  pstrdup(gaztab) completed");
    STDCache->StdCache[STDCache->NextSlot].rultab = pstrdup(rultab);
    DBG("  pstrdup(rultab) completed");
    MemoryContextSwitchTo(old_context);
    DBG(" changed memory context to %p", old_context);

    STDCache->StdCache[STDCache->NextSlot].std = std;
    STDCache->StdCache[STDCache->NextSlot].std_mcxt = STDMemoryContext;
    STDCache->NextSlot = (STDCache->NextSlot + 1) % STD_CACHE_ITEMS;
    DBG("STDCache->NextSlot=%d", STDCache->NextSlot);
}


/* pubilc api */
StdCache
GetStdCache(FunctionCallInfo fcinfo) {
    return (StdCache) GetStdPortalCache(fcinfo);
}


static StdPortalCache *
GetStdPortalCache(FunctionCallInfo fcinfo)
{
    StdPortalCache *STDCache;

    DBG("Enter: GetStdPortalCache");
    /* create it if we don't already have one for this portal */
    if (fcinfo->flinfo->fn_extra == NULL) {
        MemoryContext old_context;

        old_context = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
        STDCache = palloc(sizeof(StdPortalCache));
        MemoryContextSwitchTo(old_context);

        if (STDCache) {
            int i;

            DBG("Allocating STDCache for portal with STD MemoryContext (%p)", fcinfo->flinfo->fn_mcxt);
            /* initial the cache items */
            for (i=0; i<STD_CACHE_ITEMS; i++) {
                STDCache->StdCache[i].lextab   = NULL;
                STDCache->StdCache[i].gaztab   = NULL;
                STDCache->StdCache[i].rultab   = NULL;
                STDCache->StdCache[i].std      = NULL;
                STDCache->StdCache[i].std_mcxt = NULL;
            }
            STDCache->NextSlot = 0;
            STDCache->StdCacheContext = fcinfo->flinfo->fn_mcxt;

            /* Store the pointer in fcinfo->flinfo->fn_extra */
            fcinfo->flinfo->fn_extra = STDCache;
        }
    }
    else {
        /* Use the existing cache */
        STDCache = fcinfo->flinfo->fn_extra;
    }

    return STDCache;
}

/* public api */
STANDARDIZER *
GetStdUsingFCInfo(FunctionCallInfo fcinfo, char *lextab, char *gaztab, char *rultab)
{
    STANDARDIZER *std;
    StdCache *std_cache = NULL;

    DBG("GetStdUsingFCInfo: calling GetStdCache(fcinfo)");
    std_cache = GetStdCache(fcinfo);
    if (!std_cache)
        return NULL;

    DBG("GetStdUsingFCInfo: calling IsInStdCache(std_cache, lextab, gaztab, rultab)");
    if (!IsInStdCache(std_cache, lextab, gaztab, rultab)) {
        DBG("GetStdUsingFCInfo: calling AddToStdCache(std_cache, lextab, gaztab, rultab)");
        AddToStdCache(std_cache, lextab, gaztab, rultab);
    }

    DBG("GetStdUsingFCInfo: GetStdFromStdCache(std_cache, lextab, gaztab, rultab)");
    std = GetStdFromStdCache(std_cache, lextab, gaztab, rultab);

    return std;
}


static STANDARDIZER *
CreateStd(char *lextab, char *gaztab, char *rultab)
{
    STANDARDIZER        *std;
    LEXICON             *lex;
    LEXICON             *gaz;
    RULES               *rules;
    int                  err;
    int                  SPIcode;

    DBG("Enter: CreateStd");
    SPIcode = SPI_connect();
    if (SPIcode != SPI_OK_CONNECT) {
        elog(ERROR, "CreateStd: couldn't open a connection to SPI");
    }

    std = std_init();
    if (!std)
        elog(ERROR, "CreateStd: could not allocate memory (std)");

    lex = lex_init(std->err_p);
    if (!lex) {
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: could not allocate memory (lex)");
    }

    err = load_lex(lex, lextab);
    if (err == -1) {
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: failed to load '%s' for lexicon", lextab);
    }

    gaz = lex_init(std->err_p);
    if (!gaz) {
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: could not allocate memory (gaz)");
    }

    err = load_lex(gaz, gaztab);
    if (err == -1) {
        lex_free(gaz);
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: failed to load '%s' for gazeteer", gaztab);
    }

    rules = rules_init(std->err_p);
    if (!rules) {
        lex_free(gaz);
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: could not allocate memory (rules)");
    }

    err = load_rules(rules, rultab);
    if (err == -1) {
        rules_free(rules);
        lex_free(gaz);
        lex_free(lex);
        std_free(std);
        SPI_finish();
        elog(ERROR, "CreateStd: failed to load '%s' for rules", rultab);
    }

    std_use_lex(std, lex);
    std_use_gaz(std, gaz);
    std_use_rules(std, rules);
    std_ready_standardizer(std);

    SPI_finish();

    return std;
}


static int parse_rule(char *buf, int *rule)
{
    int nr = 0;
    int *r = rule;
    char *p = buf;
    char *q;


    while (1) {
        *r = strtol( p, &q, 10 );
        if (p == q) break;
        p = q;
        nr++;
        r++;
        if (nr > MAX_RULE_LENGTH) return -1;
    }

    return nr;
}


#define FETCH_COL(TRGT,NAME,NAME2) \
    TRGT->NAME = SPI_fnumber(SPI_tuptable->tupdesc,NAME2);\
    if (TRGT->NAME == SPI_ERROR_NOATTRIBUTE) err++;

#define CHECK_TYP(TRGT,NAME,TYPE) \
    if (SPI_gettypeid(SPI_tuptable->tupdesc, TRGT->NAME) != TYPE) {\
        DBG("CHECK_TYP: expecting %d, got: %d", TYPE, SPI_gettypeid(SPI_tuptable->tupdesc, TRGT->NAME));\
        err++;\
    }

#define GET_INT_FROM_TUPLE(TRGT,WHICH,NULLMSG) \
    binval = SPI_getbinval(tuple, tupdesc, WHICH, &isnull);\
    if (isnull) { \
        elog(NOTICE, NULLMSG); \
        return -1; \
    } \
    TRGT = DatumGetInt32(binval);

#define GET_TEXT_FROM_TUPLE(TRGT,WHICH) \
    TRGT = DatumGetCString(SPI_getvalue(tuple, tupdesc, WHICH));


static int fetch_lex_columns(SPITupleTable *tuptable, lex_columns_t *lex_cols)
{
    int err = 0;
    FETCH_COL(lex_cols,seq,"seq");
    FETCH_COL(lex_cols,word,"word");
    FETCH_COL(lex_cols,stdword,"stdword");
    FETCH_COL(lex_cols,token,"token");
    if (err) {
        elog(NOTICE, "lexicon queries must return columns 'seq', 'word', 'stdword' and 'token'");
        return -1;
    }
    CHECK_TYP(lex_cols,seq,INT4OID);
    CHECK_TYP(lex_cols,word,TEXTOID);
    CHECK_TYP(lex_cols,stdword,TEXTOID);
    CHECK_TYP(lex_cols,token,INT4OID);
    if (err) {
        elog(NOTICE, "lexicon column types must be: 'seq' int4, 'word' text, 'stdword' text, and 'token' int4");
        return -1;
    }
    return 0;
}

/* snitize table names, leave '.' for schema */

static int tableNameOk(char *t)
{
    while (*t != '\0') {
        if (!(isalnum(*t) || *t == '_' || *t == '.' || *t == '"'))
            return 0;
        t++;
    }
    return 1;
}

static int load_lex(LEXICON *lex, char *tab)
{
    int ret;
    SPIPlanPtr SPIplan;
    Portal SPIportal;
    bool moredata = TRUE;
#ifdef DEBUG
    struct timeval t1, t2;
    double elapsed;
#endif
    char *sql;

    int ntuples;
    int total_tuples = 0;

    lex_columns_t lex_columns = {.seq = -1, .word = -1, .stdword = -1, .token = -1};

    int seq;
    char *word;
    char *stdword;
    int token;

    DBG("start load_lex\n");
    SET_TIME(t1);

    if (!tab || !strlen(tab)) {
        elog(NOTICE, "load_lex: rules table is not usable");
        return -1;
    }
    if (!tableNameOk(tab)) {
        elog(NOTICE, "load_lex: lex and gaz table names may only be alphanum and '.\"_' characters (%s)", tab);
        return -1;
    }
    sql = SPI_palloc(strlen(tab)+65);
    strcpy(sql, "select seq, word, stdword, token from ");
    strcat(sql, tab);
    strcat(sql, " order by id ");

    /* get the sql for the lexicon records and prepare the query */
    SPIplan = SPI_prepare(sql, 0, NULL);
    if (SPIplan == NULL) {
        elog(NOTICE, "load_lex: couldn't create query plan for the lex data via SPI (%s)", sql);
        return -1;
    }

    /* get the sql for the lexicon records and prepare the query */
    SPIplan = SPI_prepare(sql, 0, NULL);
    if (SPIplan == NULL) {
        elog(NOTICE, "load_lex: couldn't create query plan for the lexicon data via SPI");
        return -1;
    }

    if ((SPIportal = SPI_cursor_open(NULL, SPIplan, NULL, NULL, true)) == NULL) {
        elog(NOTICE, "load_lex: SPI_cursor_open('%s') returns NULL", sql);
        return -1;
    }

    while (moredata == TRUE) {
        //DBG("calling SPI_cursor_fetch");
        SPI_cursor_fetch(SPIportal, TRUE, TUPLIMIT);

        if (SPI_tuptable == NULL) {
            elog(NOTICE, "load_lex: SPI_tuptable is NULL");
            return -1;
        }

        if (lex_columns.seq == -1) {
            ret = fetch_lex_columns(SPI_tuptable, &lex_columns);
            if (ret)
                return ret;
        }

        ntuples = SPI_processed;
        //DBG("Reading edges: %i - %i", total_tuples, total_tuples+ntuples);
        total_tuples += ntuples;

        if (ntuples > 0) {
            int t;
            Datum binval;
            bool isnull;
            SPITupleTable *tuptable = SPI_tuptable;
            TupleDesc tupdesc = SPI_tuptable->tupdesc;

            for (t = 0; t < ntuples; t++) {
                //if (t%100 == 0) { DBG("    t: %i", t); }
                HeapTuple tuple = tuptable->vals[t];
                GET_INT_FROM_TUPLE(seq,lex_columns.seq,"load_lex: seq contains a null value");
                GET_TEXT_FROM_TUPLE(word,lex_columns.word);
                GET_TEXT_FROM_TUPLE(stdword,lex_columns.stdword);
                GET_INT_FROM_TUPLE(token,lex_columns.token,"load_lex: token contains a null value");
                lex_add_entry(lex, seq, word, stdword, token);
            }
            //DBG("calling SPI_freetuptable");
            SPI_freetuptable(tuptable);
            //DBG("back from SPI_freetuptable");
        }
        else
            moredata = FALSE;

    }

    SET_TIME(t2);
    ELAPSED_T(t1, t2);
    DBG("Time to read %i lexicon records: %.1f ms.", total_tuples, elapsed);

    return 0;
}

static int fetch_rules_columns(SPITupleTable *tuptable, rules_columns_t *rules_cols)
{
    int err = 0;
    FETCH_COL(rules_cols,rule,"rule");
    if (err) {
        elog(NOTICE, "rules queries must return column 'rule'");
        return -1;
    }
    CHECK_TYP(rules_cols,rule,TEXTOID);
    if (err) {
        elog(NOTICE, "rules column type must be: 'rule' text");
        return -1;
    }
    return 0;
}

static int load_rules(RULES *rules, char *tab)
{
    int ret;
    SPIPlanPtr SPIplan;
    Portal SPIportal;
    bool moredata = TRUE;
#ifdef DEBUG
    struct timeval t1, t2;
    double elapsed;
#endif
    char *sql;

    int rule_arr[MAX_RULE_LENGTH];

    int ntuples;
    int total_tuples = 0;

    rules_columns_t rules_columns = {.rule = -1};

    char *rule;

    DBG("start load_rules\n");
    SET_TIME(t1);

    if (!tab || !strlen(tab)) {
        elog(NOTICE, "load_rules: rules table is not usable");
        return -1;
    }
    if (!tableNameOk(tab)) {
        elog(NOTICE, "load_rules: rules table name may only be alphanum and '.\"_' characters (%s)", tab);
        return -1;
    }
    sql = SPI_palloc(strlen(tab)+35);
    strcpy(sql, "select rule from ");
    strcat(sql, tab);
    strcat(sql, " order by id ");

    /* get the sql for the lexicon records and prepare the query */
    SPIplan = SPI_prepare(sql, 0, NULL);
    if (SPIplan == NULL) {
        elog(NOTICE, "load_rules: couldn't create query plan for the rule data via SPI (%s)", sql);
        return -1;
    }

    if ((SPIportal = SPI_cursor_open(NULL, SPIplan, NULL, NULL, true)) == NULL) {
        elog(NOTICE, "load_rules: SPI_cursor_open('%s') returns NULL", sql);
        return -1;
    }

    while (moredata == TRUE) {
        //DBG("calling SPI_cursor_fetch");
        SPI_cursor_fetch(SPIportal, TRUE, TUPLIMIT);

        if (SPI_tuptable == NULL) {
            elog(NOTICE, "load_rules: SPI_tuptable is NULL");
            return -1;
        }

        if (rules_columns.rule == -1) {
            ret = fetch_rules_columns(SPI_tuptable, &rules_columns);
            if (ret)
                return ret;
        }

        ntuples = SPI_processed;
        //DBG("Reading edges: %i - %i", total_tuples, total_tuples+ntuples);

        if (ntuples > 0) {
            int t;
            SPITupleTable *tuptable = SPI_tuptable;
            TupleDesc tupdesc = SPI_tuptable->tupdesc;

            for (t = 0; t < ntuples; t++) {
                int nr;
                //if (t%100 == 0) { DBG("    t: %i", t); }
                HeapTuple tuple = tuptable->vals[t];
                GET_TEXT_FROM_TUPLE(rule,rules_columns.rule);
                nr = parse_rule(rule, rule_arr);
                if (nr == -1) {
                    elog(NOTICE, "load_roles: rule exceeds 128 terms");
                    return -1;
                }
                ret = rules_add_rule(rules, nr, rule_arr);
                if (ret != 0) {
                    elog(NOTICE,"load_roles: failed to add rule %d (%d): %s",
                         total_tuples+t+1, ret, rule);
                    return -1;
                }
            }
            //DBG("calling SPI_freetuptable");
            SPI_freetuptable(tuptable);
            //DBG("back from SPI_freetuptable");
        }
        else
            moredata = FALSE;

        total_tuples += ntuples;
    }

    ret = rules_ready(rules);
    if (ret != 0) {
        elog(NOTICE, "load_roles: failed to ready the rules: err: %d", ret);
        return -1;
    }


    SET_TIME(t2);
    ELAPSED_T(t1, t2);
    DBG("Time to read %i rule records: %.1f ms.", total_tuples, elapsed);

    return 0;
}