simple-cache.c 48.9 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
David Byers's avatar
David Byers committed
2
 * $Id: simple-cache.c,v 0.53 1996/02/23 16:00:29 byers Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1992, 1993, 1994, 1995  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 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.
 */

David Byers's avatar
David Byers committed
36
static char *rcsid = "$Id: simple-cache.c,v 0.53 1996/02/23 16:00:29 byers Exp $";
37 38
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
39

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

57
#include "ldifftime.h"
Per Cederqvist's avatar
Per Cederqvist committed
58 59
#include "exp.h"
#include "misc-types.h"
60
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#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"
#include "connections.h"
#include "send-async.h"
77
#include "param.h"
Per Cederqvist's avatar
Per Cederqvist committed
78
#include "config.h"
David Byers's avatar
David Byers committed
79
#include "admin.h"
Per Cederqvist's avatar
Per Cederqvist committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

/*
 * 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 ];
95 96
static Cache_node_mcb	* pers_mcb;
static Cache_node_mcb 	* conf_mcb;
Per Cederqvist's avatar
Per Cederqvist committed
97 98
static int		  next_free_num = 1;

99 100
static Cache_node_mcb 	* text_mcb;
static int		  next_text_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
101 102 103 104 105 106 107

/*
 * 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
108
EXPORT Conf_no       *conf_table  = NULL; /* Used in conference.c. */
Per Cederqvist's avatar
Per Cederqvist committed
109 110 111 112 113 114 115 116 117 118


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 {
119
    sync_idle,
Per Cederqvist's avatar
Per Cederqvist committed
120 121 122 123 124 125 126 127
    sync_save_conf,
    sync_save_pers,
    sync_save_text,
    sync_error,
    sync_wait,
    sync_ready
} sync_state;

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/* 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
 */

Per Cederqvist's avatar
Per Cederqvist committed
143 144 145 146 147 148 149
static long sync_next;

static Conf_no highest_conf_no;
static Text_no highest_text_no;

BUGDECL;

