Skip to content

Commit

Permalink
Merge pull request #17 from leonchen83/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
leonchen83 authored Jun 12, 2017
2 parents e1fb12f + 14931d8 commit db31c9b
Show file tree
Hide file tree
Showing 235 changed files with 4,567 additions and 1,072 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ hs_err_pid*
# Eclipse
/.project
/.classpath
/.settings

# Idea
/redis-replicator.iml
/.idea
/.idea
/bin/
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
##Before pull request

* execute below commands and make sure you can pass all testcase.
* execute below commands and make sure you can pass all test cases.

```
sudo wget https://github.com/antirez/redis/archive/3.2.3.tar.gz && tar -xvzf 3.2.3.tar.gz && cd redis-3.2.3 && make && cd src && nohup ./redis-server --port 6380 --requirepass test &
Expand Down
31 changes: 25 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ redis 2.4 - 4.0
<dependency>
<groupId>com.moilioncircle</groupId>
<artifactId>redis-replicator</artifactId>
<version>2.1.2</version>
<version>2.2.0</version>
</dependency>
```

Expand Down Expand Up @@ -319,7 +319,7 @@ redis 2.4 - 4.0

@Override
public YourAppendCommand parse(Object[] command) {
return new YourAppendCommand((String) command[1], (String) command[2]);
return new YourAppendCommand(new String((byte[]) command[1], CHARSET), new String((byte[]) command[2], CHARSET));
}
}

Expand Down Expand Up @@ -396,8 +396,8 @@ redis 2.4 - 4.0
public class HelloTypeParser implements CommandParser<HelloTypeCommand> {
@Override
public HelloTypeCommand parse(Object[] command) {
String key = (String) command[1];
long value = Long.parseLong((String) command[2]);
String key = new String((byte[]) command[1], Constants.CHARSET);
long value = Long.parseLong(new String((byte[]) command[2], Constants.CHARSET));
return new HelloTypeCommand(key, value);
}
}
Expand Down Expand Up @@ -575,7 +575,8 @@ default `Configuration.getReadTimeout()` is 30 seconds

## 5.8. Handle raw bytes

* when kv.getValueRdbType() == 0, you can get the raw bytes of value. In some cases(e.g. HyperLogLog),this is very useful.
* for any `KeyValuePair` type except `KeyStringValueModule`, we can get the raw bytes. In some cases(e.g. HyperLogLog),this is very useful.


```java
Replicator replicator = new RedisReplicator("127.0.0.1", 6379, Configuration.defaultSetting());
Expand All @@ -584,16 +585,34 @@ default `Configuration.getReadTimeout()` is 30 seconds
public void handle(Replicator replicator, KeyValuePair<?> kv) {
if (kv instanceof KeyStringValueString) {
KeyStringValueString ksvs = (KeyStringValueString) kv;
System.out.println(Arrays.toString(ksvs.getRawBytes()));
byte[] rawValue = ksvs.getRawValue();
// handle raw bytes value
} else if (kv instanceof KeyStringValueHash) {
KeyStringValueHash ksvh = (KeyStringValueHash) kv;
Map<byte[], byte[]> rawValue = ksvh.getRawValue();
// handle raw bytes value
} else {
...
}
}
});
replicator.open();
```

for easy operation, the key of return type `Map<byte[], byte[]>` of `KeyStringValueHash.getRawValue`, we can `get` and `put` the key as `value type`

```java
KeyStringValueHash ksvh = (KeyStringValueHash) kv;
Map<byte[], byte[]> rawValue = ksvh.getRawValue();
byte[] value = new byte[]{2};
rawValue.put(new byte[]{1}, value);
System.out.println(rawValue.get(new byte[]{1}) == value) //will print true
```

# 6. Contributors
* Leon Chen
* Adrian Yao
* Trydofor

