Search in sources :

Example 11 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class CipherFactory method getKeyStore.

/**
 * Get the keystore object using the given password.
 *
 * @param password the keystore password
 * @return the keystore
 */
public static KeyStore getKeyStore(String password) throws IOException {
    try {
        // The following source code can be re-generated
        // if you have a keystore file.
        // This code is (hopefully) more Java version independent
        // than using keystores directly. See also:
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4887561
        // (1.4.2 cannot read keystore written with 1.4.1)
        // --- generated code start ---
        KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
        store.load(null, password.toCharArray());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        store.load(null, password.toCharArray());
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(StringUtils.convertHexToBytes("30820277020100300d06092a864886f70d010101" + "0500048202613082025d02010002818100dc0a13" + "c602b7141110eade2f051b54777b060d0f74e6a1" + "10f9cce81159f271ebc88d8e8aa1f743b505fc2e" + "7dfe38d33b8d3f64d1b363d1af4d877833897954" + "cbaec2fa384c22a415498cf306bb07ac09b76b00" + "1cd68bf77ea0a628f5101959cf2993a9c23dbee7" + "9b19305977f8715ae78d023471194cc900b231ee" + "cb0aaea98d02030100010281810099aa4ff4d0a0" + "9a5af0bd953cb10c4d08c3d98df565664ac5582e" + "494314d5c3c92dddedd5d316a32a206be4ec0846" + "16fe57be15e27cad111aa3c21fa79e32258c6ca8" + "430afc69eddd52d3b751b37da6b6860910b94653" + "192c0db1d02abcfd6ce14c01f238eec7c20bd3bb" + "750940004bacba2880349a9494d10e139ecb2355" + "d101024100ffdc3defd9c05a2d377ef6019fa62b" + "3fbd5b0020a04cc8533bca730e1f6fcf5dfceea1" + "b044fbe17d9eababfbc7d955edad6bc60f9be826" + "ad2c22ba77d19a9f65024100dc28d43fdbbc9385" + "2cc3567093157702bc16f156f709fb7db0d9eec0" + "28f41fd0edcd17224c866e66be1744141fb724a1" + "0fd741c8a96afdd9141b36d67fff6309024077b1" + "cddbde0f69604bdcfe33263fb36ddf24aa3b9922" + "327915b890f8a36648295d0139ecdf68c245652c" + "4489c6257b58744fbdd961834a4cab201801a3b1" + "e52d024100b17142e8991d1b350a0802624759d4" + "8ae2b8071a158ff91fabeb6a8f7c328e762143dc" + "726b8529f42b1fab6220d1c676fdc27ba5d44e84" + "7c72c52064afd351a902407c6e23fe35bcfcd1a6" + "62aa82a2aa725fcece311644d5b6e3894853fd4c" + "e9fe78218c957b1ff03fc9e5ef8ffeb6bd58235f" + "6a215c97d354fdace7e781e4a63e8b"));
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        Certificate[] certs = { CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(StringUtils.convertHexToBytes("3082018b3081f502044295ce6b300d06092a8648" + "86f70d0101040500300d310b3009060355040313" + "024832301e170d3035303532363133323630335a" + "170d3337303933303036353734375a300d310b30" + "0906035504031302483230819f300d06092a8648" + "86f70d010101050003818d0030818902818100dc" + "0a13c602b7141110eade2f051b54777b060d0f74" + "e6a110f9cce81159f271ebc88d8e8aa1f743b505" + "fc2e7dfe38d33b8d3f64d1b363d1af4d87783389" + "7954cbaec2fa384c22a415498cf306bb07ac09b7" + "6b001cd68bf77ea0a628f5101959cf2993a9c23d" + "bee79b19305977f8715ae78d023471194cc900b2" + "31eecb0aaea98d0203010001300d06092a864886" + "f70d01010405000381810083f4401a279453701b" + "ef9a7681a5b8b24f153f7d18c7c892133d97bd5f" + "13736be7505290a445a7d5ceb75522403e509751" + "5cd966ded6351ff60d5193de34cd36e5cb04d380" + "398e66286f99923fd92296645fd4ada45844d194" + "dfd815e6cd57f385c117be982809028bba1116c8" + "5740b3d27a55b1a0948bf291ddba44bed337b9"))) };
        store.setKeyEntry("h2", privateKey, password.toCharArray(), certs);
        // --- generated code end ---
        return store;
    } catch (Exception e) {
        throw DbException.convertToIOException(e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeyStore(java.security.KeyStore) KeyFactory(java.security.KeyFactory) DbException(org.h2.message.DbException) IOException(java.io.IOException) Certificate(java.security.cert.Certificate)

Example 12 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class MVStoreTool method repair.

/**
 * Repair a store by rolling back to the newest good version.
 *
 * @param fileName the file name
 */
public static void repair(String fileName) {
    PrintWriter pw = new PrintWriter(System.out);
    long version = Long.MAX_VALUE;
    OutputStream ignore = new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        // ignore
        }
    };
    while (version >= 0) {
        pw.println(version == Long.MAX_VALUE ? "Trying latest version" : ("Trying version " + version));
        pw.flush();
        version = rollback(fileName, version, new PrintWriter(ignore));
        try {
            String error = info(fileName + ".temp", new PrintWriter(ignore));
            if (error == null) {
                FilePath.get(fileName).moveTo(FilePath.get(fileName + ".back"), true);
                FilePath.get(fileName + ".temp").moveTo(FilePath.get(fileName), true);
                pw.println("Success");
                break;
            }
            pw.println("    ... failed: " + error);
        } catch (Exception e) {
            pw.println("Fail: " + e.getMessage());
            pw.flush();
        }
        version--;
    }
    pw.flush();
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) PrintWriter(java.io.PrintWriter)

