|
| 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 | +typedefstruct |
| 21 | +{ |
| 22 | +/* XXX we assume this struct contains no padding bytes */ |
| 23 | +uint64fs; |
| 24 | +int64fss; |
| 25 | +}htab_key; |
| 26 | + |
| 27 | +typedefstruct |
| 28 | +{ |
| 29 | +htab_keykey; |
| 30 | + |
| 31 | +/* Store ML data "AS IS". */ |
| 32 | +intnrows; |
| 33 | +intncols; |
| 34 | +double*matrix[aqo_K]; |
| 35 | +double*targets; |
| 36 | +List*relids; |
| 37 | +}htab_entry; |
| 38 | + |
| 39 | +staticHTAB*fss_htab=NULL; |
| 40 | +MemoryContextLearnCacheMemoryContext=NULL; |
| 41 | + |
| 42 | +void |
| 43 | +lc_init(void) |
| 44 | +{ |
| 45 | +HASHCTLctl; |
| 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(uint64fs,intfss,intnrows,intncols, |
| 61 | +double**matrix,double*targets,List*relids) |
| 62 | +{ |
| 63 | +htab_keykey= {fs,fss}; |
| 64 | +htab_entry*entry; |
| 65 | +boolfound; |
| 66 | +inti; |
| 67 | +MemoryContextmemctx=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(uint64fs,intfss) |
| 98 | +{ |
| 99 | +htab_keykey= {fs,fss}; |
| 100 | +boolfound; |
| 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(uint64fs,intfss,intncols,double**matrix, |
| 112 | +double*targets,int*nrows,List**relids) |
| 113 | +{ |
| 114 | +htab_keykey= {fs,fss}; |
| 115 | +htab_entry*entry; |
| 116 | +boolfound; |
| 117 | +inti; |
| 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(uint64fs,intfss) |
| 141 | +{ |
| 142 | +htab_keykey= {fs,fss}; |
| 143 | +htab_entry*entry; |
| 144 | +boolfound; |
| 145 | +inti; |
| 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 | +} |