1
0
Fork 0

Use an integer bitfield for rule properties instead of a string binary blob

pull/4239/head
Nils Adermann 2015-07-09 18:26:31 +02:00
parent 6e81f63635
commit 961ea868ac
2 changed files with 14 additions and 16 deletions

View File

@ -50,7 +50,7 @@ class Rule
$this->job = $job;
}
$this->blob = pack('ccc', -1, $reason, false);
$this->blob = (0 << 16) | ($reason << 8) | (255 << 0);
}
public function getHash()
@ -66,7 +66,7 @@ class Rule
public function getReason()
{
return $this->getBlob('a2');
return $this->getBlob(1);
}
public function getReasonData()
@ -110,32 +110,32 @@ class Rule
public function setType($type)
{
return $this->setBlob('a1', $type);
return $this->setBlob(0, $type);
}
public function getType()
{
return $this->getBlob('a1');
return $this->getBlob(0);
}
public function disable()
{
return $this->setBlob('a3', true);
return $this->setBlob(2, 1);
}
public function enable()
{
return $this->setBlob('a3', false);
return $this->setBlob(2, 0);
}
public function isDisabled()
{
return (bool) $this->getBlob('a3');
return (bool) $this->getBlob(2);
}
public function isEnabled()
{
return !$this->getBlob('a3');
return !$this->getBlob(2);
}
/**
@ -255,16 +255,14 @@ class Rule
private function getBlob($var)
{
$current = unpack('c3a', $this->blob);
return $current[$var];
$offset = 8*$var;
return ($this->blob & (255 << $offset)) >> $offset;
}
private function setBlob($var, $value)
{
$current = unpack('c3a', $this->blob);
$current[$var] = $value;
array_unshift($current, 'ccc');
$this->blob = call_user_func_array('pack', $current);
$offset = 8*$var;
$this->blob = ($this->blob & ~(255 << $offset)) | ((255 & $value) << $offset);
}
/**

View File

@ -30,7 +30,7 @@ class RuleSet implements \IteratorAggregate, \Countable
public $ruleById;
protected static $types = array(
-1 => 'UNKNOWN',
255 => 'UNKNOWN',
self::TYPE_PACKAGE => 'PACKAGE',
self::TYPE_JOB => 'JOB',
self::TYPE_LEARNED => 'LEARNED',
@ -130,7 +130,7 @@ class RuleSet implements \IteratorAggregate, \Countable
public function getTypes()
{
$types = self::$types;
unset($types[-1]);
unset($types[255]);
return array_keys($types);
}