1: # include "../ingres.h" 2: # include "../aux.h" 3: # include "../symbol.h" 4: # include "../access.h" 5: # include "../batch.h" 6: 7: getbatch(loc, count) 8: char *loc; 9: int count; 10: { 11: register char *c; 12: register int cnt, size; 13: int i; 14: 15: cnt = count; 16: # ifdef xZTR1 17: if (tTf(15, 15)) 18: printf("getbatch:%d (%d)\n", cnt, Batch_cnt); 19: # endif 20: c = loc; 21: 22: while (cnt) 23: { 24: /* see if there is anything in the buffer */ 25: if (Batch_cnt == BATCHSIZE) 26: if ((i = readbatch()) < cnt) 27: syserr("getbatch:can't read enough %d %d", i, cnt); 28: if (cnt <= BATCHSIZE - Batch_cnt) 29: size = cnt; 30: else 31: size = BATCHSIZE - Batch_cnt; 32: bmove(&Batchbuf.bbuf[Batch_cnt], c, size); 33: Batch_cnt += size; 34: cnt -= size; 35: c += size; 36: /* flush the buffer if full */ 37: if (Batch_cnt == BATCHSIZE) 38: batchflush(); /* re-write buffer if necessary */ 39: } 40: return (0); 41: } 42: putbatch(cp, count) 43: char *cp; 44: int count; 45: { 46: register char *c; 47: register int size, cnt; 48: int i; 49: 50: cnt = count; 51: c = cp; 52: # ifdef xZTR1 53: if (tTf(15, 2)) 54: printf("putbatch:%d\n", cnt); 55: # endif 56: 57: while (cnt) 58: { 59: Batch_dirty = TRUE; /* mark this buffer as dirty */ 60: if (cnt + Batch_cnt > BATCHSIZE) 61: size = BATCHSIZE - Batch_cnt; 62: else 63: size = cnt; 64: bmove(c, &Batchbuf.bbuf[Batch_cnt], size); 65: c += size; 66: Batch_cnt += size; 67: cnt -= size; 68: if (Batch_cnt == BATCHSIZE) 69: { 70: batchflush(); 71: /* is there is more to write, must read ahead first */ 72: if (cnt) 73: if ((i = readbatch()) < cnt) 74: syserr("putbatch:rd too small %d", i); 75: } 76: } 77: } 78: 79: 80: readbatch() 81: { 82: 83: if ((Batch_lread = read(Batch_fp, &Batchbuf, BATCHSIZE+IDSIZE)) < 0) 84: syserr("readbatch:can't read %d %d", Batch_lread, Batch_fp); 85: Batch_cnt = 0; 86: # ifdef xZTR1 87: if (tTf(15, 10)) 88: printf("read %d bytes from batch\n", Batch_lread); 89: # endif 90: /* check file-id */ 91: if (!sequal(Fileset, Batchbuf.file_id)) 92: syserr("readbatch:bad id '%s' '%.20s' %d", Fileset, Batchbuf.file_id, Batch_lread); 93: return (Batch_lread); 94: } 95: 96: 97: batchflush() 98: { 99: register int i; 100: long ltemp; 101: 102: if (Batch_cnt && Batch_dirty) 103: { 104: # ifdef xZTR1 105: if (tTf(15, 5)) 106: printf("flush:backing up %d\n", Batch_lread); 107: # endif 108: ltemp = -Batch_lread; 109: if (lseek(Batch_fp, ltemp, 1) < 0) 110: syserr("batchflush:can't seek %d", Batch_lread); 111: # ifdef xZTR1 112: if (tTf(15, 4)) 113: printf("flushing %d\n", Batch_cnt + IDSIZE); 114: # endif 115: if ((i = write(Batch_fp, &Batchbuf, Batch_cnt + IDSIZE)) != Batch_cnt + IDSIZE) 116: syserr("batchflush:can't write %d", i); 117: Batch_dirty = FALSE; 118: } 119: }