File: MakeMigrationLogFiles.java

package info (click to toggle)
libdb-je-java 3.3.98-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,052 kB
  • sloc: java: 153,077; xml: 2,034; makefile: 3
file content (109 lines) | stat: -rw-r--r-- 3,632 bytes parent folder | download | duplicates (3)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2010 Oracle.  All rights reserved.
 *
 * $Id: MakeMigrationLogFiles.java,v 1.2.2.2 2010/01/04 15:30:42 cwl Exp $
 */

package com.sleepycat.je.cleaner;

import java.io.File;

import com.sleepycat.bind.tuple.IntegerBinding;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.config.EnvironmentParams;

/**
 * Creates two small log files with close to 100% utilization for use by
 * FileSelectionTest.testLogVersionMigration.  This main program is with the
 * arguments: -h HOME_DIRECTORY
 *
 * This program was used to create two log files (stored in CVS as
 * migrate_f0.jdb and migrate_f1.jdb) running against JE 3.2.68, which writes
 * log version 5.  Testing with these files in testLogVersionUpgrade checks
 * that these files are migrated when je.cleaner.migrateToLogVersion is set.
 */
public class MakeMigrationLogFiles {

    private static final int FILE_SIZE = 1000000;

    public static void main(String[] args)
        throws DatabaseException {

        String homeDir = null;
        for (int i = 0; i < args.length; i += 1) {
            if (args[i].equals("-h")) {
                i += 1;
                homeDir = args[i];
            } else {
                throw new IllegalArgumentException("Unknown arg: " + args[i]);
            }
        }
        if (homeDir == null) {
            throw new IllegalArgumentException("Missing -h arg");
        }
        Environment env = openEnv(new File(homeDir), true /*allowCreate*/);
        makeMigrationLogFiles(env);
        env.close();
    }

    /**
     * Opens an Environment with a small log file size.
     */
    static Environment openEnv(File homeDir, boolean allowCreate)
        throws DatabaseException {

        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setAllowCreate(true);
        envConfig.setConfigParam
            (EnvironmentParams.LOG_FILE_MAX.getName(),
             String.valueOf(FILE_SIZE));
        envConfig.setConfigParam
            (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
        envConfig.setConfigParam
	    (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false");
        return new Environment(homeDir, envConfig);
    }

    /**
     * Creates two log files.
     */
    static void makeMigrationLogFiles(Environment env)
        throws DatabaseException {

        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        Database db = env.openDatabase(null, "foo", dbConfig);

        int nextKey = 0;
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry data = new DatabaseEntry();
        Cursor c = db.openCursor(null, null);
        OperationStatus status = c.getLast(key, data, null);
        if (status == OperationStatus.SUCCESS) {
            nextKey = IntegerBinding.entryToInt(key);
        }
        c.close();

        byte[] dataBytes = new byte[1000];
        final int OVERHEAD = dataBytes.length + 100;
        data.setData(dataBytes);

        for (int size = 0; size < FILE_SIZE * 2; size += OVERHEAD) {
            nextKey += 1;
            IntegerBinding.intToEntry(nextKey, key);
            status = db.putNoOverwrite(null, key, data);
            assert status == OperationStatus.SUCCESS;
        }

        db.close();
    }
}