IriHandler::getEqualityFieldValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Wikibase\QueryEngine\SQLStore\DVHandler;
4
5
use DataValues\DataValue;
6
use DataValues\IriValue;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Types\Type;
9
use InvalidArgumentException;
10
use Wikibase\QueryEngine\SQLStore\DataValueHandler;
11
use Wikibase\QueryEngine\StringHasher;
12
13
/**
14
 * Represents the mapping between IriValue and
15
 * the corresponding table in the store.
16
 *
17
 * @since 0.1
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class IriHandler extends DataValueHandler {
23
24
	/**
25
	 * @var StringHasher|null
26
	 */
27
	private $stringHasher = null;
28
29
	/**
30
	 * @see DataValueHandler::getBaseTableName
31
	 *
32
	 * @return string
33
	 */
34
	protected function getBaseTableName() {
35
		return 'iri';
36
	}
37
38
	/**
39
	 * @see DataValueHandler::completeTable
40
	 *
41
	 * @param Table $table
42
	 */
43
	protected function completeTable( Table $table ) {
44
		// TODO: figure out what the max field lengths should be
45
		$table->addColumn( 'value_scheme',       Type::STRING, array( 'length' => 255 ) );
46
		$table->addColumn( 'value_hierarchical', Type::STRING, array( 'length' => 255 ) );
47
		$table->addColumn( 'value_query',        Type::STRING, array( 'length' => 255 ) );
48
		$table->addColumn( 'value_fragment',     Type::STRING, array( 'length' => 255 ) );
49
		$table->addColumn( 'hash',               Type::STRING, array( 'length' => StringHasher::LENGTH ) );
50
51
		// TODO: Is an index on the first 255 bytes/chars of each BLOB/CLOB column possible?
52
		$table->addIndex( array( 'hash' ) );
53
	}
54
55
	/**
56
	 * @see DataValueHandler::getInsertValues
57
	 *
58
	 * @param DataValue $value
59
	 *
60
	 * @throws InvalidArgumentException
61
	 * @return string[]
62
	 */
63
	public function getInsertValues( DataValue $value ) {
64
		if ( !( $value instanceof IriValue ) ) {
65
			throw new InvalidArgumentException( 'Value is not a IriValue.' );
66
		}
67
68
		$values = array(
69
			'value_scheme' => $value->getScheme(),
70
			'value_hierarchical' => $value->getHierarchicalPart(),
71
			'value_query' => $value->getQuery(),
72
			'value_fragment' => $value->getFragment(),
73
74
			'hash' => $this->getEqualityFieldValue( $value ),
75
		);
76
77
		return $values;
78
	}
79
80
	/**
81
	 * @see DataValueHandler::getEqualityFieldValue
82
	 *
83
	 * @param DataValue $value
84
	 *
85
	 * @throws InvalidArgumentException
86
	 * @return string
87
	 */
88
	public function getEqualityFieldValue( DataValue $value ) {
89
		if ( !( $value instanceof IriValue ) ) {
90
			throw new InvalidArgumentException( 'Value is not a IriValue.' );
91
		}
92
93
		return $this->hash( $value->getValue() );
94
	}
95
96
	private function hash( $string ) {
97
		if ( $this->stringHasher === null ) {
98
			$this->stringHasher = new StringHasher();
99
		}
100
101
		return $this->stringHasher->hash( $string );
102
	}
103
104
}
105