* Fetch parser cache entry */
| 108 | * Fetch parser cache entry |
| 109 | */ |
| 110 | TSParserCacheEntry * |
| 111 | lookup_ts_parser_cache(Oid prsId) |
| 112 | { |
| 113 | TSParserCacheEntry *entry; |
| 114 | |
| 115 | if (TSParserCacheHash == NULL) |
| 116 | { |
| 117 | /* First time through: initialize the hash table */ |
| 118 | HASHCTL ctl; |
| 119 | |
| 120 | ctl.keysize = sizeof(Oid); |
| 121 | ctl.entrysize = sizeof(TSParserCacheEntry); |
| 122 | TSParserCacheHash = hash_create("Tsearch parser cache", 4, |
| 123 | &ctl, HASH_ELEM | HASH_BLOBS); |
| 124 | /* Flush cache on pg_ts_parser changes */ |
| 125 | CacheRegisterSyscacheCallback(TSPARSEROID, InvalidateTSCacheCallBack, |
| 126 | PointerGetDatum(TSParserCacheHash)); |
| 127 | |
| 128 | /* Also make sure CacheMemoryContext exists */ |
| 129 | if (!CacheMemoryContext) |
| 130 | CreateCacheMemoryContext(); |
| 131 | } |
| 132 | |
| 133 | /* Check single-entry cache */ |
| 134 | if (lastUsedParser && lastUsedParser->prsId == prsId && |
| 135 | lastUsedParser->isvalid) |
| 136 | return lastUsedParser; |
| 137 | |
| 138 | /* Try to look up an existing entry */ |
| 139 | entry = (TSParserCacheEntry *) hash_search(TSParserCacheHash, |
| 140 | (void *) &prsId, |
| 141 | HASH_FIND, NULL); |
| 142 | if (entry == NULL || !entry->isvalid) |
| 143 | { |
| 144 | /* |
| 145 | * If we didn't find one, we want to make one. But first look up the |
| 146 | * object to be sure the OID is real. |
| 147 | */ |
| 148 | HeapTuple tp; |
| 149 | Form_pg_ts_parser prs; |
| 150 | |
| 151 | tp = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(prsId)); |
| 152 | if (!HeapTupleIsValid(tp)) |
| 153 | elog(ERROR, "cache lookup failed for text search parser %u", |
| 154 | prsId); |
| 155 | prs = (Form_pg_ts_parser) GETSTRUCT(tp); |
| 156 | |
| 157 | /* |
| 158 | * Sanity checks |
| 159 | */ |
| 160 | if (!OidIsValid(prs->prsstart)) |
| 161 | elog(ERROR, "text search parser %u has no prsstart method", prsId); |
| 162 | if (!OidIsValid(prs->prstoken)) |
| 163 | elog(ERROR, "text search parser %u has no prstoken method", prsId); |
| 164 | if (!OidIsValid(prs->prsend)) |
| 165 | elog(ERROR, "text search parser %u has no prsend method", prsId); |
| 166 | |
| 167 | if (entry == NULL) |
no test coverage detected