1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6
7
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22 ----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39 #include "test.h"
40
41 static void
flush(CACHEFILE cf,int UU (fd),CACHEKEY key,void * v,void ** UU (dd),void * extraargs,PAIR_ATTR size,PAIR_ATTR * new_size,bool write_me,bool keep_me,bool for_checkpoint,bool UU (is_clone))42 flush (CACHEFILE cf __attribute__((__unused__)),
43 int UU(fd),
44 CACHEKEY key __attribute__((__unused__)),
45 void *v __attribute__((__unused__)),
46 void** UU(dd),
47 void *extraargs __attribute__((__unused__)),
48 PAIR_ATTR size __attribute__((__unused__)),
49 PAIR_ATTR* new_size __attribute__((__unused__)),
50 bool write_me __attribute__((__unused__)),
51 bool keep_me __attribute__((__unused__)),
52 bool for_checkpoint __attribute__((__unused__)),
53 bool UU(is_clone)
54 ) {
55 assert((long) key.b == size.size);
56 if (!keep_me) toku_free(v);
57 }
58
59 static int
fetch(CACHEFILE UU (cf),PAIR UU (p),int UU (fd),CACHEKEY key,uint32_t UU (hash),void ** vptr,void ** UU (dd),PAIR_ATTR * sizep,int * dirtyp,void * UU (extra))60 fetch (
61 CACHEFILE UU(cf),
62 PAIR UU(p),
63 int UU(fd),
64 CACHEKEY key,
65 uint32_t UU(hash),
66 void **vptr,
67 void** UU(dd),
68 PAIR_ATTR *sizep,
69 int *dirtyp,
70 void *UU(extra)
71 )
72 {
73 *sizep = make_pair_attr((long) key.b);
74 *vptr = toku_malloc(sizep->size);
75 *dirtyp = 0;
76 return 0;
77 }
78
79 static void
cachetable_getandpin_test(int n)80 cachetable_getandpin_test (int n) {
81 const int test_limit = 1024*1024;
82 int r;
83 CACHETABLE ct;
84 toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
85 const char *fname1 = TOKU_TEST_FILENAME;
86 unlink(fname1);
87 CACHEFILE f1;
88 r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
89
90 int i;
91
92 // test get_and_pin size
93 for (i=1; i<=n; i++) {
94 uint32_t hi;
95 hi = toku_cachetable_hash(f1, make_blocknum(i));
96 void *v;
97 CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
98 wc.flush_callback = flush;
99 r = toku_cachetable_get_and_pin(f1, make_blocknum(i), hi, &v, wc, fetch, def_pf_req_callback, def_pf_callback, true, 0);
100 assert(r == 0);
101 PAIR_ATTR attr;
102 r = toku_cachetable_get_attr(f1, make_blocknum(i), hi, &attr);
103 assert(r == 0 && attr.size == i);
104
105 r = toku_test_cachetable_unpin(f1, make_blocknum(i), hi, CACHETABLE_CLEAN, make_pair_attr(i));
106 assert(r == 0);
107 }
108 toku_cachetable_verify(ct);
109
110 toku_cachefile_close(&f1, false, ZERO_LSN);
111 toku_cachetable_close(&ct);
112 }
113
114 int
test_main(int argc,const char * argv[])115 test_main(int argc, const char *argv[]) {
116 default_parse_args(argc, argv);
117 cachetable_getandpin_test(8);
118 return 0;
119 }
120