diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f67062d --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.classpath +/.settings/ +/.project +workbench.xmi +/.idea/ +/redis/ +/java-rdb-hash-dump-parser.iml diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e2b7b91 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,12 @@ +[submodule "redis/3.2.11"] + path = redis/3.2.11 + url = https://github.com/antirez/redis.git +[submodule "redis/2.8.24"] + path = redis/2.8.24 + url = https://github.com/antirez/redis.git +[submodule "redis/2.4.18"] + path = redis/2.4.18 + url = https://github.com/antirez/redis.git +[submodule "redis/4.0.6"] + path = redis/4.0.6 + url = https://github.com/antirez/redis.git diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..bffd7fc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: java +before_install: + - sudo apt-get update -qq + - sudo apt-get install -y wget build-essential git + - git submodule update --init + - make -C redis diff --git a/README.md b/README.md new file mode 100644 index 0000000..2e508d8 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +代码修改自 https://github.com/jwhitbeck/java-rdb-parser \ No newline at end of file diff --git a/al-v20.txt b/al-v20.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/al-v20.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..12a34a5 --- /dev/null +++ b/pom.xml @@ -0,0 +1,137 @@ + + 4.0.0 + + com.mougua + java-rdb-hash-dump-parser + 0.0.1 + + ${project.groupId}:${project.artifactId} + + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + UTF-8 + + + + + junit + junit + 4.12 + test + + + + redis.clients + jedis + 2.8.1 + jar + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.1 + + 1.7 + 1.7 + + + + + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 2.3.2 + + false + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.17 + + + + + org.apache.maven.plugins + maven-pmd-plugin + 3.6 + + + + + + + release + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.1.1 + + ${java.home}/bin/javadoc + 8 + + + + attach-javadocs + + jar + + + + + + + + + + diff --git a/src/main/java/com/mougua/redis/rdb/Lzf.java b/src/main/java/com/mougua/redis/rdb/Lzf.java new file mode 100644 index 0000000..f934c8d --- /dev/null +++ b/src/main/java/com/mougua/redis/rdb/Lzf.java @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2015-2018 John Whitbeck. All rights reserved. + * + *

The use and distribution terms for this software are covered by the + * Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) + * which can be found in the file al-v20.txt at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * + *

You must not remove this notice, or any other, from this software. + */ + +package com.mougua.redis.rdb; + +// adapted from https://github.com/ganghuawang/java-redis-rdb +public final class Lzf { + + // The maximum number of literals in a chunk (32). + private static int MAX_LITERAL = 32; + + static void expand(byte[] src, byte[] dest) { + int srcPos = 0; + int destPos = 0; + do { + int ctrl = src[srcPos++] & 0xff; + if (ctrl < MAX_LITERAL) { + // literal run of length = ctrl + 1, + ctrl++; + // copy to output and move forward this many bytes + System.arraycopy(src, srcPos, dest, destPos, ctrl); + destPos += ctrl; + srcPos += ctrl; + } else { + /* back reference + the highest 3 bits are the match length */ + int len = ctrl >> 5; + // if the length is maxed, add the next byte to the length + if (len == 7) { + len += src[srcPos++] & 0xff; + } + /* minimum back-reference is 3 bytes, + so 2 was subtracted before storing size */ + len += 2; + + /* ctrl is now the offset for a back-reference... + the logical AND operation removes the length bits */ + ctrl = -((ctrl & 0x1f) << 8) - 1; + + // the next byte augments/increases the offset + ctrl -= src[srcPos++] & 0xff; + + /* copy the back-reference bytes from the given + location in output to current position */ + ctrl += destPos; + for (int i = 0; i < len; i++) { + dest[destPos++] = dest[ctrl++]; + } + } + } while (destPos < dest.length); + } + +} diff --git a/src/main/java/com/mougua/redis/rdb/RdbHashParser.java b/src/main/java/com/mougua/redis/rdb/RdbHashParser.java new file mode 100644 index 0000000..e00a868 --- /dev/null +++ b/src/main/java/com/mougua/redis/rdb/RdbHashParser.java @@ -0,0 +1,168 @@ +package com.mougua.redis.rdb; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +/** + * @author 某瓜 + */ +public class RdbHashParser { + private final ByteBuffer buf; + private static final Charset ASCII = Charset.forName("ASCII"); + + private static String byte2hex(byte[] buffer) { + String h = ""; + for (int i = 0; i < buffer.length; i++) { + String temp = Integer.toHexString(buffer[i] & 0xFF); + if (temp.length() == 1) { + temp = "0" + temp; + } + h = h + " " + temp; + } + + return h; + } + + public RdbHashParser(byte[] bytes) { + byte[] bytes1 = new byte[bytes.length-1]; + System.arraycopy(bytes, 1, bytes1, 0, bytes1.length); + buf = ByteBuffer.wrap(bytes1); + } + + private int readByte() throws IOException { + return buf.get() & 0xff; + } + + private int readSignedByte() throws IOException { + return buf.get(); + } + + private byte[] readBytes(int numBytes) throws IOException { + int rem = numBytes; + int pos = 0; + byte[] bs = new byte[numBytes]; + while (rem > 0) { + int avail = buf.remaining(); + if (avail >= rem) { + buf.get(bs, pos, rem); + pos += rem; + rem = 0; + } else { + buf.get(bs, pos, avail); + pos += avail; + rem -= avail; + } + } + return bs; + } + + private byte[] readStringEncoded() throws IOException { + int firstByte = readByte(); + // the first two bits determine the encoding + int flag = (firstByte & 0xc0) >> 6; + int len; + switch (flag) { + case 0: // length is read from the lower 6 bits + len = firstByte & 0x3f; + return readBytes(len); + case 1: // one additional byte is read for a 14 bit encoding + len = ((firstByte & 0x3f) << 8) | (readByte() & 0xff); + return readBytes(len); + case 2: // read next four bytes as unsigned big-endian + byte[] bs = readBytes(4); + len = ((int)bs[0] & 0xff) << 24 + | ((int)bs[1] & 0xff) << 16 + | ((int)bs[2] & 0xff) << 8 + | ((int)bs[3] & 0xff) << 0; + if (len < 0) { + throw new IllegalStateException("Strings longer than " + Integer.MAX_VALUE + + "bytes are not supported."); + } + return readBytes(len); + case 3: + return readSpecialStringEncoded(firstByte & 0x3f); + default: // never reached + return null; + } + } + + private byte[] readSpecialStringEncoded(int type) throws IOException { + switch (type) { + case 0: + return readInteger8Bits(); + case 1: + return readInteger16Bits(); + case 2: + return readInteger32Bits(); + case 3: + return readLzfString(); + default: + throw new IllegalStateException("Unknown special encoding: " + type); + } + } + + private long readLength() throws IOException { + int firstByte = readByte(); + // The first two bits determine the encoding. + int flag = (firstByte & 0xc0) >> 6; + if (flag == 0) { // 00|XXXXXX: len is the last 6 bits of this byte. + return firstByte & 0x3f; + } else if (flag == 1) { // 01|XXXXXX: len is encoded on the next 14 bits. + return (((long)firstByte & 0x3f) << 8) | ((long)readByte() & 0xff); + } else if (firstByte == 0x80) { + // 10|000000: len is a 32-bit integer encoded on the next 4 bytes. + byte[] bs = readBytes(4); + return ((long)bs[0] & 0xff) << 24 + | ((long)bs[1] & 0xff) << 16 + | ((long)bs[2] & 0xff) << 8 + | ((long)bs[3] & 0xff) << 0; + } else if (firstByte == 0x81) { + // 10|000001: len is a 64-bit integer encoded on the next 8 bytes. + byte[] bs = readBytes(8); + return ((long)bs[0] & 0xff) << 56 + | ((long)bs[1] & 0xff) << 48 + | ((long)bs[2] & 0xff) << 40 + | ((long)bs[3] & 0xff) << 32 + | ((long)bs[4] & 0xff) << 24 + | ((long)bs[5] & 0xff) << 16 + | ((long)bs[6] & 0xff) << 8 + | ((long)bs[7] & 0xff) << 0; + } else { + // 11|XXXXXX: special encoding. + throw new IllegalStateException("Expected a length, but got a special string encoding."); + } + } + + private byte[] readInteger8Bits() throws IOException { + return String.valueOf(readSignedByte()).getBytes(ASCII); + } + + private byte[] readInteger16Bits() throws IOException { + long val = ((long)readByte() & 0xff) << 0 + | (long)readSignedByte() << 8; // Don't apply 0xff mask to preserve sign. + return String.valueOf(val).getBytes(ASCII); + } + + private byte[] readInteger32Bits() throws IOException { + byte[] bs = readBytes(4); + long val = (long)bs[3] << 24 // Don't apply 0xff mask to preserve sign. + | ((long)bs[2] & 0xff) << 16 + | ((long)bs[1] & 0xff) << 8 + | ((long)bs[0] & 0xff) << 0; + return String.valueOf(val).getBytes(ASCII); + } + + private byte[] readLzfString() throws IOException { + int clen = (int)readLength(); + int ulen = (int)readLength(); + byte[] src = readBytes(clen); + byte[] dest = new byte[ulen]; + Lzf.expand(src, dest); + return dest; + } + + public ZipList readZipList() throws IOException { + return new ZipList(readStringEncoded()); + } +} diff --git a/src/main/java/com/mougua/redis/rdb/ZipList.java b/src/main/java/com/mougua/redis/rdb/ZipList.java new file mode 100644 index 0000000..a8e363e --- /dev/null +++ b/src/main/java/com/mougua/redis/rdb/ZipList.java @@ -0,0 +1,241 @@ +/** + * Copyright (c) 2015-2018 John Whitbeck. All rights reserved. + * + *

