simple-cache.c 43.5 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: simple-cache.c,v 0.29 1992/11/30 23:29:16 linus Exp $
Linus Tolke Y's avatar
Linus Tolke Y committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (C) 1991  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25 26 27 28 29 30 31 32 33 34 35
/*
 * This module contains some simple simulations of the routines in
 * cache.c.
 *
 * Extracted from ram-cache.c and rewritten by ceder.
 *
 * New database format with texts in their own file by Inge Wallin.
 *
 * New save algorithm by ceder.
 */

36
static char *rcsid = "$Id: simple-cache.c,v 0.29 1992/11/30 23:29:16 linus Exp $";
Per Cederqvist's avatar
Per Cederqvist committed
37

Per Cederqvist's avatar
Per Cederqvist committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

/*
 * Possible improvements: +++
 *  When there are consecutive items in file A that shall be copied
 *  to file B, copy them in one transfer (up to a certain limit).
 *
 *  In pre_sync: compute size of, and allocate disk space for file B.
 */

/*
 * All functions that can fail sets kom_errno to a suitable value
 * if they fail.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/file.h>
Per Cederqvist's avatar
Per Cederqvist committed
56 57
#include <unistd.h>
#include <errno.h>
Per Cederqvist's avatar
Per Cederqvist committed
58 59 60

#include <kom-errno.h>
#include <kom-types.h>
Per Cederqvist's avatar
Per Cederqvist committed
61
#include "s-collat-tabs.h"
62
#include <server/smalloc.h>
Per Cederqvist's avatar
Per Cederqvist committed
63
#include <parser.h>
Per Cederqvist's avatar
Per Cederqvist committed
64 65 66 67 68 69
#include "cache.h"
#include <debug.h>
#include "lyskomd.h"
#include "ram-parse.h"
#include "log.h"
#include "com.h"
70
#include "isc-interface.h"
Per Cederqvist's avatar
Per Cederqvist committed
71 72 73 74 75 76 77
#include "ram-output.h"
#include "connections.h"
#include "prot-a-output.h"
#include "send-async.h"
#include "memory.h"
#include "exp.h"

78
#include "tmp-limits.h"
79
#include "cache-node.h"
Per Cederqvist's avatar
Per Cederqvist committed
80
#include "disk-cache.h"
Per Cederqvist's avatar
Per Cederqvist committed
81
static Small_conf	* small_conf_arr[ MAX_CONF ];
82 83
static Cache_node_mcb	* pers_mcb;
static Cache_node_mcb 	* conf_mcb;
Per Cederqvist's avatar
Per Cederqvist committed
84 85
static int		  next_free_num = 1;

86 87
static Cache_node_mcb 	* text_mcb;
static int		  next_text_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
88 89

/* Defined in ramkomd.c */
90 91 92
extern char datafilename[BUFSIZ];
extern char backupfilename[BUFSIZ];
extern char textfilename[BUFSIZ];
Per Cederqvist's avatar
Per Cederqvist committed
93 94 95 96 97 98 99

/*
 * The elements in the following lists with same index refers to the same
 * conference.
 */
static int    no_of_match_info;
EXPORT Matching_info *match_table = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
100
EXPORT Conf_no       *conf_table  = NULL; /* Used in conference.c. */
Per Cederqvist's avatar
Per Cederqvist committed
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


static FILE	*text_file= NULL;
static FILE	*file_a = NULL;	/* Current file. */
static FILE	*file_b = NULL;	/* File under construction. */

/*
 * Four state variables for the background save.
 */
static enum {
    sync_save_conf,
    sync_save_pers,
    sync_save_text,
    sync_error,
    sync_wait,
    sync_ready
} sync_state;

static long sync_next;

static Conf_no highest_conf_no;
static Text_no highest_text_no;

BUGDECL;

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
/* Define LOGACCESSES if you want to be able to log all accesses to
   the data base. */

#ifdef LOGACCESSES
extern char *logaccess_file;	/* From ramkomd.c */

typedef enum {
    lt_restart,
    lt_text_stat,
    lt_text_mass,
    lt_conf_stat,
    lt_pers_stat,
    
    lt_text_def,
    lt_conf_def,
    lt_pers_def,

    lt_create_text,
    lt_garb_text,
    lt_delete_text,
    lt_create_conf,
    lt_delete_conf,
    lt_create_pers,
    lt_delete_pers,

    lt_lock_conf,
    lt_unlock_conf,
    lt_lock_pers,
    lt_unlock_pers,

    lt_get_highest,		/* Get highest Local_text_no for a conf. */
    lt_get_conf_type
    /* Note: mark_*_as_changed is not logged. */
} Log_type;

