function Put($key, $value)
	{
		// check the file description
		if (!$this->fd || $this->mode != 'w')
		{
			trigger_error("XDB::Put(), null db handler or readonly.", E_USER_WARNING);
			return false;
		}

		// check the length
		$klen = strlen($key);
		$vlen = strlen($value);
		if (!$klen || $klen > XDB_MAXKLEN)
			return false;

		// try to find the old data
		$rec = $this->_get_record($key);
		if (isset($rec['vlen']) && ($vlen <= $rec['vlen']))
		{
			// update the old value & length
			if ($vlen > 0)
			{
				fseek($this->fd, $rec['voff'], SEEK_SET);
				fwrite($this->fd, $value, $vlen);
			}

			if ($vlen < $rec['vlen'])
			{
				$newlen = $rec['len'] + $vlen - $rec['vlen'];
				$newbuf = pack('I', $newlen);
				fseek($this->fd, $rec['poff'] + 4, SEEK_SET);
				fwrite($this->fd, $newbuf, 4);
			}
			return true;
		}

		// 构造数据
		$new = array('loff' => 0, 'llen' => 0, 'roff' => 0, 'rlen' => 0);
		if (isset($rec['vlen']))
		{
			$new['loff'] = $rec['loff'];
			$new['llen'] = $rec['llen'];
			$new['roff'] = $rec['roff'];
			$new['rlen'] = $rec['rlen'];
		}
		$buf  = pack('IIIIC', $new['loff'], $new['llen'], $new['roff'], $new['rlen'], $klen);
		$buf .= $key . $value;
		$len  = $klen + $vlen + 17;

		$off  = $this->fsize;
		fseek($this->fd, $off, SEEK_SET);
		fwrite($this->fd, $buf, $len);
		$this->fsize += $len;

		$pbuf = pack('II', $off, $len);
		fseek($this->fd, $rec['poff'], SEEK_SET);
		fwrite($this->fd, $pbuf, 8);
		return true;
	}
function Put($key, $value)
	{
		// check the file description
		if (!$this->fd || $this->mode != 'w')
		{
			trigger_error("XDB::Put(), null db handler or readonly.", E_USER_WARNING);
			return false;
		}

		// check the length
		$klen = strlen($key);
		$vlen = strlen($value);
		if (!$klen || $klen > XDB_MAXKLEN)
			return false;

		// try to find the old data
		$rec = $this->_get_record($key);
		if (isset($rec['vlen']) && ($vlen <= $rec['vlen']))
		{
			// update the old value & length
			if ($vlen > 0)
			{
				fseek($this->fd, $rec['voff'], SEEK_SET);
				fwrite($this->fd, $value, $vlen);
			}

			if ($vlen < $rec['vlen'])
			{
				$newlen = $rec['len'] + $vlen - $rec['vlen'];
				$newbuf = pack('I', $newlen);
				fseek($this->fd, $rec['poff'] + 4, SEEK_SET);
				fwrite($this->fd, $newbuf, 4);
			}
			return true;
		}

		// 构造数据
		$new = array('loff' => 0, 'llen' => 0, 'roff' => 0, 'rlen' => 0);
		if (isset($rec['vlen']))
		{
			$new['loff'] = $rec['loff'];
			$new['llen'] = $rec['llen'];
			$new['roff'] = $rec['roff'];
			$new['rlen'] = $rec['rlen'];
		}
		$buf  = pack('IIIIC', $new['loff'], $new['llen'], $new['roff'], $new['rlen'], $klen);
		$buf .= $key . $value;
		$len  = $klen + $vlen + 17;

		$off  = $this->fsize;
		fseek($this->fd, $off, SEEK_SET);
		fwrite($this->fd, $buf, $len);
		$this->fsize += $len;

		$pbuf = pack('II', $off, $len);
		fseek($this->fd, $rec['poff'], SEEK_SET);
		fwrite($this->fd, $pbuf, 8);
		return true;
	}
function Put ( $ key , $ value ) { // check the file description if ( ! $ this -> fd || $ this -> mode != 'w' ) { trigger_error ( "XDB::Put(), null db handler or readonly." , E_USER_WARNING ) ; return false ; } // check the length $ klen = strlen ( $ key ) ; $ vlen = strlen ( $ value ) ; if ( ! $ klen || $ klen > XDB_MAXKLEN ) return false ; // try to find the old data $ rec = $ this -> _get_record ( $ key ) ; if ( isset ( $ rec [ 'vlen' ] ) && ( $ vlen <= $ rec [ 'vlen' ] ) ) { // update the old value & length if ( $ vlen > 0 ) { fseek ( $ this -> fd , $ rec [ 'voff' ] , SEEK_SET ) ; fwrite ( $ this -> fd , $ value , $ vlen ) ; } if ( $ vlen < $ rec [ 'vlen' ] ) { $ newlen = $ rec [ 'len' ] + $ vlen - $ rec [ 'vlen' ] ; $ newbuf = pack ( 'I' , $ newlen ) ; fseek ( $ this -> fd , $ rec [ 'poff' ] + 4 , SEEK_SET ) ; fwrite ( $ this -> fd , $ newbuf , 4 ) ; } return true ; } // 构造数据 $ new = array ( 'loff' => 0 , 'llen' => 0 , 'roff' => 0 , 'rlen' => 0 ) ; if ( isset ( $ rec [ 'vlen' ] ) ) { $ new [ 'loff' ] = $ rec [ 'loff' ] ; $ new [ 'llen' ] = $ rec [ 'llen' ] ; $ new [ 'roff' ] = $ rec [ 'roff' ] ; $ new [ 'rlen' ] = $ rec [ 'rlen' ] ; } $ buf = pack ( 'IIIIC' , $ new [ 'loff' ] , $ new [ 'llen' ] , $ new [ 'roff' ] , $ new [ 'rlen' ] , $ klen ) ; $ buf .= $ key . $ value ; $ len = $ klen + $ vlen + 17 ; $ off = $ this -> fsize ; fseek ( $ this -> fd , $ off , SEEK_SET ) ; fwrite ( $ this -> fd , $ buf , $ len ) ; $ this -> fsize += $ len ; $ pbuf = pack ( 'II' , $ off , $ len ) ; fseek ( $ this -> fd , $ rec [ 'poff' ] , SEEK_SET ) ; fwrite ( $ this -> fd , $ pbuf , 8 ) ; return true ; }
Insert Or Update the value
Insert Or Update the value