Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 4 commits
  • 8 files changed
  • 0 commit comments
  • 3 contributors
Commits on May 21, 2012
@robertotena robertotena Added 'insertIgnore' operation to avoid exception handling and
to let server message processing as String.
Remove unnecessary import.
d01de5d
Commits on May 22, 2012
Roberto Tena García Revert "Added 'insertIgnore' operation to avoid exception handling and"
This reverts commit d01de5d.
06f2733
Roberto Tena García Added 'insertIgnore' operation. 19e2f53
Commits on May 23, 2012
@killme2008 Merge pull request #5 from robertotena/master
Added 'insertIgnore' operation
1117fbe
View
9 src/main/java/com/google/code/hs4j/CommandFactory.java
@@ -55,6 +55,15 @@ public Command createOpenIndexCommand(String id, String db,
public Command createInsertCommand(String id, byte[][] values);
/**
+ * Create an insert ignore command
+ *
+ * @param id
+ * @param values
+ * @return
+ */
+ public Command createInsertIgnoreCommand(String id, byte[][] values);
+
+ /**
* create a find command
*
* @param id
View
13 src/main/java/com/google/code/hs4j/HSClient.java
@@ -332,6 +332,19 @@ public int delete(int indexId, String[] keys, FindOperator operator) throws Inte
public boolean insert(int indexId, String[] values) throws InterruptedException, TimeoutException,
HandlerSocketException;
+ /**
+ * Insert data and does not throw exception if it is unsuccessful
+ *
+ * @param indexId
+ * @param values
+ * the column values to set
+ * @return the server message received
+ * @throws InterruptedException
+ * @throws TimeoutException
+ * @throws HandlerSocketException
+ */
+ public String insertIgnore(int indexId, String[] values) throws InterruptedException, TimeoutException,
+ HandlerSocketException;
/**
* Shutdown client
View
13 src/main/java/com/google/code/hs4j/IndexSession.java
@@ -196,6 +196,19 @@ public boolean insert(String[] values) throws InterruptedException,
TimeoutException, HandlerSocketException;
/**
+ * Insert data, not throwing an exception if it is unsuccessful
+ *
+ * @param values
+ * the column values to set
+ * @return the server message if successful, or an empty string if there was an error.
+ * @throws InterruptedException
+ * @throws TimeoutException
+ * @throws HandlerSocketException
+ */
+ public String insertIgnore(String[] values) throws InterruptedException,
+ TimeoutException, HandlerSocketException;
+
+ /**
* Create a modify statement for this index session
*
* @return
View
17 src/main/java/com/google/code/hs4j/command/text/AbstractCommand.java
@@ -54,7 +54,8 @@
protected Object result;
private Future<Boolean> writeFuture;
private String exceptionMsg;
-
+ private boolean ignoreError = false;
+
public AbstractCommand() {
super();
this.latch = new CountDownLatch(1);
@@ -70,6 +71,14 @@ public void countDown() {
}
+ public boolean getIgnoreError() {
+ return ignoreError;
+ }
+
+ public void setIgnoreError(final boolean ignoreError) {
+ this.ignoreError = ignoreError;
+ }
+
public String getEncoding() {
return encoding;
}
@@ -231,7 +240,7 @@ public boolean decode(HandlerSocketSession session, IoBuffer buffer) {
}
int index = TERMIATER_MATCHER.matchFirst(buffer);
if (index > 0) {
- if (this.responseStatus == 0) {
+ if (this.responseStatus == 0 || ignoreError) {
this.copyDataFromBufferToBody(buffer, index
- buffer.position() + 1);
this.decodeBody(session, this.body, index);
@@ -281,7 +290,7 @@ protected void onDone() {
public String encodingString(byte[] data) {
try {
- return new String(data, this.encoding);
+ return new String(data, 0, data.length-1, this.encoding); // skip new line
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported encoding:" + this.encoding,
e);
@@ -299,7 +308,7 @@ public String encodingString(byte[] data) {
protected void decodeBody(HandlerSocketSession session, byte[] body,
int index) {
-
+ result = this.encodingString(body);
}
CountDownLatch getLatch() {
View
1  src/main/java/com/google/code/hs4j/command/text/OpenIndexCommand.java
@@ -11,7 +11,6 @@
*/
package com.google.code.hs4j.command.text;
-import com.google.code.hs4j.Filter;
import com.google.code.hs4j.network.buffer.IoBuffer;
import com.google.code.hs4j.utils.HSUtils;
View
7 src/main/java/com/google/code/hs4j/command/text/TextCommandFactory.java
@@ -50,6 +50,13 @@ public Command createInsertCommand(String id, byte[][] values) {
return result;
}
+ public Command createInsertIgnoreCommand(String id, byte[][] values) {
+ InsertCommand result = new InsertCommand(id, values);
+ result.setIgnoreError(true);
+ result.setEncoding(encoding);
+ return result;
+ }
+
public Command createFindCommand(String id, FindOperator operator,
String[] keys, int limit, int offset, String[] fieldList, Filter[] filter) {
FindCommand result = new FindCommand(id, operator, keys, limit, offset,
View
14 src/main/java/com/google/code/hs4j/impl/HSClientImpl.java
@@ -50,6 +50,8 @@
* @date 2010-11-29
*/
public class HSClientImpl implements HSClient {
+ private static final String EMPTY_STR = "";
+
private boolean started = false;
private final CommandFactory commandFactory;
@@ -338,6 +340,18 @@ public boolean insert(int indexId, String[] values)
return insert0(indexId, bytes);
}
+ public String insertIgnore(int indexId, String[] values)
+ throws InterruptedException, TimeoutException,
+ HandlerSocketException {
+ byte[][] bytes = HSUtils.getByteArrayFromStringArray(values,this.encoding);
+ Command cmd = this.commandFactory.createInsertIgnoreCommand(String
+ .valueOf(indexId), bytes);
+ this.connector.send(cmd);
+ this.awaitResponse(cmd);
+
+ return cmd.getResponseStatus() == 0 ? (String)cmd.getResult() : EMPTY_STR;
+ }
+
public ModifyStatement createStatement(int indexId)
throws HandlerSocketException {
IndexRecord indexRecord = getRecord(indexId);
View
5 src/main/java/com/google/code/hs4j/impl/IndexSessionImpl.java
@@ -87,6 +87,11 @@ public boolean insert(String[] values) throws InterruptedException,
return this.client.insert(this.indexId, values);
}
+ public String insertIgnore(String[] values) throws InterruptedException,
+ TimeoutException, HandlerSocketException {
+ return this.client.insertIgnore(this.indexId, values);
+ }
+
public int update(String[] keys, String[] values, FindOperator operator,
int limit, int offset) throws InterruptedException,
TimeoutException, HandlerSocketException {

No commit comments for this range

Something went wrong with that request. Please try again.