static FILE *logfile = NULL;
static int syncing_or_saving = 0;
static int garb_running = 0;

static void log_access(Log_type t,
		       int id)
{
168 169
    extern int putw(int, FILE *);

170 171 172 173 174 175 176 177 178 179 180
    if (garb_running + syncing_or_saving == 0)
    {
	putc(t, logfile);
	putw(id, logfile);
    }
}
#define LOGACC(a,b) {if (logfile) log_access(a, b);}
#else
#define LOGACC(a,b)
#endif

Per Cederqvist's avatar
Per Cederqvist committed
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

/* Macros */

#define TRACE2(format, arg) if ( buglevel > 2 ) printf(format, arg)
#define TRACE1(format) if ( buglevel > 2 ) printf(format)
#define TRACESTR(str)  if ( buglevel > 2 ) s_puts(str)


static Person *
read_person(FILE *fp,
	    long pos,
	    long size)
{
    Person *p;

    p = alloc_person();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
    if ( fparse_person(fp, p) != OK )
    {
	free_person(p);
	return NULL;
    }
    else
	return p;
}



static Conference *
read_conference(FILE *fp,
		long pos,
		long size)
{
    Conference *c;

    c = alloc_conference();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
    if ( fparse_conference(fp, c) != OK )
    {
	free_conference(c);
	return NULL;
    }
    else
	return c;
}
	

static Text_stat *
read_text_stat(FILE *fp,
	       long pos,
	       long size)
{
    Text_stat *t;

    t = alloc_text_stat();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
    if ( fparse_text_stat(fp, t) != OK )
    {
	free_text_stat(t);
	return NULL;
    }
    else
	return t;
}

static void
247
pers_set_mru(Pers_no pers_no)
Per Cederqvist's avatar
Per Cederqvist committed
248
{
249
    set_mru(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
250 251 252
}

static void
253
text_set_mru(Text_no text_no)
Per Cederqvist's avatar
Per Cederqvist committed
254
{
255
    set_mru(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
256 257 258
}

static void
259
conf_set_mru(Conf_no conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
260
{
261 262
    set_mru(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
263

264 265 266
static Cache_node *
get_pers_node(Pers_no pers_no)
{
267 268 269
    if ( pers_no >= next_free_num )
	return NULL;

270
    return get_cache_node(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
271 272 273
}

static void
274
unlink_text_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
275
{
276
    unlink_lru (node, &text_mcb->lru, &text_mcb->mru);
Per Cederqvist's avatar
Per Cederqvist committed
277 278
}

279 280 281 282 283
static void
unlink_conf_lru (Cache_node *node)
{
    unlink_lru (node, &conf_mcb->lru, &conf_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
284 285

static void
286
unlink_pers_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
287
{
288 289
    unlink_lru (node, &pers_mcb->lru, &pers_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
290

291 292 293
static Cache_node *
get_conf_node(Conf_no conf_no)
{
294 295 296
    if ( conf_no >= next_free_num )
	return NULL;

297 298
    return get_cache_node(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
299

300 301 302
static Cache_node *
get_text_node(Text_no text_no)
{
303 304 305
    if ( text_no >= next_text_num )
	return NULL;

306
    return get_cache_node(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
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
}

	
/*
 * Name caching routines
 */

/*
 * change_name changes the cached conference name. It is only called when
 * a conference name is changed or a conference is deleted.
 */
void
cached_change_name( Conf_no name_num,
		    String new_name )
{
    if ( name_num < 1 || name_num >= next_free_num )
	restart_kom("cached_change_name(%d, ----): next_free_num==%d",
		    name_num, next_free_num);

    s_clear( &small_conf_arr[name_num]->name );
    s_strcpy( &small_conf_arr[name_num]->name, new_name);
    build_matching_info();
}


extern Conf_type
cached_get_conf_type (Conf_no conf_no)
{
    if ( conf_no < 1 || conf_no >= next_free_num )
	restart_kom("cached_get_conf_type(%d): next_free_num==%d",
		    conf_no, next_free_num);
338 339 340 341 342

    if ( small_conf_arr [ conf_no ] == NULL )
	restart_kom("cached_get_conf_type(%d): conference does not exist.\n",
		    conf_no);

343 344
    LOGACC(lt_get_conf_type, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
345 346 347 348 349 350 351 352 353 354 355 356
    return small_conf_arr [ conf_no ]->type;
}



/*
 * Various function calls to tell the cache that something is changed.
 */

void
mark_person_as_changed(Pers_no	pers_no)
{
357 358 359 360
    Cache_node *node;

    node = get_pers_node(pers_no);
    
Per Cederqvist's avatar
Per Cederqvist committed
361
    TRACE2("Person %d is changed\n", pers_no);
362
    if ( node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
363 364
	restart_kom("mark_person_as_changed(%d): nonexistent.\n", pers_no);

365
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
366 367 368
    pers_set_mru( pers_no );
}

369 370 371 372 373 374 375 376 377 378 379
/*
 * Mark the conference as dirty, so that it will be written to
 * the disk.
 *
 * Also update all fields in the Small_conf except then name, so that
 * they are always current.
 *
 * NOTE: You must call cached_change_name when the name changes.
 *       It is not necessary to call cached_change_name after
 *       cached_create_conf.
 */
Per Cederqvist's avatar
Per Cederqvist committed
380 381 382 383

void
mark_conference_as_changed(Conf_no	conf_no)
{
384
    Cache_node *node;
385
    Conference *conf_c;
386 387 388

    node = get_conf_node (conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
389
    TRACE2("Conf.  %d is changed\n", conf_no);
390
    if ( node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
391 392
	restart_kom("mark_conference_as_changed(%d): nonexistent.\n", conf_no);

393
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
394
    conf_set_mru( conf_no );
395 396 397

    conf_c = (Conference *) node->ptr;

Per Cederqvist's avatar
Per Cederqvist committed
398
    small_conf_arr[ conf_no ]->highest_local_no
399 400 401 402
	= (conf_c->texts.first_local_no - 1 + conf_c->texts.no_of_texts );

    small_conf_arr[ conf_no ]->nice = conf_c->nice;
    small_conf_arr[ conf_no ]->type = conf_c->type;
Per Cederqvist's avatar
Per Cederqvist committed
403 404 405 406 407
}

void
mark_text_as_changed( Text_no text_no )
{
408 409 410 411
    Cache_node *node;

    node = get_text_node (text_no);

Per Cederqvist's avatar
Per Cederqvist committed
412 413
    TRACE2("Text %d is changed.\n", text_no);
    if ( text_no < 1 || text_no >= next_text_num
414
	|| node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
415 416 417 418
    {
	restart_kom("mark_text_as_changed(%d): nonexistent.\n", text_no);
    }

419 420
    node->s.dirty = 1;
    text_set_mru (text_no);
Per Cederqvist's avatar
Per Cederqvist committed
421 422 423 424 425 426 427 428 429 430 431 432
}    



/*
 * Person-related calls
 */


extern Success
cached_create_person( Pers_no person )
{
433 434
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
435 436 437 438 439 440 441 442
    TRACE2("Person %d is being created.\n", person);

    if ( person < 1 || person >= next_free_num )
    {
	restart_kom("cached_create_person(%d): next_free_num == %d.\n",
		    person, next_free_num);
    }

443
    if ( get_pers_node(person) != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
444 445 446 447 448
    {
	restart_kom("cached_create_person(%d): Person existed.\n",
		    person);
    }

449 450 451 452 453
    create_cache_node (pers_mcb, person);
    node = get_pers_node (person);

    if ( node == NULL )
	restart_kom("cached_create_person(): couldn't get cache_node.\n");
454

455 456 457
    node->ptr = alloc_person();
    node->s.dirty = 1;
    node->s.exists = 1;
Per Cederqvist's avatar
Per Cederqvist committed
458
    pers_set_mru( person );
459 460 461

    LOGACC(lt_create_pers, person);

Per Cederqvist's avatar
Per Cederqvist committed
462 463 464 465 466 467 468
    return OK;
}


extern Person *
cached_get_person_stat( Pers_no	person )
{
469 470
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
471 472 473 474 475 476 477
    TRACE2("cached_get_person_stat %d\n", person);

    if ( person == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
478

479 480 481 482 483 484
    if ( person >= next_free_num )
    {
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

485 486
    node = get_pers_node (person);

487
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
488 489 490 491 492
    {
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

493 494
    LOGACC(lt_pers_stat, person);

495
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
496 497
    {
	pers_set_mru( person );
498
	++pers_mcb->hits;
499
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
500 501
    }

502
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
503
    {
504
	node->ptr = copy_person (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
505
	pers_set_mru (person);
506
	++pers_mcb->hits;
507
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
508 509
    }

510
    node->ptr = read_person(file_a, node->pos, node->size);
Per Cederqvist's avatar
Per Cederqvist committed
511

512
    ++pers_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
513
    pers_set_mru (person);
514
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
515 516 517 518 519 520
}


/*
 * Conference-related calls
 */
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


static int no_of_allocated_small_confs = 0;

static void
free_small_conf (Small_conf *sc)
{
    if ( sc != NULL )
    {
	--no_of_allocated_small_confs;
	s_clear ( &sc->name );
	sfree (sc);
    }
}


static Small_conf *
alloc_small_conf(void)
{
    Small_conf *s;
    s = smalloc(sizeof(Small_conf));
    *s = EMPTY_SMALL_CONF;
    ++no_of_allocated_small_confs;

    return s;
}


549 550 551 552 553 554
/*
 * Create a conference.
 *
 * Set up a Conference and cache the name in the small_conf_array.
 */
extern Conf_no
Per Cederqvist's avatar
Per Cederqvist committed
555 556 557 558
cached_create_conf (String  name)
{
    Conference * conf_c;
    Conf_no	 conf_no;
559
    Cache_node  *node;
Per Cederqvist's avatar
Per Cederqvist committed
560 561 562 563 564 565 566 567 568 569 570 571 572

    TRACE1("cached_create_conf( ");
    TRACESTR(name);
    TRACE1(" )\n");

    if ( next_free_num >= MAX_CONF )
    {
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return 0;
    }
    
    conf_no = next_free_num++;

573 574 575 576 577 578 579 580 581
    create_cache_node (conf_mcb, conf_no);
    node = get_conf_node (conf_no);

    if ( node == NULL )
	restart_kom("cached_create_conf(): failed to allocate cache_node.\n");
    
    node->s.exists = 1;
    node->s.dirty = 1;
    node->ptr = conf_c = alloc_conference();
Per Cederqvist's avatar
Per Cederqvist committed
582 583
    conf_set_mru(conf_no);    

584
    zero_init_cache_node (pers_mcb, conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
585 586 587 588 589 590
    small_conf_arr[ conf_no ] = alloc_small_conf();

    conf_c->name = EMPTY_STRING;
    s_strcpy(&conf_c->name, name);
    cached_change_name( conf_no, name);

591
    TRACE2("Created conference number %d\n", conf_no);
592 593 594

    LOGACC(lt_create_conf, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
595 596 597 598 599 600 601
    return conf_no;
}


extern Success
cached_delete_conf( Conf_no	conf )
{
602 603
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
604 605 606 607 608
    if ( conf == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
609

610 611 612 613 614 615
    if ( conf >= next_free_num )
    {
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

616 617
    node = get_conf_node (conf);

618
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
619 620 621 622 623
    {
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

624 625 626
    if ( node->lock_cnt > 0 )
	log("WNG: cached_delete_conf(%d): lock_cnt === %d\n",
	    conf, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
627

628 629 630
    free_conference(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
631

632 633
    LOGACC(lt_delete_conf, conf);

Per Cederqvist's avatar
Per Cederqvist committed
634 635 636 637 638 639
    return OK;
}

Success
cached_delete_person(Pers_no pers)
{
640 641
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
642 643 644 645 646
    if ( pers == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
647

648 649 650 651 652 653 654 655
    if ( pers >= next_free_num )
    {
	log("cached_delete_person(%lu): next_free_num == %lu\n",
	    (u_long)pers, (u_long)next_free_num);
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

656 657 658
    node = get_pers_node (pers);

    if ( pers >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
659 660 661 662 663 664 665
    {
	log("cached_delete_person(): attempt to"
	    " delete non-existing person.\n");
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

666
    if ( node->lock_cnt > 0 )
667 668
	log("cached_delete_pers(%lu): lock_cnt === %lu\n",
	    (u_long)pers, (u_long)node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
669

670 671
    LOGACC(lt_delete_pers, pers);

672 673 674
    free_person (node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
675 676 677 678 679 680
    return OK;
}

Success
cached_delete_text(Text_no text)
{
681 682
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
683 684 685 686 687
    if ( text == 0 )
    {
	kom_errno = KOM_TEXT_ZERO;
	return FAILURE;
    }
688 689 690 691 692

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
693 694 695 696 697 698 699
    {
	log("cached_delete_text(): attempt to"
	    " delete non-existing text %d.\n", text);
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }

700
    if ( node->lock_cnt > 0 )
Per Cederqvist's avatar
Per Cederqvist committed
701
	log("cached_delete_text(%d): lock_cnt === %d\n",
702
	    text, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
703 704


705 706 707
    free_text_stat(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
708

709 710 711
#ifdef LOGACCESSES
    if (garb_running)
    {
712
	LOGACC(lt_garb_text, text);
713 714
    }
    else
715
	LOGACC(lt_delete_text, text);
716
#endif
Per Cederqvist's avatar
Per Cederqvist committed
717 718 719 720 721
    return OK;
}


extern Conference *
722
cached_get_conf_stat (Conf_no   conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
723
{
724 725
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
726 727 728 729 730 731 732
    TRACE2("cached_get_conf_stat %d\n", conf_no);

    if ( conf_no == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
733 734 735 736

    node = get_conf_node (conf_no);

    if ( conf_no >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
737 738 739 740 741
    {
	kom_errno = KOM_UNDEF_CONF;
	return NULL;
    }

742 743
    LOGACC(lt_conf_stat, conf_no);

744
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
745
    {
746
	conf_set_mru (conf_no);
747
	++conf_mcb->hits;
748
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
749 750
    }

751
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
752
    {
753
	node->ptr = copy_conf (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
754
	conf_set_mru (conf_no);
755
	++conf_mcb->hits;
756
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
757 758
    }

759
    node->ptr = read_conference(file_a, node->pos, node->size);
760
    ++conf_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
761 762

    conf_set_mru (conf_no);
763
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
764 765 766 767 768 769 770 771
}

/*
 * Return TRUE if conf_no exists.
 */
Bool
cached_conf_exists(Conf_no conf_no)
{
772 773 774 775 776 777 778 779
    Cache_node *node;

    if (conf_no == 0 || conf_no >= next_free_num )
	return FALSE;

    node = get_conf_node (conf_no);

    return node != NULL && node->s.exists != 0;
Per Cederqvist's avatar
Per Cederqvist committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
}

    
/*
 * Calls to handle texts
 */

/*
 * +++ Should return Success.
 */
extern String
cached_get_text( Text_no text )
{
    String  the_string;
    Text_stat *t_stat;

    TRACE2("cached_get_text %d\n", text);

    if ( (t_stat = cached_get_text_stat (text)) == NULL )
	return EMPTY_STRING;
    else
    {
802
	LOGACC(lt_text_mass, text);
Per Cederqvist's avatar
Per Cederqvist committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
	the_string.string = tmp_alloc( t_stat->no_of_chars );
	the_string.len = t_stat->no_of_chars;
	fseek(text_file, t_stat->file_pos, SEEK_SET);

	if ( fread(the_string.string, sizeof(char), the_string.len, text_file)
	    != the_string.len )
	{
	    log("WARNING: cached_get_text: "
		"couldn't read enough characters of text %d\n",
		text);
	    return EMPTY_STRING;
	}
		    
	return the_string;
    }
}


extern Text_stat *	/* NULL on error */
cached_get_text_stat(	Text_no		text )
{
824 825
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
826 827 828 829 830 831 832 833
    TRACE2("cached_get_text_stat(%d);  next_text_num == ", text);
    TRACE2("%d\n", next_text_num);

    if ( text == 0 )
    {
	kom_errno = KOM_TEXT_ZERO;
	return NULL;
    }
834 835 836 837

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
838 839 840 841 842 843
    {
	TRACE1("cached_get_text_stat: no such text.\n");
	kom_errno = KOM_NO_SUCH_TEXT;
	return NULL;
    }

844 845
    LOGACC(lt_text_stat, text);

846
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
847 848 849
    {
	TRACE1("Found in ptr.\n");
	text_set_mru( text );
850
	++text_mcb->hits;
851
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
852 853
    }

854
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
855 856
    {
	TRACE1("Found in snap_shot\n");
857
	node->ptr = copy_text_stat(node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
858
	text_set_mru (text);
859
	++text_mcb->hits;
860
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
861 862 863
    }

    TRACE1("Found in file A.\n");
864
    node->ptr = read_text_stat(file_a, node->pos, node->size);
Per Cederqvist's avatar
Per Cederqvist committed
865 866

    text_set_mru (text);
867
    ++text_mcb->misses;
868
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
869 870 871 872 873 874
}



/*
 * The text is set up with an empty misc-field. The misc field is
875
 * then initialized by create_text.
Per Cederqvist's avatar
Per Cederqvist committed
876 877 878 879 880 881
 */

extern Text_no
cached_create_text( String message)
{
    Text_no tno;
882
    Cache_node *node;
Per Cederqvist's avatar
Per Cederqvist committed
883 884 885 886 887 888 889 890 891 892 893 894 895

    tno = next_text_num++;

    TRACE2("cached_create_text (len=%d)\n", message.len);

    if ( tno >= MAX_TEXT )
    {
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	next_text_num = MAX_TEXT;
	
	return 0;
    }

896 897
    create_cache_node(text_mcb, tno);
    node = get_text_node (tno);
Per Cederqvist's avatar
Per Cederqvist committed
898

899
    if ( node == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
900
	restart_kom("cached_create_text(): couldn't create cache-node.\n");
901 902 903 904 905 906 907 908 909
    
    node->s.exists = 1;
    node->s.dirty = 1;
    node->ptr = alloc_text_stat();
    ((Text_stat *)node->ptr)->no_of_misc = 0;
    ((Text_stat *)node->ptr)->misc_items = NULL;
    ((Text_stat *)node->ptr)->no_of_marks = 0;
    ((Text_stat *)node->ptr)->no_of_lines = 0;
    ((Text_stat *)node->ptr)->no_of_chars = 0;
Per Cederqvist's avatar
Per Cederqvist committed
910
    fseek(text_file, 0, SEEK_END);
911
    ((Text_stat *)node->ptr)->file_pos = ftell(text_file);
Per Cederqvist's avatar
Per Cederqvist committed
912 913 914 915 916 917 918 919 920 921 922

    text_set_mru( tno );

    if ( fwrite(message.string, sizeof(char), message.len, text_file)
	!= message.len ) {
	log("WARNING: cached_create_text: Couldn't write the text %d\n",
	    tno);
    }

    fflush(text_file);
    
923
    LOGACC(lt_create_text, tno);
Per Cederqvist's avatar
Per Cederqvist committed
924 925 926 927 928 929
    TRACE2("cached_create_text -> %d\n", tno);
    
    return tno;
}


930
EXPORT Text_no
Per Cederqvist's avatar
Per Cederqvist committed
931 932
traverse_text(Text_no seed)
{
933 934
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
935 936
    seed++;
    
937
    while ( seed < next_text_num )
Per Cederqvist's avatar
Per Cederqvist committed
938
    {
939 940 941 942
	node = get_text_node (seed);
	if ( node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
943 944 945 946 947 948 949 950 951
	seed++;
    }
    
    return (seed >= next_text_num) ? 0 : seed ;
}

Pers_no
traverse_person(Pers_no seed)
{
952 953
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
954 955
    seed++;

956
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
957
    {
958 959 960 961
	node = get_pers_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
962 963 964 965 966 967 968 969 970
	seed++;
    }

    return (seed >= next_free_num) ? 0 : seed ;
}

Conf_no
traverse_conference(Conf_no seed)
{
971 972
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
973 974
    seed++;
    
975
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
976
    {
977 978 979 980
	node = get_conf_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
	seed++;
    }

    return (seed >= next_free_num) ? 0 : seed ;
}

extern Garb_nice
cached_get_garb_nice (Conf_no conf_no)
{
    return small_conf_arr [ conf_no ]->nice;
}


extern Local_text_no
cached_get_highest_local_no (Conf_no conf_no)
{
997 998
    LOGACC(lt_get_highest, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
999 1000 1001 1002 1003 1004 1005
    return small_conf_arr[ conf_no ]->highest_local_no;
}

/* Lock a person struct in memory. Increase a referenc count. */
void
cached_lock_person(Pers_no pers_no)
{
1006 1007
    Cache_node *node;

1008 1009
    LOGACC(lt_lock_pers, pers_no);

1010 1011 1012
    node = get_pers_node(pers_no);
    
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
1013 1014
	restart_kom("cached_lock_person(%d): nonexistent.\n", pers_no);

1015
    if ( node->ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1016
    {
1017
	Person *pers_stat_ptr;
Per Cederqvist's avatar
Per Cederqvist committed
1018

1019
	pers_stat_ptr = cached_get_person_stat( pers_no );
Per Cederqvist's avatar
Per Cederqvist committed
1020

1021
	if ( pers_stat_ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1022 1023 1024
	    restart_kom("cached_lock_person(%d): couldn't read in person.\n",
			pers_no);

1025 1026 1027 1028
	if ( pers_stat_ptr != node->ptr )
	    restart_kom("cached_lock_person(%d): pers_stat_ptr == %d, node"
			"->ptr == %d.\n", pers_no, pers_stat_ptr,
			node->ptr);
Per Cederqvist's avatar
Per Cederqvist committed
1029 1030
    }

1031
    node->lock_cnt++;
Per Cederqvist's avatar
Per Cederqvist committed
1032 1033 1034 1035 1036 1037
}

/* Decrease reference count. If zero, unlock person. */
void
cached_unlock_person(Pers_no pers_no)
{
1038 1039
    Cache_node *node;

1040 1041
    LOGACC(lt_unlock_pers, pers_no);

1042 1043 1044 1045 1046 1047
    node = get_pers_node (pers_no);

    if ( node == NULL )
	restart_kom("cached_unlock_person(): couldn't get cache-node.\n");

    if ( node->lock_cnt <= 0 )
Per Cederqvist's avatar
Per Cederqvist committed
1048 1049
    {
	log("cached_unlock_person(%d): lock_cnt == %d.\n",
1050
	    pers_no, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
1051

1052
	node->lock_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
1053 1054
    }
    else
1055
	node->lock_cnt--;
Per Cederqvist's avatar
Per Cederqvist committed
1056 1057 1058
}


1059
/* Lock a conf struct in memory. Increase a reference count. */
Per Cederqvist's avatar
Per Cederqvist committed
1060 1061 1062
void
cached_lock_conf(Conf_no conf_no)
{
1063 1064 1065 1066 1067 1068 1069 1070
    Cache_node *node;

    node = get_conf_node(conf_no);

    if ( node == NULL)
	restart_kom("cached_lock_conf(): can't get cache-node.\n");

    if ( node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
1071 1072
	restart_kom("cached_lock_conf(%d): nonexistent.\n", conf_no);

1073 1074
    LOGACC(lt_lock_conf, conf_no);

1075
    if ( node->ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1076
    {
1077
	Conference *conference_ptr;
Per Cederqvist's avatar
Per Cederqvist committed
1078

1079
	conference_ptr = cached_get_conf_stat( conf_no );
Per Cederqvist's avatar
Per Cederqvist committed
1080

1081
	if ( conference_ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1082 1083 1084
	    restart_kom("cached_lock_conf(%d): couldn't read in conf.\n",
			conf_no);

1085 1086 1087 1088
	if ( conference_ptr != node->ptr )
	    restart_kom("cached_lock_conf(%d): conference_ptr == %d, node"
			"->ptr == %d.\n", conf_no, conference_ptr,
			node->ptr);
Per Cederqvist's avatar
Per Cederqvist committed
1089 1090
    }

1091
    node->lock_cnt++;
Per Cederqvist's avatar
Per Cederqvist committed
1092 1093 1094 1095 1096 1097
}

/* Decrease reference count. If zero, unlock conf. */
void
cached_unlock_conf(Conf_no conf_no)
{
1098 1099
    Cache_node *node;

1100 1101
    LOGACC(lt_unlock_conf, conf_no);

1102 1103 1104 1105 1106
    node = get_conf_node(conf_no);
    if ( node == NULL )
	restart_kom("cached_unlock_conf(): can't get node.\n");

    if ( node->lock_cnt <= 0 )
Per Cederqvist's avatar
Per Cederqvist committed
1107 1108
    {
	log("cached_unlock_conf(%d): lock_cnt == %d.\n",
1109
	    conf_no, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
1110

1111
	node->lock_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
1112 1113
    }
    else
1114
	node->lock_cnt--;
Per Cederqvist's avatar
Per Cederqvist committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
}



/*
 * And here comes some functions to deal with lookup_names.
 */

/* Free the _contents_ of a match_table. The table itself i _not_ freed. */

static void
free_match_table(Matching_info *match)
{
    if ( match == NULL )
	return;
    
    while ( ! s_empty( match->name ))
    {
	free_tokens( match->tokens );
	/* match->name is not freed since it points into conf_arr[]. */

	++match;
    }
}    

extern Success
build_matching_info(void)
{