150 151 152 153 154 155 156 157 158 159 160 161 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
/* 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)
{
190 191
    extern int putw(int, FILE *);

192 193 194 195 196 197 198 199 200 201 202
    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
203 204 205 206 207 208 209

/* 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)

David Byers's avatar
David Byers committed
210 211
#define TIMESTAMP_FORMAT "%y %m %d %H %M %S"

Per Cederqvist's avatar
Per Cederqvist committed
212 213 214 215 216 217 218

static Person *
read_person(FILE *fp,
	    long pos,
	    long size)
{
    Person *p;
David Byers's avatar
David Byers committed
219
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
220 221 222

    p = alloc_person();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
David Byers's avatar
David Byers committed
223
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    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
241
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
242 243 244

    c = alloc_conference();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
David Byers's avatar
David Byers committed
245
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    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
262
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
263 264 265

    t = alloc_text_stat();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
David Byers's avatar
David Byers committed
266
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
267 268 269 270 271 272 273 274 275 276
    if ( fparse_text_stat(fp, t) != OK )
    {
	free_text_stat(t);
	return NULL;
    }
    else
	return t;
}

static void
277
pers_set_mru(Pers_no pers_no)
Per Cederqvist's avatar
Per Cederqvist committed
278
{
279
    set_mru(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
280 281 282
}

static void
283
text_set_mru(Text_no text_no)
Per Cederqvist's avatar
Per Cederqvist committed
284
{
285
    set_mru(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
286 287 288
}

static void
289
conf_set_mru(Conf_no conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
290
{
291 292
    set_mru(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
293

294 295 296
static Cache_node *
get_pers_node(Pers_no pers_no)
{
297 298 299
    if ( pers_no >= next_free_num )
	return NULL;

300
    return get_cache_node(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
301 302 303
}

static void
304
unlink_text_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
305
{
306
    unlink_lru (node, &text_mcb->lru, &text_mcb->mru);
Per Cederqvist's avatar
Per Cederqvist committed
307 308
}

309 310 311 312 313
static void
unlink_conf_lru (Cache_node *node)
{
    unlink_lru (node, &conf_mcb->lru, &conf_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
314 315

static void
316
unlink_pers_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
317
{
318 319
    unlink_lru (node, &pers_mcb->lru, &pers_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
320

321 322 323
static Cache_node *
get_conf_node(Conf_no conf_no)
{
324 325 326
    if ( conf_no >= next_free_num )
	return NULL;

327 328
    return get_cache_node(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
329

330 331 332
static Cache_node *
get_text_node(Text_no text_no)
{
333 334 335
    if ( text_no >= next_text_num )
	return NULL;

336
    return get_cache_node(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
}

	
/*
 * 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);
368 369 370 371 372

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

373 374
    LOGACC(lt_get_conf_type, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
375 376 377
    return small_conf_arr [ conf_no ]->type;
}

378 379 380 381 382 383 384 385 386
/*
 * 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
387 388 389 390 391 392 393 394

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

void
mark_person_as_changed(Pers_no	pers_no)
{
395 396 397 398
    Cache_node *node;

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

403
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
404 405 406
    pers_set_mru( pers_no );
}

407 408 409 410 411 412 413 414 415 416 417
/*
 * 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
418 419 420 421

void
mark_conference_as_changed(Conf_no	conf_no)
{
422
    Cache_node *node;
423
    Conference *conf_c;
424 425 426

    node = get_conf_node (conf_no);

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

431
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
432
    conf_set_mru( conf_no );
433 434 435

    conf_c = (Conference *) node->ptr;

Per Cederqvist's avatar
Per Cederqvist committed
436
    small_conf_arr[ conf_no ]->highest_local_no
437 438 439 440
	= (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
441 442 443 444 445
}

void
mark_text_as_changed( Text_no text_no )
{
446 447 448 449
    Cache_node *node;

    node = get_text_node (text_no);

450
    TRACE2("Text %lu is changed.\n", text_no);
Per Cederqvist's avatar
Per Cederqvist committed
451
    if ( text_no < 1 || text_no >= next_text_num
452
	|| node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
453 454 455 456
    {
	restart_kom("mark_text_as_changed(%d): nonexistent.\n", text_no);
    }

457 458
    node->s.dirty = 1;
    text_set_mru (text_no);
Per Cederqvist's avatar
Per Cederqvist committed
459 460 461 462 463 464 465 466 467 468 469 470
}    



/*
 * Person-related calls
 */


extern Success
cached_create_person( Pers_no person )
{
471 472
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
473 474 475 476 477 478 479 480
    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);
    }

481
    if ( get_pers_node(person) != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
482 483 484 485 486
    {
	restart_kom("cached_create_person(%d): Person existed.\n",
		    person);
    }

487 488 489 490 491
    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");
492

493 494 495
    node->ptr = alloc_person();
    node->s.dirty = 1;
    node->s.exists = 1;
Per Cederqvist's avatar
Per Cederqvist committed
496
    pers_set_mru( person );
497 498 499

    LOGACC(lt_create_pers, person);

Per Cederqvist's avatar
Per Cederqvist committed
500 501 502 503 504 505 506
    return OK;
}


