<commit_before><?php
/* Notice:  Copyright 2016, The Care Connections Initiative c.i.c.
 * Author:  <NAME> <<EMAIL>>
 * License: Apache License, Version 2 (http://www.apache.org/licenses/LICENSE-2.0)
 */

use Flintstone\Flintstone;

class NetspaceKvs implements SpringDvs\iNetspace {
	private $geosubNetspace;
	private $geotopNetspace;
	
	public function __construct($unitTest = false, $testDir = "") {
		echo __DIR__;
		$options = array();
		if( $unitTest ) {
			$options['dir'] = $testDir;
		} else {
			// Actual init
		}
		$this->geosubNetspace = new Flintstone('gsn_testunit', $options);
		$this->geotopNetspace = new Flintstone('gtn_testunit', $options);
	}


	public function gsnNodesByAddress($address) {
		
		$addr = is_string($address) ? 
				$address : 
				SpringDvs\Node::addressToString($address);
		
				
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			if($v['address'] == $addr) {
				return $this->constructNode ($s, $v);
			}
		}
		
		return false;		
	}

	public function gsnNodeByHostname($hostname) {
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			if($v['hostname'] == $hostname) {
				return $this->constructNode ($s, $v);
			}
		}
		
		return false;
	}

	public function gsnNodeBySpringName($springname) {
		if( ($n = $this->geosubNetspace->get($springname)) )
			return $this->constructNode($springname, $n);
		else
			return false;
	}

	public function gsnNodesByType($types) {
		$list = array();
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			if($v['types'] & $types) {
				$list[] = $this->constructNode ($s, $v);
			}
		}
		
		return $list;
	}

	public function gsnNodesByState($state) {
		$list = array();
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			if($v['status'] == $state) {
				$list[] = $this->constructNode ($s, $v);
			}
		}
		
		return $list;
	}

	public function gsnNodes() {
		$list = array();
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			$list[] = $this->constructNode($s, $v);
		}
		
		return $list;
	}

	public function gsnNodeRegister($node) {
		if($this->geosubNetspace->get($node->springname()) !== false)
			return false; 
 
		$nodeArray = array(
			'hostname' => $node->toHostResource(),
			'address' => SpringDvs\Node::addressToString($node->address()),
			'service' => $node->service(),
			'status' => SpringDvs\DvspNodeState::disabled,
			'types' => $node->types(),
		);
		
		$this->geosubNetspace->set($node->springname(), $nodeArray);
	}

	public function gsnNodeUnregister($node) {
		if($this->geosubNetspace->get($node->springname()) === false)
			return false;

		$this->geosubNetspace->delete($node->springname());
	}

	public function gsnNodeUpdate($node) {
		$details = $this->geosubNetspace->get($node->springname());
		
		if($details === false) return false;
		$details['status'] = $node->state();
		
		$this->geosubNetspace->set($node->springname(), $details);
	}

	public function gtnRootNodes() {
		
	}

	public function gtnGeosubs() {
		
	}

	public function gtnGeosubRegister($node, $geosub) {
		$key = $node->springname() ."__".$geosub;
		if($this->geotopNetspace->get($key) !== false)
			return false; 
 
		$nodeArray = array(
			'hostname' => $node->toHostResource(),
			'address' => SpringDvs\Node::addressToString($node->address()),
			'service' => $node->service(),
			'priority' => SpringDvs\DvspNodeState::disabled,
			'geosub' => $geosub,
		);
		
		$this->geotopNetspace->set($key, $nodeArray);		
	}

	public function gtnGeosubUnregister($node, $geosub) {

		$key = $node->springname() ."__".$geosub;

		if($this->geotopNetspace->get($key) === false)
			return false; 
		
		$this->geotopNetspace->delete($key);
	}

	public function gtnGeosubRootNodes($geosub) {

		$list = array();

		foreach($this->geotopNetspace->getKeys() as $k) {
			$v = $this->geotopNetspace->get($k);
			if($v['geosub'] == $geosub)
				$list[] = $this->constructRootNode($k, $v);
		}

		return $list;
	}
<commit_msg>netspace: Fix return true on success<commit_after><?php
/* Notice:  Copyright 2016, The Care Connections Initiative c.i.c.
 * Author:  <NAME>-Gauld <<EMAIL>>
 * License: Apache License, Version 2 (http://www.apache.org/licenses/LICENSE-2.0)
 */

use Flintstone\Flintstone;

class NetspaceKvs implements SpringDvs\iNetspace {
	private $geosubNetspace;
	private $geotopNetspace;
	
	public function __construct($unitTest = false, $testDir = "") {
		$options = array();
		if( $unitTest ) {
			$options['dir'] = $testDir;
		} else {
			// Actual init
		}
		$this->geosubNetspace = new Flintstone('gsn_testunit', $options);
		$this->geotopNetspace = new Flintstone('gtn_testunit', $options);
	}


	public function gsnNodesByAddress($address) {
		
		$addr = is_string($address) ? 
				$address : 
				SpringDvs\Node::addressToString($address);
		
				
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			if($v['address'] == $addr) {
				return $this->constructNode ($s, $v);
			}
		}
		
		return false;		
	}

	public function gsnNodeByHostname($hostname) {
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			if($v['hostname'] == $hostname) {
				return $this->constructNode ($s, $v);
			}
		}
		
		return false;
	}

	public function gsnNodeBySpringName($springname) {
		if( ($n = $this->geosubNetspace->get($springname)) )
			return $this->constructNode($springname, $n);
		else
			return false;
	}

	public function gsnNodesByType($types) {
		$list = array();
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			if($v['types'] & $types) {
				$list[] = $this->constructNode ($s, $v);
			}
		}
		
		return $list;
	}

	public function gsnNodesByState($state) {
		$list = array();
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			if($v['status'] == $state) {
				$list[] = $this->constructNode ($s, $v);
			}
		}
		
		return $list;
	}

	public function gsnNodes() {
		$list = array();
		foreach($this->geosubNetspace->getKeys() as $s) {
			$v = $this->geosubNetspace->get($s);
			$list[] = $this->constructNode($s, $v);
		}
		
		return $list;
	}

	public function gsnNodeRegister($node) {
		if($this->geosubNetspace->get($node->springname()) !== false)
			return false; 
 
		$nodeArray = array(
			'hostname' => $node->toHostResource(),
			'address' => SpringDvs\Node::addressToString($node->address()),
			'service' => $node->service(),
			'status' => SpringDvs\DvspNodeState::disabled,
			'types' => $node->types(),
		);
		
		$this->geosubNetspace->set($node->springname(), $nodeArray);
		return true;
	}

	public function gsnNodeUnregister($node) {
		if($this->geosubNetspace->get($node->springname()) === false)
			return false;

		$this->geosubNetspace->delete($node->springname());
		return true;
	}

	public function gsnNodeUpdate($node) {
		$details = $this->geosubNetspace->get($node->springname());
		
		if($details === false) return false;
		$details['status'] = $node->state();
		
		$this->geosubNetspace->set($node->springname(), $details);
		return true;
	}

	public function gtnRootNodes() {
		
	}

	public function gtnGeosubs() {
		
	}

	public function gtnGeosubRegister($node, $geosub) {
		$key = $node->springname() ."__".$geosub;
		if($this->geotopNetspace->get($key) !== false)
			return false; 
 
		$nodeArray = array(
			'hostname' => $node->toHostResource(),
			'address' => SpringDvs\Node::addressToString($node->address()),
			'service' => $node->service(),
			'priority' => SpringDvs\DvspNodeState::disabled,
			'geosub' => $geosub,
		);
		
		$this->geotopNetspace->set($key, $nodeArray);
		return true;
	}

	public function gtnGeosubUnregister($node, $geosub) {

		$key = $node->springname() ."__".$geosub;

		if($this->geotopNetspace->get($key) === false)
			return false; 
		
		$this->geotopNetspace->delete($key);
		return true;
	}

	public function gtnGeosubRootNodes($geosub) {

		$list = array();

		foreach($this->geotopNetspace->getKeys() as $k) {
			$v = $this->geotopNetspace->get($k);
			if($v['geosub'] == $geosub)
				$list[] = $this->constructRootNode($k, $v);
		}

		return $list;
	}

{'fraction_non_alphanumeric': 0.19441098530474585, 'fraction_numerical': 0.0016863406408094434, 'mean_word_length': 4.016918429003021, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 171, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3925653', 'n_tokens_mistral': 3444, 'n_tokens_neox': 3012, 'n_words': 519}