Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
/*
How to use:
Get this british words dictionary from here: http://www.curlewcommunications.co.uk/wordlist.html
IN PHP install the mypeb extension from source:
http://code.google.com/p/mypeb/
Then in the command line do:
erl -sname node -setcookie abc
ets:new(test, [set, named_table, public]).
*/
class Ets
{
protected $link;
protected $name;
public function __construct($link, $name)
{
$this->link = $link;
$this->name = $name;
}
public function newTable($options)
{
$options = array_merge($options, array('named_table', 'public'));
$x = peb_encode("[~a, [" . $this->repeatFormat('~a', $options) . "]]", array(array(
$this->name,
$options
)));
$result = peb_rpc("ets", "new", $x, $this->link);
return peb_decode($result);
}
public function insert($key, $value)
{
$x = peb_encode("[~a, {~a, ~s}]", array(array(
$this->name,
array($key, $value)
)));
$result = peb_rpc("ets", "insert", $x, $this->link);
return peb_decode($result);
}
public function bulkInsert($values)
{
$x = peb_encode("[~a, [" . $this->repeatFormat('{~a, ~s}', $values). "]]",
array(array(
$this->name,
$values
))
);
$result = peb_rpc("ets", "insert", $x, $this->link);
return peb_decode($result);
}
public function info($item)
{
$x = peb_encode("[~a, ~a]", array(
array($this->name, $item)
));
$result = peb_rpc("ets", "info", $x, $this->link);
return peb_decode($result);
}
public function delete_all_objects()
{
$x = peb_encode("[~a]", array(
array($this->name)
));
$result = peb_rpc("ets", "delete_all_objects", $x, $this->link);
return peb_decode($result);
}
public function tab2list()
{
$x = peb_encode("[~a]", array(array(
$this->name
)));
$result = peb_rpc("ets", "insert", $x, $this->link);
return peb_decode($result);
}
protected function repeatFormat($format, $arr)
{
return implode(', ', array_fill(0, count($arr), $format));
}
}
$link = peb_connect('node@127.0.0.1', 'abc', 1);
$ets = new Ets($link, 'test');
$tmp = array();
$words = file('./brit-a-z.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($words as $word)
{
$tmp[] = array($word, $word);
}
//insert in chunks of 5000 objects
$chunk = array_chunk($tmp, 5000);
echo "Empty Table: \n";
var_dump($ets->delete_all_objects());
echo "Table Size: \n";
var_dump($ets->info('size'));
$start = microtime(true);
foreach($chunk as $v)
{
$ets->bulkInsert($v);
}
echo "Elapsed Time: ", (microtime(true) - $start), "\n";
echo "Table Size: \n";
var_dump($ets->info('size'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment