Permalink
Browse files

Add methods to get changes in CouchDB database.

  • Loading branch information...
Florent Torregrosa
Florent Torregrosa committed Jan 10, 2017
1 parent e001e2f commit edce6f49a8ef25352abe738582763e64b3ac9f82
Showing with 74 additions and 0 deletions.
  1. +55 −0 src/Replication.php
  2. +19 −0 src/Replicator.php
View
55 src/Replication.php 100644 → 100755
@@ -94,6 +94,21 @@ public function start($printStatus = true, $getFinalReport = true)
return $replicationLog;
}
+ public function getDiff()
+ {
+ $this->startTime = new \DateTime();
+ // DB info (via GET /{db}) for source and target.
+ $this->verifyPeers();
+ $this->task->setRepId($this->generateReplicationId());
+ // Replication log (via GET /{db}/_local/{docid}) for source and target.
+ list($sourceLog, $targetLog) = $this->getReplicationLog();
+
+ $this->task->setSinceSeq($this->compareReplicationLogs($sourceLog, $targetLog));
+ // Main replication processing
+ $entities = $this->locateChangedDocuments();
+
+ return $entities;
+ }
/**
* @return array
@@ -466,6 +481,46 @@ public function locateChangedDocumentsAndReplicate($printStatus, $getFinalReport
}
}
+ public function locateChangedDocuments()
+ {
+ if (!$this->task->getContinuous()) {
+ $changes = $this->source->getChanges(
+ array(
+ 'feed' => 'normal',
+ 'style' => $this->task->getStyle(),
+ 'since' => $this->task->getSinceSeq(),
+ 'filter' => $this->task->getFilter(),
+ 'parameters' => $this->task->getParameters(),
+ 'doc_ids' => $this->task->getDocIds()
+ //'limit' => 10000 //taking large value for now, needs optimisation
+ )
+ );
+ $mapping = $this->getMapping($changes);
+ $revDiff = (count($mapping) > 0 ? $this->target->getRevisionDifference($mapping) : array());
+ $entities = $this->getChanges($revDiff);
+ return $entities;
+ }
+ }
+
+ /**
+ * @param array $revDiff
+ * @return array|void
+ * @throws HTTPException
+ */
+ public function getChanges($revDiff)
+ {
+ $entities = array();
+ foreach ($revDiff as $docId => $revMisses) {
+ $document = $this->source->findDocument($docId);
+
+ // TODO: Need to handle document deletion.
+ if ($document->status == 200) {
+ $entities[$docId] = $document->body;
+ }
+ }
+ return $entities;
+ }
+
/**
* @param array $revDiff
* @return array|void
View
19 src/Replicator.php 100644 → 100755
@@ -68,6 +68,25 @@ public function startReplication($printStatus = true, $getFinalReport = false)
return $replication->start($printStatus, $getFinalReport);
}
+
+ public function getDiff()
+ {
+ if ($this->source == null) {
+ throw new \UnexpectedValueException('Source is Null.');
+ }
+ if ($this->target == null) {
+ throw new \UnexpectedValueException('Target is Null.');
+ }
+ if ($this->task == null) {
+ throw new \UnexpectedValueException('Task is Null.');
+ }
+
+ $replication = new Replication($this->source, $this-> target, $this->task);
+
+ // Start and return the details of the Diff.
+ return $replication->getDiff();
+ }
+
/**
* @throws Exception
*/

0 comments on commit edce6f4

Please sign in to comment.