StringHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 74
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseTableName() 0 3 1
A completeTable() 0 7 1
A getInsertValues() 0 13 2
A getEqualityFieldValue() 0 7 2
A hash() 0 7 2
1
<?php
2
3
namespace Wikibase\QueryEngine\SQLStore\DVHandler;
4
5
use DataValues\DataValue;
6
use DataValues\StringValue;
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 StringValue 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 StringHandler 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 'string';
36
	}
37
38
	/**
39
	 * @see DataValueHandler::completeTable
40
	 */
41
	protected function completeTable( Table $table ) {
42
		$table->addColumn( 'value', Type::TEXT );
43
		$table->addColumn( 'hash', Type::STRING, array( 'length' => StringHasher::LENGTH ) );
44
45
		// TODO: Is an index on the first 255 bytes/chars of each BLOB/CLOB column possible?
46
		$table->addIndex( array( 'hash' ) );
47
	}
48
49
	/**
50
	 * @see DataValueHandler::getInsertValues
51
	 *
52
	 * @param DataValue $value
53
	 *
54
	 * @return array
55
	 * @throws InvalidArgumentException
56
	 */
57
	public function getInsertValues( DataValue $value ) {
58
		if ( !( $value instanceof StringValue ) ) {
59
			throw new InvalidArgumentException( 'Value is not a StringValue.' );
60
		}
61
62
		$values = array(
63
			'value' => $value->getValue(),
64
65
			'hash' => $this->getEqualityFieldValue( $value ),
66
		);
67
68
		return $values;
69
	}
70
71
	/**
72
	 * @see DataValueHandler::getEqualityFieldValue
73
	 *
74
	 * @param DataValue $value
75
	 *
76
	 * @return string
77
	 * @throws InvalidArgumentException
78
	 */
79
	public function getEqualityFieldValue( DataValue $value ) {
80
		if ( !( $value instanceof StringValue ) ) {
81
			throw new InvalidArgumentException( 'Value is not a StringValue.' );
82
		}
83
84
		return $this->hash( $value->getValue() );
85
	}
86
87
	private function hash( $string ) {
88
		if ( $this->stringHasher === null ) {
89
			$this->stringHasher = new StringHasher();
90
		}
91
92
		return $this->stringHasher->hash( $string );
93
	}
94
95
}
96