Example 13 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class MVStoreTool method info.

/**
 * Read the summary information of the file and write them to system out.
 *
 * @param fileName the name of the file
 * @param writer the print writer
 * @return null if successful (if there was no error), otherwise the error
 *         message
 */
public static String info(String fileName, Writer writer) {
    PrintWriter pw = new PrintWriter(writer, true);
    if (!FilePath.get(fileName).exists()) {
        pw.println("File not found: " + fileName);
        return "File not found: " + fileName;
    }
    long fileLength = FileUtils.size(fileName);
    MVStore store = new MVStore.Builder().fileName(fileName).readOnly().open();
    try {
        MVMap<String, String> meta = store.getMetaMap();
        Map<String, Object> header = store.getStoreHeader();
        long fileCreated = DataUtils.readHexLong(header, "created", 0L);
        TreeMap<Integer, Chunk> chunks = new TreeMap<>();
        long chunkLength = 0;
        long maxLength = 0;
        long maxLengthLive = 0;
        long maxLengthNotEmpty = 0;
        for (Entry<String, String> e : meta.entrySet()) {
            String k = e.getKey();
            if (k.startsWith("chunk.")) {
                Chunk c = Chunk.fromString(e.getValue());
                chunks.put(c.id, c);
                chunkLength += c.len * MVStore.BLOCK_SIZE;
                maxLength += c.maxLen;
                maxLengthLive += c.maxLenLive;
                if (c.maxLenLive > 0) {
                    maxLengthNotEmpty += c.maxLen;
                }
            }
        }
        pw.printf("Created: %s\n", formatTimestamp(fileCreated, fileCreated));
        pw.printf("Last modified: %s\n", formatTimestamp(FileUtils.lastModified(fileName), fileCreated));
        pw.printf("File length: %d\n", fileLength);
        pw.printf("The last chunk is not listed\n");
        pw.printf("Chunk length: %d\n", chunkLength);
        pw.printf("Chunk count: %d\n", chunks.size());
        pw.printf("Used space: %d%%\n", getPercent(chunkLength, fileLength));
        pw.printf("Chunk fill rate: %d%%\n", maxLength == 0 ? 100 : getPercent(maxLengthLive, maxLength));
        pw.printf("Chunk fill rate excluding empty chunks: %d%%\n", maxLengthNotEmpty == 0 ? 100 : getPercent(maxLengthLive, maxLengthNotEmpty));
        for (Entry<Integer, Chunk> e : chunks.entrySet()) {
            Chunk c = e.getValue();
            long created = fileCreated + c.time;
            pw.printf("  Chunk %d: %s, %d%% used, %d blocks", c.id, formatTimestamp(created, fileCreated), getPercent(c.maxLenLive, c.maxLen), c.len);
            if (c.maxLenLive == 0) {
                pw.printf(", unused: %s", formatTimestamp(fileCreated + c.unused, fileCreated));
            }
            pw.printf("\n");
        }
        pw.printf("\n");
    } catch (Exception e) {
        pw.println("ERROR: " + e);
        e.printStackTrace(pw);
        return e.getMessage();
    } finally {
        store.close();
    }
    pw.flush();
    return null;
}
Also used : TreeMap(java.util.TreeMap) IOException(java.io.IOException) DbException(org.h2.message.DbException) PrintWriter(java.io.PrintWriter)

Example 14 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class Page method write.

/**
 * Store the page and update the position.
 *
 * @param chunk the chunk
 * @param buff the target buffer
 * @return the position of the buffer just after the type
 */
