summaryrefslogtreecommitdiff
path: root/test/java/com/wiredtiger/test/PackTest03.java
blob: 81e7987f9873a6b1d8e0731debaecacedc5aedb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*-
 * Public Domain 2014-2017 MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.wiredtiger.test;

import com.wiredtiger.db.Connection;
import com.wiredtiger.db.Cursor;
import com.wiredtiger.db.PackOutputStream;
import com.wiredtiger.db.PackInputStream;
import com.wiredtiger.db.Session;
import com.wiredtiger.db.WiredTigerException;
import com.wiredtiger.db.WiredTigerPackingException;
import com.wiredtiger.db.wiredtiger;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/*
 * PackTest03 tests packing/unpacking in three ways:
 *  1) using PackInputStream/PackOutputStream
 *  2) putting values into a main table and rereading them.
 *  3) checking that values are stored in a reverse index on the main table.
 */
public class PackTest03 {

    private Connection connection;
    private Session session;

    public interface PackingAdaptor {
        public void addval(PackOutputStream packer, long val);
        public long getval(PackInputStream unpacker);
        public void addval(Cursor cur, long val);
        public void addkey(Cursor cur, long key);
        public long getval(Cursor cur);
    }
    public static class Scenario {
        public Scenario(String format, long low, long high, int nbits,
                        PackingAdaptor a) {
            this.format = format;
            this.low = low;
            this.high = high;
            this.nbits = nbits;
            if (format.equals("Q"))
                this.mask = -1L;
            else if (low == 0) // unsigned
                this.mask = (1L << nbits) - 1;
            else // signed - preserve the sign and lose one of the lower bits
                this.mask = (1L << (nbits - 1)) - 1;
            this.adaptor = a;
        }
        public String format;
        public long low;
        public long high;
        public int nbits;
        public long mask;
        public PackingAdaptor adaptor;
    }
    public static final Scenario scenarios[] = {
        new Scenario("b", -128, 127, 8, new PackingAdaptor() {
                public void addval(PackOutputStream packer, long val) {
                    packer.addByte((byte)val); }
                public long getval(PackInputStream unpacker) {
                    return unpacker.getByte(); }
                public void addval(Cursor cur, long val) {
                    cur.putValueByte((byte)val); }
                public void addkey(Cursor cur, long key) {
                    cur.putKeyByte((byte)key); }
                public long getval(Cursor cur) {
                    return cur.getValueByte(); }
            }),
        new Scenario("B", 0, 255, 8, new PackingAdaptor() {
                public void addval(PackOutputStream packer, long val) {
                    packer.addByte((byte)val); }
                public long getval(PackInputStream unpacker) {
                    return unpacker.getByte(); }
                public void addval(Cursor cur, long val) {
                    cur.putValueByte((byte)val); }
                public void addkey(Cursor cur, long key) {
                    cur.putKeyByte((byte)key); }
                public long getval(Cursor cur) {
                    return cur.getValueByte(); }
            }),
        new Scenario("h", -32768, 32767, 16, new PackingAdaptor() {
                public void addval(PackOutputStream packer, long val) {
                    packer.addShort((short)val); }
                public long getval(PackInputStream unpacker) {
                    return unpacker.getShort(); }
                public void addval(Cursor cur, long val) {
                    cur.putValueShort((short)val); }
                public void addkey(Cursor cur, long key) {
                    cur.putKeyShort((short)key); }
                public long getval(Cursor cur) {
                    return cur.getValueShort(); }
            }),
        /*
         * Note: high should be 65535, there's not currently a way to insert
         * unsigned shorts between 2^15 and 2^16.
         */
        new Scenario("H", 0, 32767, 16, new PackingAdaptor() {
                public void addval(PackOutputStream packer, long val) {
                    packer.addShort((short)val); }
                public long getval(PackInputStream unpacker) {
                    return unpacker.getShort(); }
                public void addval(Cursor cur, long val) {
                    cur.putValueShort((short)val); }
                public void addkey(Cursor cur, long key) {
                    cur.putKeyShort((short)key); }
                public long getval(Cursor cur) {
                    return cur.getValueShort(); }
            }),
        new Scenario("i", -2147483648, 2147483647, 32, new PackingAdaptor() {
                public void addval(PackOutputStream packer, long val) {
                    packer.addInt((int)val); }
                public long getval(PackInputStream unpacker) {
                    return unpacker.getInt(); }
                public void addval(Cursor cur, long val) {
                    cur.putValueInt((int)val); }
                public void addkey(Cursor cur, long key) {
                    cur.putKeyInt((int)key); }
                public long getval(Cursor cur) {
                    return cur.getValueInt(); }
            }),
        /*
         * Note: high should be 4294967295L, there's not currently a way to
         * insert unsigned ints between 2^31 and 2^32.
         */
        new Scenario("I", 0, 2147483647L, 32, new PackingAdaptor() {
                public void addval(PackOutputStream packer, long val) {
                    packer.addInt((int)val); }
                public long getval(PackInputStream unpacker) {
                    return unpacker.getInt(); }
                public void addval(Cursor cur, long val) {
                    cur.putValueInt((int)val); }
                public void addkey(Cursor cur, long key) {
                    cur.putKeyInt((int)key); }
                public long getval(Cursor cur) {
                    return cur.getValueInt(); }
            }),
        new Scenario("q", -9223372036854775808L,
                     9223372036854775807L, 64, new PackingAdaptor() {
                public void addval(PackOutputStream packer, long val) {
                    packer.addLong(val); }
                public long getval(PackInputStream unpacker) {
                    return unpacker.getLong(); }
                public void addval(Cursor cur, long val) {
                    cur.putValueLong(val); }
                public void addkey(Cursor cur, long key) {
                    cur.putKeyLong(key); }
                public long getval(Cursor cur) {
                    return cur.getValueLong(); }
            }),

        /* 'unsigned long' numbers larger than 2^63 cannot be
         * expressed in Java (except indirectly as negative numbers).
         */
        new Scenario("Q", 0, -1L, 64, new PackingAdaptor() {
                public void addval(PackOutputStream packer, long val) {
                    packer.addLong(val); }
                public long getval(PackInputStream unpacker) {
                    return unpacker.getLong(); }
                public void addval(Cursor cur, long val) {
                    cur.putValueLong(val); }
                public void addkey(Cursor cur, long key) {
                    cur.putKeyLong(key); }
                public long getval(Cursor cur) {
                    return cur.getValueLong(); }
            }),
    };