extern Person *
cached_get_person_stat( Pers_no	person )
{
507 508
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
509 510 511 512 513 514 515
    TRACE2("cached_get_person_stat %d\n", person);

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

517 518 519 520 521 522
    if ( person >= next_free_num )
    {
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

523 524
    node = get_pers_node (person);

525
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
526 527 528 529 530
    {
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

531 532
    LOGACC(lt_pers_stat, person);

533
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
534 535
    {
	pers_set_mru( person );
536
	++pers_mcb->hits;
537
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
538 539
    }

540
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
541
    {
542
	node->ptr = copy_person (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
543
	pers_set_mru (person);
544
	++pers_mcb->hits;
545
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
546 547
    }

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

550
    ++pers_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
551
    pers_set_mru (person);
552
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
553 554 555 556 557 558
}


/*
 * Conference-related calls
 */
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574


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


575 576 577 578 579 580 581 582 583
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;
}

584 585 586 587 588
static Small_conf *
alloc_small_conf(void)
{
    Small_conf *s;
    s = smalloc(sizeof(Small_conf));
589
    init_small_conf(s);
590 591 592 593 594 595
    ++no_of_allocated_small_confs;

    return s;
}


596 597 598 599 600 601
/*
 * 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
602 603 604 605
cached_create_conf (String  name)
{
    Conference * conf_c;
    Conf_no	 conf_no;
606
    Cache_node  *node;
Per Cederqvist's avatar
Per Cederqvist committed
607 608 609 610 611 612 613 614 615 616 617 618 619

    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++;

620 621 622 623 624 625 626 627 628
    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
629 630
    conf_set_mru(conf_no);    

631
    zero_init_cache_node (pers_mcb, conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
632 633 634 635 636 637
    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);

638
    TRACE2("Created conference number %d\n", conf_no);
639 640 641

    LOGACC(lt_create_conf, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
642 643 644 645 646 647 648
    return conf_no;
}


extern Success
cached_delete_conf( Conf_no	conf )
{
649 650
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
651 652 653 654 655
    if ( conf == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
656

657 658 659 660 661 662
    if ( conf >= next_free_num )
    {
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

663 664
    node = get_conf_node (conf);

665
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
666 667 668 669 670
    {
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

671 672 673
    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
674

675 676 677
    free_conference(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
678

679 680
    LOGACC(lt_delete_conf, conf);

Per Cederqvist's avatar
Per Cederqvist committed
681 682 683 684 685 686
    return OK;
}

Success
cached_delete_person(Pers_no pers)
{
687 688
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
689 690 691 692 693
    if ( pers == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
694

695 696 697
    if ( pers >= next_free_num )
    {
	log("cached_delete_person(%lu): next_free_num == %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
698
	    (unsigned long)pers, (unsigned long)next_free_num);
699 700 701 702
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

703 704 705
    node = get_pers_node (pers);

    if ( pers >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
706
    {
Per Cederqvist's avatar
Per Cederqvist committed
707
	log("cached_delete_person(): attempt to delete void person.\n");
Per Cederqvist's avatar
Per Cederqvist committed
708 709 710 711
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

712
    if ( node->lock_cnt > 0 )
713
	log("cached_delete_pers(%lu): lock_cnt === %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
714
	    (unsigned long)pers, (unsigned long)node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
715

716 717
    LOGACC(lt_delete_pers, pers);

718 719 720
    free_person (node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
721 722 723 724 725 726
    return OK;
}

Success
cached_delete_text(Text_no text)
{
727 728
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
729 730 731 732 733
    if ( text == 0 )
    {
	kom_errno = KOM_TEXT_ZERO;
	return FAILURE;
    }
734 735 736 737 738

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
739
    {
Per Cederqvist's avatar
Per Cederqvist committed
740
	log("cached_delete_text(): attempt to delete void text %d.\n", text);
Per Cederqvist's avatar
Per Cederqvist committed
741 742 743 744
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }

745
    if ( node->lock_cnt > 0 )
Per Cederqvist's avatar
Per Cederqvist committed
746
	log("cached_delete_text(%d): lock_cnt === %d\n",
747
	    text, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
748 749


750 751 752
    free_text_stat(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
753

754 755 756
#ifdef LOGACCESSES
    if (garb_running)
    {
757
	LOGACC(lt_garb_text, text);
758 759
    }
    else
760
	LOGACC(lt_delete_text, text);
761
#endif
Per Cederqvist's avatar
Per Cederqvist committed
762 763 764 765 766
    return OK;
}


extern Conference *
767
cached_get_conf_stat (Conf_no   conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
768
{
769 770
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
771 772 773 774 775 776 777
    TRACE2("cached_get_conf_stat %d\n", conf_no);

    if ( conf_no == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
778 779 780 781

    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
782 783 784 785 786
    {
	kom_errno = KOM_UNDEF_CONF;
	return NULL;
    }

787 788
    LOGACC(lt_conf_stat, conf_no);

789
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
790
    {
791
	conf_set_mru (conf_no);
792
	++conf_mcb->hits;
793
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
794 795
    }

796
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
797
    {
798
	node->ptr = copy_conference (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
799
	conf_set_mru (conf_no);
800
	++conf_mcb->hits;
801
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
802 803
    }

804
    node->ptr = read_conference(file_a, node->pos, node->size);
805
    ++conf_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
806 807

    conf_set_mru (conf_no);
808
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
809 810 811 812 813 814 815 816
}

/*
 * Return TRUE if conf_no exists.
 */
Bool
cached_conf_exists(Conf_no conf_no)
{
817 818 819 820 821 822 823
    Cache_node *node;

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

    node = get_conf_node (conf_no);

824 825 826 827
    if (node != NULL && node->s.exists != 0)
      return TRUE;
    else
      return FALSE;
Per Cederqvist's avatar
Per Cederqvist committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
}

    
/*
 * Calls to handle texts
 */

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

844
    TRACE2("cached_get_text %lu\n", text);
Per Cederqvist's avatar
Per Cederqvist committed
845 846 847 848 849

    if ( (t_stat = cached_get_text_stat (text)) == NULL )
	return EMPTY_STRING;
    else
    {
850
	LOGACC(lt_text_mass, text);
Per Cederqvist's avatar
Per Cederqvist committed
851 852 853 854 855 856 857
	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 )
	{
Per Cederqvist's avatar
Per Cederqvist committed
858
	    log("WARNING: cached_get_text: premature end on text %d\n",
Per Cederqvist's avatar
Per Cederqvist committed
859 860 861 862 863 864 865 866 867 868 869 870
		text);
	    return EMPTY_STRING;
	}
		    
	return the_string;
    }
}


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

