Commit 47b74f62 authored by Bas de Nooijer's avatar Bas de Nooijer

Added support for versioning / optimistic concurrency control

parent 058c6f51
......@@ -78,6 +78,27 @@ class Document extends AbstractDocument implements DocumentInterface
*/
const MODIFIER_ADD = 'add';
/**
* This value has the same effect as not setting a version
*
* @var int
*/
const VERSION_DONT_CARE = 0;
/**
* This value requires an existing document with the same key, but no specific version
*
* @var int
*/
const VERSION_MUST_EXIST = 1;
/**
* This value requires that no document with the same key exists (so no automatic overwrite like default)
*
* @var int
*/
const VERSION_MUST_NOT_EXIST = -1;
/**
* Document boost value
*
......@@ -108,6 +129,15 @@ class Document extends AbstractDocument implements DocumentInterface
*/
protected $fieldBoosts;
/**
* Version value
*
* Can be used for updating using Solr's optimistic concurrency control
*
* @var int
*/
protected $version;
/**
* Constructor
*
......@@ -373,4 +403,26 @@ class Document extends AbstractDocument implements DocumentInterface
return parent::getFields();
}
/**
* Set version
*
* @param int $version
* @return self
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get version
*
* @return int
*/
public function getVersion()
{
return $this->version;
}
}
......@@ -134,6 +134,11 @@ class RequestBuilder extends BaseRequestBuilder
}
}
$version = $doc->getVersion();
if ($version !== null) {
$xml .= $this->buildFieldXml('_version_', null, $version);
}
$xml .= '</doc>';
}
......
......@@ -424,4 +424,24 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
$this->doc->getFields();
}
public function testSetAndGetVersion()
{
$this->assertEquals(
null,
$this->doc->getVersion()
);
$this->doc->setVersion(Document::VERSION_MUST_NOT_EXIST);
$this->assertEquals(
Document::VERSION_MUST_NOT_EXIST,
$this->doc->getVersion()
);
$this->doc->setVersion(234);
$this->assertEquals(
234,
$this->doc->getVersion()
);
}
}
......@@ -194,6 +194,20 @@ class RequestBuilderTest extends \PHPUnit_Framework_TestCase
);
}
public function testBuildAddXmlWithVersionedDocument()
{
$doc = new Document(array('id' => 1));
$doc->setVersion(Document::VERSION_MUST_NOT_EXIST);
$command = new AddCommand;
$command->addDocument($doc);
$this->assertEquals(
'<add><doc><field name="id">1</field><field name="_version_">-1</field></doc></add>',
$this->builder->buildAddXml($command)
);
}
public function testBuildDeleteXml()
{
$command = new DeleteCommand;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment