simple-cache.c 52.4 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: simple-cache.c,v 0.65 1998/06/14 14:51:22 byers Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
/*
 * 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.
David Byers's avatar
David Byers committed
34
 * Attempt at newer algorithm by byers (FASTSAVE)
Per Cederqvist's avatar
Per Cederqvist committed
35 36
 */

37
static char *rcsid = "$Id: simple-cache.c,v 0.65 1998/06/14 14:51:22 byers Exp $";
38 39
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
40

Per Cederqvist's avatar
Per Cederqvist committed
41 42
#include <errno.h>
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
43
#include <sys/types.h>
44 45 46
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
47 48 49
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
50 51 52 53
#ifndef SEEK_END
#  include <fcntl.h>
#endif
#ifndef SEEK_END
Per Cederqvist's avatar
Per Cederqvist committed
54 55 56
#  define SEEK_SET 0
#  define SEEK_END 2
#endif
Per Cederqvist's avatar
Per Cederqvist committed
57 58
#include <time.h>
#include <setjmp.h>
59 60 61
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
62

63
#include "ldifftime.h"
Per Cederqvist's avatar
Per Cederqvist committed
64 65
#include "exp.h"
#include "misc-types.h"
66
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include "kom-types.h"
#include "tmp-limits.h"
#include "cache-node.h"
#include "cache.h"
#include "parser.h"
#include "ram-parse.h"
#include "ram-output.h"
#include "server/smalloc.h"
#include "kom-memory.h"
#include "lyskomd.h"
#include "debug.h"
#include "kom-errno.h"
#include "log.h"
#include "com.h"
81
#include "async.h"
Per Cederqvist's avatar
Per Cederqvist committed
82 83
#include "connections.h"
#include "send-async.h"
84
#include "param.h"
Per Cederqvist's avatar
Per Cederqvist committed
85
#include "config.h"
David Byers's avatar
David Byers committed
86
#include "admin.h"
Per Cederqvist's avatar
Per Cederqvist committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

/*
 * 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.
 */

static Small_conf	* small_conf_arr[ MAX_CONF ];
102 103
static Cache_node_mcb	* pers_mcb;
static Cache_node_mcb 	* conf_mcb;
Per Cederqvist's avatar
Per Cederqvist committed
104 105
static int		  next_free_num = 1;

106 107
static Cache_node_mcb 	* text_mcb;
static int		  next_text_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
108 109 110 111 112 113 114

/*
 * 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
115
EXPORT Conf_no       *conf_table  = NULL; /* Used in conference.c. */
Per Cederqvist's avatar
Per Cederqvist committed
116 117 118 119 120


static FILE	*text_file= NULL;
static FILE	*file_a = NULL;	/* Current file. */
static FILE	*file_b = NULL;	/* File under construction. */
David Byers's avatar
David Byers committed
121 122 123
#ifdef FASTSAVE
static FILE     *file_b_r = NULL; /* Read from file under construction */
#endif
Per Cederqvist's avatar
Per Cederqvist committed
124 125 126 127 128

/*
 * Four state variables for the background save.
 */
static enum {
129
    sync_idle,
Per Cederqvist's avatar
Per Cederqvist committed
130 131 132 133 134 135 136 137
    sync_save_conf,
    sync_save_pers,
    sync_save_text,
    sync_error,
    sync_wait,
    sync_ready
} sync_state;

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/* The state machine sync_state works like this:
 *
 *  Old state              action           new state
 *  none               sync_part called     sync_idle
 *                     for the first time
 *  sync_idle	       SYNC_INTERVAL        sync_save_conf
 *  sync_save_conf     all confs saved      sync_save_pers
 *  sync_save_pers     all persons saved    sync_save_text
 *  sync_save_text     all texts saved      sync_ready
 *  sync_ready         sync_part called     sync_idle
 *  any                error occurs         sync_error
 *  sync_error         sync_part called     sync_wait
 *  sync_wait          SYNC_RETRY_INTERVAL  sync_save_conf
 */

David Byers's avatar
David Byers committed
153
#ifndef FASTSAVE
Per Cederqvist's avatar
Per Cederqvist committed
154
static long sync_next;
David Byers's avatar
David Byers committed
155
#endif
Per Cederqvist's avatar
Per Cederqvist committed
156 157 158 159 160 161

static Conf_no highest_conf_no;
static Text_no highest_text_no;

