changeset 14:e6a408d3e2f4

First step in move over to new "optimisticflock" locking system
author joshjcarrier
date Fri, 14 May 2010 11:52:26 -0700
parents 332332cc49ab
children f689fb02e258
files admin/application/libraries/hgconf2ini.php admin/application/libraries/optimisticflock.php
diffstat 2 files changed, 107 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/admin/application/libraries/hgconf2ini.php	Fri May 14 10:17:39 2010 -0700
+++ b/admin/application/libraries/hgconf2ini.php	Fri May 14 11:52:26 2010 -0700
@@ -15,6 +15,8 @@
  */
 class HgConf2Ini
 {
+	private $_ci;
+	
 	function __construct($config = array())
 	{
 		if(!empty($config))
@@ -23,9 +25,10 @@
 		}
 		else
 		{
-			$ci =& get_instance();
-			$ci->config->load('hgphp', TRUE);
-			$this->initialize($ci->config->item('hgphp'));
+			$this->_ci =& get_instance();
+			$this->_ci->config->load('hgphp', TRUE);
+			$this->initialize($this->_ci->config->item('hgphp'));
+			$this->_ci->load->library('optimisticflock');
 		}
 		
 		//$this->_ci =& get_instance();
@@ -215,6 +218,7 @@
 	 */
 	function __hgwebconf_compat_load($fs_hgwebdir = 'hgweb.config')
 	{
+		/* old half-locking stuff
 		$hgwebconf_lock_path = $this->_lock_dir . 'hgweb.config.lock';
 		
 		// need to regenerate lock? (a php ini-parseable file)
@@ -233,6 +237,22 @@
 			// FIXME error check
 			file_put_contents($hgwebconf_lock_path, $hgwebconf_str);
 		}
+		*/
+		
+		// FIXME replace with temp file or delete on update
+		$hgwebconf_lock_path = $this->_lock_dir . 'hgweb.config.lock';
+		
+		// load the PHP to file
+		$hgwebconf = $this->_repositories_abs_dir . $fs_hgwebdir;
+		// FIXME error check
+		$hgwebconf_str = $this->_ci->optimisticflock->ofl_file_get_contents('OFL_TODO', $hgwebconf);
+		
+		// replace all occurances of the forward slash '/'
+		$hgwebconf_str = str_replace('/', $this->_compatability_delimiter, $hgwebconf_str);
+		
+		// write temp compatible ini file
+		// FIXME error check
+		$this->_ci->optimisticflock->ofl_file_put_contents('OFL_TODO', $hgwebconf_lock_path, $hgwebconf_str);
 		
 		// load the new compat ini
 		// FIXME minimize the number of times we need to do this
@@ -289,6 +309,9 @@
 		// undo the compatibility to allow Mercurial
 		$hgwebconf_new_ini = str_replace($this->_compatability_delimiter, '/', $hgwebconf_new_ini);
 		
+		$this->_ci->optimisticflock->ofl_file_put_contents('OFL_LOCK_TODO', $this->_repositories_abs_dir . $fs_hgwebdir, $hgwebconf_new_ini);
+		
+		/* bad half-locking stuff
 		// FIXME double declared lock path..
 		$hgwebconf_lock_path = $this->_lock_dir . 'hgweb.config.lock';
 		$hgwebconf_tmp_path = $this->_lock_dir . 'hgweb.config.tmp';
@@ -305,6 +328,7 @@
 		// cleanup
 		// FIXME error check 
 		unlink($hgwebconf_lock_path);
+		*/
 		
 		return true;
 	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/admin/application/libraries/optimisticflock.php	Fri May 14 11:52:26 2010 -0700
@@ -0,0 +1,80 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+
+/**
+ * CodeIgniter Optimistic File Locking Class
+ * 
+ * Imposes optimistic locking strategy on files edited over the web. Necessary over standard
+ * file locking procedures because transactions are:
+ * - multi-user
+ * - done over multiple requests
+ * 
+ * @package        	CodeIgniter
+ * @subpackage    	Libraries
+ * @category    	Libraries
+ * @author 			Josh Carrier
+ * @link			blog.joshjcarrier.com
+ * 
+ */
+class OptimisticFLock
+{
+	function ofl_file_get_contents($ofl_lock = '', $f_name)
+	{
+		// check file exists
+		// shared flock index
+		// get or register in index
+		// read in content
+		return file_get_contents($f_name);
+		// release shared flock index
+		// return contents + oflock
+	}
+	
+	function ofl_file_put_contents($ofl_lock = '', $f_name, $f_contents)
+	{
+		// check file exists
+		// exclusive flock index
+		// compare optimistic locks
+		// if match or force enabled? then write
+		file_put_contents($f_name, $f_contents);
+		// write new oflock in index
+		// release exclusive flock index
+		// return oflock
+	}
+	
+	function ofl_parse_ini_file()
+	{
+		// check file exists
+		// shared flock index
+		// get or register in index
+		// parse ini
+		// release shared flock index
+		// release parsed ini + oflock
+	}
+	
+	/**
+	 * 
+	 * Direct file system locking to the Transaction Manager's optimistic locking index.
+	 */
+	function __tm_index_flock()
+	{
+		
+	}
+	
+	/**
+	 * 
+	 */
+	function __tm_update_ofl($f_name)
+	{
+		return -1;
+	}
+	
+	/**
+	 * __tm_read_or_insert_ofl (private method)
+	 * Returns the optimistic locking number of the specified file name. If file exists but is not in index,
+	 * it is registered in the index and this value is returned.
+	 * @return the optimistic locking number of the specified file name, FALSE on error
+	 */
+	function __tm_read_or_insert_ofl($f_name)
+	{
+		return -1;
+	}
+}
\ No newline at end of file