<?php

$key_space = 'png';
$column_family = 'png';
$column_family_date = 'date';

$graph_base_dir="./png/";

require_once 'Cassandra.php';


// list of seed servers to randomly connect to
// all the parameters are optional and default to given values
$servers = array(
	array(
		'host' => '127.0.0.1',
		'port' => 9160,
		'use-framed-transport' => true,
		'send-timeout-ms' => 1000,
		'receive-timeout-ms' => 1000
	)
);

$cassandra = Cassandra::createInstance($servers);
$cassandra->useKeyspace($key_space);
$cassandra->setMaxCallRetries(5);

/*
$cassandra->createStandardColumnFamily(
	$key_space, // keyspace name
	$column_family, // the column-family name
	array( // list of columns with metadata
		array(
			'name' => 'name',
			'type' => Cassandra::TYPE_UTF8,
			'index-type' => Cassandra::INDEX_KEYS, // create secondary index
			'index-name' => 'NameIdx'
		),
		array(
			'name' => 'content',
			'type' => Cassandra::TYPE_BYTES,
		),
		array(
			'name' => 'type',
			'type' => Cassandra::TYPE_UTF8,
		),
		array(
			'name' => 'date',
			'type' => Cassandra::TYPE_INTEGER,
			'index-type' => Cassandra::INDEX_KEYS,
			'index-name' => 'DateIdx'
		),
		array(
			'name' => 'hour',
			'type' => Cassandra::TYPE_INTEGER,
			'index-type' => Cassandra::INDEX_KEYS,
			'index-name' => 'HourIdx'
		)
	)
// actually accepts more parameters with reasonable defaults
);
*/

$od_graph_base=opendir($graph_base_dir);
while ($date_dir=readdir($od_graph_base))
{
	if ($date_dir != "." && $date_dir != "..") {
		$data_date = $date_dir;
		$tmp=$graph_base_dir.$date_dir;
		if(is_dir($tmp)){
//			echo $tmp."<br>";
			$arr_dir_list[]=$tmp;
			$od_file=opendir($tmp);
			while ($file_name=readdir($od_file))
			{
				$file_full_path = $tmp."/".$file_name;
				if(is_file($file_full_path)){
					$data_hr= substr($file_name,9,2);
					$data_name=$data_date.$data_hr;
					$data_type=substr($file_name,-3,3);
					$data_content=file_get_contents($file_full_path);
					echo $file_full_path."=>";
					echo " ,name=".$data_name;
					echo " ,date=".$data_date;
					echo " ,hour=".$data_hr;
					echo " ,type=".$data_type;
					echo "<br>";
					echo cass_insert($data_name,$data_content,$data_date,$data_hr,$data_type);
				}
			}
		}
	}
}
closedir ($od_graph_base);
closedir ($od_file);


//$target = $cassandra->get($id);
//echo "content :".$target["name"].": <pre>".print_r($target, true).'</pre><hr/>';


function cass_insert($name,$content,$date,$hour,$type){
	global $cassandra, $column_family, $column_family_date, $key_space ;
	$ret_png= $cassandra->cf($column_family)->set(
		$name,
		array(
			'name' => $name,
			'content' => $content,
			'date' => $date,
			'hour' => $hour,
			'type' => $type
		)
	);
	$ret_date= $cassandra->cf($column_family_date)->set(
		$date,
		array(
			'date' => $date,
			$hour => "1",
		)
	);
	if ($ret_date && $ret_png) return true;
	else return false;
}

?>