BUGDECL;

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/* Define LOGACCESSES if you want to be able to log all accesses to
   the data base. */

#ifdef LOGACCESSES
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)
{
202 203
    extern int putw(int, FILE *);

204 205 206 207 208 209 210 211 212 213 214
    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
215 216 217 218 219 220 221 222 223 224 225 226 227 228

/* 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;
David Byers's avatar
David Byers committed
229
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
230 231 232

    p = alloc_person();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
David Byers's avatar
David Byers committed
233
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    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;
David Byers's avatar
David Byers committed
251
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
252 253 254

    c = alloc_conference();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
David Byers's avatar
David Byers committed
255
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    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;
David Byers's avatar
David Byers committed
272
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
273 274 275

    t = alloc_text_stat();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
David Byers's avatar
David Byers committed
276
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
277 278 279 280 281 282 283 284 285 286
    if ( fparse_text_stat(fp, t) != OK )
    {
	free_text_stat(t);
	return NULL;
    }
    else
	return t;
}

static void
287
pers_set_mru(Pers_no pers_no)
Per Cederqvist's avatar
Per Cederqvist committed
288
{
289
    set_mru(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
290 291 292
}

static void
293
text_set_mru(Text_no text_no)
Per Cederqvist's avatar
Per Cederqvist committed
294
{
295
    set_mru(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
296 297 298
}

static void
299
conf_set_mru(Conf_no conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
300
{
301 302
    set_mru(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
303

304 305 306
static Cache_node *
get_pers_node(Pers_no pers_no)
{
307 308 309
    if ( pers_no >= next_free_num )
	return NULL;

310
    return get_cache_node(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
311 312 313
}

static void
314
unlink_text_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
315
{
316
    unlink_lru (node, &text_mcb->lru, &text_mcb->mru);
Per Cederqvist's avatar
Per Cederqvist committed
317 318
}

319 320 321 322 323
static void
unlink_conf_lru (Cache_node *node)
{
    unlink_lru (node, &conf_mcb->lru, &conf_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
324 325

static void
326
unlink_pers_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
327
{
328 329
    unlink_lru (node, &pers_mcb->lru, &pers_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
330

331 332 333
static Cache_node *
get_conf_node(Conf_no conf_no)
{
334 335 336
    if ( conf_no >= next_free_num )
	return NULL;

337 338
    return get_cache_node(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
339

340 341 342
static Cache_node *
get_text_node(Text_no text_no)
{
343 344 345
    if ( text_no >= next_text_num )
	return NULL;

346
    return get_cache_node(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
}

	
/*
 * 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);
378 379 380 381 382

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

383 384
    LOGACC(lt_get_conf_type, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
385 386 387
    return small_conf_arr [ conf_no ]->type;
}

388 389 390 391 392 393 394 395 396
/*
 * Return number of conferences present.  (Actually, return a number
 * at least as large as the number of conferences present).
 */
extern Conf_no
cached_no_of_existing_conferences(void)
{
    return next_free_num;	/* This is too large, but who cares? */
}
Per Cederqvist's avatar
Per Cederqvist committed
397 398 399 400 401 402 403 404

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

void
mark_person_as_changed(Pers_no	pers_no)
{
405 406 407 408
    Cache_node *node;

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

413
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
414 415 416
    pers_set_mru( pers_no );
}

417 418 419 420 421 422 423 424 425 426 427
/*
 * 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
428 429 430 431

void
mark_conference_as_changed(Conf_no	conf_no)
{
432
    Cache_node *node;
433
    Conference *conf_c;
434 435 436

    node = get_conf_node (conf_no);

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

441
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
442
    conf_set_mru( conf_no );
443 444 445

    conf_c = (Conference *) node->ptr;

Per Cederqvist's avatar
Per Cederqvist committed
446
    small_conf_arr[ conf_no ]->highest_local_no
447 448 449 450
	= (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
451 452 453 454 455
}

void
mark_text_as_changed( Text_no text_no )
{
456 457 458 459
    Cache_node *node;

    node = get_text_node (text_no);

460
    TRACE2("Text %lu is changed.\n", text_no);
Per Cederqvist's avatar
Per Cederqvist committed
461
    if ( text_no < 1 || text_no >= next_text_num
462
	|| node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
463
    {
464
	restart_kom("mark_text_as_changed(%lu): nonexistent.\n", text_no);
Per Cederqvist's avatar
Per Cederqvist committed
465 466
    }

467 468
    node->s.dirty = 1;
    text_set_mru (text_no);
Per Cederqvist's avatar
Per Cederqvist committed
469 470 471 472 473 474 475 476 477 478 479 480
}    



/*
 * Person-related calls
 */


extern Success
cached_create_person( Pers_no person )
{
481 482
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
483 484 485 486 487 488 489 490
    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);
    }

491
    if ( get_pers_node(person) != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
492 493 494 495 496
    {
	restart_kom("cached_create_person(%d): Person existed.\n",
		    person);
    }

497 498 499 500 501
    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");
502

503 504 505
    node->ptr = alloc_person();
    node->s.dirty = 1;
    node->s.exists = 1;
Per Cederqvist's avatar
Per Cederqvist committed
506
    pers_set_mru( person );
507 508 509

    LOGACC(lt_create_pers, person);

Per Cederqvist's avatar
Per Cederqvist committed
510 511 512 513 514 515 516
    return OK;
}


extern Person *
cached_get_person_stat( Pers_no	person )
{
517 518
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
519 520 521 522
    TRACE2("cached_get_person_stat %d\n", person);

    if ( person == 0 )
    {
523
        err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
524 525 526
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
527

528 529
    if ( person >= next_free_num )
    {
530
        err_stat = person;
531 532 533 534
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

535 536
    node = get_pers_node (person);

537
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
538
    {
539
        err_stat = person;
Per Cederqvist's avatar
Per Cederqvist committed
540 541 542 543
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

544 545
    LOGACC(lt_pers_stat, person);

546
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
547 548
    {
	pers_set_mru( person );
549
	++pers_mcb->hits;
550
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
551 552
    }

553
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
554
    {
555
	node->ptr = copy_person (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
556
	pers_set_mru (person);
557
	++pers_mcb->hits;
558
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
559 560
    }

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

563
    ++pers_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
564
    pers_set_mru (person);
565
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
566 567 568 569 570 571
}


/*
 * Conference-related calls
 */
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587


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);
    }
}


588 589 590 591 592 593 594 595 596
static void
init_small_conf(Small_conf *sc)
{
    sc->name = EMPTY_STRING;
    init_conf_type(&sc->type);
    sc->highest_local_no = 0;
    sc->nice = param.default_nice;
}

597 598 599 600 601
static Small_conf *
alloc_small_conf(void)
{
    Small_conf *s;
    s = smalloc(sizeof(Small_conf));
602
    init_small_conf(s);
603 604 605 606 607 608
    ++no_of_allocated_small_confs;

    return s;
}


609 610 611 612 613 614
/*
 * 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
615 616 617 618
cached_create_conf (String  name)
{
    Conference * conf_c;
    Conf_no	 conf_no;
619
    Cache_node  *node;
Per Cederqvist's avatar
Per Cederqvist committed
620 621 622 623 624 625 626

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

    if ( next_free_num >= MAX_CONF )
    {
627
        err_stat = next_free_num;
Per Cederqvist's avatar
Per Cederqvist committed
628 629 630 631 632 633
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return 0;
    }
    
    conf_no = next_free_num++;

634 635 636 637 638 639 640 641 642
    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
643 644
    conf_set_mru(conf_no);    

645
    zero_init_cache_node (pers_mcb, conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
646 647 648 649 650 651
    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);

652
    TRACE2("Created conference number %d\n", conf_no);
653 654 655

    LOGACC(lt_create_conf, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
656 657 658 659 660 661 662
    return conf_no;
}


extern Success
cached_delete_conf( Conf_no	conf )
{
663 664
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
665 666
    if ( conf == 0 )
    {
667
        err_stat = conf;
Per Cederqvist's avatar
Per Cederqvist committed
668 669 670
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
671

672 673
    if ( conf >= next_free_num )
    {
674
        err_stat = conf;
675 676 677 678
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

679 680
    node = get_conf_node (conf);

681
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
682
    {
683
        err_stat = conf;
Per Cederqvist's avatar
Per Cederqvist committed
684 685 686 687
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

688 689 690
    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
691

692
    s_clear( &small_conf_arr[conf]->name );
693 694 695
    free_conference(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
696

697 698
    LOGACC(lt_delete_conf, conf);

699
    build_matching_info();
Per Cederqvist's avatar
Per Cederqvist committed
700 701 702 703 704 705
    return OK;
}

Success
cached_delete_person(Pers_no pers)
{
706 707
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
708 709
    if ( pers == 0 )
    {
710
        err_stat = pers;
Per Cederqvist's avatar
Per Cederqvist committed
711 712 713
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
714

715 716 717
    if ( pers >= next_free_num )
    {
	log("cached_delete_person(%lu): next_free_num == %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
718
	    (unsigned long)pers, (unsigned long)next_free_num);
719
        err_stat = pers;
720 721 722 723
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

724 725 726
    node = get_pers_node (pers);

    if ( pers >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
727
    {
Per Cederqvist's avatar
Per Cederqvist committed
728
	log("cached_delete_person(): attempt to delete void person.\n");
729
        err_stat = pers;
Per Cederqvist's avatar
Per Cederqvist committed
730 731 732 733
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

734
    if ( node->lock_cnt > 0 )
735
	log("cached_delete_pers(%lu): lock_cnt === %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
736
	    (unsigned long)pers, (unsigned long)node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
737

738 739
    LOGACC(lt_delete_pers, pers);

740 741 742
    free_person (node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
743 744 745 746 747 748
    return OK;
}

Success
cached_delete_text(Text_no text)
{
749 750
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
751 752
    if ( text == 0 )
    {
753
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
754 755 756
	kom_errno = KOM_TEXT_ZERO;
	return FAILURE;
    }
757 758 759 760 761

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
762
    {
763
	log("cached_delete_text(): attempt to delete void text %lu.\n", text);
764
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
765 766 767 768
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }

769
    if ( node->lock_cnt > 0 )
770
	log("cached_delete_text(%lu): lock_cnt === %d\n",
771
	    text, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
772 773


774 775 776
    free_text_stat(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
777

778 779 780
#ifdef LOGACCESSES
    if (garb_running)
    {
781
	LOGACC(lt_garb_text, text);
782 783
    }
    else
784
	LOGACC(lt_delete_text, text);
785
#endif
Per Cederqvist's avatar
Per Cederqvist committed
786 787 788 789 790
    return OK;
}


extern Conference *
791
cached_get_conf_stat (Conf_no   conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
792
{
793 794
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
795 796 797 798
    TRACE2("cached_get_conf_stat %d\n", conf_no);

    if ( conf_no == 0 )
    {
799
        err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
800 801 802
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
803 804 805 806

    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
807
    {
808
        err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
809 810 811 812
	kom_errno = KOM_UNDEF_CONF;
	return NULL;
    }

813 814
    LOGACC(lt_conf_stat, conf_no);

815
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
816
    {
817
	conf_set_mru (conf_no);
818
	++conf_mcb->hits;
819
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
820 821
    }

822
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
823
    {
824
	node->ptr = copy_conference (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
825
	conf_set_mru (conf_no);
826
	++conf_mcb->hits;
827
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
828 829
    }

830
    node->ptr = read_conference(file_a, node->pos, node->size);
831
    ++conf_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
832 833

    conf_set_mru (conf_no);
834
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
835 836 837 838 839 840 841 842
}

/*
 * Return TRUE if conf_no exists.
 */
Bool
cached_conf_exists(Conf_no conf_no)
{
843 844 845 846 847 848 849
    Cache_node *node;

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

    node = get_conf_node (conf_no);

850 851 852 853
    if (node != NULL && node->s.exists != 0)
      return TRUE;
    else
      return FALSE;
Per Cederqvist's avatar
Per Cederqvist committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
}

    
/*
 * Calls to handle texts
 */

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

870
    TRACE2("cached_get_text %lu\n", text);
Per Cederqvist's avatar
Per Cederqvist committed
871 872 873 874 875

    if ( (t_stat = cached_get_text_stat (text)) == NULL )
	return EMPTY_STRING;
    else
    {
876
	LOGACC(lt_text_mass, text);
Per Cederqvist's avatar
Per Cederqvist committed
877 878 879 880 881 882 883
	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 )
	{
884
	    log("WARNING: cached_get_text: premature end on text %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
885 886 887 888 889 890 891 892 893 894 895 896
		text);
	    return EMPTY_STRING;
	}
		    
	return the_string;
    }
}


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

899
    TRACE2("cached_get_text_stat(%lu);  next_text_num == ", text);
Per Cederqvist's avatar
Per Cederqvist committed
900 901 902 903
    TRACE2("%d\n", next_text_num);

    if ( text == 0 )
    {
904
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
905 906 907
	kom_errno = KOM_TEXT_ZERO;
	return NULL;
    }
908 909 910 911

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
912 913
    {
	TRACE1("cached_get_text_stat: no such text.\n");
914
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
915 916 917 918
	kom_errno = KOM_NO_SUCH_TEXT;
	return NULL;
    }

919 920
    LOGACC(lt_text_stat, text);

921
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
922 923 924
    {
	TRACE1("Found in ptr.\n");
	text_set_mru( text );
925
	++text_mcb->hits;
926
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
927 928
    }

929
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
930 931
    {
	TRACE1("Found in snap_shot\n");
932
	node->ptr = copy_text_stat(node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
933
	text_set_mru (text);
934
	++text_mcb->hits;
935
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
936 937 938
    }

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

    text_set_mru (text);
942
    ++text_mcb->misses;
943
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
944 945 946 947 948 949
}



/*
 * The text is set up with an empty misc-field. The misc field is
950
 * then initialized by create_text.
Per Cederqvist's avatar
Per Cederqvist committed
951 952 953
 */