873
    TRACE2("cached_get_text_stat(%lu);  next_text_num == ", text);
Per Cederqvist's avatar
Per Cederqvist committed
874 875 876 877 878 879 880
    TRACE2("%d\n", next_text_num);

    if ( text == 0 )
    {
	kom_errno = KOM_TEXT_ZERO;
	return NULL;
    }
881 882 883 884

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
885 886 887 888 889 890
    {
	TRACE1("cached_get_text_stat: no such text.\n");
	kom_errno = KOM_NO_SUCH_TEXT;
	return NULL;
    }

891 892
    LOGACC(lt_text_stat, text);

893
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
894 895 896
    {
	TRACE1("Found in ptr.\n");
	text_set_mru( text );
897
	++text_mcb->hits;
898
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
899 900
    }

901
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
902 903
    {
	TRACE1("Found in snap_shot\n");
904
	node->ptr = copy_text_stat(node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
905
	text_set_mru (text);
906
	++text_mcb->hits;
907
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
908 909 910
    }

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

    text_set_mru (text);
914
    ++text_mcb->misses;
915
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
916 917 918 919 920 921
}



/*
 * The text is set up with an empty misc-field. The misc field is
922
 * then initialized by create_text.
Per Cederqvist's avatar
Per Cederqvist committed
923 924 925
 */

extern Text_no
926
cached_create_text(const String message)
Per Cederqvist's avatar
Per Cederqvist committed
927 928
{
    Text_no tno;
929
    Cache_node *node;
930
    long file_pos;
Per Cederqvist's avatar
Per Cederqvist committed
931 932 933

    tno = next_text_num++;

934
    TRACE2("cached_create_text (len=%lu)\n", message.len);
Per Cederqvist's avatar
Per Cederqvist committed
935 936 937 938 939 940 941 942 943

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

944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
    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)
	    log("WARNING: cached_create_text: Couldn't write text %d: %s\n",
		tno, strerror(errno));
	kom_errno = KOM_TEMPFAIL;
	clearerr(text_file);
	return 0;
    }

    if (fflush(text_file) != 0)
    {
	if (errno != ENOSPC)
	    log("WARNING: cached_create_text: Couldn't fflush text %d: %s\n",
		tno, strerror(errno));
	kom_errno = KOM_TEMPFAIL;
	clearerr(text_file);
	return 0;
    }

    if (fsync(fileno(text_file)) != 0)
    {
	if (errno != ENOSPC)
	    log("WARNING: cached_create_text: Couldn't fsync text %d: %s\n",
		tno, strerror(errno));
	kom_errno = KOM_TEMPFAIL;
	clearerr(text_file);
	return 0;
    }