# 7. References
* [rdb.c](https://github.com/antirez/redis/blob/unstable/src/rdb.c)
Expand Down
30 changes: 24 additions & 6 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ redis 2.4 - 4.0
<dependency>
<groupId>com.moilioncircle</groupId>
<artifactId>redis-replicator</artifactId>
<version>2.1.2</version>
<version>2.2.0</version>
</dependency>
```

Expand Down Expand Up @@ -325,7 +325,7 @@ redis 2.4 - 4.0

@Override
public YourAppendCommand parse(Object[] command) {
return new YourAppendCommand((String) command[1], (String) command[2]);
return new YourAppendCommand(new String((byte[]) command[1], CHARSET), new String((byte[]) command[2], CHARSET));
}
}

Expand Down Expand Up @@ -402,8 +402,8 @@ redis 2.4 - 4.0
public class HelloTypeParser implements CommandParser<HelloTypeCommand> {
@Override
public HelloTypeCommand parse(Object[] command) {
String key = (String) command[1];
long value = Long.parseLong((String) command[2]);
String key = new String((byte[])command[1],Constants.CHARSET);
long value = Long.parseLong(new String((byte[])command[2],Constants.CHARSET));
return new HelloTypeCommand(key, value);
}
}
Expand Down Expand Up @@ -581,7 +581,7 @@ redis 2.4 - 4.0

## 5.8. 处理原始字节数组

* 当kv.getValueRdbType() == 0时, 可以得到原始的字节数组. 在某些情况(比如HyperLogLog)下会很有用.
* `KeyStringValueModule`以外的kv类型, 都可以得到原始的字节数组. 在某些情况(比如HyperLogLog)下会很有用.

```java
Replicator replicator = new RedisReplicator("127.0.0.1", 6379, Configuration.defaultSetting());
Expand All @@ -590,16 +590,34 @@ redis 2.4 - 4.0
public void handle(Replicator replicator, KeyValuePair<?> kv) {
if (kv instanceof KeyStringValueString) {
KeyStringValueString ksvs = (KeyStringValueString) kv;
System.out.println(Arrays.toString(ksvs.getRawBytes()));
byte[] rawValue = ksvs.getRawValue();
// handle raw bytes value
} else if (kv instanceof KeyStringValueHash) {
KeyStringValueHash ksvh = (KeyStringValueHash) kv;
Map<byte[], byte[]> rawValue = ksvh.getRawValue();
// handle raw bytes value
} else {
...
}
}
});
replicator.open();
```

为了操作简便`KeyStringValueHash.getRawValue`返回的`Map<byte[], byte[]>`中的key可以当做值类型存取

```java
KeyStringValueHash ksvh = (KeyStringValueHash) kv;
Map<byte[], byte[]> rawValue = ksvh.getRawValue();
byte[] value = new byte[]{2};
rawValue.put(new byte[]{1}, value);
System.out.println(rawValue.get(new byte[]{1}) == value) //will print true
```

# 6. 贡献者
* Leon Chen
* Adrian Yao
* Trydofor

# 7. 相关引用
* [rdb.c](https://github.com/antirez/redis/blob/unstable/src/rdb.c)
Expand Down
2 changes: 2 additions & 0 deletions examples/com/moilioncircle/examples/AofFileExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class AofFileExample {

public static void main(String[] args) throws IOException {
final Replicator replicator = new RedisReplicator(
new File("appendonly.aof"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class AofInputStreamExample {
public static void main(String[] args) throws IOException {
final Replicator replicator = new RedisReplicator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class BroadcastEventExample {
public static void main(String[] args) throws IOException {
Replicator replicator = new RedisReplicator("127.0.0.1", 6379, Configuration.defaultSetting());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class CommandBackupExample {
public static void main(String[] args) throws IOException {
final FileOutputStream out = new FileOutputStream(new File("./appendonly.aof"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import com.moilioncircle.redis.replicator.cmd.CommandName;
import com.moilioncircle.redis.replicator.cmd.CommandParser;

import static com.moilioncircle.redis.replicator.Constants.CHARSET;

/**
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class CommandParserExample {
public static void main(String[] args) throws Exception {
final Replicator replicator = new RedisReplicator("127.0.0.1", 6379, Configuration.defaultSetting());
Expand All @@ -51,10 +54,14 @@ public static class YourAppendParser implements CommandParser<YourAppendParser.Y

@Override
public YourAppendCommand parse(Object[] command) {
return new YourAppendCommand((String) command[1], (String) command[2]);
return new YourAppendCommand(new String((byte[]) command[1], CHARSET), new String((byte[]) command[2], CHARSET));
}

public static class YourAppendCommand implements Command {
/**
*
*/
private static final long serialVersionUID = 1L;
public final String key;
public final String value;

Expand Down
1 change: 1 addition & 0 deletions examples/com/moilioncircle/examples/MixFileExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class MixFileExample {
public static void main(String[] args) throws IOException {
final Replicator replicator = new RedisReplicator(new File("appendonly.aof"), FileType.MIXED,
Expand Down
15 changes: 13 additions & 2 deletions examples/com/moilioncircle/examples/ModuleParserExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
import java.io.IOException;
import java.util.Arrays;

import static com.moilioncircle.redis.replicator.Constants.CHARSET;

/**
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class ModuleParserExample {
public static void main(String[] args) throws IOException {
RedisReplicator replicator = new RedisReplicator("127.0.0.1", 6379, Configuration.defaultSetting());
Expand Down Expand Up @@ -80,6 +83,10 @@ public HelloTypeModule parse(RedisInputStream in) throws IOException {
}

public static class HelloTypeModule implements Module {
/**
*
*/
private static final long serialVersionUID = 1L;
private final long[] value;

public HelloTypeModule(long[] value) {
Expand All @@ -101,13 +108,17 @@ public String toString() {
public static class HelloTypeParser implements CommandParser<HelloTypeCommand> {
@Override
public HelloTypeCommand parse(Object[] command) {
String key = (String) command[1];
long value = Long.parseLong((String) command[2]);
String key = new String((byte[]) command[1], CHARSET);
long value = Long.parseLong(new String((byte[]) command[2], CHARSET));
return new HelloTypeCommand(key, value);
}
}

public static class HelloTypeCommand implements Command {
/**
*
*/
private static final long serialVersionUID = 1L;
private final String key;
private final long value;

Expand Down
1 change: 1 addition & 0 deletions examples/com/moilioncircle/examples/RdbBackupExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class RdbBackupExample {
public static void main(String[] args) throws IOException {

Expand Down
1 change: 1 addition & 0 deletions examples/com/moilioncircle/examples/RdbFileExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class RdbFileExample {
public static void main(String[] args) throws IOException {
final Replicator replicator = new RedisReplicator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class RdbInputStreamExample {
public static void main(String[] args) throws IOException {
final Replicator replicator = new RedisReplicator(
Expand Down
1 change: 1 addition & 0 deletions examples/com/moilioncircle/examples/SocketExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class SocketExample {
public static void main(String[] args) throws IOException {
final Replicator replicator = new RedisReplicator(
Expand Down
1 change: 1 addition & 0 deletions examples/com/moilioncircle/examples/TimerTaskExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class TimerTaskExample {
public static void main(String[] args) throws IOException {
final Timer timer = new Timer("sync");
Expand Down
15 changes: 13 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>com.moilioncircle</groupId>
<artifactId>redis-replicator</artifactId>
<version>2.1.3</version>
<version>2.2.0</version>

<name>redis-replicator</name>
<description>Redis Replicator is a redis RDB and Command parser written in java.
Expand Down Expand Up @@ -56,13 +56,24 @@
<organization>unstudy</organization>
<timezone>+8</timezone>
</developer>

<developer>
<name>Trydofor</name>
<email>[email protected]</email>
<organization>moilioncircle</organization>
<organizationUrl>http://www.moilioncircle.com/</organizationUrl>
<roles>
<role>Developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>

<scm>
<connection>scm:git:[email protected]:leonchen83/redis-replicator.git</connection>
<url>scm:git:[email protected]:leonchen83/redis-replicator.git</url>
<developerConnection>scm:git:[email protected]:leonchen83/redis-replicator.git</developerConnection>
<tag>2.1.3</tag>
<tag>2.2.0</tag>
</scm>

<issueManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void builtInCommandParserRegister() {

protected void doClose() throws IOException {
if (inputStream != null) try {
this.inputStream.removeRawByteListener(this);
this.inputStream.setRawByteListeners(null);
inputStream.close();
} catch (IOException ignore) { /*NOP*/ }
doCloseListener(this);
Expand Down
Loading

0 comments on commit db31c9b

Please sign in to comment.