Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide proper implementation for streaming results of RiakClient#map…
…Reduce and RiakClient#listKeys. If you forget to close a connection, the connection is closed properly upon garbage collection.
- Loading branch information
1 parent
8d1370c
commit a5d72e4
Showing
7 changed files
with
343 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,89 @@ | |||
/** | |||
* This file is part of riak-java-pb-client | |||
* | |||
* Copyright (c) 2010 by Trifork | |||
* | |||
* 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. | |||
**/ | |||
|
|||
package com.trifork.riak; | |||
|
|||
import java.io.IOException; | |||
import java.util.NoSuchElementException; | |||
|
|||
import com.google.protobuf.ByteString; | |||
import com.trifork.riak.RPB.RpbListKeysResp; | |||
|
|||
public class KeySource extends RiakStreamClient<ByteString> { | |||
|
|||
private RpbListKeysResp r; | |||
private int i; | |||
|
|||
public KeySource(RiakClient client, RiakConnection conn) throws IOException { | |||
super(client, conn); | |||
get_next_response(); | |||
} | |||
|
|||
public boolean hasNext() throws IOException { | |||
if (isClosed()) { | |||
return false; | |||
} | |||
|
|||
if (r_is_exhausted()) { | |||
get_next_response(); | |||
} | |||
|
|||
return !isClosed(); | |||
} | |||
|
|||
private boolean r_is_exhausted() { | |||
return i == r.getKeysCount(); | |||
} | |||
|
|||
public ByteString next() throws IOException { | |||
if (!hasNext()) | |||
throw new NoSuchElementException(); | |||
return r.getKeys(i++); | |||
} | |||
|
|||
private void get_next_response() throws IOException { | |||
if (isClosed()) | |||
return; | |||
|
|||
// either we're in the first call (r == null) | |||
// or we got here because we ran out of keys. | |||
assert r == null || r_is_exhausted(); | |||
|
|||
do { | |||
|
|||
if (r != null) { | |||
if (r.hasDone() && r.getDone()) { | |||
close(); | |||
return; | |||
} | |||
} | |||
|
|||
byte[] data = conn.receive(RiakMessageCodes.MSG_ListKeysResp); | |||
if (data == null) { | |||
close(); | |||
throw new IOException("received empty response"); | |||
} | |||
|
|||
r = RPB.RpbListKeysResp.parseFrom(data); | |||
i = 0; | |||
|
|||
// did we got an empty chunk? get another one. | |||
} while (r.getKeysCount() == 0); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,89 @@ | |||
/** | |||
* This file is part of riak-java-pb-client | |||
* | |||
* Copyright (c) 2010 by Trifork | |||
* | |||
* 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. | |||
**/ | |||
|
|||
package com.trifork.riak; | |||
|
|||
import java.io.IOException; | |||
import java.util.NoSuchElementException; | |||
|
|||
import com.google.protobuf.ByteString; | |||
import com.trifork.riak.RPB.RpbMapRedResp; | |||
import com.trifork.riak.mapreduce.MapReduceResponse; | |||
|
|||
public class MapReduceResponseSource extends | |||
RiakStreamClient<MapReduceResponse> { | |||
|
|||
private RpbMapRedResp r; | |||
private boolean is_given; | |||
private final ByteString contentType; | |||
|
|||
protected MapReduceResponseSource(RiakClient client, RiakConnection conn, | |||
ByteString contentType) throws IOException { | |||
super(client, conn); | |||
this.contentType = contentType; | |||
get_next_response(); | |||
} | |||
|
|||
@Override | |||
public boolean hasNext() throws IOException { | |||
if (isClosed()) { | |||
return false; | |||
} | |||
|
|||
if (is_given) { | |||
get_next_response(); | |||
} | |||
|
|||
return !isClosed(); | |||
} | |||
|
|||
@Override | |||
public MapReduceResponse next() throws IOException { | |||
if (!hasNext()) | |||
throw new NoSuchElementException(); | |||
is_given = true; | |||
return new MapReduceResponse(r, contentType); | |||
} | |||
|
|||
private void get_next_response() throws IOException { | |||
if (isClosed()) | |||
return; | |||
|
|||
// either we're in the first call (r == null) | |||
// or we got here because we gave the reply away. | |||
assert r == null || is_given; | |||
|
|||
if (r != null && is_given) { | |||
if (r.hasDone() && r.getDone()) { | |||
close(); | |||
return; | |||
} | |||
} | |||
|
|||
byte[] data = conn.receive(RiakMessageCodes.MSG_MapRedResp); | |||
if (data == null) { | |||
close(); | |||
throw new IOException("received empty response"); | |||
} | |||
|
|||
r = RPB.RpbMapRedResp.parseFrom(data); | |||
is_given = false; | |||
|
|||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.