Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 71b6a72

Browse files
committed
Use saved long-lived memory context for constructing dictionary after dict's reset
1 parent 7403e28 commit 71b6a72

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

contrib/shared_ispell/expected/shared_ispell.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,9 @@ SELECT ts_lexize('shared_hunspell', 'skies');
211211
{sky}
212212
(1 row)
213213

214+
SELECT ts_lexize('shared_hunspell', 'skies');
215+
ts_lexize
216+
-----------
217+
{sky}
218+
(1 row)
219+

contrib/shared_ispell/sql/shared_ispell.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ SELECT stop_name, words FROM shared_ispell_stoplists();
5353
SELECT shared_ispell_reset();
5454

5555
SELECT ts_lexize('shared_ispell', 'skies');
56-
SELECT ts_lexize('shared_hunspell', 'skies');
56+
SELECT ts_lexize('shared_hunspell', 'skies');
57+
SELECT ts_lexize('shared_hunspell', 'skies');

contrib/shared_ispell/src/shared_ispell.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,9 @@ static void
294294
init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
295295
{
296296
int size;
297-
298297
SharedIspellDict *shdict = NULL;
299298
SharedStopList *shstop = NULL;
300299

301-
IspellDict *dict;
302-
StopList stoplist;
303-
304300
/* DICTIONARY + AFFIXES */
305301

306302
/* TODO This should probably check that the filenames are not NULL, and maybe that
@@ -319,6 +315,8 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
319315
/* load the dictionary (word list) if not yet defined */
320316
if (shdict == NULL)
321317
{
318+
IspellDict *dict;
319+
322320
dict = (IspellDict *) palloc0(sizeof(IspellDict));
323321

324322
NIStartBuild(dict);
@@ -389,6 +387,8 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
389387
/* load the stopwords if not yet defined */
390388
if (shstop == NULL)
391389
{
390+
StopList stoplist;
391+
392392
readstoplist(stopFile, &stoplist, lowerstr);
393393

394394
size = sizeStopList(&stoplist, stopFile);
@@ -418,6 +418,9 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
418418
memcpy(info->stopFile, stopFile, strlen(stopFile) + 1);
419419
else
420420
memset(info->stopFile, 0, sizeof(info->stopFile));
421+
422+
/* save current context as long-lived */
423+
info->saveCntx = CurrentMemoryContext;
421424
}
422425

423426
Datum dispell_init(PG_FUNCTION_ARGS);
@@ -504,6 +507,9 @@ dispell_mem_used(PG_FUNCTION_ARGS)
504507
* The StopWords parameter is optional, the two other are required.
505508
*
506509
* If any of the filenames are incorrect, the call to init_shared_dict will fail.
510+
*
511+
* Do not call it directly - it saves current memory context as long-lived
512+
* context.
507513
*/
508514
Datum
509515
dispell_init(PG_FUNCTION_ARGS)
@@ -605,11 +611,27 @@ dispell_lexize(PG_FUNCTION_ARGS)
605611
/* do we need to reinit the dictionary? was the dict reset since the lookup */
606612
if (timestamp_cmp_internal(info->lookup, segment_info->lastReset) < 0)
607613
{
614+
DictInfo saveInfo = *info;
615+
MemoryContext ctx;
616+
608617
/* relock in exclusive mode */
609618
LWLockRelease(segment_info->lock);
610619
LWLockAcquire(segment_info->lock, LW_EXCLUSIVE);
611620

612-
init_shared_dict(info, info->dictFile, info->affixFile, info->stopFile);
621+
/*
622+
* info is allocated in info->saveCntx, so that's why we use a copy of
623+
* info here
624+
*/
625+
626+
MemoryContextResetAndDeleteChildren(saveInfo.saveCntx);
627+
ctx = MemoryContextSwitchTo(saveInfo.saveCntx);
628+
629+
info = palloc0(sizeof(*info));
630+
631+
init_shared_dict(info, saveInfo.dictFile,
632+
saveInfo.affixFile, saveInfo.stopFile);
633+
634+
MemoryContextSwitchTo(ctx);
613635
}
614636

615637
res = NINormalizeWord(&(info->dict), txt);

contrib/shared_ispell/src/shared_ispell.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __SHARED_ISPELL_H__
33

44
#include "storage/lwlock.h"
5+
#include "utils/memutils.h"
56
#include "utils/timestamp.h"
67
#include "tsearch/dicts/spell.h"
78
#include "tsearch/ts_public.h"
@@ -66,6 +67,9 @@ typedef struct DictInfo
6667
SharedIspellDict *shdict;
6768
IspellDict dict;
6869
SharedStopList *shstop;
70+
71+
/* MemoryContext of dict local content */
72+
MemoryContext saveCntx;
6973
} DictInfo;
7074

71-
#endif
75+
#endif

0 commit comments

Comments
 (0)