<commit_before><?php

require_once 'YASS/Algorithm.php';
require_once 'YASS/DataStore.php';
require_once 'YASS/SyncStore.php';
require_once 'YASS/ConflictResolver.php';

class YASS_Algorithm_Bidir extends YASS_Algorithm {
	function run(
		YASS_DataStore $srcData, YASS_SyncStore $srcSync,
		YASS_DataStore $destData, YASS_SyncStore $destSync,
		YASS_ConflictResolver $conflictResolver
	) {
		$this->srcData = $srcData;
		$this->srcSync = $srcSync;
		$this->destData = $destData;
		$this->destSync = $destSync;
		$this->conflictResolver = $conflictResolver;

		// BEGIN transaction
		
		$srcLastSeenVersions = $srcSync->getLastSeenVersions();    // array(replicaId => YASS_Version)
		$destLastSeenVersions = $destSync->getLastSeenVersions(); // array(replicaId => YASS_Version)
		$srcChanges = array();  // array(entityGuid => YASS_SyncState)
		$destChanges = array(); // array(entityGuid => YASS_SyncState)
		foreach ($srcLastSeenVersions as $replicaId => $srcVersion) {
			$destVersion = $destLastSeenVersions[$replicaId] ? $destLastSeenVersions[$replicaId] : NULL;
			// print_r(array('srcChanges += ', $srcSync->getModified($destVersion)));
			$srcChanges += $srcSync->getModified($destVersion);
		}
		foreach ($destLastSeenVersions as $replicaId => $destVersion) {
			$srcVersion = $srcLastSeenVersions[$replicaId] ? $srcLastSeenVersions[$replicaId] : NULL;
			// print_r(array('destChanges +=', $destSync->getModified($srcVersion)));
			$destChanges += $destSync->getModified($srcVersion);
		}

		// A conflict arises when srcChanges and destChanges reference the same entityGuid
		$srcChangesClean = array_diff(array_keys($srcChanges), array_keys($destChanges));
		$destChangesClean = array_diff(array_keys($destChanges), array_keys($srcChanges));
		$conflictedChanges = array_intersect(array_keys($srcChanges), array_keys($destChanges));
		
		// print_r(array('srcLastSeenVersions' => $srcLastSeenVersions, 'destLastSeenVersions' => $destLastSeenVersions, 'srcChanges' => $srcChanges, 'destChanges' => $destChanges,'srcChangesClean' => $srcChangesClean,'destChangesClean' => $destChangesClean, 'conflictedChanges' => $conflictedChanges,));
		
		foreach ($srcChangesClean as $entityGuid) {
			$srcSyncState = $srcChanges[$entityGuid];
			$entity = $srcData->getEntity($srcSyncState->entityType, $srcSyncState->entityGuid);
			$destData->putEntity($entity);
			$destSync->setSyncState($srcSyncState->entityType, $srcSyncState->entityGuid, $srcSyncState->modified);
		}
		
		foreach ($destChangesClean as $entityGuid) {
			$destSyncState = $destChanges[$entityGuid];
			$entity = $destData->getEntity($destSyncState->entityType, $destSyncState->entityGuid);
			$srcData->putEntity($entity);
			$srcSync->setSyncState($destSyncState->entityType, $destSyncState->entityGuid, $destSyncState->modified);
		}
		
		foreach ($conflictedChanges as $entityGuid) {
			$conflictResolver->resolve($this, $srcChanges[$entityGuid], $destChanges[$entityGuid]);
		}
		
		foreach ($destLastSeenVersions as $destVersion) {
			$srcSync->markSeen($destVersion);
		}
		
		foreach ($srcLastSeenVersions as $srcVersion) {
			$destSync->markSeen($srcVersion);
		}
		
		// print_r(array('srcSync' => $srcSync, 'destSync' => $destSync, 'srcData' => $srcData, 'destData' => $destData));

		// COMMIT transaction
	}
}<commit_msg>refs #1242 - YASS_Algorithm_Bidir - Consolidate code which transfers records (in anticipation of data-mapping functionality)<commit_after><?php

require_once 'YASS/Algorithm.php';
require_once 'YASS/DataStore.php';
require_once 'YASS/SyncStore.php';
require_once 'YASS/ConflictResolver.php';

