<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(SF_ROOT_DIR . '/lib/RiakClient.class.php');
class ProofStorage
{
    var $Host;
    var $RiakClient;
    var $RiakBucket;
    var $ProofData;
    var $OrderProductId;
    
    public function __construct($Cluster=1)
    {
        switch($Cluster)
        {
            case 2:
                $this->Host     = '192.168.10.234';
                break;
            default:
                $this->Host     = '192.168.10.152';
                break;
        }
        $this->RiakClient   = new RiakClient($this->Host);
        $this->RiakBucket   = $this->RiakClient->bucket('op');    # op = Order Proofs
        $this->ProofData    = array();
        $this->OrderProductId= 0;
    }
    
    public function GetProofData($OrderProductId)
    {
        $obj = $this->RiakBucket->get($OrderProductId);
        $Result = false;
        if ( $obj->exists() )
        {
            $this->OrderProductId   = $OrderProductId;
            $this->ProofData        = $obj->getData();
            return $this->ProofData;
            
        } else {
            return $Result;
        }
    }
    
    public function SaveProofData($orderProductId, $arrProofData)
    {
        $this->ProofData = json_encode($arrProofData);
        //echo "ProofData: " . $this->ProofData . "\n";
        $this->OrderProductId = $orderProductId;
        $obj = $this->RiakBucket->newObject($this->OrderProductId, $this->ProofData);
        return $obj->store();
    }
    
    
    public function GetProofPart($Key)
    {
        //echo "GetProofPart($Key)<br />";
        $data = false; 
        $obj = $this->RiakBucket->getBinary($Key);
        if ( $obj->exists() )
        {
            //echo "Found something<br />";
            $data = $obj->getData();
            //echo "returned bytes: " . strlen($data) . "<br />";
        }
        return $data;
    }
    
    
    public function SaveProofPart($Key, $binData)
    {
        $obj = $this->RiakBucket->newBinary($Key, $binData);
        //echo "SaveProofPart::obj = " . print_r($obj, true) . "\n";
        return $obj->store();        
    }
}
?>