The use and distribution terms for this software are covered by the + * Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) + * which can be found in the file al-v20.txt at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * + *

You must not remove this notice, or any other, from this software. + */ + +package com.mougua.redis.rdb; + +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class ZipList { + + private static final Charset ASCII = Charset.forName("ASCII"); + private final byte[] envelope; + + + public ZipList(byte[] envelope) { + this.envelope = envelope; + } + + public List get() { + // skip the first 8 bytes representing the total size in bytes of the ziplist and the offset to + // the last element. + int pos = 8; + // read number of elements as a 2 byte little-endian integer + int num = ((int)envelope[pos++] & 0xff) << 0 + | ((int)envelope[pos++] & 0xff) << 8; + List list = new ArrayList(num); + int idx = 0; + while (idx < num) { + // skip length of previous entry. If len is <= 253 (0xfd), it represents the length of the + // previous entry, otherwise, the next four bytes are used to store the length + int prevLen = (int)envelope[pos++] & 0xff; + if (prevLen > 0xfd) { + pos += 4; + } + int special = (int)envelope[pos++] & 0xff; + int top2bits = special >> 6; + int len; + byte[] buf; + switch (top2bits) { + case 0: // string value with length less than or equal to 63 bytes (6 bits) + len = special & 0x3f; + buf = new byte[len]; + System.arraycopy(envelope, pos, buf, 0, len); + pos += len; + list.add(buf); + break; + case 1: // String value with length less than or equal to 16383 bytes (14 bits). + len = ((special & 0x3f) << 8) | ((int)envelope[pos++] & 0xff); + buf = new byte[len]; + System.arraycopy(envelope, pos, buf, 0, len); + pos += len; + list.add(buf); + break; + case 2: /* String value with length greater than or equal to 16384 bytes. Length is read + from 4 following bytes. */ + len = ((int)envelope[pos++] & 0xff) << 24 + | ((int)envelope[pos++] & 0xff) << 16 + | ((int)envelope[pos++] & 0xff) << 8 + | ((int)envelope[pos++] & 0xff) << 0; + buf = new byte[len]; + System.arraycopy(envelope, pos, buf, 0, len); + pos += len; + list.add(buf); + break; + case 3: // integer encodings + int flag = (special & 0x30) >> 4; + long val; + switch (flag) { + case 0: // read next 2 bytes as a 16 bit signed integer + val = (long)envelope[pos++] & 0xff + | (long)envelope[pos++] << 8; + list.add(String.valueOf(val).getBytes(ASCII)); + break; + case 1: // read next 4 bytes as a 32 bit signed integer + val = ((long)envelope[pos++] & 0xff) << 0 + | ((long)envelope[pos++] & 0xff) << 8 + | ((long)envelope[pos++] & 0xff) << 16 + | (long)envelope[pos++] << 24; + list.add(String.valueOf(val).getBytes(ASCII)); + break; + case 2: // read next 8 as a 64 bit signed integer + val = ((long)envelope[pos++] & 0xff) << 0 + | ((long)envelope[pos++] & 0xff) << 8 + | ((long)envelope[pos++] & 0xff) << 16 + | ((long)envelope[pos++] & 0xff) << 24 + | ((long)envelope[pos++] & 0xff) << 32 + | ((long)envelope[pos++] & 0xff) << 40 + | ((long)envelope[pos++] & 0xff) << 48 + | (long)envelope[pos++] << 56; + list.add(String.valueOf(val).getBytes(ASCII)); + break; + case 3: + int loBits = special & 0x0f; + switch (loBits) { + case 0: // read next 3 bytes as a 24 bit signed integer + val = ((long)envelope[pos++] & 0xff) << 0 + | ((long)envelope[pos++] & 0xff) << 8 + | (long)envelope[pos++] << 16; + list.add(String.valueOf(val).getBytes(ASCII)); + break; + case 0x0e: // read next byte as an 8 bit signed integer + val = (long)envelope[pos++]; + list.add(String.valueOf(val).getBytes(ASCII)); + break; + default: /* an immediate 4 bit unsigned integer between 0 and 12. Substract 1 as the + range is actually between 1 and 13. */ + list.add(String.valueOf(loBits - 1).getBytes(ASCII)); + break; + } + break; + default: // never reached + } + break; + default: // never reached + } + idx += 1; + } + return list; + } + + public HashMap getHashMap() { + // skip the first 8 bytes representing the total size in bytes of the ziplist and the offset to + // the last element. + int pos = 8; + // read number of elements as a 2 byte little-endian integer + int num = ((int)envelope[pos++] & 0xff) << 0 + | ((int)envelope[pos++] & 0xff) << 8; + int idx = 0; + HashMap map = new HashMap<>(); + String key = null; + byte[] res = null; + while (idx < num) { + int r = idx % 2; + // skip length of previous entry. If len is <= 253 (0xfd), it represents the length of the + // previous entry, otherwise, the next four bytes are used to store the length + int prevLen = (int)envelope[pos++] & 0xff; + if (prevLen > 0xfd) { + pos += 4; + } + int special = (int)envelope[pos++] & 0xff; + int top2bits = special >> 6; + int len; + byte[] buf; + switch (top2bits) { + case 0: // string value with length less than or equal to 63 bytes (6 bits) + len = special & 0x3f; + buf = new byte[len]; + System.arraycopy(envelope, pos, buf, 0, len); + pos += len; + res = buf; + break; + case 1: // String value with length less than or equal to 16383 bytes (14 bits). + len = ((special & 0x3f) << 8) | ((int)envelope[pos++] & 0xff); + buf = new byte[len]; + System.arraycopy(envelope, pos, buf, 0, len); + pos += len; + res = buf; + break; + case 2: /* String value with length greater than or equal to 16384 bytes. Length is read + from 4 following bytes. */ + len = ((int)envelope[pos++] & 0xff) << 24 + | ((int)envelope[pos++] & 0xff) << 16 + | ((int)envelope[pos++] & 0xff) << 8 + | ((int)envelope[pos++] & 0xff) << 0; + buf = new byte[len]; + System.arraycopy(envelope, pos, buf, 0, len); + pos += len; + res = buf; + break; + case 3: // integer encodings + int flag = (special & 0x30) >> 4; + long val; + switch (flag) { + case 0: // read next 2 bytes as a 16 bit signed integer + val = (long)envelope[pos++] & 0xff + | (long)envelope[pos++] << 8; + res = String.valueOf(val).getBytes(ASCII); + break; + case 1: // read next 4 bytes as a 32 bit signed integer + val = ((long)envelope[pos++] & 0xff) << 0 + | ((long)envelope[pos++] & 0xff) << 8 + | ((long)envelope[pos++] & 0xff) << 16 + | (long)envelope[pos++] << 24; + res = String.valueOf(val).getBytes(ASCII); + break; + case 2: // read next 8 as a 64 bit signed integer + val = ((long)envelope[pos++] & 0xff) << 0 + | ((long)envelope[pos++] & 0xff) << 8 + | ((long)envelope[pos++] & 0xff) << 16 + | ((long)envelope[pos++] & 0xff) << 24 + | ((long)envelope[pos++] & 0xff) << 32 + | ((long)envelope[pos++] & 0xff) << 40 + | ((long)envelope[pos++] & 0xff) << 48 + | (long)envelope[pos++] << 56; + res = String.valueOf(val).getBytes(ASCII); + break; + case 3: + int loBits = special & 0x0f; + switch (loBits) { + case 0: // read next 3 bytes as a 24 bit signed integer + val = ((long)envelope[pos++] & 0xff) << 0 + | ((long)envelope[pos++] & 0xff) << 8 + | (long)envelope[pos++] << 16; + res = String.valueOf(val).getBytes(ASCII); + break; + case 0x0e: // read next byte as an 8 bit signed integer + val = (long)envelope[pos++]; + res = String.valueOf(val).getBytes(ASCII); + break; + default: /* an immediate 4 bit unsigned integer between 0 and 12. Substract 1 as the + range is actually between 1 and 13. */ + res = String.valueOf(loBits - 1).getBytes(ASCII); + break; + } + break; + default: // never reached + } + break; + default: // never reached + } + if (r > 0) { + map.put(key, new String(res, ASCII)); + } else { + key = new String(res, ASCII); + } + idx += 1; + } + return map; + } +} diff --git a/src/test/java/com/mougua/ParserApplicationTests.java b/src/test/java/com/mougua/ParserApplicationTests.java new file mode 100644 index 0000000..10e8f8c --- /dev/null +++ b/src/test/java/com/mougua/ParserApplicationTests.java @@ -0,0 +1,26 @@ +package com.mougua; + +import com.mougua.redis.rdb.RdbHashParser; +import com.mougua.redis.rdb.ZipList; +import org.junit.Ignore; +import org.junit.Test; +import redis.clients.jedis.Jedis; + + +public class ParserApplicationTests { + @Test + public void contextLoads() { + } + + @Ignore + @Test + public void testJedis() throws Exception { + Jedis jedis = new Jedis("localhost"); + byte[] bytes = jedis.dump("bbcc"); + RdbHashParser parser = new RdbHashParser(bytes); + ZipList list = parser.readZipList(); + + System.out.println(list.getHashMap()); + } +} +