class YASS_Algorithm_Bidir extends YASS_Algorithm {
	function run(
		YASS_DataStore $srcData, YASS_SyncStore $srcSync,
		YASS_DataStore $destData, YASS_SyncStore $destSync,
		YASS_ConflictResolver $conflictResolver
	) {
		arms_util_include_api('array');
		
		$this->srcData = $srcData;
		$this->srcSync = $srcSync;
		$this->destData = $destData;
		$this->destSync = $destSync;
		$this->conflictResolver = $conflictResolver;

		// BEGIN transaction
		
		$srcLastSeenVersions = $srcSync->getLastSeenVersions();    // array(replicaId => YASS_Version)
		$destLastSeenVersions = $destSync->getLastSeenVersions(); // array(replicaId => YASS_Version)
		$srcChanges = array();  // array(entityGuid => YASS_SyncState)
		$destChanges = array(); // array(entityGuid => YASS_SyncState)
		foreach ($srcLastSeenVersions as $replicaId => $srcVersion) {
			$destVersion = $destLastSeenVersions[$replicaId] ? $destLastSeenVersions[$replicaId] : NULL;
			// print_r(array('srcChanges += ', $srcSync->getModified($destVersion)));
			$srcChanges += $srcSync->getModified($destVersion);
		}
		foreach ($destLastSeenVersions as $replicaId => $destVersion) {
			$srcVersion = $srcLastSeenVersions[$replicaId] ? $srcLastSeenVersions[$replicaId] : NULL;
			// print_r(array('destChanges +=', $destSync->getModified($srcVersion)));
			$destChanges += $destSync->getModified($srcVersion);
		}

		// A conflict arises when srcChanges and destChanges reference the same entityGuid
		$srcChangesClean = array_diff(array_keys($srcChanges), array_keys($destChanges));
		$destChangesClean = array_diff(array_keys($destChanges), array_keys($srcChanges));
		$conflictedChanges = array_intersect(array_keys($srcChanges), array_keys($destChanges));
		
		// print_r(array('srcLastSeenVersions' => $srcLastSeenVersions, 'destLastSeenVersions' => $destLastSeenVersions, 'srcChanges' => $srcChanges, 'destChanges' => $destChanges,'srcChangesClean' => $srcChangesClean,'destChangesClean' => $destChangesClean, 'conflictedChanges' => $conflictedChanges,));
		
		$this->transfer($srcData, $srcSync, $destData, $destSync, arms_util_array_keyslice($srcChanges, $srcChangesClean));
		$this->transfer($destData, $destSync, $srcData, $srcSync, arms_util_array_keyslice($destChanges, $destChangesClean));
		
		foreach ($conflictedChanges as $entityGuid) {
			$conflictResolver->resolve($this, $srcChanges[$entityGuid], $destChanges[$entityGuid]);
		}
		
		foreach ($destLastSeenVersions as $destVersion) {
			$srcSync->markSeen($destVersion);
		}
		
		foreach ($srcLastSeenVersions as $srcVersion) {
			$destSync->markSeen($srcVersion);
		}
		
		// print_r(array('srcSync' => $srcSync, 'destSync' => $destSync, 'srcData' => $srcData, 'destData' => $destData));

		// COMMIT transaction
	}
	
	/**
	 * Transfer a set of records from one replica to another
	 *
	 * @param $syncStates array(YASS_SyncState) List of entities/revisions to transfer
	 */
	function transfer(
		YASS_DataStore $srcData, YASS_SyncStore $srcSync,
		YASS_DataStore $destData, YASS_SyncStore $destSync,
		$syncStates)
	{
		foreach ($syncStates as $srcSyncState) {
			$entity = $srcData->getEntity($srcSyncState->entityType, $srcSyncState->entityGuid);
			$destData->putEntity($entity);
			$destSync->setSyncState($srcSyncState->entityType, $srcSyncState->entityGuid, $srcSyncState->modified);
		}
	}
}
{'fraction_non_alphanumeric': 0.14510767451943923, 'fraction_numerical': 0.0005781182251770487, 'mean_word_length': 6.2460732984293195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2325075', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2236, 'n_words': 367}