|
| 1 | +/* |
| 2 | + ******************************************************************************* |
| 3 | + * |
| 4 | + * |
| 5 | + * |
| 6 | + ******************************************************************************* |
| 7 | + * |
| 8 | + * Copyright (c) 2016-2022, Postgres Professional |
| 9 | + * |
| 10 | + * IDENTIFICATION |
| 11 | + * aqo/learn_cache.c |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +#include "postgres.h" |
| 16 | + |
| 17 | +#include "aqo.h" |
| 18 | +#include "learn_cache.h" |
| 19 | + |
| 20 | +typedef struct |
| 21 | +{ |
| 22 | + /* XXX we assume this struct contains no padding bytes */ |
| 23 | + uint64 fs; |
| 24 | + int64 fss; |
| 25 | +} htab_key; |
| 26 | + |
| 27 | +typedef struct |
| 28 | +{ |
| 29 | + htab_key key; |
| 30 | + |
| 31 | + /* Store ML data "AS IS". */ |
| 32 | + int nrows; |
| 33 | + int ncols; |
| 34 | + double *matrix[aqo_K]; |
| 35 | + double *targets; |
| 36 | + List *relids; |
| 37 | +} htab_entry; |
| 38 | + |
| 39 | +static HTAB *fss_htab = NULL; |
| 40 | +MemoryContext LearnCacheMemoryContext = NULL; |
| 41 | + |
| 42 | +void |
| 43 | +lc_init(void) |
| 44 | +{ |
| 45 | + HASHCTL ctl; |
| 46 | + |
| 47 | + Assert(!LearnCacheMemoryContext); |
| 48 | + LearnCacheMemoryContext = AllocSetContextCreate(TopMemoryContext, |
| 49 | + "lcache context", |
| 50 | + ALLOCSET_DEFAULT_SIZES); |
| 51 | + |
| 52 | + ctl.keysize = sizeof(htab_key); |
| 53 | + ctl.entrysize = sizeof(htab_entry); |
| 54 | + ctl.hcxt = LearnCacheMemoryContext; |
| 55 | + |
| 56 | + fss_htab = hash_create("Remote Con hash", 32, &ctl, HASH_ELEM | HASH_BLOBS); |
| 57 | +} |
| 58 | + |
| 59 | +bool |
| 60 | +lc_update_fss(uint64 fs, int fss, int nrows, int ncols, |
| 61 | + double **matrix, double *targets, List *relids) |
| 62 | +{ |
| 63 | + htab_key key = {fs, fss}; |
| 64 | + htab_entry *entry; |
| 65 | + bool found; |
| 66 | + int i; |
| 67 | + MemoryContext memctx = MemoryContextSwitchTo(LearnCacheMemoryContext); |
| 68 | + |
| 69 | + Assert(fss_htab); |
| 70 | + |
| 71 | + entry = (htab_entry *) hash_search(fss_htab, &key, HASH_ENTER, &found); |
| 72 | + if (found) |
| 73 | + { |
| 74 | + /* Clear previous version of the cached data. */ |
| 75 | + for (i = 0; i < entry->nrows; ++i) |
| 76 | + pfree(entry->matrix[i]); |
| 77 | + pfree(entry->targets); |
| 78 | + list_free(entry->relids); |
| 79 | + } |
| 80 | + |
| 81 | + entry->nrows = nrows; |
| 82 | + entry->ncols = ncols; |
| 83 | + for (i = 0; i < entry->nrows; ++i) |
| 84 | + { |
| 85 | + entry->matrix[i] = palloc(sizeof(double) * ncols); |
| 86 | + memcpy(entry->matrix[i], matrix[i], sizeof(double) * ncols); |
| 87 | + } |
| 88 | + entry->targets = palloc(sizeof(double) * nrows); |
| 89 | + memcpy(entry->targets, targets, sizeof(double) * nrows); |
| 90 | + entry->relids = list_copy(relids); |
| 91 | + |
| 92 | + MemoryContextSwitchTo(memctx); |
| 93 | + return true; |
| 94 | +} |
| 95 | + |
| 96 | +bool |
| 97 | +lc_has_fss(uint64 fs, int fss) |
| 98 | +{ |
| 99 | + htab_key key = {fs, fss}; |
| 100 | + bool found; |
| 101 | + |
| 102 | + Assert(fss_htab); |
| 103 | + |
| 104 | + (void) hash_search(fss_htab, &key, HASH_FIND, &found); |
| 105 | + if (!found) |
| 106 | + return false; |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +bool |
| 111 | +lc_load_fss(uint64 fs, int fss, int ncols, double **matrix, |
| 112 | + double *targets, int *nrows, List **relids) |
| 113 | +{ |
| 114 | + htab_key key = {fs, fss}; |
| 115 | + htab_entry *entry; |
| 116 | + bool found; |
| 117 | + int i; |
| 118 | + |
| 119 | + Assert(fss_htab); |
| 120 | + |
| 121 | + entry = (htab_entry *) hash_search(fss_htab, &key, HASH_FIND, &found); |
| 122 | + if (!found) |
| 123 | + return false; |
| 124 | + |
| 125 | + *nrows = entry->nrows; |
| 126 | + Assert(entry->ncols == ncols); |
| 127 | + for (i = 0; i < entry->nrows; ++i) |
| 128 | + memcpy(matrix[i], entry->matrix[i], sizeof(double) * ncols); |
| 129 | + memcpy(targets, entry->targets, sizeof(double) * entry->nrows); |
| 130 | + if (relids) |
| 131 | + *relids = list_copy(entry->relids); |
| 132 | + return true; |
| 133 | +} |
| 134 | + |
| 135 | +/* |
| 136 | + * Remove record from fss cache. Should be done at learning stage of successfully |
| 137 | + * finished query execution. |
| 138 | +*/ |
| 139 | +void |
| 140 | +lc_remove_fss(uint64 fs, int fss) |
| 141 | +{ |
| 142 | + htab_key key = {fs, fss}; |
| 143 | + htab_entry *entry; |
| 144 | + bool found; |
| 145 | + int i; |
| 146 | + |
| 147 | + Assert(fss_htab); |
| 148 | + |
| 149 | + entry = (htab_entry *) hash_search(fss_htab, &key, HASH_FIND, &found); |
| 150 | + if (!found) |
| 151 | + return; |
| 152 | + |
| 153 | + for (i = 0; i < entry->nrows; ++i) |
| 154 | + pfree(entry->matrix[i]); |
| 155 | + pfree(entry->targets); |
| 156 | + hash_search(fss_htab, &key, HASH_REMOVE, NULL); |
| 157 | +} |
0 commit comments