private int write(Chunk chunk, WriteBuffer buff) {
    int start = buff.position();
    int len = keys.length;
    int type = children != null ? DataUtils.PAGE_TYPE_NODE : DataUtils.PAGE_TYPE_LEAF;
    buff.putInt(0).putShort((byte) 0).putVarInt(map.getId()).putVarInt(len);
    int typePos = buff.position();
    buff.put((byte) type);
    if (type == DataUtils.PAGE_TYPE_NODE) {
        writeChildren(buff);
        for (int i = 0; i <= len; i++) {
            buff.putVarLong(children[i].count);
        }
    }
    int compressStart = buff.position();
    map.getKeyType().write(buff, keys, len, true);
    if (type == DataUtils.PAGE_TYPE_LEAF) {
        map.getValueType().write(buff, values, len, false);
    }
    MVStore store = map.getStore();
    int expLen = buff.position() - compressStart;
    if (expLen > 16) {
        int compressionLevel = store.getCompressionLevel();
        if (compressionLevel > 0) {
            Compressor compressor;
            int compressType;
            if (compressionLevel == 1) {
                compressor = map.getStore().getCompressorFast();
                compressType = DataUtils.PAGE_COMPRESSED;
            } else {
                compressor = map.getStore().getCompressorHigh();
                compressType = DataUtils.PAGE_COMPRESSED_HIGH;
            }
            byte[] exp = new byte[expLen];
            buff.position(compressStart).get(exp);
            byte[] comp = new byte[expLen * 2];
            int compLen = compressor.compress(exp, expLen, comp, 0);
            int plus = DataUtils.getVarIntLen(compLen - expLen);
            if (compLen + plus < expLen) {
                buff.position(typePos).put((byte) (type + compressType));
                buff.position(compressStart).putVarInt(expLen - compLen).put(comp, 0, compLen);
            }
        }
    }
    int pageLength = buff.position() - start;
    int chunkId = chunk.id;
    int check = DataUtils.getCheckValue(chunkId) ^ DataUtils.getCheckValue(start) ^ DataUtils.getCheckValue(pageLength);
    buff.putInt(start, pageLength).putShort(start + 4, (short) check);
    if (pos != 0) {
        throw DataUtils.newIllegalStateException(DataUtils.ERROR_INTERNAL, "Page already stored");
    }
    pos = DataUtils.getPagePos(chunkId, start, pageLength, type);
    store.cachePage(pos, this, getMemory());
    if (type == DataUtils.PAGE_TYPE_NODE) {
        // cache again - this will make sure nodes stays in the cache
        // for a longer time
        store.cachePage(pos, this, getMemory());
    }
    long max = DataUtils.getPageMaxLength(pos);
    chunk.maxLen += max;
    chunk.maxLenLive += max;
    chunk.pageCount++;
    chunk.pageCountLive++;
    if (removedInMemory) {
        // if the page was removed _before_ the position was assigned, we
        // need to mark it removed here, so the fields are updated
        // when the next chunk is stored
        map.removePage(pos, memory);
    }
    return typePos + 1;
}
Also used : Compressor(org.h2.compress.Compressor)

Example 15 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class MemoryFootprint method main.

/**
 * Run just this test.
 *
 * @param a ignored
 */
public static void main(String... a) {
    // System.getProperties().store(System.out, "");
    print("Object", new Object());
    print("Timestamp", new java.sql.Timestamp(0));
    print("Date", new java.sql.Date(0));
    print("Time", new java.sql.Time(0));
    print("BigDecimal", new BigDecimal("0"));
    print("BigInteger", new BigInteger("0"));
    print("String", new String("Hello"));
    print("Data", Data.create(null, 10));
    print("Row", new RowImpl(new Value[0], 0));
    System.out.println();
    for (int i = 1; i < 128; i += i) {
        System.out.println(getArraySize(1, i) + " bytes per p1[]");
        print("boolean[" + i + "]", new boolean[i]);
        System.out.println(getArraySize(2, i) + " bytes per p2[]");
        print("char[" + i + "]", new char[i]);
        print("short[" + i + "]", new short[i]);
        System.out.println(getArraySize(4, i) + " bytes per p4[]");
        print("int[" + i + "]", new int[i]);
        print("float[" + i + "]", new float[i]);
        System.out.println(getArraySize(8, i) + " bytes per p8[]");
        print("long[" + i + "]", new long[i]);
        print("double[" + i + "]", new double[i]);
        System.out.println(getArraySize(Constants.MEMORY_POINTER, i) + " bytes per obj[]");
        print("Object[" + i + "]", new Object[i]);
        System.out.println();
    }
}
Also used : RowImpl(org.h2.result.RowImpl) Value(org.h2.value.Value) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Aggregations

MVStore (org.h2.mvstore.MVStore)29 IOException (java.io.IOException)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)8 HashMap (java.util.HashMap)8 DbException (org.h2.message.DbException)8 StreamStore (org.h2.mvstore.StreamStore)8 PageStore (org.h2.store.PageStore)8 Random (java.util.Random)7 OutputStream (java.io.OutputStream)6 FileStore (org.h2.store.FileStore)6 BufferedInputStream (java.io.BufferedInputStream)5 ArrayList (java.util.ArrayList)5 FileStore (org.h2.mvstore.FileStore)5 Store (org.h2.mvstore.db.MVTableEngine.Store)5 Row (org.h2.result.Row)5 FileStoreInputStream (org.h2.store.FileStoreInputStream)5 PrintWriter (java.io.PrintWriter)4 Database (org.h2.engine.Database)4