    void packSingle(Scenario scen, Cursor cur, Cursor curinv, long value)
    throws WiredTigerPackingException {
        if ((value < scen.low || value > scen.high) && scen.low < scen.high)
            throw new IllegalArgumentException("value out of range: " + value);
        
        // The Java API does not handle large unsigned values
        // via the cursor interface.  We can fake it via the
        // Packing interface.
        if (scen.low == 0 && value > (scen.high / 2)) {
            // disable cursor checks
            cur = null;
            curinv = null;
        }
        PackOutputStream packer = new PackOutputStream(scen.format);
        scen.adaptor.addval(packer, value);
        byte[] bytes = packer.getValue();
        PackInputStream unpacker =
            new PackInputStream(scen.format, packer.getValue());
        long unpacked = scen.adaptor.getval(unpacker);

        // We jump through hoops to allow values to be packed/unpacked.
        if (scen.low < 0 && unpacked < 0) {
            unpacked = (unpacked & scen.mask) | ~scen.mask;
        }
        else {
            unpacked = unpacked & scen.mask;
        }

        if (value != unpacked) {
            System.err.println("ERROR: format " + scen.format +
                               ": wanted=" + value +
                               " did not match got=" + unpacked);
            assertEquals(value, unpacked);
        }
        if (cur != null) {
            cur.reset();
            cur.putKeyInt(9999);
            scen.adaptor.addval(cur, value);
            cur.insert();
            cur.reset();
            cur.putKeyInt(9999);
            assertEquals(0, cur.search());
            long unpacked2 = scen.adaptor.getval(cur);
            if (value != unpacked2) {
                System.out.println("ERROR: format " + scen.format +
                                   ": cursor wanted=" + value +
                                   " did not match got=" + unpacked2);
                assertEquals(value, unpacked2);
            }
            curinv.reset();
            scen.adaptor.addkey(curinv, value);
            assertEquals(0, curinv.search());
            assertEquals(9999, curinv.getValueInt());
        }
    }

