1 5 package org.h2.test.db; 6 7 import java.io.File ; 8 import java.sql.Connection ; 9 import java.sql.PreparedStatement ; 10 import java.sql.Statement ; 11 import java.util.ArrayList ; 12 13 import org.h2.test.TestBase; 14 import org.h2.tools.FileBase; 15 16 public class TestLogFile extends TestBase { 17 18 private Connection conn; 19 private static final int MAX_LOG_SIZE = 1; 20 21 private long reconnect(int maxFiles) throws Exception { 22 if(conn != null) { 23 conn.close(); 24 } 25 long length = 0; 26 ArrayList files = FileBase.getDatabaseFiles(BASE_DIR, "logfile", false); 27 checkSmaller(files.size(), maxFiles+2); 28 for(int i=0; i<files.size(); i++) { 29 String fileName = (String ) files.get(i); 30 long len = new File (fileName).length(); 31 length += len; 32 } 33 conn = getConnection("logfile"); 34 return length; 35 } 36 37 public void test() throws Exception { 38 if(config.memory) { 39 return; 40 } 41 deleteDb("logfile"); 42 reconnect(0); 43 insert(); 44 int maxFiles = 3; for(int i=0; i<3; i++) { 46 long length = reconnect(maxFiles); 47 insert(); 48 long l2 = reconnect(maxFiles); 49 trace("l2="+ l2); 50 check(l2 <= length * 2); 51 } 52 conn.close(); 53 } 54 55 private void checkLogSize() throws Exception { 56 String [] files = new File (".").list(); 57 for(int j=0; j<files.length; j++) { 58 String name = files[j]; 59 if(name.startsWith("logfile") && name.endsWith(".log.db")) { 60 long length = new File (name).length(); 61 checkSmaller(length, MAX_LOG_SIZE * 1024 * 1024 * 2); 62 } 63 } 64 } 65 66 void insert() throws Exception { 67 Statement stat = conn.createStatement(); 68 stat.execute("SET LOGSIZE 200"); 69 stat.execute("SET MAX_LOG_SIZE " + MAX_LOG_SIZE); 70 stat.execute("DROP TABLE IF EXISTS TEST"); 71 stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))"); 72 PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, 'Hello' || ?)"); 73 int len = getSize(1, 10000); 74 for(int i=0; i<len; i++) { 75 prep.setInt(1, i); 76 prep.setInt(2, i); 77 prep.execute(); 78 if(i>0 && (i % 2000) == 0) { 79 checkLogSize(); 80 } 81 } 82 checkLogSize(); 83 } 84 85 } 86 | Popular Tags |