Skip to content

Commit 2eea88d

Browse files
committed
turn off value validation by default
this makes document creation faster
1 parent efd047e commit 2eea88d

File tree

4 files changed

+93
-90
lines changed

4 files changed

+93
-90
lines changed

lib/triagens/ArangoDb/Document.php

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Document
5353
*
5454
* @var bool
5555
*/
56-
protected $_changed;
56+
protected $_changed = false;
5757

5858
/**
5959
* Flag to indicate whether document is a new document (never been saved to the server)
@@ -62,6 +62,14 @@ class Document
6262
*/
6363
protected $_isNew = true;
6464

65+
/**
66+
* Flag to indicate whether validation of document values should be performed
67+
* This can be turned on, but has a performance penalty
68+
*
69+
* @var bool
70+
*/
71+
protected $_doValidate = false;
72+
6573
/**
6674
* Flag to indicate whether document was changed locally
6775
*
@@ -116,19 +124,22 @@ class Document
116124
*
117125
* @return Document
118126
*/
119-
public function __construct(array $options = array())
127+
public function __construct(array $options = null)
120128
{
121-
// keeping the non-underscored version for backwards-compatibility
122-
$this->setChanged(false);
123-
if (array_key_exists('hiddenAttributes', $options)) {
124-
$this->setHiddenAttributes($options['hiddenAttributes']);
125-
}
126-
if (array_key_exists('_hiddenAttributes', $options)) {
127-
$this->setHiddenAttributes($options['_hiddenAttributes']);
128-
}
129-
130-
if (array_key_exists('_isNew', $options)) {
131-
$this->setIsNew($options['_isNew']);
129+
if (is_array($options)) {
130+
// keeping the non-underscored version for backwards-compatibility
131+
if (isset($options['hiddenAttributes'])) {
132+
$this->setHiddenAttributes($options['hiddenAttributes']);
133+
}
134+
if (isset($options['_hiddenAttributes'])) {
135+
$this->setHiddenAttributes($options['_hiddenAttributes']);
136+
}
137+
if (isset($options[self::ENTRY_ISNEW])) {
138+
$this->setIsNew($options[self::ENTRY_ISNEW]);
139+
}
140+
if (isset($options['_validate'])) {
141+
$this->_doValidate = $options['_validate'];
142+
}
132143
}
133144
}
134145

@@ -150,7 +161,7 @@ public static function createFromArray(array $values, array $options = array())
150161
}
151162

152163
$document->setChanged(true);
153-
164+
154165
return $document;
155166
}
156167

