Skip to content

Instantly share code, notes, and snippets.

@ukd1
Created November 29, 2010 20:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukd1/720565 to your computer and use it in GitHub Desktop.
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
<?php
/**
* Delete all keys from a riak bucket using key streaming
*
* History:
*
* 29-Nov-10
* First version
*
* @author Russell Smith <russell.smith@ukd1.co.uk>
* @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