extern Text_no
954
cached_create_text(const String message)
Per Cederqvist's avatar
Per Cederqvist committed
955 956
{
    Text_no tno;
957
    Cache_node *node;
958
    long file_pos;
Per Cederqvist's avatar
Per Cederqvist committed
959 960 961

    tno = next_text_num++;

962
    TRACE2("cached_create_text (len=%lu)\n", message.len);
Per Cederqvist's avatar
Per Cederqvist committed
963 964 965

    if ( tno >= MAX_TEXT )
    {
966
        err_stat = tno;
Per Cederqvist's avatar
Per Cederqvist committed
967 968 969 970 971 972
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	next_text_num = MAX_TEXT;
	
	return 0;
    }

973 974 975 976 977 978 979 980 981 982 983 984
    if (fseek(text_file, 0, SEEK_END) != 0) 
    {
	log("ERROR: cannot seek to end of text_file: %s\n", strerror(errno));
	clearerr(text_file);
	return 0;
    }

    file_pos = ftell(text_file);

    if (fwrite(message.string, 1, message.len, text_file) != message.len)
    {
	if (errno != ENOSPC)
985
	    log("WARNING: cached_create_text: Couldn't write text %lu: %s\n",
986
		tno, strerror(errno));
987
        err_stat = 0;
988 989 990 991 992 993 994 995
	kom_errno = KOM_TEMPFAIL;
	clearerr(text_file);
	return 0;
    }

    if (fflush(text_file) != 0)
    {
	if (errno != ENOSPC)
996
	    log("WARNING: cached_create_text: Couldn't fflush text %lu: %s\n",
997
		tno, strerror(errno));
998
        err_stat = 0;
999 1000 1001 1002 1003 1004 1005 1006
	kom_errno = KOM_TEMPFAIL;
	clearerr(text_file);
	return 0;
    }

    if (fsync(fileno(text_file)) != 0)
    {
	if (errno != ENOSPC)
1007
	    log("WARNING: cached_create_text: Couldn't fsync text %lu: %s\n",
1008
		tno, strerror(errno));
1009
        err_stat = 0;
1010 1011 1012 1013 1014
	kom_errno = KOM_TEMPFAIL;
	clearerr(text_file);
	return 0;
    }

1015 1016
    create_cache_node(text_mcb, tno);
    node = get_text_node (tno);
Per Cederqvist's avatar
Per Cederqvist committed
1017

1018
    if ( node == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1019
	restart_kom("cached_create_text(): couldn't create cache-node.\n");
1020 1021 1022 1023 1024 1025 1026 1027 1028
    
    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;
1029
    ((Text_stat *)node->ptr)->file_pos = file_pos;
Per Cederqvist's avatar
Per Cederqvist committed
1030 1031 1032 1033

    text_set_mru( tno );


1034
    LOGACC(lt_create_text, tno);
1035
    TRACE2("cached_create_text -> %lu\n", tno);
Per Cederqvist's avatar
Per Cederqvist committed
1036 1037 1038 1039 1040
    
    return tno;
}


1041
EXPORT Text_no
Per Cederqvist's avatar
Per Cederqvist committed
1042 1043
traverse_text(Text_no seed)
{
1044 1045
    Cache_node *node;