/** * @param string $key * @param string $value */ public function set($key, $value) { if (strval($key) === '') { // DBA cross-check hack return; } $this->addbegin(strlen($key), strlen($value)); $this->write($key); $this->write($value); $this->addend(strlen($key), strlen($value), Util::hash($key)); }
/** * @param string $key * @return bool */ protected function findNext($key) { if (!$this->loop) { $u = Util::hash($key); $buf = $this->read(8, $u << 3 & 2047); $this->hslots = $this->unpack31(substr($buf, 4)); if (!$this->hslots) { return false; } $this->hpos = $this->unpack31(substr($buf, 0, 4)); $this->khash = $u; $u = Util::unsignedShiftRight($u, 8); $u = Util::unsignedMod($u, $this->hslots); $u <<= 3; $this->kpos = $this->hpos + $u; } while ($this->loop < $this->hslots) { $buf = $this->read(8, $this->kpos); $pos = $this->unpack31(substr($buf, 4)); if (!$pos) { return false; } $this->loop += 1; $this->kpos += 8; if ($this->kpos == $this->hpos + ($this->hslots << 3)) { $this->kpos = $this->hpos; } $u = $this->unpackSigned(substr($buf, 0, 4)); if ($u === $this->khash) { $buf = $this->read(8, $pos); $keyLen = $this->unpack31(substr($buf, 0, 4)); if ($keyLen == strlen($key) && $this->match($key, $pos + 8)) { // Found $this->dlen = $this->unpack31(substr($buf, 4)); $this->dpos = $pos + 8 + $keyLen; return true; } } } return false; }
/** * Search the CDB file for a key. * * Sets `dataLen` and `dataPos` properties if successful. * * @param string $key * @return bool Whether the key was found. */ protected function find($key) { $keyLen = strlen($key); $u = Util::hash($key); $upos = $u << 3 & 2047; $hashSlots = $this->readInt31($upos + 4); if (!$hashSlots) { return false; } $hashPos = $this->readInt31($upos); $keyHash = $u; $u = Util::unsignedShiftRight($u, 8); $u = Util::unsignedMod($u, $hashSlots); $u <<= 3; $keyPos = $hashPos + $u; for ($i = 0; $i < $hashSlots; $i++) { $hash = $this->readInt32($keyPos); $pos = $this->readInt31($keyPos + 4); if (!$pos) { return false; } $keyPos += 8; if ($keyPos == $hashPos + ($hashSlots << 3)) { $keyPos = $hashPos; } if ($hash === $keyHash) { if ($keyLen === $this->readInt31($pos)) { $dataLen = $this->readInt31($pos + 4); $dataPos = $pos + 8 + $keyLen; $foundKey = $this->read($pos + 8, $keyLen); if ($foundKey === $key) { // Found $this->dataLen = $dataLen; $this->dataPos = $dataPos; return true; } } } } return false; }