|
|
1.1 ! root 1: #include "cbt.h" ! 2: extern char *malloc(); ! 3: ! 4: bfile *bt; ! 5: mbuf *key; ! 6: char *state; ! 7: #define IN 1 ! 8: #define OUT 2 ! 9: int cnt, keysz; ! 10: char *buf; ! 11: char cmdbuf[128]; ! 12: mbuf rec = {"", 0}; ! 13: extern char *outkey(); ! 14: char *filename; ! 15: ! 16: main(argc, argv) ! 17: char **argv; ! 18: { int i; ! 19: if(argc != 3) ! 20: error("usage: file key-cnt"); ! 21: bt = bopen(filename = argv[1], 2); ! 22: if(bt == 0) ! 23: error("bopen"); ! 24: sprintf(cmdbuf, "btdiag %s", argv[1]); ! 25: cnt = atoi(argv[2]); ! 26: printf("cnt = %d\n", cnt); ! 27: key = (mbuf *)malloc(cnt * sizeof(mbuf)); ! 28: keysz = NDSZ/5; ! 29: if(keysz > MAXKLEN) ! 30: keysz = MAXKLEN; ! 31: printf("keysz %d\n", keysz); ! 32: buf = malloc((keysz + 1) * cnt); ! 33: state = malloc(cnt); ! 34: if(buf == 0 || state == 0) ! 35: error("key malloc"); ! 36: for(i = 0; i < cnt; i++) ! 37: state[i] = OUT; ! 38: for(i = 0; i < cnt; i++) ! 39: key[i].mlen = keysz; ! 40: for(i = 0; i < cnt; i++) ! 41: key[i].mdata = buf + i * (keysz + 1); ! 42: srand(0); ! 43: for(i = 0; i < cnt * (keysz + 1); i++) ! 44: buf[i] = rand() % 94 + ' ' + 1; ! 45: for(i = 0; i < cnt; i++) ! 46: buf[i * (keysz + 1) + keysz] = 0; ! 47: doit(); ! 48: printf("sbrk %d\n", sbrk(0)); ! 49: exit(0); ! 50: } ! 51: ! 52: doit() ! 53: { ! 54: allin(); ! 55: test("allin ok\n"); ! 56: checkseek(); ! 57: out(500); ! 58: test("out 500 ok\n"); ! 59: checkseek(); ! 60: in(500); ! 61: test("in 500 ok\n"); ! 62: checkseek(); ! 63: out(700); ! 64: test("out 700 ok\n"); ! 65: checkseek(); ! 66: in(700); ! 67: test("in 700 ok\n"); ! 68: checkseek(); ! 69: out(1000); ! 70: test("out 1000 ok\n"); ! 71: checkseek(); ! 72: in(100); ! 73: test("in 100 ok\n"); ! 74: checkseek(); ! 75: in(200); ! 76: test("in 200 ok\n"); ! 77: checkseek(); ! 78: in(300); ! 79: test("in 300 ok\n"); ! 80: checkseek(); ! 81: } ! 82: ! 83: test(s) ! 84: char *s; ! 85: { int n; ! 86: bclose(bt); ! 87: n = system(cmdbuf); ! 88: if(n) { ! 89: printf("%s returned %d\n", cmdbuf, n); ! 90: exit(1); ! 91: } ! 92: else ! 93: printf(s); ! 94: bt = bopen(filename, 2); ! 95: if(bt == NULL) ! 96: error(filename); ! 97: } ! 98: ! 99: error(s) ! 100: char *s; ! 101: { ! 102: perror(s); ! 103: exit(1); ! 104: } ! 105: ! 106: allin() ! 107: { int i; ! 108: for(i = 0; i < cnt; i++) { ! 109: if(state[i] == IN) ! 110: continue; ! 111: if(bwrite(bt, key[i], rec) == EOF) ! 112: printf("write error %d on key %d\n", errno, i); ! 113: else ! 114: state[i] = IN; ! 115: } ! 116: } ! 117: ! 118: in(n) ! 119: { int i; ! 120: for(i = 0; i < cnt; i++) { ! 121: if(state[i] == IN || (rand() % 1007) >= n) ! 122: continue; ! 123: else if(bwrite(bt, key[i], rec) == EOF) ! 124: printf("write error %d on key %d\n", errno, i); ! 125: else ! 126: state[i] = IN; ! 127: } ! 128: } ! 129: ! 130: out(n) ! 131: { int i, count = 0; ! 132: for(i = 0; i < cnt; i++) { ! 133: if(state[i] == OUT || (rand() % 1007) >= n) ! 134: continue; ! 135: if(bdelete(bt, key[i], rec) != 1) { ! 136: printf("bdelete error %d on key %s\n", errno, outkey(i)); ! 137: printf("out(%d) deleted %d\n", n, count); ! 138: bflush(bt); ! 139: exit(1); ! 140: } ! 141: else if(bt->fatal) ! 142: printf("set fatal flag, bdelete key %d %s err %d\n", i, outkey(i), errno); ! 143: else { ! 144: count++; ! 145: state[i] = OUT; ! 146: } ! 147: } ! 148: } ! 149: ! 150: checkseek() ! 151: { int i, j, count = 0; ! 152: mbuf x; ! 153: printf("\tcheckseek"); ! 154: for(i = 0; i < cnt; i++) { ! 155: if(state[i] == OUT) ! 156: continue; ! 157: if(bseek(bt, key[i]) != FOUND) { ! 158: printf("sought key %s, not found\n", key[i].mdata); ! 159: continue; ! 160: } ! 161: x = bkey(bt); ! 162: for(j = 0; j < keysz; j++) ! 163: if(key[i].mdata[j] != x.mdata[j]) { ! 164: printf("bkey mismatch at key %d\n", i); ! 165: break; ! 166: } ! 167: count++; ! 168: } ! 169: printf(" saw %d\n", count); ! 170: } ! 171: ! 172: char * ! 173: outkey(n) ! 174: { static char kb[MAXKLEN + 1]; ! 175: int i; ! 176: strncpy(kb, key[n].mdata, keysz); ! 177: return(kb); ! 178: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.