Created
November 29, 2010 20:38
-
-
Save ukd1/720565 to your computer and use it in GitHub Desktop.
Allows deleting of all keys in a riak bucket using the streaming api
This file contains hidden or 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
<?php | |
/** | |
* Delete all keys from a riak bucket using key streaming | |
* | |
* History: | |
* | |
* 29-Nov-10 | |
* First version | |
* | |
* @author Russell Smith <[email protected]> | |
* @copyright UKD1 Limited 2010 | |
* @license licence.txt ISC license | |
* @see https://gist.github.com/gists/720565 | |
*/ | |
// comment this out, just incase you run it and trash something useful... | |
die('You should have commented this out...'); | |
// Enter your riak URL here, including the bucket... | |
define('RIAK_URL', 'http://127.0.0.1:8098/riak/test/'); | |
// You shouldn't need to change this, it allows extracting the | |
// json block of keys from the stream. | |
define('REGEX', '~'.preg_quote('{"keys":[').'(.+?)\]\}~'); | |
// Init curl, 5 sec timeout...? | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
// delete the specified key | |
function delete($key) { | |
global $ch; | |
curl_setopt($ch, CURLOPT_URL, RIAK_URL . $key); | |
curl_exec($ch); | |
$tmp = curl_getinfo($ch); | |
return $tmp['http_code'] === 204; | |
} | |
// stream the keys | |
$f = fopen(RIAK_URL . '?keys=stream&props=false', 'r'); | |
// empty buffer | |
$buf = ''; | |
// stream whilst not eof | |
while (!feof($f)) { | |
// read 1024 bytes | |
$buf .= fgets($f, 1024); | |
// repeatedly check for matches against the buffer | |
// as sometimes you might get two json blocks in | |
// the read size | |
while (preg_match(REGEX, $buf, $matches)) { | |
// decode | |
$result = json_decode($matches[0]); | |
// run through each key and delete | |
array_map(function($k){ | |
print "$k\t" . (delete($k) ? '[deleted]' : '[failed]') . "\n"; | |
}, $result->keys); | |
// remove the keys we've just processed from the block | |
$buf = substr($buf, strlen($matches[0])); | |
} | |
} | |
// close | |
fclose($f); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment