alexander.behm | 0aecb4f | 2010-08-30 17:52:33 +0000 | [diff] [blame] | 1 | package edu.uci.ics.asterix.test.storage; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.RandomAccessFile; |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Random; |
| 7 | import java.util.logging.Level; |
| 8 | |
| 9 | import org.junit.Assert; |
| 10 | import org.junit.Test; |
| 11 | |
| 12 | import edu.uci.ics.asterix.common.config.GlobalConfig; |
| 13 | import edu.uci.ics.asterix.indexing.btree.frames.FieldPrefixNSMLeaf; |
| 14 | import edu.uci.ics.asterix.indexing.btree.impls.FieldPrefixSlotManager; |
| 15 | import edu.uci.ics.asterix.indexing.btree.impls.MultiComparator; |
| 16 | import edu.uci.ics.asterix.indexing.btree.interfaces.IComparator; |
| 17 | import edu.uci.ics.asterix.indexing.btree.interfaces.IFieldAccessor; |
| 18 | import edu.uci.ics.asterix.indexing.btree.interfaces.IPrefixSlotManager; |
| 19 | import edu.uci.ics.asterix.indexing.types.Int32Accessor; |
| 20 | import edu.uci.ics.asterix.indexing.types.Int32Comparator; |
| 21 | import edu.uci.ics.asterix.om.AInt32; |
| 22 | import edu.uci.ics.asterix.storage.buffercache.BufferAllocator; |
| 23 | import edu.uci.ics.asterix.storage.buffercache.BufferCache; |
| 24 | import edu.uci.ics.asterix.storage.buffercache.ClockPageReplacementStrategy; |
| 25 | import edu.uci.ics.asterix.storage.buffercache.IBufferCache; |
| 26 | import edu.uci.ics.asterix.storage.buffercache.ICacheMemoryAllocator; |
| 27 | import edu.uci.ics.asterix.storage.buffercache.ICachedPage; |
| 28 | import edu.uci.ics.asterix.storage.buffercache.IPageReplacementStrategy; |
| 29 | import edu.uci.ics.asterix.storage.file.FileInfo; |
| 30 | import edu.uci.ics.asterix.storage.file.FileManager; |
| 31 | |
| 32 | public class BTreeFieldPrefixNSMTest { |
| 33 | |
| 34 | //private static final int PAGE_SIZE = 8192; |
| 35 | //private static final int PAGE_SIZE = 8192; |
| 36 | //private static final int PAGE_SIZE = 32768; // 32K |
| 37 | //private static final int PAGE_SIZE = 65536; // 64K |
| 38 | private static final int PAGE_SIZE = 131072; // 128K |
| 39 | private static final int NUM_PAGES = 40; |
| 40 | |
| 41 | // to help with the logger madness |
| 42 | private void print(String str) { |
| 43 | if(GlobalConfig.ASTERIX_LOGGER.isLoggable(Level.FINEST)) { |
| 44 | GlobalConfig.ASTERIX_LOGGER.finest(str); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | private void tupleInsert(FieldPrefixNSMLeaf frame, MultiComparator cmp, int f0, int f1, int f2, boolean print, ArrayList<byte[]> records) throws Exception { |
| 49 | if(print) System.out.println("INSERTING: " + f0 + " " + f1 + " " + f2); |
| 50 | |
| 51 | byte[] record = new byte[12]; |
| 52 | AInt32 field0 = new AInt32(f0); |
| 53 | AInt32 field1 = new AInt32(f1); |
| 54 | AInt32 field2 = new AInt32(f2); |
| 55 | System.arraycopy(field0.toBytes(), 0, record, 0, 4); |
| 56 | System.arraycopy(field1.toBytes(), 0, record, 4, 4); |
| 57 | System.arraycopy(field2.toBytes(), 0, record, 8, 4); |
| 58 | frame.insert(record, cmp); |
| 59 | |
| 60 | if(records != null) records.add(record); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void test01() throws Exception { |
| 65 | |
| 66 | FileManager fileManager = new FileManager(); |
| 67 | ICacheMemoryAllocator allocator = new BufferAllocator(); |
| 68 | IPageReplacementStrategy prs = new ClockPageReplacementStrategy(); |
| 69 | IBufferCache bufferCache = new BufferCache(allocator, prs, fileManager, PAGE_SIZE, NUM_PAGES); |
| 70 | |
| 71 | File f = new File("/tmp/btreetest.bin"); |
| 72 | RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
| 73 | int fileId = 0; |
| 74 | FileInfo fi = new FileInfo(fileId, raf); |
| 75 | fileManager.registerFile(fi); |
| 76 | |
| 77 | int numFields = 3; |
| 78 | IFieldAccessor[] fields = new IFieldAccessor[numFields]; |
| 79 | fields[0] = new Int32Accessor(); // first key field |
| 80 | fields[1] = new Int32Accessor(); // second key field |
| 81 | fields[2] = new Int32Accessor(); // third key field |
| 82 | |
| 83 | int keyLen = 3; |
| 84 | IComparator[] cmps = new IComparator[keyLen]; |
| 85 | cmps[0] = new Int32Comparator(); |
| 86 | cmps[1] = new Int32Comparator(); |
| 87 | cmps[2] = new Int32Comparator(); |
| 88 | MultiComparator cmp = new MultiComparator(cmps, fields); |
| 89 | |
| 90 | Random rnd = new Random(); |
| 91 | rnd.setSeed(50); |
| 92 | |
| 93 | ICachedPage page = bufferCache.pin(FileInfo.getDiskPageId(fileId, 0), false); |
| 94 | try { |
| 95 | |
| 96 | IPrefixSlotManager slotManager = new FieldPrefixSlotManager(); |
| 97 | FieldPrefixNSMLeaf frame = new FieldPrefixNSMLeaf(); |
| 98 | frame.setPage(page); |
| 99 | frame.initBuffer((byte)0); |
| 100 | slotManager.setFrame(frame); |
| 101 | frame.setNumPrefixRecords(0); |
| 102 | |
| 103 | String before = new String(); |
| 104 | String after = new String(); |
| 105 | |
| 106 | int compactFreq = 5; |
| 107 | int compressFreq = 5; |
| 108 | int smallMax = 10; |
| 109 | int numRecords = 5000; |
| 110 | ArrayList<byte[]> records = new ArrayList<byte[]>(); |
| 111 | records.ensureCapacity(numRecords); |
| 112 | |
| 113 | // insert records with random calls to compact and compress |
| 114 | for(int i = 0; i < numRecords; i++) { |
| 115 | |
| 116 | if((i+1) % 100 == 0) print("INSERTING " + (i+1) + " / " + numRecords + "\n"); |
| 117 | |
| 118 | int a = rnd.nextInt() % smallMax; |
| 119 | int b = rnd.nextInt() % smallMax; |
| 120 | int c = i; |
| 121 | tupleInsert(frame, cmp, a, b, c, false, records); |
| 122 | |
| 123 | if(rnd.nextInt() % compactFreq == 0) { |
| 124 | before = frame.printKeys(cmp); |
| 125 | frame.compact(cmp); |
| 126 | after = frame.printKeys(cmp); |
| 127 | Assert.assertEquals(before, after); |
| 128 | } |
| 129 | |
| 130 | if(rnd.nextInt() % compressFreq == 0) { |
| 131 | before = frame.printKeys(cmp); |
| 132 | frame.compress(cmp); |
| 133 | after = frame.printKeys(cmp); |
| 134 | Assert.assertEquals(before, after); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // delete records with random calls to compact and compress |
| 139 | for(int i = 0; i < records.size(); i++) { |
| 140 | |
| 141 | if((i+1) % 100 == 0) print("DELETING " + (i+1) + " / " + numRecords + "\n"); |
| 142 | |
| 143 | frame.delete(records.get(i), cmp, true); |
| 144 | |
| 145 | if(rnd.nextInt() % compactFreq == 0) { |
| 146 | before = frame.printKeys(cmp); |
| 147 | frame.compact(cmp); |
| 148 | after = frame.printKeys(cmp); |
| 149 | Assert.assertEquals(before, after); |
| 150 | } |
| 151 | |
| 152 | if(rnd.nextInt() % compressFreq == 0) { |
| 153 | before = frame.printKeys(cmp); |
| 154 | frame.compress(cmp); |
| 155 | after = frame.printKeys(cmp); |
| 156 | Assert.assertEquals(before, after); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | } finally { |
| 161 | bufferCache.unpin(page); |
| 162 | } |
| 163 | |
| 164 | bufferCache.close(); |
| 165 | fileManager.close(); |
| 166 | } |
| 167 | } |