@@ -255,39 +266,35 @@ public function filterHiddenAttributes($attributes)
255266
*/
256267
public function set($key, $value)
257268
{
258-
if (!is_string($key)) {
259-
throw new ClientException('Invalid document attribute key');
269+
if ($this->_doValidate) {
270+
// validate the value passed
271+
ValueValidator::validate($value);
260272
}
261273

262-
// validate the value passed
263-
ValueValidator::validate($value);
264-
265-
if ($key === self::ENTRY_ID) {
266-
$this->setInternalId($value);
267-
268-
return;
269-
}
270-
271-
if ($key === self::ENTRY_KEY) {
272-
$this->setInternalKey($value);
273-
274-
return;
275-
}
276-
277-
if ($key === self::ENTRY_REV) {
278-
$this->setRevision($value);
279-
280-
return;
281-
}
274+
if ($key[0] === '_') {
275+
if ($key === self::ENTRY_ID) {
276+
$this->setInternalId($value);
277+
return;
278+
}
282279

283-
if ($key === self::ENTRY_ISNEW) {
284-
$this->setIsNew($value);
280+
if ($key === self::ENTRY_KEY) {
281+
$this->setInternalKey($value);
282+
return;
283+
}
285284

286-
return;
285+
if ($key === self::ENTRY_REV) {
286+
$this->setRevision($value);
287+
return;
288+
}
289+
290+
if ($key === self::ENTRY_ISNEW) {
291+
$this->setIsNew($value);
292+
return;
293+
}
287294
}
288295

289-
if (!$this->_changed) {
290-
if (!isset($this->_values[$key]) || $this->_values[$key] !== $value) {
296+
if (! $this->_changed) {
297+
if (! isset($this->_values[$key]) || $this->_values[$key] !== $value) {
291298
// set changed flag
292299
$this->_changed = true;
293300
}

lib/triagens/ArangoDb/Edge.php

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,46 +79,40 @@ public function __clone()
7979
*/
8080
public function set($key, $value)
8181
{
82-
if (!is_string($key)) {
83-
throw new ClientException('Invalid document attribute key');
82+
if ($this->_doValidate) {
83+
// validate the value passed
84+
ValueValidator::validate($value);
8485
}
8586

86-
// validate the value passed
87-
ValueValidator::validate($value);
88-
89-
if ($key === self::ENTRY_ID) {
90-
$this->setInternalId($value);
91-
92-
return;
93-
}
94-
95-
if ($key === self::ENTRY_KEY) {
96-
$this->setInternalKey($value);
97-
98-
return;
99-
}
100-
101-
if ($key === self::ENTRY_REV) {
102-
$this->setRevision($value);
103-
104-
return;
105-
}
87+
if ($key[0] === '_') {
88+
if ($key === self::ENTRY_ID) {
89+
$this->setInternalId($value);
90+
return;
91+
}
10692

107-
if ($key === self::ENTRY_FROM) {
108-
$this->setFrom($value);
93+
if ($key === self::ENTRY_KEY) {
94+
$this->setInternalKey($value);
95+
return;
96+
}
10997

110-
return;
111-
}
98+
if ($key === self::ENTRY_REV) {
99+
$this->setRevision($value);
100+
return;
101+
}
112102

113-
if ($key === self::ENTRY_TO) {
114-
$this->setTo($value);
103+
if ($key === self::ENTRY_FROM) {
104+
$this->setFrom($value);
105+
return;
106+
}
115107

116-
return;
108+
if ($key === self::ENTRY_TO) {
109+
$this->setTo($value);
110+
return;
111+
}
117112
}
118113

119-
120-
if (!$this->_changed) {
121-
if (!isset($this->_values[$key]) || $this->_values[$key] !== $value) {
114+
if (! $this->_changed) {
115+
if (! isset($this->_values[$key]) || $this->_values[$key] !== $value) {
122116
// set changed flag
123117
$this->_changed = true;
124118
}

lib/triagens/ArangoDb/Graph.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,13 @@ public function getOrphanCollections()
226226
*/
227227
public function set($key, $value)
228228
{
229+
if ($key === self::ENTRY_EDGE_DEFINITIONS) {
230+
if ($this->_doValidate) {
231+
ValueValidator::validate($value);
232+
}
229233

230-
if (in_array($key, array(self::ENTRY_EDGE_DEFINITIONS, self::ENTRY_ORPHAN_COLLECTIONS))) {
231-
ValueValidator::validate($value);
232-
if ($key === self::ENTRY_EDGE_DEFINITIONS) {
233-
foreach ($value as $ed) {
234-
$edgeDefinition = new EdgeDefinition();
234+
foreach ($value as $ed) {
235+
$edgeDefinition = new EdgeDefinition();
235236
foreach ($ed[self::ENTRY_FROM] as $from) {
236237
$edgeDefinition->addFromCollection($from);
237238
}
@@ -240,16 +241,18 @@ public function set($key, $value)
240241
}
241242
$edgeDefinition->setRelation($ed[self::ENTRY_COLLECTION]);
242243
$this->addEdgeDefinition($edgeDefinition);
243-
};
244-
return;
245244
}
246-
if ($key === self::ENTRY_ORPHAN_COLLECTIONS) {
247-
foreach ($value as $o) {
245+
}
246+
else if ($key === self::ENTRY_ORPHAN_COLLECTIONS) {
247+
if ($this->_doValidate) {
248+
ValueValidator::validate($value);
249+
}
250+
251+
foreach ($value as $o) {
248252
$this->addOrphanCollection($o);
249-
}
250-
return;
251253
}
252-
} else {
254+
}
255+
else {
253256
parent::set($key, $value);
254257
}
255258
}

lib/triagens/ArangoDb/ValueValidator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ class ValueValidator
3333
*/
3434
public static function validate($value)
3535
{
36-
if (is_string($value) || is_int($value) || is_double($value) || is_bool($value) || is_null($value)) {
37-
// type is allowed
38-
return;
39-
}
40-
4136
if (is_array($value)) {
4237
// must check all elements contained
4338
foreach ($value as $subValue) {
@@ -46,6 +41,10 @@ public static function validate($value)
4641

4742
return;
4843
}
44+
else if (! is_object($value) && ! is_resource($value) && ! is_callable($value)) {
45+
// type is allowed
46+
return;
47+
}
4948

5049
// type is invalid
5150
throw new ClientException('Invalid bind parameter value');

0 commit comments

Comments
 (0)