983 984
    create_cache_node(text_mcb, tno);
    node = get_text_node (tno);
Per Cederqvist's avatar
Per Cederqvist committed
985

986
    if ( node == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
987
	restart_kom("cached_create_text(): couldn't create cache-node.\n");
988 989 990 991 992 993 994 995 996
    
    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;
997
    ((Text_stat *)node->ptr)->file_pos = file_pos;
Per Cederqvist's avatar
Per Cederqvist committed
998 999 1000 1001

    text_set_mru( tno );


1002
    LOGACC(lt_create_text, tno);
1003
    TRACE2("cached_create_text -> %lu\n", tno);
Per Cederqvist's avatar
Per Cederqvist committed
1004 1005 1006 1007 1008
    
    return tno;
}


1009
EXPORT Text_no
Per Cederqvist's avatar
Per Cederqvist committed
1010 1011
traverse_text(Text_no seed)
{
1012 1013
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
1014 1015
    seed++;
    
1016
    while ( seed < next_text_num )
Per Cederqvist's avatar
Per Cederqvist committed
1017
    {
1018 1019 1020 1021
	node = get_text_node (seed);
	if ( node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
1022 1023 1024 1025 1026 1027 1028 1029 1030
	seed++;
    }
    
    return (seed >= next_text_num) ? 0 : seed ;
}

Pers_no
traverse_person(Pers_no seed)
{
1031 1032
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
1033 1034
    seed++;

1035
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
1036
    {
1037 1038 1039 1040
	node = get_pers_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
1041 1042 1043 1044 1045 1046 1047 1048 1049
	seed++;
    }

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

Conf_no
traverse_conference(Conf_no seed)
{
1050 1051
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
1052 1053
    seed++;
    
1054
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
1055
    {
1056 1057 1058 1059
	node = get_conf_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	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;
}

1072 1073 1074 1075 1076
extern String
cached_get_name (Conf_no conf_no)
{
    return small_conf_arr [ conf_no ]->name;
}
Per Cederqvist's avatar
Per Cederqvist committed
1077 1078 1079 1080

extern Local_text_no
cached_get_highest_local_no (Conf_no conf_no)
{
1081 1082
    LOGACC(lt_get_highest, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
1083 1084 1085
    return small_conf_arr[ conf_no ]->highest_local_no;
}

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
extern Small_conf *
cached_get_small_conf_stat (Conf_no conf_no)
{
    TRACE2("cached_get_small_conf_stat %d\n", conf_no);

    if (conf_no == 0)
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
    
    if (conf_no >= next_free_num ||
	small_conf_arr[conf_no] == NULL)
    {
	kom_errno = KOM_UNDEF_CONF;
	return NULL;
    }
    return small_conf_arr[conf_no];
}

Per Cederqvist's avatar
Per Cederqvist committed
1106 1107 1108 1109
/* Lock a person struct in memory. Increase a referenc count. */
void
cached_lock_person(Pers_no pers_no)
{
1110 1111
    Cache_node *node;

1112 1113
    LOGACC(lt_lock_pers, pers_no);

1114 1115 1116
    node = get_pers_node(pers_no);
    
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
1117 1118
	restart_kom("cached_lock_person(%d): nonexistent.\n", pers_no);

1119
    if ( node->ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1120
    {
1121
	Person *pers_stat_ptr;
Per Cederqvist's avatar
Per Cederqvist committed
1122

1123
	pers_stat_ptr = cached_get_person_stat( pers_no );
Per Cederqvist's avatar
Per Cederqvist committed
1124

1125
	if ( pers_stat_ptr == NULL )