    /**
     * An envelope method around tryPackSingle() that reports any exceptions
     */
    void tryPackSingle(Scenario scen, Cursor cur, Cursor curinv, long value)
    throws WiredTigerPackingException {
        try {
            packSingle(scen, cur, curinv, value);
        }
        catch (WiredTigerPackingException pe) {
            System.err.println("ERROR: scenario " + scen.format +
                               ": value=" + value);
            throw pe;
        }
        catch (AssertionError ae) {
            System.err.println("ERROR: scenario " + scen.format +
                               ": value=" + value);
            throw ae;
        }
    }

    void tryPackRange(Scenario scen, long lowRange, long highRange)
    throws WiredTigerPackingException {
        //System.out.println("Scenario " + scen.format +
        //                   ": [" + lowRange + "," + highRange + "]");
        String uri = "table:PackTest03";
        String uriinv = "index:PackTest03:inverse";
        session.create(uri, "columns=(k,v),key_format=i,value_format=" +
                       scen.format);
        session.create(uriinv, "columns=(v)");
        Cursor cur = session.open_cursor(uri, null, null);
        Cursor curinv = session.open_cursor(uriinv + "(k)", null, null);
        if (lowRange < scen.low)
            lowRange = scen.low;
        if (highRange > scen.high && scen.high != -1L)
            highRange = scen.high;
        for (long val = lowRange; val <= highRange; val++) {
            try {
                tryPackSingle(scen, cur, curinv, val);
            }
            catch (Exception ex) {
                System.out.println("EXCEPTION: format " + scen.format +
                                   ": value: " + val + ": " + ex);
                throw ex;
            }
            // watch for long overflow
            if (val == Long.MAX_VALUE)
                break;
        }
        cur.close();
        curinv.close();
        session.drop(uriinv, null);
        session.drop(uri, null);
    }

    // A debug helper method
    private void printByteArray(byte[] bytes, int len) {
        for (int i = 0; i < len; i++) {
            System.out.println(String.format(
                                   "\t%8s", Integer.toBinaryString(
                                       bytes[i] & 0xff)).replace(' ', '0'));
        }
    }

    @Test
    public void packUnpackRanges()
    throws WiredTigerPackingException {
        // Do a comprehensive test of packing for various formats.
        // Based on test suite's test_intpack.py
        long imin = Integer.MIN_VALUE;
        long imax = Integer.MAX_VALUE;
        long e32 = 1L << 32;
        long lmin = Long.MIN_VALUE;
        long lmax = Long.MAX_VALUE;

        setup();
        for (int scenidx = 0; scenidx < scenarios.length; scenidx++) {
            Scenario scen = scenarios[scenidx];
            tryPackRange(scen, -100000, 100000);
            tryPackRange(scen, imin - 100, imin + 100);
            tryPackRange(scen, imax - 100, imax + 100);
            tryPackRange(scen, -e32 - 100, -e32 + 100);
            tryPackRange(scen, e32 - 100, e32 + 100);
            tryPackRange(scen, lmin, lmin + 100);
            tryPackRange(scen, lmax - 100, lmax);
        }
        teardown();
    }

    private void setup() {
        connection = wiredtiger.open("WT_HOME", "create");
        session = connection.open_session(null);
    }

    private void teardown() {
        session.close(null);
        connection.close(null);
    }

    public static void main(String[] args) {
        PackTest03 tester = new PackTest03();
        try {
            tester.packUnpackRanges();
        } catch (WiredTigerPackingException wtpe) {
            System.err.println("Packing exception: " + wtpe);
        }
    }
}