1#!/usr/bin/env python
2#
3# Public Domain 2014-2018 MongoDB, Inc.
4# Public Domain 2008-2014 WiredTiger, Inc.
5#
6# This is free and unencumbered software released into the public domain.
7#
8# Anyone is free to copy, modify, publish, use, compile, sell, or
9# distribute this software, either in source code form or as a compiled
10# binary, for any purpose, commercial or non-commercial, and by any
11# means.
12#
13# In jurisdictions that recognize copyright laws, the author or authors
14# of this software dedicate any and all copyright interest in the
15# software to the public domain. We make this dedication for the benefit
16# of the public at large and to the detriment of our heirs and
17# successors. We intend this dedication to be an overt act of
18# relinquishment in perpetuity of all present and future rights to this
19# software under copyright law.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27# OTHER DEALINGS IN THE SOFTWARE.
28
29import wiredtiger, wttest
30from wtscenario import make_scenarios
31
32# test_alter01.py
33#    Smoke-test the session alter operations.
34class test_alter01(wttest.WiredTigerTestCase):
35    name = "alter01"
36    entries = 100
37    # Settings for access_pattern_hint
38    types = [
39        ('file', dict(uri='file:', use_cg=False, use_index=False)),
40        ('lsm', dict(uri='lsm:', use_cg=False, use_index=False)),
41        ('table-cg', dict(uri='table:', use_cg=True, use_index=False)),
42        ('table-index', dict(uri='table:', use_cg=False, use_index=True)),
43        ('table-simple', dict(uri='table:', use_cg=False, use_index=False)),
44    ]
45    hints = [
46        ('default', dict(acreate='')),
47        ('none', dict(acreate='none')),
48        ('random', dict(acreate='random')),
49        ('sequential', dict(acreate='sequential')),
50    ]
51    access_alter=('', 'none', 'random', 'sequential')
52    # Settings for cache_resident
53    resid = [
54        ('default', dict(ccreate='')),
55        ('false', dict(ccreate='false')),
56        ('true', dict(ccreate='true')),
57    ]
58    reopen = [
59        ('no-reopen', dict(reopen=False)),
60        ('reopen', dict(reopen=True)),
61    ]
62    cache_alter=('', 'false', 'true')
63    scenarios = make_scenarios(types, hints, resid, reopen)
64
65    def verify_metadata(self, metastr):
66        if metastr == '':
67            return
68        cursor = self.session.open_cursor('metadata:', None, None)
69        #
70        # Walk through all the metadata looking for the entries that are
71        # the file URIs for components of the table.
72        #
73        found = False
74        while True:
75            ret = cursor.next()
76            if ret != 0:
77                break
78            key = cursor.get_key()
79            check_meta = ((key.find("lsm:") != -1 or key.find("file:") != -1) \
80                and key.find(self.name) != -1)
81            if check_meta:
82                value = cursor[key]
83                found = True
84                self.assertTrue(value.find(metastr) != -1)
85        cursor.close()
86        self.assertTrue(found == True)
87
88    # Alter: Change the access pattern hint after creation
89    def test_alter01_access(self):
90        uri = self.uri + self.name
91        create_params = 'key_format=i,value_format=i,'
92        complex_params = ''
93        #
94        # If we're not explicitly setting the parameter, then don't
95        # modify create_params to test using the default.
96        #
97        if self.acreate != '':
98            access_param = 'access_pattern_hint=%s' % self.acreate
99            create_params += '%s,' % access_param
100            complex_params += '%s,' % access_param
101        else:
102            # NOTE: This is hard-coding the default value.  If the default
103            # changes then this will fail and need to be fixed.
104            access_param = 'access_pattern_hint=none'
105        if self.ccreate != '':
106            cache_param = 'cache_resident=%s' % self.ccreate
107            create_params += '%s,' % cache_param
108            complex_params += '%s,' % cache_param
109        else:
110            # NOTE: This is hard-coding the default value.  If the default
111            # changes then this will fail and need to be fixed.
112            cache_param = 'cache_resident=false'
113
114        cgparam = ''
115        if self.use_cg or self.use_index:
116            cgparam = 'columns=(k,v),'
117        if self.use_cg:
118            cgparam += 'colgroups=(g0),'
119
120        self.session.create(uri, create_params + cgparam)
121        # Add in column group or index settings.
122        if self.use_cg:
123            cgparam = 'columns=(v),'
124            suburi = 'colgroup:' + self.name + ':g0'
125            self.session.create(suburi, complex_params + cgparam)
126        if self.use_index:
127            suburi = 'index:' + self.name + ':i0'
128            self.session.create(suburi, complex_params + cgparam)
129
130        # Put some data in table.
131        c = self.session.open_cursor(uri, None)
132        for k in range(self.entries):
133            c[k+1] = 1
134        c.close()
135
136        # Verify the string in the metadata
137        self.verify_metadata(access_param)
138        self.verify_metadata(cache_param)
139
140        # Run through all combinations of the alter commands
141        # for all allowed settings.  This tests having only one or
142        # the other set as well as having both set.  It will also
143        # cover trying to change the setting to its current value.
144        for a in self.access_alter:
145            alter_param = ''
146            access_str = ''
147            if a != '':
148                access_str = 'access_pattern_hint=%s' % a
149            for c in self.cache_alter:
150                alter_param = '%s' % access_str
151                cache_str = ''
152                if c != '':
153                    cache_str = 'cache_resident=%s' % c
154                    alter_param += ',%s' % cache_str
155                if alter_param != '':
156                    self.session.alter(uri, alter_param)
157                    if self.reopen:
158                        self.reopen_conn()
159                    special = self.use_cg or self.use_index
160                    if not special:
161                        self.verify_metadata(access_str)
162                        self.verify_metadata(cache_str)
163                    else:
164                        self.session.alter(suburi, alter_param)
165                        self.verify_metadata(access_str)
166                        self.verify_metadata(cache_str)
167
168if __name__ == '__main__':
169    wttest.run()
170