<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"  xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>SoftwareProjects - Internet Marketing and Web Development</title>
	<link>http://www.softwareprojects.com/images/logo.png</link>
	<description>Internet Marketing and Web Development</description>

	<generator>http://www.softwareprojects.com</generator>
	<language>en</language>

		<copyright>SoftwareProjects Inc</copyright>
		<managingEditor>support@softwareprojects.com (Adrian Singer)</managingEditor>
		<webMaster>support@softwareprojects.com</webMaster>
		<category>SoftwareProjects</category>
		<ttl>3000</ttl>

		<image>
			<url>http://www.softwareprojects.com/images/logo.png</url>
			<title>SoftwareProjects - Internet Marketing and Web Development</title>
			<link>http://www.softwareprojects.com/images/logo.png</link>
			<width>142</width>
			<height>55</height>

		</image>

<item>
<title><![CDATA[MSN Messenger Integration - Java Robot script]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-msn-messenger-integration-java-robot-s-1994.html</link>
<pubDate>Thu, 02 Sep 2010 21:44:22 GMT</pubDate>
<dc:creator><![CDATA[Brett Batie]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-msn-messenger-integration-java-robot-s-1994.html</guid>
<description><![CDATA[I recently worked on a project that involved integrating with MSN Messenger, creating my very own "IM Robot".  <br />
<br />
<b><font size="4">What can an IM Robot do?</font></b><br />
<br />
<li>Keep track of contacts online/offline status and when they were last seen.<br />
<li>Broadcast a message to all contacts.<br />
<li>Automatically answer common questions.<br />
<li>Notify contacts about new events. (See  <a href="http://notify.me" target="_blank">http://notify.me</a>)<br />
<li>Keep track of code snippets<br />
<br />
<b><font size="4">Which IM Library to Use</font></b><br />
<br />
Setting up a IM robot can be a bit of work especially if starting from scratch. There are a lot of libraries out there that can be used to help simplify the process. The trouble is a lot of libraries are not kept up to date and fail to work as IM protocols change. <br />
<br />
We did some digging and found a library that would provide a good foundation to build a IM Robot that can do just about anything. We say implementations in PHP, C, Java, Perl and Python. After some testing we concluded the <a href="http://sourceforge.net/apps/trac/java-jml">Java MSN Library</a> would be a very good fit.<br />
<br />
<b><font size="4">How To Use It</font></b><br />
<br />
Using this library with java is pretty straight forward. First the library must be added to the classpath. The step to take to complete this will depend on how your developing your java code. The most basic method to add a library to your classpath is to do this at run time with a command such as:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">java -classpath MyLibrary.jar MyPackage.MyClass</code><hr />
</div>
<br />
A better approach would be to setup the classpath in a manifest file. The manifest file is then placed inside the jar file and tells the executable jar where to look for the libraries. This manifest file should look something like this (note the class-path on line 5):<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">Manifest-Version: 1.0<BR>Ant-Version: Apache Ant 1.7.1<BR>Created-By: 14.3-b01 (Sun Microsystems Inc.)<BR>Main-Class: imstatus.Main<BR>Class-Path: lib/jml-1.0b4-full.jar lib/httpcore-4.0.1.jar lib/mysql-co<BR> nnector-java-5.1.6-bin.jar<BR>X-COMMENT: Main-Class will be added automatically by build</code><hr />
</div>
<br />
This is setup so that the 3 required libraries are in the lib folder. These 3 libraries are needed for setting up an IM robot and can be downloaded from the following locations:<br />
<br />
<li><a href="http://sourceforge.net/projects/java-jml/files/java-jml/jml-1.0b4/jml-1.0b4-full.jar/download">jml-1.0b4-full.jar</a><br />
<li><a href="http://apache.imghat.com//httpcomponents/httpclient/binary/httpcomponents-client-4.0.1-bin.zip">httpcore-4.0.1.jar</a><br />
<li><a href="http://dev.mysql.com/downloads/connector/j/">mysql-connector-java-5.1.6-bin.jar</a><br />
<br />
Now that the libraries are setup we can begin to use them.<br />
<br />
<b><font size="4">Developing the IM Robot Code</font></b><br />
<br />
There are a few examples of using the Java MSN Library on the main page. However, they are a tad confusing as it creates a new BasicMessenger class. This is confusing as the library already has a BasicMessenger class which is abstract. The library also has a SimpleMessenger class which is a subclass of BasicMessenger. This class appears to be the correct implementation that we would want to use to create a new IM Robot. <br />
<br />
However, the original authors made the constructor protected so that we cannot instantiate the class outside of the original package. Since we want a simple way to create an IM Robot I have modified the original source code to have a public constructor for the SimpleMessenger class. <br />
<br />
With this new package we can very easily create a new IM Robot with the following two lines of code (make sure to replace yourLogin and yourPassword):<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">SimpleMessenger messenger = new SimpleMessenger(Email.parseStr("yourLogin@msn.com"), "yourPassword");<BR>messenger.login();</code><hr />
</div>
<br />
With that code in our main funtion we can run it and test that the Robot automatically logs into Windows Live Messenger.<br />
<br />
Of course that code just logs the Robot into Windows Live Messenger. The next step is to setup the robot to do something interesting. This is one feature that is very nice about the Java Msn Library as it as listeners for many different events. For example we can detect when the robot has finished logging in with the following:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">messenger.addListener(new MsnAdapter() {<BR>&nbsp; &nbsp; &nbsp; &nbsp; // Setup the login completed event<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; @Override<BR>&nbsp; &nbsp; &nbsp; &nbsp; public void loginCompleted(MsnMessenger messenger) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsnOwner owner = messenger.getOwner();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; owner.setInitStatus(MsnUserStatus.ONLINE);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; owner.setStatus(MsnUserStatus.ONLINE);<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Setup the contact list event<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messenger.addContactListListener(new ContactListAdapter());<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR>});</code><hr />
</div><p>Then we can take this a step further and detect when a status changes for one of the robots contacts with something like the following:</p><div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">messenger.addListener(new MsnAdapter() {<BR>&nbsp; &nbsp; &nbsp; &nbsp; // Setup the login completed event<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; @Override<BR>&nbsp; &nbsp; &nbsp; &nbsp; public void loginCompleted(MsnMessenger messenger) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsnOwner owner = messenger.getOwner();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; owner.setInitStatus(MsnUserStatus.ONLINE);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; owner.setStatus(MsnUserStatus.ONLINE);<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Setup the contact list event<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messenger.addContactListListener(new ContactListAdapter());<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR>});</code><hr />
</div>
<br />
The above code will detect when the robot has finished logging in and then setup a new listener to detect when a contacts status has changed. The new listener invokes the ContactListAdapter class when a status has changed. This contactListAdapter class is setup as followes:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">class ContactListAdapter extends MsnContactListAdapter {<BR>&nbsp; &nbsp; &nbsp; &nbsp; @Override<BR>&nbsp; &nbsp; &nbsp; &nbsp; public void contactStatusChanged(MsnMessenger messenger, MsnContact contact) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(contact.getEmail()+" is currently "+contact.getStatus());<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Can add code here to store the status in a database<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR>}</code><hr />
</div>
<br />
We can still take this a step further and setup the robot to handle automatically adding contacts when a contact requests it. This logic can be added to the ContactListAdapter class with something like the following:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">class ContactListAdapter extends MsnContactListAdapter {<BR>&nbsp; &nbsp; &nbsp; &nbsp; @Override<BR>&nbsp; &nbsp; &nbsp; &nbsp; public void contactListSyncCompleted(MsnMessenger messenger) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsnContact[] contacts = messenger.getContactList().getContactsInList(MsnList.AL);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < contacts.length; i++) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contactStatusChanged(messenger,contacts[i]);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; @Override<BR>&nbsp; &nbsp; &nbsp; &nbsp; public void contactAddedMe(MsnMessenger messenger, MsnContact contact) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messenger.addFriend(contact.getEmail(), contact.getDisplayName());<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; @Override<BR>&nbsp; &nbsp; &nbsp; &nbsp; public void contactAddedMe(MsnMessenger messenger, MsnContactPending[] pending){<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<pending.length; i++){<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messenger.addFriend(pending[i].getEmail(), pending[i].getDisplayName());<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; @Override<BR>&nbsp; &nbsp; &nbsp; &nbsp; public void contactStatusChanged(MsnMessenger messenger, MsnContact contact) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(contact.getEmail()+" is currently "+contact.getStatus());<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Can add code here to store the status in a database<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR>}</code><hr />
</div>
<br />
There you have it! Put all of the above code together and you will have a robot that knows how to automatically add contacts and keep track of when a contact's status changes.</p>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-msn-messenger-integration-java-robot-s-1994.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-msn-messenger-integration-java-robot-s-1994.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[Kajabi vs SoftwareProjects]]></title>
<link>http://www.softwareprojects.com/resources/conversion-traffic-to-cash/t-kajabi-vs-softwareprojects-1991.html</link>
<pubDate>Wed, 01 Sep 2010 14:16:00 GMT</pubDate>
<dc:creator><![CDATA[Adrian Singer]]></dc:creator>
<category><![CDATA[Conversion - Traffic to Cash]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/conversion-traffic-to-cash/t-kajabi-vs-softwareprojects-1991.html</guid>
<description><![CDATA[There's a new kid on the block.  <br />
<br />
A new system designed to "simplify the deployment of Marketing Campaigns, Sales Funnels, and Product Launches from their current technical complexity down to Drag-and-Drop Ease" (Source: <a href="http://kajabi.com/" target="_blank">Kajabi.com</a>)<br />
<br />
Kajabi was created by <a href="http://www.andyjenkinsblog.com/" target="_blank">Andy Jenkins</a> (VideoBoss, formerly with Stompernet), Kenny Rueter and Travis Rosser.  With this roster, you can bet your Inbox is about to be flooded with emails about how Kajabi is the best thing since sliced bread.<br />
<br />
With this post, I wanted to address a question by one of our clients - how does Kajabi compare with SoftwareProjects' own <a href="http://www.softwareprojects.com/shopping-cart" target="_blank">Shopping Cart</a> solution.<br />
<br />
<b>Disclaimer:</b> Kajabi is still under development.  The information I gathered for this post was collected by analyzing two recent <a href="http://www.softwareprojects.com/resources/traffic-attract-customers/t-product-launch-marketing-quickest-path-to-a-6-figu-1886.html" target="_blank">product launches</a> powered by Kajabi, along with a series of emails from the Kajabi team.<br />
<br />
<font size="4"><b>First things first: Kajabi is NOT a Shopping Cart</b></font><br />
<br />
Kajabi is not designed to be an end-to-end shopping cart system.<br />
<br />
They will integrate with all popular shopping cart systems, but it's just not designed to be a CRM / Affiliate &amp; Lead Manager / Fulfillment / Backoffice, like SoftwareProjects or Infusionsoft.<br />
<br />
You can expect limited built-in support for selling your content, but you're not going to get an <a href="http://www.softwareprojects.com/resources/the-platform-all-in-one/t-tabs-templates-the-executive-dashboard-1722.html" target="_blank">executive dashboard</a> that lets you manage the full 360 of your Internet Business.<br />
<br />
<a href="http://www.softwareprojects.com/resources/the-platform-all-in-one/t-tabs-templates-the-executive-dashboard-1722.html" target="_blank"><img src="http://www.softwareprojects.com/blogimages/aac16633.png" border="0" alt="" /></a><br />
<br />
<font size="4"><b>Membership System</b></font><br />
<br />
Kajabi offers a built-in membership system, where paying members login to access the content (videos / tutorials) they paid for.<br />
<br />
You can customize the membership system skin to some degree and manage members from the admin panel.  Think aMember plus a CMS (Content Management System).<br />
<br />
At this point my understanding is there's going to be a single backoffice (unlike SPI's Customer's Backoffice and Affiliate's Backoffice), which will support a single product per Kajabai deployment.<br />
<br />
<font size="4"><b>Content Delivery</b></font><br />
<br />
This is one area that keeps being emphasized in all communications I hear about Kajabi.<br />
<br />
Still not clear how this is different from hosting all videos and MultiMedia on <a href="http://www.softwareprojects.com/resources/building-content/t-how-to-upload-files-to-amazon-s3-with-firefox-1757.html" target="_blank">Amazon S3</a> or EdgeCast, like everyone are doing.  <br />
<br />
I'm guessing they use the same underlying technology and just make it easier to use.<br />
<br />
<font size="4"><b>Autoresponder</b></font><br />
<br />
Kajabi will not have a built-in Autoresponder.<br />
<br />
It is said to integrate with popular Autoresponder services such as <a href="http://www.aweber.com" target="_blank">AWeber</a>, <a href="http://www.softwareprojects.com/selling-online/spi-autoresponder-manager" target="_blank">SoftwareProjects</a>, <a href="http://www.icontact.com" target="_blank">iContact</a> etc.<br />
<br />
<font size="4"><b>Help desk</b></font><br />
<br />
Kajabi will not have a built-in <a href="http://www.softwareprojects.com/resources/the-platform-all-in-one/t-video-customer-manager-help-desk-quickstart-1526.html" target="_blank">help desk system</a>.<br />
<br />
<font size="4"><b>Hosting</b></font><br />
<br />
Kajabi is said to offer "Cloud Computing Technology, (so that) you can publish anything you create with Kajabi instantly to the web - with theoretically infinitely scalable virtual hardware resources".<br />
<br />
They seem to be using Rackspace Cloud hosting, to create virtual instances of your site as needed, to accommodate traffic demands.<br />
<br />
This sounds very similar to SoftwareProjects' <a href="http://www.softwareprojects.com/hosting" target="_blank">Multi-Homed hosting</a>, where we do what no other company has ever done before and guarantee 100% uptime, no matter what.<br />
<br />
<font size="4"><b>Battle Tested</b></font><br />
<br />
Kajabi successfully powered the product launches of Andy Jenkins' Video Boss and Frank Kern's List Control.<br />
<br />
While still under development, the technology has proven its ability to handle launch-day traffic without too many glitches.<br />
<br />
Here at SoftwareProjects, we have been powering the majority of the big <a href="http://www.softwareprojects.com/resources/traffic-attract-customers/t-product-launch-marketing-quickest-path-to-a-6-figu-1886.html" target="_blank">product launches</a> you hear about, including Magnetic Sponsoring WWN, the ShoeMoney System, ListBuilding, PPC Classroom, Affiliate Classroom, OnlineMLMSecrets and others.<br />
<br />
Then again... we've been doing this since 1998. <br />
<br />
<font size="4"><b>In Summary</b></font><br />
<br />
We really look forward to working with the Kajabi team on the integration side.<br />
<br />
Where Kajabi shines is simplifying the process of getting an info-marketing product up in no time.<br />
<br />
I would describe it as an InfoMarketing CMS with a membership backoffice.  It may not have all the bells and whistles, but as long as the integration is straightforward, you get the best of both worlds.<br />
<br />
-<br />
<br />
More on this topic:<br />
* <a href="http://www.kajabi.com" target="_blank">Kajabi's home page</a><br />
* <a href="http://www.kajabireviewed.com/the-kajabi-beta-testing-portal" target="_blank">Kajabi Beta Testing portal</a><br />
* <a href="http://twitter.com/kajabi" target="_blank">Follow Kajabi on Twitter</a><br />
* <a href="http://www.andyjenkinsblog.com/" target="_blank">Andy Jenkins blog</a>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/conversion-traffic-to-cash/t-kajabi-vs-softwareprojects-1991.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/conversion-traffic-to-cash/t-kajabi-vs-softwareprojects-1991.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[Troubleshooting Cassandra]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-troubleshooting-cassandra-1988.html</link>
<pubDate>Tue, 31 Aug 2010 17:38:50 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-troubleshooting-cassandra-1988.html</guid>
<description><![CDATA[Keynotes from a great presentation titled <a href="http://riptano.blip.tv/file/4012133/" target="_blank">Cassandra Troubleshooting: out of the shadows</a>, presented by <a href="http://twitter.com/b6n" target="_blank">Benjamin Black</a> at the Cassandra Summit in San Francisco two weeks ago.<br />
<br />
The slides are <a href="http://www.slideshare.net/benjaminblack/cassandra-summit-2010-operations-troubleshooting-intro" target="_blank">here</a><br />
<br />
-<br />
<br />
<b><font size="4">Is your Ring unbalanced?</font></b><br />
<br />
That's because when you add one node at a time using RandomPartitioner, the new nodes takes over half of the most balanced node:<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">32<BR>16 16<BR>8 8 16<BR>8 8 8 8<BR>4 4 8 8 8<BR>4 4 4 4 8 8</code><hr />
</div>
<br />
Note that as long as you're doubling-up the size of your cluster,  everything will be balanced.  But when you're growing one node at <br />
 a time, the cluster will be unbalanced.<br />
<br />
To fix: Manually assign tokens.<br />
<br />
How do you know which tokens to assign?  Use this Python script:<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">def tokens(nodes)<BR> 0.upto(nodes - 1) do {n}<BR>&nbsp;  p (n * (2**127 - 1) / nodes)<BR> end<BR>end</code><hr />
</div>
<br />
<b><font size="4">Writes are slow</font></b><br />
<br />
Make sure your commitlog is on a separate drive.<br />
<br />
<b><font size="4">Writes are fast. Reads keep getting slower</font></b><br />
<br />
<b>Step 1:</b> <br />
<br />
Look at <b>iostat -x</b> to see if you're maxing out utilization<br />
<br />
If you are, get more nodes<br />
<br />
<b>Step 2:</b><br />
<br />
Look at <b>nodetool tpstats</b><br />
<br />
Focus on the middle column (pending) and specifically:<br />
* Row-Read-STage<br />
* Message-Deserializer-pool<br />
<br />
If these two are high (4096 is the max), it means your client is sending too many reads to this node.<br />
<br />
Update your client or get more nodes to distribute reads.<br />
<br />
<b>Step 3:</b><br />
<br />
Adjust memtable settings<br />
<br />
When does a memtable get flushed to disk?<br />
<br />
<u>Size</u>: When it gets to a certain size<br />
<u>Time</u>: If it hasn't been flushed in x seconds<br />
<u>Operations</u>: When certain operations occur<br />
<br />
If you're flushing memtables too often, you're triggering follow-up effects (compactions, sstable merges) that is consuming a lot of bandwidth.  <br />
<br />
You want less frequent memtable flush, which leads to less frequent compaction and less disk bandwidth demand.<br />
<br />
If memtable is not compatible with your data needs, you begin  consuming huge amounts of your bandwidth on compactions.<br />
<br />
once a minute = bad<br />
<br />
<b>Step 4:</b><br />
<br />
Use SSDs for the disk drives.  Makes no difference on the commit log drive.<br />
<br />
<b><font size="4">I inserted a bunch of data, now my nodes are flapping</font></b><br />
<br />
Flapping = nodes are marked down/up<br />
<br />
<b>Step 1:</b><br />
<br />
Monitor swap (vmstat on linux, swapinfo on freebsd)<br />
<br />
mmap takes 2gb per segment.<br />
<br />
Swapping can delay gossip long enough to cause a node<br />
to be marked down.<br />
<br />
Swapping is bad.<br />
<br />
To fix: Change DiskAccessMode in the Cassandra config file, to mmap_index_only <br />
<br />
We avoid risking driving ourselves into swap by the JVM<br />
allocating large chunks of mmap blocks.<br />
<br />
<b>Step 2:</b><br />
<br />
Tell the O/S you want to avoid swapping if possible.<br />
On FreeBSD: add this line to /etc/sysctl.conf<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">vm.swap_enabled=0</code><hr />
</div>
<br />
On Linux, echo 0 into /proc/sys/vm/swappiness]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-troubleshooting-cassandra-1988.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-troubleshooting-cassandra-1988.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[Scaling in the real world]]></title>
<link>http://www.softwareprojects.com/resources/traffic-attract-customers/t-scaling-in-the-real-world-1985.html</link>
<pubDate>Tue, 31 Aug 2010 12:55:58 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Traffic - Attract Customers]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/traffic-attract-customers/t-scaling-in-the-real-world-1985.html</guid>
<description><![CDATA[In our ongoing journey to deliver faster, more reliable <a href="http://www.softwareprojects.com/shopping-cart" target="_blank">shopping carts</a>, <a href="http://www.softwareprojects.com/selling-online/email-marketing-spi" target="_blank">email marketing</a> and <a href="http://www.softwareprojects.com/hosting" target="_blank">hosting</a> solutions, we're always on the lookout for new technologies and techniques to deal with a lot of data.<br />
<br />
I'd like to share a few presentations we recently came across, that talk about scaling in the real world:<br />
<br />
* <a href="http://live.visitmix.com/MIX10/Sessions/EX04" target="_blank">MySpace: Massive scaling a .Net website with the Microsoft Robotic Studio</a><br />
* <a href="http://www.slideshare.net/nkallen/q-con-3770885" target="_blank">Twitter: Big Data in Real-Time at Twitter</a><br />
* <a href="http://www.infoq.com/presentations/Scale-at-Facebook" target="_blank">Scale at Facebook</a><br />
* <a href="http://research.yahoo.com/files/sigmod278-silberstein.pdf" target="_blank">Yahoo: Selectively Materializing User's Event Feeds</a><br />
* <a href="http://riptano.blip.tv/file/4015190/" target="_blank">Four Kitchens: Scalable queuing with flexible service levels</a><br />
* <a href="http://riptano.blip.tv/file/4012043/" target="_blank">Cassandra at Mahalo</a><br />
* <a href="http://paul.querna.org/slides/cassandra-summit-cloudkick.pdf" target="_blank">Cassandra at CloudKick</a>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/traffic-attract-customers/t-scaling-in-the-real-world-1985.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/traffic-attract-customers/t-scaling-in-the-real-world-1985.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[PHP Thrift library for Cassandra]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-php-thrift-library-for-cassandra-1982.html</link>
<pubDate>Fri, 06 Aug 2010 15:50:20 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-php-thrift-library-for-cassandra-1982.html</guid>
<description><![CDATA[If you're using <a href="http://www.softwareprojects.com/resources/programming/t-how-to-install-cassandra-+-thrift-and-why-you-shou-1956.html" target="_blank">Cassandra</a> over PHP, you are probably aware of several bugs in the PHP Thrift interface that get in the way.<br />
<br />
Courtesy of <a href="http://spyced.blogspot.com/" target="_blank">Jonathan Ellis</a>, here's a list of known patches you need to apply to your PHP Thrift library:<br />
<br />
* <a href="https://issues.apache.org/jira/browse/THRIFT-347" target="_blank">https://issues.apache.org/jira/browse/THRIFT-347</a> (TSocket Timeout)<br />
* <a href="https://issues.apache.org/jira/browse/THRIFT-638" target="_blank">https://issues.apache.org/jira/browse/THRIFT-638</a> (Block until recv timeout)<br />
* <a href="https://issues.apache.org/jira/browse/THRIFT-780" target="_blank">https://issues.apache.org/jira/browse/THRIFT-780</a> (Prevent aborts)<br />
* <a href="https://issues.apache.org/jira/browse/THRIFT-788" target="_blank">https://issues.apache.org/jira/browse/THRIFT-788</a> (Multiget more than 17 keys)<br />
<br />
To make your life easier, you can download a patched version of Thrift 0.2 for PHP, with all patches applied, <a href="http://www.softwareprojects.com/files/thrift.zip" target="_blank">here</a>.<br />
<br />
-<br />
<br />
A great presentation by <a href="http://twitter.com/moonpolysoft" target="_blank">Cliff Moon</a>: <a href="http://blip.tv/file/4016592" target="_blank">Dealing with the Thrift API</a>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-php-thrift-library-for-cassandra-1982.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-php-thrift-library-for-cassandra-1982.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[Cassandra PHP Wrapper 0.7]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-07-1979.html</link>
<pubDate>Thu, 29 Jul 2010 12:32:19 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-07-1979.html</guid>
<description><![CDATA[A new version of the SoftwareProjects Cassandra PHP Wrapper (db_cassandra.php) is now available, for Cassandra 0.7.<br />
<br />
This version of the PHP Wrapper adds support for:<br />
* Adding Keyspaces and Column families on the fly<br />
* Better error logging<br />
* Disconnect on destructor to prevent java fd leaks<br />
* Reconnect on timeout, up to 10 times<br />
* Deletes<br />
* Updated all API calls to 0.7<br />
<br />
You can download the  <a href="http://www.softwareprojects.com/files/db_cassandra.txt" target="_blank">Cassandra 0.7 PHP wrapper</a>, modify and distribute it freely.<br />
<br />
Please let us know if you find any issues.<br />
<br />
<b>Usage Examples</b><br />
<br />
Adding a new Keyspace with one column family<br />
<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #FF9900">// Initialize Cassandra <br /></span><span style="color: #0000CC">$cassandra </span><span style="color: #006600">= new </span><span style="color: #0000CC">CassandraDB</span><span style="color: #006600">(</span><span style="color: #CC0000">"mytest_keyspace"</span><span style="color: #006600">); <br /><br /></span><span style="color: #FF9900">// Debug on <br /></span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">SetDisplayErrors</span><span style="color: #006600">(</span><span style="color: #0000CC">true</span><span style="color: #006600">); <br /><br /></span><span style="color: #FF9900">// Prepare Column Family<br />// (You can add as many as you need to the cfdef array)<br /></span><span style="color: #0000CC">$cfdef </span><span style="color: #006600">= </span><span style="color: #0000CC">null</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$record </span><span style="color: #006600">= array();<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'keyspace'</span><span style="color: #006600">&#93;&nbsp; &nbsp; &nbsp;  = </span><span style="color: #CC0000">"mytest_keyspace"</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'name'</span><span style="color: #006600">&#93;&nbsp; &nbsp; &nbsp; &nbsp;  = </span><span style="color: #CC0000">"mytable"</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'column_type'</span><span style="color: #006600">&#93;&nbsp; &nbsp;   = </span><span style="color: #CC0000">"Standard"</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'comparator_type'</span><span style="color: #006600">&#93;&nbsp;   = </span><span style="color: #CC0000">"BytesType"</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'row_cache_size'</span><span style="color: #006600">&#93;&nbsp;    = </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'key_cache_size'</span><span style="color: #006600">&#93;&nbsp;    = </span><span style="color: #0000CC">0.01</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$cfdef</span><span style="color: #006600">&#91;&#93; = new </span><span style="color: #0000CC">cassandra_CfDef</span><span style="color: #006600">(</span><span style="color: #0000CC">$record</span><span style="color: #006600">);<br /><br /></span><span style="color: #FF9900">// Add keyspace with one columnfamily<br /></span><span style="color: #0000CC">$result </span><span style="color: #006600">= </span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">AddKeyspace</span><span style="color: #006600">(</span><span style="color: #0000CC">$cfdef</span><span style="color: #006600">);  </span><span style="color: #0000CC"></font></span></div>
<br />
Adding records and reading back values<br />
<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #FF9900">// Initialize Cassandra <br /></span><span style="color: #0000CC">$cassandra </span><span style="color: #006600">= new </span><span style="color: #0000CC">CassandraDB</span><span style="color: #006600">(</span><span style="color: #CC0000">"mytest_keyspace"</span><span style="color: #006600">); <br /><br /></span><span style="color: #FF9900">// Debug on <br /></span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">SetDisplayErrors</span><span style="color: #006600">(</span><span style="color: #0000CC">true</span><span style="color: #006600">); <br /><br /></span><span style="color: #FF9900">// Set our key<br /></span><span style="color: #0000CC">$key </span><span style="color: #006600">= </span><span style="color: #CC0000">"Mike at softwareprojects.com"</span><span style="color: #006600">;<br /><br /></span><span style="color: #FF9900">// Insert record ("Columns" in Cassandra) <br /></span><span style="color: #0000CC">$record </span><span style="color: #006600">= array(); <br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">"name"</span><span style="color: #006600">&#93; = </span><span style="color: #CC0000">"Mike Peters"</span><span style="color: #006600">; <br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">"email"</span><span style="color: #006600">&#93; = </span><span style="color: #CC0000">"mike at softwareprojects.com"</span><span style="color: #006600">; <br />if (</span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">InsertRecord</span><span style="color: #006600">(</span><span style="color: #CC0000">'mytable'</span><span style="color: #006600">, </span><span style="color: #0000CC">$key</span><span style="color: #006600">, </span><span style="color: #0000CC">$record</span><span style="color: #006600">)) <br />{ <br />  echo </span><span style="color: #CC0000">"Record (Columns) inserted successfully.\r\n"</span><span style="color: #006600">; <br />} <br /><br /></span><span style="color: #FF9900">// Print record <br /></span><span style="color: #0000CC">$record </span><span style="color: #006600">= </span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">GetRecordByKey</span><span style="color: #006600">(</span><span style="color: #CC0000">'mytable'</span><span style="color: #006600">, </span><span style="color: #0000CC">$key</span><span style="color: #006600">); <br /></span><span style="color: #0000CC">print_r</span><span style="color: #006600">(</span><span style="color: #0000CC">$record</span><span style="color: #006600">);  </span><span style="color: #0000CC"></font></span></div>
<br />
<b>Setup tips</b><br />
<br />
The PHP Wrapper was tested with the 0.7 trunk version, June 30, 2010 build.<br />
<br />
Make sure you patch your Thrift TSocket as described <a href="http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-1959.html" target="_blank">here</a> (comments section).<br />
<br />
-<br />
<br />
<b>Further Reading</b><br />
<br />
If you're just getting started with Cassandra, you'll enjoy the selection of hand picked Cassandra tutorials listed below.  These are the best of the best:<br />
<br />
= Tutorials<br />
* Start here: <a href="http://arin.me/blog/wtf-is-a-supercolumn-cassandra-data-model" target="_blank">WTF is a SuperColumn</a><br />
* <a href="http://www.slideshare.net/benjaminblack/cassandra-basics-indexing" target="_blank">Cassandra Basics - Indexing</a><br />
* <a href="http://www.slideshare.net/gdusbabek/introduction-to-cassandra-june-2010" target="_blank">Introduction to Cassandra</a><br />
* <a href="http://www.parleys.com/#st=5&amp;id=1866" target="_blank">History of Cassandra (Video)</a><br />
* <a href="http://www.slideshare.net/mongodb/nosql-the-shift-to-a-nonrelational-world" target="_blank">The shift to NoSQL</a><br />
<br />
= Case Studies<br />
* <a href="http://www.slideshare.net/ryansking/scaling-twitter-with-cassandra" target="_blank">Scaling Twitter with Cassandra</a><br />
* <a href="http://www.scribd.com/doc/21962142/Just-Add-an-Index" target="_blank">Cassandra at Digg</a><br />
* <a href="http://www.rackspacecloud.com/blog/2010/05/12/cassandra-by-example/" target="_blank">Cassandra at RackSpace</a><br />
<br />
= Internals<br />
* <a href="http://www.mikeperham.com/2010/03/17/cassandra-internals-reading/" target="_blank">How Reads work in Cassandra</a><br />
* <a href="http://www.mikeperham.com/2010/03/13/cassandra-internals-writing/" target="_blank">How Writes work in Cassandra</a><br />
* <a href="http://www.slideshare.net/benjaminblack/introduction-to-cassandra-replication-and-consistency" target="_blank">Cassandra Replication and Consistency</a><br />
* <a href="http://ria101.wordpress.com/2010/02/22/cassandra-randompartitioner-vs-orderpreservingpartitioner/" target="_blank">Random Partitioner vs Order Preserving Partitioner (OPP)</a><br />
* <a href="http://www.scribd.com/doc/17056900/Cassandra" target="_blank">Cassandra 101</a><br />
* <a href="http://maxgrinev.com/2010/07/12/do-you-really-need-sql-to-do-it-all-in-cassandra/" target="_blank">Do you really need SQL to do it all in Cassandra</a><br />
* <a href="http://blog.octo.com/en/nosql-lets-play-with-cassandra-part-13/" target="_blank">Let's play with Cassandra</a>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-07-1979.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-07-1979.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[Mercurial Hook: PHP Syntax Check]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-mercurial-hook-php-syntax-check-1976.html</link>
<pubDate>Wed, 16 Jun 2010 04:40:18 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-mercurial-hook-php-syntax-check-1976.html</guid>
<description><![CDATA[In a previous post, we wrote about <a href="http://www.softwareprojects.com/resources/programming/t-realtime-php-syntax-checking-1964.html" target="_blank">PHP real time syntax checking</a>.  A method to send email alerts when any critical scripts on your live servers fail to pass PHP syntax validation.<br />
<br />
The problem with this approach is that it alerts you of a problem after-the-fact.<br />
<br />
Someone uploads a bad PHP script which breaks a critical module, you are alerted, but by that time users are already affected.<br />
<br />
Today we'll take it this concept one step further by writing a hook for the <a href="http://www.softwareprojects.com/resources/programming/t-mercurial-version-control-1853.html" target="_blank">Mercurial version control system</a>, preventing developers from checking-in scripts that don't pass syntax-checking.<br />
<br />
<b>Installing PHP Syntax Check Mercurial hook</b><br />
<br />
<b>Step 1:</b> The shell script<br />
<br />
Save this shell script under your Mercurial folder, calling it: php_syntax.sh<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">#!/usr/local/bin/bash<BR>echo "STARTING PHP SYNTAX CHECK..."<BR># create a random temp file<BR>temp_file=`/usr/bin/mktemp -t php_syntax_files`<BR><BR># get all modified files and remove duplicate's<BR>#note: use file_mods,file_adds instead<BR>hg log -r $HG_NODE:tip --template "{files}\n" | sort | uniq > $temp_file<BR><BR># Walk through each line<BR>#for line in "$temp_file"; do<BR>for line in $(< $temp_file); do<BR>&nbsp; &nbsp; # Make sure it is a php file<BR>&nbsp; &nbsp; if [ `echo $line |&nbsp; grep -E "\.(php)|(php4)|(php5)$"` ]<BR>&nbsp; &nbsp; then<BR>&nbsp; &nbsp; &nbsp; &nbsp; # create a random temp file<BR>&nbsp; &nbsp; &nbsp; &nbsp; php_file=`/usr/bin/mktemp -t php_syntax_check`<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; # save the contents of this file (latest commit) to the temp file<BR>&nbsp; &nbsp; &nbsp; &nbsp; hg cat -r tip $line > $php_file<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; # check the syntax<BR>&nbsp; &nbsp; &nbsp; &nbsp; php_syntax_output=`/usr/local/bin/php-cgi -l -d display_errors=1 -d error_reporting=4 -d html_errors=0 < $php_file`;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; # remove the temp file<BR>&nbsp; &nbsp; &nbsp; &nbsp; rm -f $php_file;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; test_syntax=`echo $php_syntax_output | grep "Parse error"`<BR>&nbsp; &nbsp; &nbsp; &nbsp; if [ "$test_syntax" ];then<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit 1;<BR>&nbsp; &nbsp; &nbsp; &nbsp; fi<BR>&nbsp; &nbsp; fi<BR>done<BR><BR>rm -f "$temp_file"</code><hr />
</div>
<br />
<b>Step 2:</b> Add hook to Mercurial config file<br />
<br />
Update your Mercurial hgweb.config, adding the new hook under the 'hooks' section, like this:<br />
<br />
[hooks]<br />
pretxnchangegroup.syntax_check = /usr/home/mercurial/php_syntax.sh<br />
<br />
--<br />
<br />
Replace '/usr/home/mercurial' with the path where you saved php_syntax.sh<br />
<br />
Attempting to push changes to the repository that don't pass php-syntax check, are now blocked.<br />
<br />
Users will be presented with an error message and the transaction rolled back, until the user fixes the problem and pushes changes again.]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-mercurial-hook-php-syntax-check-1976.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-mercurial-hook-php-syntax-check-1976.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[How to install APC on Linux / RedHat]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-how-to-install-apc-on-linux-redhat-1974.html</link>
<pubDate>Mon, 14 Jun 2010 18:32:53 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-how-to-install-apc-on-linux-redhat-1974.html</guid>
<description><![CDATA[<a href="http://php.net/manual/en/book.apc.php" target="_blank">APC</a> (Alternative PHP Cache) is an opcode cache for PHP that pre-compiles commonly used PHP scripts to machine-language, speeding up execution.<br />
<br />
We previously used <a href="http://eaccelerator.net/" target="_blank">eAccelarator</a> which is another opcode cache for PHP.  We decided to switch from eAccelarator to APC due to stability issues and the fact that APC was chosen as the best opcode cache engine to be included with PHP6. <br />
<br />
In this post, I'll cover the steps to install APC:<br />
<br />
1. Type: 'pecl config-set preferred-state stable'<br />
2. Type: 'pecl install apc'<br />
3. When prompted type 'all' to change configuration<br />
4. Type 'no' to change the option<br />
5. Hit the enter key to run the installation script.<br />
6. When complete, open /usr/local/lib/php.ini for editing<br />
7. Comment out all eaccelerator lines if they exist<br />
8. Add the following block:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left"> <BR>extension=apc.so<BR>apc.enabled="1"<BR>apc.shm_segments="1"<BR>apc.shm_size="100"<BR>apc.ttl="5"<BR>apc.user_ttl="5"<BR>apc.gc_ttl="5"<BR>apc.file_update_protection="2"<BR>apc.enable_cli="1"<BR>apc.max_file_size="1M"<BR>apc.write-lock="1"<BR>apc.report_autofilter="0"<BR>apc.include_once_override="0"</code><hr />
</div>
<br />
Additional settings and explanations for each can be found at: <a href="http://www.php.net/manual/en/apc.configuration.php" target="_blank">http://www.php.net/manual/en/apc.configuration.php</a><br />
 <br />
9. Save the file and exit the editor<br />
<br />
10. Change the system setting for shared memory as follows:<br />
a. edit /etc/sysctl.conf<br />
b. add this line: <br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">kern.ipc.shmmax=134217728</code><hr />
</div>
<br />
c. the run the following command: <br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">sysctl -w kern.ipc.shmmax=134217728</code><hr />
</div>
<br />
11. Restart FastCGI manager with:<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">/usr/local/bin/php-fpm restart</code><hr />
</div>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-how-to-install-apc-on-linux-redhat-1974.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-how-to-install-apc-on-linux-redhat-1974.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[Temporary File Names in C]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-temporary-file-names-in-c-1973.html</link>
<pubDate>Thu, 27 May 2010 10:24:03 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-temporary-file-names-in-c-1973.html</guid>
<description><![CDATA[There's a lot of confusion among developers about the best way to generate a unique temporary file name in C.<br />
<br />
If you're using C, the most suitable function that comes to mind is <a href="http://www.opengroup.org/onlinepubs/000095399/functions/tmpnam.html" target="_blank">tmpnam()</a>:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">char *tmpnam(char *str);<BR><BR>// Usage<BR>printf ("My temporary file name is: %s\r\n", tmpnam("/usr/tmp"));</code><hr />
</div> <br />
While it seems like a great fit, you should <b>never ever use tmpnam</b>.<br />
<br />
I'll repeat it again - avoid using tmpnam() at all cost.<br />
<br />
The reason is - tmpnam() suffers from a race condition:  <br />
<br />
Since the temporary file never gets created, if you have multiple threads/processes calling tmpnam() at the same time, it is very possible for two instances to end up with the same exact temporary file name...  And the consequences can be fatal.<br />
<br />
The tmpnam() function should be deprecated.  That's probably why it was never ported over to PHP.  <br />
<br />
In PHP you should use <a href="http://php.net/manual/en/function.tempnam.php" target="_blank">tempnam()</a> or <a href="http://www.php.net/manual/en/function.tmpfile.php" target="_blank">tmpfile()</a>, both of which create the temporary file before returning the name, so you are guaranteed no two instances will ever end up with the same temporary file name.<br />
<br />
Here's the correct way to get a temporary file name in C:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">&nbsp; &nbsp; char sTempfile[] = "/usr/tmp/mytmpfileXXXXXX"; // The X's are important<BR>&nbsp; &nbsp; int tmp_handle;<BR><BR>&nbsp; &nbsp; if ( (tmp_handle=mkstemp(sTempfile)) < 1)<BR>&nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; return 0;<BR>&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; close(tmp_handle);<BR><BR>&nbsp; &nbsp; // We now have the temporary filename in sTempfile<BR>&nbsp; &nbsp; printf ("My temporary file name is: %s\r\n", sTempfile);</code><hr />
</div>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-temporary-file-names-in-c-1973.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-temporary-file-names-in-c-1973.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[MySQL InnoDB Deadlocks and Duplicate key errors (1213, 1205, 1062)]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-mysql-innodb-deadlocks-and-duplicate-key-1970.html</link>
<pubDate>Thu, 06 May 2010 03:41:28 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-mysql-innodb-deadlocks-and-duplicate-key-1970.html</guid>
<description><![CDATA[<b>InnoDB</b> is one of the best <a href="http://www.softwareprojects.com/resources/programming/t-mysql-storage-engines-1470.html" target="_blank">MySQL storage engines</a> when you're looking for concurrent writes, transactions support and ACID reliability.<br />
<br />
Switching from MyISAM to InnoDB is very straightforward (<i>alter table MYTABLE engine=innodb</i>), but there are a few pitfalls you should be prepared for.<br />
<br />
Review the full list of <a href="http://dev.mysql.com/doc/refman/5.0/en/innodb-restrictions.html" target="_blank">InnoDB restrictions</a>, understand them and make sure nothing collides with your existing design...  Watch out for SELECT COUNT(*) on InnoDB - they require full table scans.<br />
<br />
As part of this post I'd like to touch on three key issues with InnoDB that many developers fail to properly deal with:<br />
<br />
<b>#1062 Duplicate entry for key 'PRIMARY' error</b><br />
<br />
This is <a href="http://bugs.mysql.com/bug.php?id=26316" target="_blank">bug 26316</a> that has been around for quite some time and is still alive and kicking as of version 5.1.43 of MySQL that we're using.<br />
<br />
Not sure if it is really linked to triggers or not.  Out of nowhere, MySQL will throw a 1062 duplicate key error on auto_increment keys.  Something which by design is impossible.<br />
<br />
To fix, you have to retry the transaction and it will go through on the second attempt. <br />
 <br />
<b>#1213 Deadlock found when trying to get lock error</b><br />
<br />
InnoDB locks rows and starts transactions internally as needed.<br />
<br />
From time to time, particularly when concurrent threads are hitting the same rows, you're going to experience a deadlock.<br />
<br />
Deadlocks happen when two transactions wait on each other to acquire a lock. For example:<br />
<br />
Tx 1: lock A, then B<br />
Tx 2: lock B, then A  <br />
<br />
Because InnoDB starts transactions on the internally, you -are- going to experience deadlocks.  <br />
<br />
No way of escaping it.  <br />
<br />
Fortunately when deadlocks do happen with this error 1213, all you have to do is retry the query until it goes through.<br />
<br />
<b>#1205 Lock wait timeout exceeded error</b><br />
<br />
Similar to error 1213, this one will occur whether or not you are manually starting a transaction.<br />
<br />
This error is more complicated to deal with -  <br />
<br />
If the transaction was triggered by InnoDB (atomic to the query you're running), you can simply retry the query and it will eventually go through.  <br />
<br />
If however this error shows up inside a transaction you started, it means any uncommitted inserts/updates are lost and will have to be resent in a new transaction block. <br />
<br />
--<br />
<br />
Below are two simple PHP MySQL wrapper functions you can use to gracefully handle the three issues I described, while logging all errors to a file.<br />
<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #006600">function </span><span style="color: #0000CC">DBRead</span><span style="color: #006600">(</span><span style="color: #0000CC">$query</span><span style="color: #006600">, </span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">=</span><span style="color: #0000CC">0</span><span style="color: #006600">)<br />{<br />&nbsp; </span><span style="color: #FF9900">// Read query (SELECT)<br />&nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">)<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$result </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_query</span><span style="color: #006600">(</span><span style="color: #0000CC">$query</span><span style="color: #006600">, </span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">);<br />&nbsp; else<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$result </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_query</span><span style="color: #006600">(</span><span style="color: #0000CC">$query</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// Return result<br />&nbsp; </span><span style="color: #006600">return </span><span style="color: #0000CC">$result</span><span style="color: #006600">;<br />}<br /><br />function </span><span style="color: #0000CC">DBWrite</span><span style="color: #006600">(</span><span style="color: #0000CC">$query</span><span style="color: #006600">, </span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">=</span><span style="color: #0000CC">0</span><span style="color: #006600">)<br />{<br />&nbsp; </span><span style="color: #FF9900">// If any of these error codes is returned by MySQL, we'll retry<br />&nbsp; </span><span style="color: #0000CC">$arr_need_to_retry_error_codes </span><span style="color: #006600">= array<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">1213</span><span style="color: #006600">,&nbsp;    </span><span style="color: #FF9900">// Deadlock found when trying to get lock<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">1205&nbsp; &nbsp; </span><span style="color: #FF9900">// Lock wait timeout exceeded<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// Initialize<br />&nbsp; </span><span style="color: #0000CC">$cnt_retry </span><span style="color: #006600">= </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />&nbsp; </span><span style="color: #0000CC">$error_str </span><span style="color: #006600">=</span><span style="color: #CC0000">""</span><span style="color: #006600">;<br /><br />&nbsp; </span><span style="color: #FF9900">// Main loop<br />&nbsp; </span><span style="color: #006600">do<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Initialize 'flag_retry' indicating whether or not we need to retry this transaction<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$flag_retry </span><span style="color: #006600">= </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Write query (UPDATE, INSERT)<br />&nbsp; &nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$result </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_query</span><span style="color: #006600">(</span><span style="color: #0000CC">$query</span><span style="color: #006600">, </span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">);<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$mysql_errno&nbsp; </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_errno</span><span style="color: #006600">(</span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">);<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$mysql_error&nbsp; </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_error</span><span style="color: #006600">(</span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">);<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; else<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$result </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_query</span><span style="color: #006600">(</span><span style="color: #0000CC">$query</span><span style="color: #006600">);<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$mysql_errno&nbsp; </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_errno</span><span style="color: #006600">();<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$mysql_error&nbsp; </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_error</span><span style="color: #006600">();<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// If failed,<br />&nbsp; &nbsp; </span><span style="color: #006600">if (!</span><span style="color: #0000CC">$result</span><span style="color: #006600">)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Determine if we need to retry this transaction -<br />&nbsp; &nbsp; &nbsp; &nbsp; // If duplicate PRIMARY key error,<br />&nbsp; &nbsp; &nbsp; &nbsp; // or one of the errors in 'arr_need_to_retry_error_codes'<br />&nbsp; &nbsp; &nbsp; &nbsp; // then we need to retry<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$flag_retry </span><span style="color: #006600">=  ((</span><span style="color: #0000CC">$mysql_errno</span><span style="color: #006600">==</span><span style="color: #0000CC">1062 </span><span style="color: #006600">&amp;&amp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;    </span><span style="color: #0000CC">strpos</span><span style="color: #006600">(</span><span style="color: #0000CC">$mysql_error</span><span style="color: #006600">,</span><span style="color: #CC0000">"for key 'PRIMARY'"</span><span style="color: #006600">)!==</span><span style="color: #0000CC">false</span><span style="color: #006600">) ||<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;    </span><span style="color: #0000CC">in_array</span><span style="color: #006600">(</span><span style="color: #0000CC">$mysql_errno</span><span style="color: #006600">, </span><span style="color: #0000CC">$arr_need_to_retry_error_codes</span><span style="color: #006600">)) ;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// If this was error 1205, log it<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$mysql_errno</span><span style="color: #006600">==</span><span style="color: #0000CC">1205</span><span style="color: #006600">)<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">DBLogError</span><span style="color: #006600">(</span><span style="color: #0000CC">$query</span><span style="color: #006600">, </span><span style="color: #CC0000">"Error #1205 detected - review application logic"</span><span style="color: #006600">, </span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">);<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// If successful or failed but no need to retry<br />&nbsp; &nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$result </span><span style="color: #006600">|| empty(</span><span style="color: #0000CC">$flag_retry</span><span style="color: #006600">))<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// We're done<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #006600">break;<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// If we're up to here this means that -<br />&nbsp; &nbsp; // Error occured, wait 1 second before we try again<br />&nbsp; &nbsp; </span><span style="color: #0000CC">sleep</span><span style="color: #006600">(</span><span style="color: #0000CC">1</span><span style="color: #006600">);<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$cnt_retry</span><span style="color: #006600">++;<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// If we already retried 10 times, log error<br />&nbsp; &nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$cnt_retry</span><span style="color: #006600">&gt;=</span><span style="color: #0000CC">10</span><span style="color: #006600">)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$result&nbsp;  </span><span style="color: #006600">= </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$error_str  </span><span style="color: #006600">= </span><span style="color: #CC0000">"Retried $cnt_retry times due to error "</span><span style="color: #006600">.<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;   </span><span style="color: #0000CC">$mysql_errno</span><span style="color: #006600">.</span><span style="color: #CC0000">" and finally gave up"</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; }<br /><br />&nbsp; } while (</span><span style="color: #0000CC">1</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// If update query failed, log<br />&nbsp; </span><span style="color: #006600">if (!</span><span style="color: #0000CC">$result</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #0000CC">DBLogError</span><span style="color: #006600">(</span><span style="color: #0000CC">$query</span><span style="color: #006600">, </span><span style="color: #0000CC">$error_str</span><span style="color: #006600">, </span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">);<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Return result<br />&nbsp; </span><span style="color: #006600">return </span><span style="color: #0000CC">$result</span><span style="color: #006600">;<br />}<br /><br />function </span><span style="color: #0000CC">DBLogError</span><span style="color: #006600">(</span><span style="color: #0000CC">$query</span><span style="color: #006600">=</span><span style="color: #CC0000">""</span><span style="color: #006600">, </span><span style="color: #0000CC">$msg</span><span style="color: #006600">=</span><span style="color: #CC0000">""</span><span style="color: #006600">, </span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">=</span><span style="color: #0000CC">0</span><span style="color: #006600">)<br />{<br />&nbsp; </span><span style="color: #FF9900">// Set these for easier access<br />&nbsp; </span><span style="color: #006600">if (!empty(</span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">))<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$mysql_errno </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_errno</span><span style="color: #006600">(</span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">);<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$mysql_error </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_error</span><span style="color: #006600">(</span><span style="color: #0000CC">$link_identifier</span><span style="color: #006600">);<br />&nbsp; }<br />&nbsp; else<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$mysql_errno </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_errno</span><span style="color: #006600">();<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$mysql_error </span><span style="color: #006600">= </span><span style="color: #0000CC">mysql_error</span><span style="color: #006600">();<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Format 'msg' if we have it<br />&nbsp; </span><span style="color: #006600">if (!empty(</span><span style="color: #0000CC">$msg</span><span style="color: #006600">)) </span><span style="color: #0000CC">$msg </span><span style="color: #006600">= </span><span style="color: #CC0000">"$msg"</span><span style="color: #006600">;<br />&nbsp; else </span><span style="color: #0000CC">$msg </span><span style="color: #006600">= </span><span style="color: #CC0000">"Error #"</span><span style="color: #006600">.</span><span style="color: #0000CC">$mysql_errno</span><span style="color: #006600">.</span><span style="color: #CC0000">" $mysql_error"</span><span style="color: #006600">;<br /><br />&nbsp; </span><span style="color: #FF9900">// Log<br />&nbsp; </span><span style="color: #0000CC">$file </span><span style="color: #006600">= @</span><span style="color: #0000CC">fopen</span><span style="color: #006600">(</span><span style="color: #CC0000">"/usr/tmp/dbwrite_error.log"</span><span style="color: #006600">,</span><span style="color: #CC0000">"a"</span><span style="color: #006600">);<br />&nbsp; @</span><span style="color: #0000CC">fwrite</span><span style="color: #006600">(</span><span style="color: #0000CC">$file</span><span style="color: #006600">, </span><span style="color: #0000CC">date</span><span style="color: #006600">(</span><span style="color: #CC0000">"Y-m-d h:i:s"</span><span style="color: #006600">).</span><span style="color: #CC0000">" "</span><span style="color: #006600">.</span><span style="color: #0000CC">$_SERVER</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'REQUEST_URI'</span><span style="color: #006600">&#93;.<br />&nbsp; </span><span style="color: #CC0000">": $msg; Query was: $query;  "</span><span style="color: #006600">.<br />&nbsp; </span><span style="color: #0000CC">$_SERVER</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'SCRIPT_FILENAME'</span><span style="color: #006600">&#93;.</span><span style="color: #CC0000">")\r\n"</span><span style="color: #006600">);<br />&nbsp; @</span><span style="color: #0000CC">fclose</span><span style="color: #006600">(</span><span style="color: #0000CC">$file</span><span style="color: #006600">);<br />}  </span><span style="color: #0000CC"></font></span></div>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-mysql-innodb-deadlocks-and-duplicate-key-1970.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-mysql-innodb-deadlocks-and-duplicate-key-1970.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[Thickbox - ColorBox - FancyBox - ShadowBox Comparison]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-thickbox-colorbox-fancybox-shadowb-1968.html</link>
<pubDate>Wed, 28 Apr 2010 19:21:22 GMT</pubDate>
<dc:creator><![CDATA[Brett Batie]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-thickbox-colorbox-fancybox-shadowb-1968.html</guid>
<description><![CDATA[There are a large number of pop-up/dialog box libraries that are now available. Many of the pop-up dialog options out there support the same features. However, in our testing we found that some failed terribly. <br />
<br />
As part of this post, I'd like to cover a quick comparison of what I found in hopes that it will help others in their search for the ideal pop-up library.<br />
<br />
The popup controls I tested:<br />
<br />
	<li><a href="http://jqueryui.com/demos/dialog/">jQueryUI Dialog</a><br />
	<li><a href="http://swip.codylindley.com/DOMWindowDemo.html">DOM Window</a><br />
	<li><a href="http://jquery.com/demo/thickbox/">Thickbox</a><br />
	<li><a href="http://fancybox.net/">fancybox</a><br />
	<li><a href="http://colorpowered.com/colorbox/">Colorbox</a><br />
	<li><a href="http://www.shadowbox-js.com/index.html">Shadowbox.js</a><br />
<br />
At a first glance all of the above looked like they would work well. However we did find some major flaws with the above libraries. Here is what we found:<br />
<br />
<a href="http://jqueryui.com/demos/dialog/">jQueryUI Dialog</a> - This is by far our favorite pop-up dialog. It supports features such as modal, dragging it to other locations on the screen and allows the user to resize it. The features that jQuery offers makes it a top choice. However, the major flaw is it makes IE6 hang for about 10 seconds when the page is loading. That one flaw forced us to look at other options.<br />
<br />
<a href="http://swip.codylindley.com/DOMWindowDemo.html">Dom window</a> - For us this package is too basic. It does support the basic needs of a pop-up but when compared to some of the other libraries it was not nearly as good.<br />
<br />
<a href="http://jquery.com/demo/thickbox/">Thickbox </a>- This library had all the features needed. We have used it in the past and have found it very easy to work with. The major pitfall with this library is it is no longer supported. The library works correctly today but there is no guarantee it will continue to work into the future and therefore is not a great choice.<br />
<br />
<a href="http://fancybox.net/">FancyBox</a> - This library has a good set of features unfortunately it loads very slowly in IE6 and can cause the browser to crash.<br />
<br />
<a href="http://colorpowered.com/colorbox/">ColorBox</a> - This library works very well, supports a good amount of features and does load correctly in all browsers including IE6. It would be nice if this library had all of the features of the jQueryUI Dialog and some may consider it a flaw that jQuery is required in order to use this library.<br />
<br />
<a href="http://www.shadowbox-js.com/index.html">ShadowBox</a> - This library works well in all of the major browsers and supports most features that are necessary for a nice pop-up. It does not have all of the features of jQueryUI Dialog. This library includes everything needed in one javascript and css file.<br />
<br />
Based on my research I found the two best libraries to be <b>colorbox</b> and <b>shadowbox</b>. <br />
<br />
These libraries offer very similar features and they both work well with all of the major browsers. For us ColorBox was the right choice because it has is under an MIT license, the size of the library is smaller than ShadowBox and it supports auto-resize-to-fit-content which ShadowBox didn't have.]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-thickbox-colorbox-fancybox-shadowb-1968.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-thickbox-colorbox-fancybox-shadowb-1968.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[RabbitMQ Message Queue]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-rabbitmq-message-queue-1967.html</link>
<pubDate>Fri, 16 Apr 2010 22:13:02 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-rabbitmq-message-queue-1967.html</guid>
<description><![CDATA[<a href="http://www.rabbitmq.com/index.html" target="_blank">RabbitMQ</a> is an open source messaging server, allowing you to scale by queuing jobs to be processed offline.<br />
<br />
If you're looking to scale your service, integrating message-queues into your application is a MUST.  Without <b>offline processing</b>, you'll quickly hit a glass ceiling on maximum throughput.<br />
<br />
In the world of message-queues, there are lots of different solutions.  From the over-simplistic <a href="http://github.com/s0enke/dropr/" target="_blank">PHP Dropr</a>, through RabbitMQ, ActiveMQ, Fuse, ZeroMQ and commercial enterprise queue systems.<br />
<br />
There's a great post by the makers of SecondLife, sharing their evaluation of 9 popular message-queue systems <a href="http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_Notes" target="_blank">here</a>.  Take the time to review available message-queues and test which one best suits your needs.<br />
<br />
Here at SPI, our original implementation of distributed message-queues was MySQL based.  Two master-master databases, sharing a queue table.  While it did work for our needs, <a href="http://www.rabbitmq.com/resources/RabbitMQ_Oxford_Geek_Night.pdf" target="_blank">databases suck for message queues</a> and we needed a better solution.<br />
<br />
We had a short run with <a href="http://activemq.apache.org/" target="_blank">ActiveMQ</a> -- another popular message-queue solution, but were not happy with the test results.  RabbitMQ is a lot faster (x40 times), supports <a href="http://en.wikipedia.org/wiki/Advanced_Message_Queuing_Protocol" target="_blank">AMQP</a> and is better suited for distributed clusters.<br />
<br />
Let's cover the steps to install RabbitMQ on your server.<br />
<br />
<img src="http://www.softwareprojects.com/blogimages/867f8e39.png" border="0" alt="" /><br />
<br />
<font size="4">1. Getting started</font><br />
<br />
Download the latest version of RabbitMQ from <a href="http://www.rabbitmq.com/download.html" target="_blank">this page</a>.<br />
<br />
Use the Bin version.  No need to recompile.<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">unsetenv JAVA_HOME<BR>mkdir /usr/tmp<BR>fetch "http://www.rabbitmq.com/releases/rabbitmq-server/v1.7.2/rabbitmq-server-generic-unix-1.7.2.tar.gz"<BR>tar xvfz rabbitmq-server-generic-unix-1.7.2.tar.gz</code><hr />
</div>
<br />
<font size="4">2. Download JRE</font><br />
<br />
Download and install the version of JRE matching your system (32bit or 64bit) from the <a href="http://java.sun.com/javase/downloads/index.jsp#jre" target="_blank">Java SE Download page</a>.<br />
<br />
<font size="4">3. Install Erlang</font><br />
<br />
If you're on FreeBSD, simply do:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">cd /usr/ports/lang/erlang<BR>make all<BR>make install</code><hr />
</div>
<br />
Otherwise, <a href="http://www.erlang.org/download.html" target="_blank">Download Erlang</a>.<br />
<br />
<font size="4">4. Start RabbitMQ</font><br />
<br />
Changedir to the path where you installed RabbitMQ and start it with:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">cd bin<BR>./rabbitmq-server&nbsp; -detached</code><hr />
</div>
<br />
Simple right?  You now have the RabbitMQ up and running (in the background), ready to relay messages.<br />
<br />
It's important to understand the terminology and building blocks (Queues, Exchanges, Bindings) of RabbitMQ.<br />
<br />
Take the time to review the <a href="http://www.slideshare.net/somic/introduction-to-amqp-messaging-with-rabbitmq" target="_blank">Getting Started Slides</a> and <a href="http://www.rabbitmq.com/faq.html" target="_blank">FAQ</a>.<br />
<br />
<font size="4">5. Communicating with RabbitMQ</font><br />
<br />
Now that we have RabbitMQ up and running we can start the fun part of writing and reading messages.<br />
<br />
For the purpose of this guide, we'll use PHP to communicate with RabbitMQ.<br />
<br />
There are <a href="http://www.rabbitmq.com/how.html" target="_blank">several</a> PHP-wrappers for RabbitMQ.  Our favorite one is the clean and simple <a href="http://code.google.com/p/php-amqplib/" target="_blank">PHP amqp lib</a>.  <br />
<br />
The code below uses php-amqplib to connect and send messages to a RabbitMQ server.<br />
<br />
The publisher (writes messages to queue):<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #006600">require_once(</span><span style="color: #CC0000">'amqp.inc'</span><span style="color: #006600">);<br /><br /></span><span style="color: #0000CC">$HOST </span><span style="color: #006600">= </span><span style="color: #CC0000">'localhost'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$PORT </span><span style="color: #006600">= </span><span style="color: #0000CC">5672</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$USER </span><span style="color: #006600">= </span><span style="color: #CC0000">'guest'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$PASS </span><span style="color: #006600">= </span><span style="color: #CC0000">'guest'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$VHOST </span><span style="color: #006600">= </span><span style="color: #CC0000">'/'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$EXCHANGE </span><span style="color: #006600">= </span><span style="color: #CC0000">'router'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$QUEUE </span><span style="color: #006600">= </span><span style="color: #CC0000">'msgs'</span><span style="color: #006600">;<br /><br /></span><span style="color: #0000CC">$conn </span><span style="color: #006600">= new </span><span style="color: #0000CC">AMQPConnection</span><span style="color: #006600">(</span><span style="color: #0000CC">$HOST</span><span style="color: #006600">, </span><span style="color: #0000CC">$PORT</span><span style="color: #006600">, </span><span style="color: #0000CC">$USER</span><span style="color: #006600">, </span><span style="color: #0000CC">$PASS</span><span style="color: #006600">);<br /></span><span style="color: #0000CC">$ch </span><span style="color: #006600">= </span><span style="color: #0000CC">$conn</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">channel</span><span style="color: #006600">();<br /></span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">access_request</span><span style="color: #006600">(</span><span style="color: #0000CC">$VHOST</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #0000CC">true</span><span style="color: #006600">, </span><span style="color: #0000CC">true</span><span style="color: #006600">);<br /><br /></span><span style="color: #0000CC">$msg_body </span><span style="color: #006600">= </span><span style="color: #0000CC">implode</span><span style="color: #006600">(</span><span style="color: #CC0000">' '</span><span style="color: #006600">, </span><span style="color: #0000CC">array_slice</span><span style="color: #006600">(</span><span style="color: #0000CC">$argv</span><span style="color: #006600">, </span><span style="color: #0000CC">1</span><span style="color: #006600">));<br /></span><span style="color: #0000CC">$msg </span><span style="color: #006600">= new </span><span style="color: #0000CC">AMQPMessage</span><span style="color: #006600">(</span><span style="color: #0000CC">$msg_body</span><span style="color: #006600">, array(</span><span style="color: #CC0000">'content_type' </span><span style="color: #006600">=&gt; </span><span style="color: #CC0000">'text/plain'</span><span style="color: #006600">));<br /></span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">basic_publish</span><span style="color: #006600">(</span><span style="color: #0000CC">$msg</span><span style="color: #006600">, </span><span style="color: #0000CC">$EXCHANGE</span><span style="color: #006600">);<br /><br /></span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">close</span><span style="color: #006600">();<br /></span><span style="color: #0000CC">$conn</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">close</span><span style="color: #006600">();  </span><span style="color: #0000CC"></font></span></div>
<br />
The consumer (reads messages from queue):<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #006600">require_once(</span><span style="color: #CC0000">'amqp.inc'</span><span style="color: #006600">);<br /><br /></span><span style="color: #0000CC">$HOST </span><span style="color: #006600">= </span><span style="color: #CC0000">'localhost'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$PORT </span><span style="color: #006600">= </span><span style="color: #0000CC">5672</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$USER </span><span style="color: #006600">= </span><span style="color: #CC0000">'guest'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$PASS </span><span style="color: #006600">= </span><span style="color: #CC0000">'guest'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$VHOST </span><span style="color: #006600">= </span><span style="color: #CC0000">'/'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$EXCHANGE </span><span style="color: #006600">= </span><span style="color: #CC0000">'router'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$QUEUE </span><span style="color: #006600">= </span><span style="color: #CC0000">'msgs'</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$CONSUMER_TAG </span><span style="color: #006600">= </span><span style="color: #CC0000">'consumer'</span><span style="color: #006600">;<br /><br /></span><span style="color: #0000CC">$conn </span><span style="color: #006600">= new </span><span style="color: #0000CC">AMQPConnection</span><span style="color: #006600">(</span><span style="color: #0000CC">$HOST</span><span style="color: #006600">, </span><span style="color: #0000CC">$PORT</span><span style="color: #006600">, </span><span style="color: #0000CC">$USER</span><span style="color: #006600">, </span><span style="color: #0000CC">$PASS</span><span style="color: #006600">);<br /></span><span style="color: #0000CC">$ch </span><span style="color: #006600">= </span><span style="color: #0000CC">$conn</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">channel</span><span style="color: #006600">();<br /></span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">access_request</span><span style="color: #006600">(</span><span style="color: #0000CC">$VHOST</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #0000CC">true</span><span style="color: #006600">, </span><span style="color: #0000CC">true</span><span style="color: #006600">);<br /><br /></span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">queue_declare</span><span style="color: #006600">(</span><span style="color: #0000CC">$QUEUE</span><span style="color: #006600">);<br /></span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">exchange_declare</span><span style="color: #006600">(</span><span style="color: #0000CC">$EXCHANGE</span><span style="color: #006600">, </span><span style="color: #CC0000">'direct'</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">);<br /></span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">queue_bind</span><span style="color: #006600">(</span><span style="color: #0000CC">$QUEUE</span><span style="color: #006600">, </span><span style="color: #0000CC">$EXCHANGE</span><span style="color: #006600">);<br /><br />function </span><span style="color: #0000CC">process_message</span><span style="color: #006600">(</span><span style="color: #0000CC">$msg</span><span style="color: #006600">) {<br />&nbsp; global </span><span style="color: #0000CC">$ch</span><span style="color: #006600">, </span><span style="color: #0000CC">$CONSUMER_TAG</span><span style="color: #006600">;<br />&nbsp; <br />&nbsp; echo </span><span style="color: #CC0000">"\n--------\n"</span><span style="color: #006600">;<br />&nbsp; echo </span><span style="color: #0000CC">$msg</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">body</span><span style="color: #006600">;<br />&nbsp; echo </span><span style="color: #CC0000">"\n--------\n"</span><span style="color: #006600">;<br />&nbsp; <br />&nbsp; </span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">basic_ack</span><span style="color: #006600">(</span><span style="color: #0000CC">$msg</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">delivery_info</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'delivery_tag'</span><span style="color: #006600">&#93;);<br />&nbsp; <br />&nbsp; </span><span style="color: #FF9900">// Cancel callback<br />&nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$msg</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">body </span><span style="color: #006600">=== </span><span style="color: #CC0000">'quit'</span><span style="color: #006600">) {<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">basic_cancel</span><span style="color: #006600">(</span><span style="color: #0000CC">$CONSUMER_TAG</span><span style="color: #006600">);<br />&nbsp; }<br />}<br /><br /></span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">basic_consume</span><span style="color: #006600">(</span><span style="color: #0000CC">$QUEUE</span><span style="color: #006600">, </span><span style="color: #0000CC">$CONSUMER_TAG</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">, </span><span style="color: #CC0000">'process_message'</span><span style="color: #006600">);<br /><br /></span><span style="color: #FF9900">// Loop as long as the channel has callbacks registered<br /></span><span style="color: #006600">while(</span><span style="color: #0000CC">count</span><span style="color: #006600">(</span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">callbacks</span><span style="color: #006600">)) {<br />&nbsp; </span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">wait</span><span style="color: #006600">();<br />}<br /><br /></span><span style="color: #0000CC">$ch</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">close</span><span style="color: #006600">();<br /></span><span style="color: #0000CC">$conn</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">close</span><span style="color: #006600">();  </span><span style="color: #0000CC"></font></span></div>
<br />
<br />
--<br />
<br />
Further reading:<br />
* Why <a href="http://www.rabbitmq.com/resources/RabbitMQ_Oxford_Geek_Night.pdf" target="_blank">Databases suck for messaging</a><br />
* Introduction to <a href="http://skillsmatter.com/custom/presentations/talk3.rabbitmqintroduction.pdf" target="_blank">Message Queues and how RabbitMQ works</a><br />
* SecondLife (the game) <a href="http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_Notes" target="_blank">evaluation of 9 message-queue solutions</a><br />
* <a href="http://www.rabbitmq.com/admin-guide.html" target="_blank">RabbitMQ Administration</a><br />
* <a href="http://www.rabbitmq.com/clustering.html" target="_blank">RabbitMQ Clustering</a>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-rabbitmq-message-queue-1967.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-rabbitmq-message-queue-1967.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[Realtime PHP Syntax Checking]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-realtime-php-syntax-checking-1964.html</link>
<pubDate>Wed, 14 Apr 2010 17:08:35 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-realtime-php-syntax-checking-1964.html</guid>
<description><![CDATA[If you love <a href="http://en.wikipedia.org/wiki/Agile_software_development" target="_blank">Agile Development</a> and PHP as much as we do, you're probably going to have developers editing critical PHP scripts on live machines.<br />
<br />
While many PHP editors include built-in syntax checking, it doesn't hurt to have a fail-safe in place.  We wanted to come up with a way to apply <a href="http://www.php.net/manual/en/features.commandline.options.php" target="_blank">php -l SCRIPT</a> syntax checking on any PHP script as soon as it is updated.<br />
<br />
At first the idea was to write a plugin for <a href="http://www.softwareprojects.com/resources/programming/t-mercurial-version-control-1853.html" target="_blank">Mercurial</a> - our version control system, so that whenever a changeset is pushed, the syntax will be checked.<br />
<br />
Unfortunately this doesn't cover changes done via SSH or files uploaded via FTP by clients.<br />
<br />
We came up with a shell script, executed by cron every minute, that iterates through all files <b>changed within the last 3 days</b>, applying "php -l" on those files and sending an email to the appropriate developer with the information about the error.<br />
<br />
Regardless of how files are updated, we now have an automated way to syntax-check every single change to a PHP script.<br />
<br />
The emails generated by the script follow this format:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">Oops! Found a FATAL syntax error in a production PHP script...<BR><BR>DIRECTORY: /magnetic/<BR><BR>---------------------------------------------<BR>FILE: ./affiliate/offline_credit_commission.php<BR>---------------------------------------------<BR>Parse error: syntax error, unexpected '<' in /offline_credit_commission.php on line 8<BR>changeset:&nbsp;  2673:fe54a1e4dbe0<BR>user:&nbsp; &nbsp; &nbsp; &nbsp; Doug <BR>date:&nbsp; &nbsp; &nbsp; &nbsp; Wed Apr 14 00:29:26 2010 +0300<BR>summary:&nbsp; &nbsp;  Change offline credit affiliate commission</code><hr />
</div>
<br />
CheckSyntax cronjob:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">#!/bin/sh<BR><BR># Change to source directory directory<BR>cd $(dirname $0)<BR><BR># Generate report<BR>./CheckSyntax.sh >log.$$<BR>a=$?<BR><BR># Email report<BR># TODO: Update these with your settings<BR>if [ $a = 1 ]<BR>then<BR>&nbsp; &nbsp; ./sendEmail -f CheckSyntax@mycompany.com \<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -t developers@mycompany.com&nbsp; \<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -u "Check Syntax" \<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -s mail.mymailserver.com \<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -m < log.$$<BR>fi<BR><BR># Remove temporary file<BR>rm -rf log.$$<BR><BR># (eof)</code><hr />
</div>
<br />
CheckSyntax.sh:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">#!/usr/local/bin/bash<BR><BR>##################################<BR># Config<BR>##################################<BR>#<BR><BR># TODO: Update these with your settings<BR>php="/usr/local/bin/php"<BR>hg="/usr/local/bin/hg"<BR>def="/magnetic" <BR><BR>#<BR>##################################<BR><BR># Directory passed in parameters?<BR>if [ "$1" != "" ]<BR>then<BR>&nbsp; &nbsp; # Get the directory from parameters<BR>&nbsp; &nbsp; dir=$1<BR>else<BR>&nbsp; &nbsp; # No parameter, use default<BR>&nbsp; &nbsp; dir=$def<BR>fi<BR><BR># Change to the directory we need to check<BR>cd $dir<BR><BR># Prepare tmp file<BR>tmp=/tmp/CheckSyntax.$$<BR><BR># Header<BR>echo "Oops! Found a FATAL syntax error in a production PHP script..."<BR>echo ""<BR>echo "DIRECTORY: $dir"<BR><BR># Find PHP files<BR>#&nbsp;  Note: '-newerct' only workds under FreeBSD<BR>find . -newerct '3 days ago' ! -path "*/temp/*" | grep "\.php$" >$tmp<BR><BR># Loop files<BR>cnt=0<BR>err=0<BR>tot=0<BR>while read i<BR>do<BR>&nbsp; &nbsp; # If file isn't a file, skip<BR>&nbsp; &nbsp; [ ! -f "$i" ] &amp;&amp; continue;<BR><BR><BR>&nbsp; &nbsp; ((tot++))<BR><BR>&nbsp; &nbsp; # Syntax check<BR>&nbsp; &nbsp; a=$($php -l $i | grep "^No syntax errors detected in")<BR><BR>&nbsp; &nbsp; # Is it OK?<BR>&nbsp; &nbsp; if [ "$a" = "" ]<BR>&nbsp; &nbsp; then<BR>&nbsp; &nbsp; &nbsp; &nbsp; # Not OK, display details<BR>&nbsp; &nbsp; &nbsp; &nbsp; echo ""<BR>&nbsp; &nbsp; &nbsp; &nbsp; echo "----------------------------------------------------------------------------------------------"<BR>&nbsp; &nbsp; &nbsp; &nbsp; echo "FILE: $i"<BR>&nbsp; &nbsp; &nbsp; &nbsp; echo "----------------------------------------------------------------------------------------------"<BR>&nbsp; &nbsp; &nbsp; &nbsp; a=$($php -d display_errors=1 -l $i | grep "Parse error")<BR>&nbsp; &nbsp; &nbsp; &nbsp; echo $a | sed "s/<[a-zA-Z\/][^>]*>//g"<BR>&nbsp; &nbsp; &nbsp; &nbsp; $hg log $i | head -n 5 | grep -v "^$"<BR>&nbsp; &nbsp; &nbsp; &nbsp; ((cnt++))<BR>&nbsp; &nbsp; &nbsp; &nbsp; err=1<BR>&nbsp; &nbsp; fi<BR>done < $tmp<BR><BR>echo ""<BR>echo "--"<BR>echo ""<BR>echo "$cnt BAD FILES"<BR>echo "$tot TOTAL FILES"<BR>echo ""<BR><BR># Remove tmp file<BR>rm -rf $tmp<BR><BR># 0 - no error | 1 - error<BR>exit $err<BR><BR># (eof)</code><hr />
</div>
<br />
-<br />
<br />
To install, save the two files (CheckSyntax and CheckSyntax.sh), chmod a+rx, update the email settings in CheckSyntax and add this line to your cronjob:<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left"># CheckSyntax<BR># TODO: Update path with the location of CheckSyntax<BR>*/1 * * * * /home/CheckSyntax</code><hr />
</div>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-realtime-php-syntax-checking-1964.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-realtime-php-syntax-checking-1964.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[How to create a PHP Daemon]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-how-to-create-a-php-daemon-1961.html</link>
<pubDate>Wed, 14 Apr 2010 16:15:19 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-how-to-create-a-php-daemon-1961.html</guid>
<description><![CDATA[A daemon is a Linux program that runs in the background. <br />
<br />
Most daemons are written in C. While C is faster and more robust than PHP, looking at development time and cost, PHP generally scores a lot better than C.<br />
<br />
Converting your C daemons to PHP where applicable, makes the code more accessible to a wider percentage of your development force and allows better re-use and connecting of existing code.<br />
<br />
To Facebook, keeping all code accessible to all developers was so important, that they've decided to come up with a way to <a href="http://developers.facebook.com/news.php?story=358&amp;blog=1" target="_blank">compile PHP to C in real time</a>.  So you get PHP speed of development with C speed.  The <a href="http://wiki.github.com/facebook/hiphop-php/" target="_blank">HipHop toolkit</a> is available on an open source license.<br />
<br />
The simple PHP4 functions included here, will let you easily create PHP daemons.<br />
<br />
Features:<br />
* Daemon will not start if <a href="http://www.softwareprojects.com/resources/programming/t-php-check-if-script-is-running-1288.html" target="_blank">another instance is already running</a><br />
* Daemon PID saved to /var/run/<br />
* Daemon log messages saved to /var/log/<br />
* Run in foreground mode with the "-f" flag<br />
<br />
Sample daemon:<br />
<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #006600">require_once(</span><span style="color: #CC0000">"common_daemon.php"</span><span style="color: #006600">);<br /><br /></span><span style="color: #FF9900">// Get parameters from command line<br /></span><span style="color: #006600">foreach (</span><span style="color: #0000CC">$argv </span><span style="color: #006600">as </span><span style="color: #0000CC">$parameter</span><span style="color: #006600">)<br />{<br />&nbsp; </span><span style="color: #0000CC">$run_in_foreground </span><span style="color: #006600">= </span><span style="color: #0000CC">Strcasecmp</span><span style="color: #006600">(</span><span style="color: #0000CC">$parameter</span><span style="color: #006600">,</span><span style="color: #CC0000">"-f"</span><span style="color: #006600">)==</span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />}<br /><br /></span><span style="color: #FF9900">// Start our daemon<br /></span><span style="color: #006600">if (!</span><span style="color: #0000CC">PHPDaemonStart</span><span style="color: #006600">(</span><span style="color: #CC0000">"mydaemon"</span><span style="color: #006600">, </span><span style="color: #0000CC">$run_in_foreground</span><span style="color: #006600">))<br />{<br />&nbsp; </span><span style="color: #FF9900">// Already running<br />&nbsp; </span><span style="color: #006600">return;<br />}<br /><br /></span><span style="color: #FF9900">// Do something useful<br /></span><span style="color: #006600">while (</span><span style="color: #0000CC">1</span><span style="color: #006600">)<br />{<br />&nbsp; </span><span style="color: #0000CC">PHPDaemonLog</span><span style="color: #006600">(</span><span style="color: #CC0000">"Hello, the time is "</span><span style="color: #006600">.</span><span style="color: #0000CC">date</span><span style="color: #006600">(</span><span style="color: #CC0000">"h:i:s"</span><span style="color: #006600">, </span><span style="color: #0000CC">time</span><span style="color: #006600">()));<br />&nbsp; </span><span style="color: #0000CC">sleep</span><span style="color: #006600">(</span><span style="color: #0000CC">1</span><span style="color: #006600">);<br />}<br /><br /></span><span style="color: #FF9900">// Never here  </font></span></div>
<br />
The common_daemon.php functions:<br />
<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #FF9900">// Give us eternity to execute the script. <br /></span><span style="color: #0000CC">ini_set</span><span style="color: #006600">(</span><span style="color: #CC0000">"max_execution_time"</span><span style="color: #006600">, </span><span style="color: #CC0000">"0"</span><span style="color: #006600">);<br /></span><span style="color: #0000CC">ini_set</span><span style="color: #006600">(</span><span style="color: #CC0000">"max_input_time"</span><span style="color: #006600">, </span><span style="color: #CC0000">"0"</span><span style="color: #006600">);<br /></span><span style="color: #0000CC">set_time_limit</span><span style="color: #006600">(</span><span style="color: #0000CC">0</span><span style="color: #006600">);<br /><br />function </span><span style="color: #0000CC">PHPDaemonStart</span><span style="color: #006600">(</span><span style="color: #0000CC">$name</span><span style="color: #006600">, </span><span style="color: #0000CC">$run_in_foreground</span><span style="color: #006600">=</span><span style="color: #0000CC">0</span><span style="color: #006600">)<br />{<br />&nbsp; </span><span style="color: #FF9900">// This must be global so that the handle is kept alive<br />&nbsp; // after this function exits<br />&nbsp; </span><span style="color: #006600">global </span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">;<br /><br />&nbsp; </span><span style="color: #FF9900">// Remember the daemon name<br />&nbsp; </span><span style="color: #006600">global </span><span style="color: #0000CC">$phpdaemon_name</span><span style="color: #006600">;<br />&nbsp; </span><span style="color: #0000CC">$phpdaemon_name </span><span style="color: #006600">= </span><span style="color: #0000CC">$name</span><span style="color: #006600">;<br /><br />&nbsp; </span><span style="color: #FF9900">// Remember if we're running in foreground mode<br />&nbsp; </span><span style="color: #006600">global </span><span style="color: #0000CC">$phpdaemon_run_in_foreground</span><span style="color: #006600">;<br />&nbsp; </span><span style="color: #0000CC">$phpdaemon_run_in_foreground </span><span style="color: #006600">= </span><span style="color: #0000CC">$run_in_foreground</span><span style="color: #006600">;<br /><br />&nbsp; </span><span style="color: #FF9900">// Open PID file<br />&nbsp; </span><span style="color: #0000CC">$tmpfilename </span><span style="color: #006600">= </span><span style="color: #CC0000">"/var/run/$name.pid"</span><span style="color: #006600">;<br />&nbsp; if (!(</span><span style="color: #0000CC">$handle_lockfile </span><span style="color: #006600">= @</span><span style="color: #0000CC">fopen</span><span style="color: #006600">(</span><span style="color: #0000CC">$tmpfilename</span><span style="color: #006600">,</span><span style="color: #CC0000">"a+"</span><span style="color: #006600">)))<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Script already running - abort<br />&nbsp; &nbsp; </span><span style="color: #006600">return </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Obtain an exlcusive lock on file<br />&nbsp; // (If script is running this will fail)<br />&nbsp; </span><span style="color: #006600">if (!@</span><span style="color: #0000CC">flock</span><span style="color: #006600">( </span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">, </span><span style="color: #0000CC">LOCK_EX </span><span style="color: #006600">| </span><span style="color: #0000CC">LOCK_NB</span><span style="color: #006600">, &amp;</span><span style="color: #0000CC">$wouldblock</span><span style="color: #006600">) || </span><span style="color: #0000CC">$wouldblock</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Script already running - abort<br />&nbsp; &nbsp; </span><span style="color: #006600">@</span><span style="color: #0000CC">fclose</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">);<br />&nbsp; &nbsp; return </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Write our PID<br />&nbsp; </span><span style="color: #006600">@</span><span style="color: #0000CC">ftruncate</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">,</span><span style="color: #0000CC">0</span><span style="color: #006600">);<br />&nbsp; @</span><span style="color: #0000CC">fseek</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">, </span><span style="color: #0000CC">0</span><span style="color: #006600">, </span><span style="color: #0000CC">0</span><span style="color: #006600">);<br />&nbsp; @</span><span style="color: #0000CC">fwrite</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">, </span><span style="color: #0000CC">getmypid</span><span style="color: #006600">());<br />&nbsp; @</span><span style="color: #0000CC">fflush</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// If we need to run in foreground, we're done<br />&nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$run_in_foreground</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; return </span><span style="color: #0000CC">1</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// If we're up to here we need to fork our process<br />&nbsp; </span><span style="color: #0000CC">$pid </span><span style="color: #006600">= </span><span style="color: #0000CC">pcntl_fork</span><span style="color: #006600">();<br />&nbsp; if (</span><span style="color: #0000CC">$pid</span><span style="color: #006600">==-</span><span style="color: #0000CC">1</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Can't fork<br />&nbsp; &nbsp; </span><span style="color: #006600">return </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// If we are the parent<br />&nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$pid</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Sleep for 2 seconds, letting the forked process wake up<br />&nbsp; &nbsp; // and attempt to grab our lock<br />&nbsp; &nbsp; </span><span style="color: #0000CC">sleep</span><span style="color: #006600">(</span><span style="color: #0000CC">2</span><span style="color: #006600">);<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Release the lock<br />&nbsp; &nbsp; </span><span style="color: #006600">@</span><span style="color: #0000CC">flock</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">, </span><span style="color: #0000CC">LOCK_UN</span><span style="color: #006600">);<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Close file<br />&nbsp; &nbsp; </span><span style="color: #0000CC">fclose</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">);<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Kill parent process<br />&nbsp; &nbsp; // By now the child has the lock<br />&nbsp; &nbsp; </span><span style="color: #006600">die;<br />&nbsp; }<br />&nbsp; </span><span style="color: #FF9900">// (Else - we are the child)<br />&nbsp; </span><span style="color: #006600">else<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Open file<br />&nbsp; &nbsp; </span><span style="color: #006600">while (!(</span><span style="color: #0000CC">$handle_lockfile </span><span style="color: #006600">= @</span><span style="color: #0000CC">fopen</span><span style="color: #006600">(</span><span style="color: #0000CC">$tmpfilename</span><span style="color: #006600">, </span><span style="color: #CC0000">"a+"</span><span style="color: #006600">)))<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Try again<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">usleep</span><span style="color: #006600">(</span><span style="color: #0000CC">100</span><span style="color: #006600">);<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Re-establish lock on file<br />&nbsp; &nbsp; </span><span style="color: #006600">while (!@</span><span style="color: #0000CC">flock</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">, </span><span style="color: #0000CC">LOCK_EX</span><span style="color: #006600">, &amp;</span><span style="color: #0000CC">$wouldblock</span><span style="color: #006600">))<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Try again<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">usleep</span><span style="color: #006600">(</span><span style="color: #0000CC">100</span><span style="color: #006600">);<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Write pid into file<br />&nbsp; &nbsp; </span><span style="color: #006600">@</span><span style="color: #0000CC">ftruncate</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">,</span><span style="color: #0000CC">0</span><span style="color: #006600">);<br />&nbsp; &nbsp; @</span><span style="color: #0000CC">fseek</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">, </span><span style="color: #0000CC">0</span><span style="color: #006600">, </span><span style="color: #0000CC">0</span><span style="color: #006600">);<br />&nbsp; &nbsp; @</span><span style="color: #0000CC">fwrite</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">, </span><span style="color: #0000CC">getmypid</span><span style="color: #006600">());<br />&nbsp; &nbsp; @</span><span style="color: #0000CC">fflush</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle_lockfile</span><span style="color: #006600">);<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// We have a lock - all good<br />&nbsp; </span><span style="color: #006600">return </span><span style="color: #0000CC">1</span><span style="color: #006600">;<br />}<br /><br />function </span><span style="color: #0000CC">PHPDaemonLog</span><span style="color: #006600">(</span><span style="color: #0000CC">$str</span><span style="color: #006600">)<br />{<br />&nbsp; </span><span style="color: #FF9900">// This is the daemon name<br />&nbsp; </span><span style="color: #006600">global  </span><span style="color: #0000CC">$phpdaemon_name</span><span style="color: #006600">;<br /><br />&nbsp; </span><span style="color: #FF9900">// This indicates whether or not we are running in foreground mode<br />&nbsp; </span><span style="color: #006600">global </span><span style="color: #0000CC">$phpdaemon_run_in_foreground </span><span style="color: #006600">;<br /><br />&nbsp; </span><span style="color: #FF9900">// Open log file<br />&nbsp; </span><span style="color: #0000CC">$handle </span><span style="color: #006600">= @</span><span style="color: #0000CC">fopen</span><span style="color: #006600">(</span><span style="color: #CC0000">"/var/log/$phpdaemon_name.log"</span><span style="color: #006600">, </span><span style="color: #CC0000">"a"</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// Obtain an exclusive lock (so that this is thread safe)<br />&nbsp; </span><span style="color: #0000CC">flock</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle</span><span style="color: #006600">, </span><span style="color: #0000CC">LOCK_EX</span><span style="color: #006600">, &amp;</span><span style="color: #0000CC">$wouldblock</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// Write<br />&nbsp; </span><span style="color: #0000CC">$output </span><span style="color: #006600">= </span><span style="color: #0000CC">date</span><span style="color: #006600">(</span><span style="color: #CC0000">"Y-m-d h:i:s"</span><span style="color: #006600">, </span><span style="color: #0000CC">time</span><span style="color: #006600">()).</span><span style="color: #CC0000">" $str\r\n"</span><span style="color: #006600">;<br />&nbsp; @</span><span style="color: #0000CC">fwrite</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle</span><span style="color: #006600">, </span><span style="color: #0000CC">$output</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// If we are running in foreground mode, output to screen as well<br />&nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$phpdaemon_run_in_foreground</span><span style="color: #006600">) echo </span><span style="color: #0000CC">$output</span><span style="color: #006600">;<br /><br />&nbsp; </span><span style="color: #FF9900">// Release lock<br />&nbsp; </span><span style="color: #0000CC">flock</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle</span><span style="color: #006600">, </span><span style="color: #0000CC">LOCK_UN</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// Close file<br />&nbsp; </span><span style="color: #0000CC">fclose</span><span style="color: #006600">(</span><span style="color: #0000CC">$handle</span><span style="color: #006600">);<br />}<br /><br />function </span><span style="color: #0000CC">IsPHPDaemonRunning</span><span style="color: #006600">(</span><span style="color: #0000CC">$name</span><span style="color: #006600">)<br />{<br />&nbsp; </span><span style="color: #FF9900">// Open PID file<br />&nbsp; </span><span style="color: #0000CC">$tmpfilename </span><span style="color: #006600">= </span><span style="color: #CC0000">"/var/run/$name.pid"</span><span style="color: #006600">;<br />&nbsp; if (!(</span><span style="color: #0000CC">$tmpfile </span><span style="color: #006600">= @</span><span style="color: #0000CC">fopen</span><span style="color: #006600">(</span><span style="color: #0000CC">$tmpfilename</span><span style="color: #006600">,</span><span style="color: #CC0000">"r"</span><span style="color: #006600">)))<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Script not running<br />&nbsp; &nbsp; </span><span style="color: #006600">return </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Obtain an exlcusive lock on file<br />&nbsp; // (If script is running this will fail)<br />&nbsp; </span><span style="color: #0000CC">$not_running </span><span style="color: #006600">= </span><span style="color: #0000CC">flock</span><span style="color: #006600">( </span><span style="color: #0000CC">$tmpfile</span><span style="color: #006600">, </span><span style="color: #0000CC">LOCK_EX </span><span style="color: #006600">| </span><span style="color: #0000CC">LOCK_NB</span><span style="color: #006600">, &amp;</span><span style="color: #0000CC">$wouldblock</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// Release lock if successful<br />&nbsp; </span><span style="color: #006600">if (</span><span style="color: #0000CC">$not_running</span><span style="color: #006600">) </span><span style="color: #0000CC">flock</span><span style="color: #006600">(</span><span style="color: #0000CC">$tmpfile</span><span style="color: #006600">, </span><span style="color: #0000CC">LOCK_UN</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// Close file<br />&nbsp; </span><span style="color: #006600">@</span><span style="color: #0000CC">fclose</span><span style="color: #006600">(</span><span style="color: #0000CC">$tmpfile</span><span style="color: #006600">);<br /><br />&nbsp; </span><span style="color: #FF9900">// Return result<br />&nbsp; </span><span style="color: #006600">return !</span><span style="color: #0000CC">$not_running </span><span style="color: #006600">|| </span><span style="color: #0000CC">$wouldblock</span><span style="color: #006600">;<br />}  </span><span style="color: #0000CC"></font></span></div>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-how-to-create-a-php-daemon-1961.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-how-to-create-a-php-daemon-1961.html</feedburner:origLink>

</item>
<item>
<title><![CDATA[Cassandra PHP Wrapper]]></title>
<link>http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-1959.html</link>
<pubDate>Wed, 07 Apr 2010 14:40:29 GMT</pubDate>
<dc:creator><![CDATA[Mike Peters]]></dc:creator>
<category><![CDATA[Programming]]></category>
<guid isPermaLink="false">http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-1959.html</guid>
<description><![CDATA[<b>Update:</b> Check out the <a href="http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-07-1979.html" target="_blank">Cassandra PHP Wrapper 0.7</a><br />
<br />
In a previous post, I explained how the <a href="http://www.softwareprojects.com/resources/programming/t-how-to-install-cassandra-+-thrift-and-why-you-shou-1956.html" target="_blank">Cassandra</a> decentralized database can allow you to scale well beyond what's possible with MySQL.<br />
<br />
The Cassandra <a href="http://wiki.apache.org/cassandra/DataModel" target="_blank">Data Model</a> and <a href="http://wiki.apache.org/cassandra/API" target="_blank">API</a> are very different than traditional RDBMS way-of-thinking.  That's why a lot of developers switching from MySQL to Cassandra are finding it difficult to grasp at first.  <br />
<br />
To simplify the migration from MySQL to Cassandra, we created a high-level PHP wrapper for Cassandra, using function prototypes and variable names that are close in meaning to RDBMS.<br />
<br />
While there are <a href="http://wiki.apache.org/cassandra/ClientOptions" target="_blank">several other</a> high level PHP wrappers for Cassandra, none of the existing ones answered our requirements -<br />
<br />
* Simple <br />
* Don't throw exceptions, return error codes<br />
* As close as possible to the low level Thrift<br />
* Make it easy for RDBMS developers to adopt<br />
<br />
This is how the <b>CassandraDB</b> class was born.<br />
<br />
Adding records to the database is as simple as:<br />
<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #FF9900">// Initialize Cassandra <br /></span><span style="color: #0000CC">$cassandra </span><span style="color: #006600">= new </span><span style="color: #0000CC">CassandraDB</span><span style="color: #006600">(</span><span style="color: #CC0000">"SPI"</span><span style="color: #006600">);<br /><br /></span><span style="color: #FF9900">// Debug on<br /></span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">SetDisplayErrors</span><span style="color: #006600">(</span><span style="color: #0000CC">true</span><span style="color: #006600">);<br /><br /></span><span style="color: #FF9900">// Insert record ("Columns" in Cassandra)<br /></span><span style="color: #0000CC">$record </span><span style="color: #006600">= array();<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">"name"</span><span style="color: #006600">&#93; = </span><span style="color: #CC0000">"Mike Peters"</span><span style="color: #006600">;<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">"email"</span><span style="color: #006600">&#93; = </span><span style="color: #CC0000">"mike at softwareprojects.com"</span><span style="color: #006600">;<br />if (</span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">InsertRecord</span><span style="color: #006600">(</span><span style="color: #CC0000">'mytable'</span><span style="color: #006600">, </span><span style="color: #CC0000">"Mike Peters"</span><span style="color: #006600">, </span><span style="color: #0000CC">$record</span><span style="color: #006600">))<br />{<br />&nbsp; echo </span><span style="color: #CC0000">"Record (Columns) inserted successfully.\r\n"</span><span style="color: #006600">;<br />}<br /><br /></span><span style="color: #FF9900">// Print record<br /></span><span style="color: #0000CC">$record </span><span style="color: #006600">= </span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">GetRecordByKey</span><span style="color: #006600">(</span><span style="color: #CC0000">'mytable'</span><span style="color: #006600">, </span><span style="color: #CC0000">"Mike Peters"</span><span style="color: #006600">);<br /></span><span style="color: #0000CC">print_r</span><span style="color: #006600">(</span><span style="color: #0000CC">$record</span><span style="color: #006600">);  </span><span style="color: #0000CC"></font></span></div>
<br />
Output is<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">Array<BR>(<BR>&nbsp; &nbsp; [email] => mike at softwareprojects.com<BR>&nbsp; &nbsp; [name] => Mike Peters<BR>)</code><hr />
</div>
<br />
Adding record arrays:<br />
<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #FF9900">// Initialize Cassandra <br /></span><span style="color: #0000CC">$cassandra </span><span style="color: #006600">= new </span><span style="color: #0000CC">CassandraDB</span><span style="color: #006600">(</span><span style="color: #CC0000">"SPI"</span><span style="color: #006600">);<br /><br /></span><span style="color: #FF9900">// Debug on<br /></span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">SetDisplayErrors</span><span style="color: #006600">(</span><span style="color: #0000CC">true</span><span style="color: #006600">);<br /><br /></span><span style="color: #FF9900">// Insert record array ("SuperColumns" in Cassandra)<br /></span><span style="color: #0000CC">$record </span><span style="color: #006600">= array();<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">"Mike Peters"</span><span style="color: #006600">&#93; = array(</span><span style="color: #CC0000">"name" </span><span style="color: #006600">=&gt; </span><span style="color: #CC0000">"Mike Peters"</span><span style="color: #006600">, </span><span style="color: #CC0000">"email" </span><span style="color: #006600">=&gt; </span><span style="color: #CC0000">"Mike at Peters"</span><span style="color: #006600">);<br /></span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">"Jonathan Ellis"</span><span style="color: #006600">&#93; = array(</span><span style="color: #CC0000">"name" </span><span style="color: #006600">=&gt; </span><span style="color: #CC0000">"Jonathan Ellis"</span><span style="color: #006600">, </span><span style="color: #CC0000">"email" </span><span style="color: #006600">=&gt; </span><span style="color: #CC0000">"Jonathan at Ellis"</span><span style="color: #006600">);<br />if (</span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">InsertRecordArray</span><span style="color: #006600">(</span><span style="color: #CC0000">'my_super_table'</span><span style="color: #006600">, </span><span style="color: #CC0000">"People"</span><span style="color: #006600">, </span><span style="color: #0000CC">$record</span><span style="color: #006600">))<br />{<br />&nbsp; echo </span><span style="color: #CC0000">"RecordArray (SuperColumns) inserted successfully.\r\n"</span><span style="color: #006600">;<br />}<br /><br /></span><span style="color: #FF9900">// Print record array<br /></span><span style="color: #0000CC">$record </span><span style="color: #006600">= </span><span style="color: #0000CC">$cassandra</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">GetRecordByKey</span><span style="color: #006600">(</span><span style="color: #CC0000">'my_super_table'</span><span style="color: #006600">, </span><span style="color: #CC0000">'People'</span><span style="color: #006600">);<br /></span><span style="color: #0000CC">print_r</span><span style="color: #006600">(</span><span style="color: #0000CC">$record</span><span style="color: #006600">);  </span><span style="color: #0000CC"></font></span></div>
<br />
Output is<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<hr /><code style="margin:0px" dir="ltr" style="text-align:left">Array<BR>(<BR>&nbsp; &nbsp; [Jonathan Ellis] => Array<BR>&nbsp; &nbsp; &nbsp; &nbsp; (<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [email] => Jonathan at Ellis<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [name] => Jonathan Ellis<BR>&nbsp; &nbsp; &nbsp; &nbsp; )<BR><BR>&nbsp; &nbsp; [Mike Peters] => Array<BR>&nbsp; &nbsp; &nbsp; &nbsp; (<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [email] => Mike at Peters<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [name] => Mike Peters<BR>&nbsp; &nbsp; &nbsp; &nbsp; )<BR><BR>)</code><hr />
</div>
<br />
--<br />
<br />
Here's the complete Cassandra PHP Wrapper class.  Enjoy!<br />
<br />
<div style="margin:10px;  padding:4px; border:1px inset; background-color: #FDF8EB">

	<span style="margin:0px; width:550px; padding:4px;  overflow:auto; "><font face="courier new">  <br /></span><span style="color: #FF9900">// CassandraDB version 0.1<br />// Software Projects Inc<br />// http://www.softwareprojects.com<br />//<br /><br />// Includes<br /></span><span style="color: #0000CC">$GLOBALS</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'THRIFT_ROOT'</span><span style="color: #006600">&#93; = </span><span style="color: #CC0000">'/services/thrift/'</span><span style="color: #006600">;<br />require_once </span><span style="color: #0000CC">$GLOBALS</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'THRIFT_ROOT'</span><span style="color: #006600">&#93;.</span><span style="color: #CC0000">'/packages/cassandra/Cassandra.php'</span><span style="color: #006600">;<br />require_once </span><span style="color: #0000CC">$GLOBALS</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'THRIFT_ROOT'</span><span style="color: #006600">&#93;.</span><span style="color: #CC0000">'/packages/cassandra/cassandra_types.php'</span><span style="color: #006600">;<br />require_once </span><span style="color: #0000CC">$GLOBALS</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'THRIFT_ROOT'</span><span style="color: #006600">&#93;.</span><span style="color: #CC0000">'/transport/TSocket.php'</span><span style="color: #006600">;<br />require_once </span><span style="color: #0000CC">$GLOBALS</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'THRIFT_ROOT'</span><span style="color: #006600">&#93;.</span><span style="color: #CC0000">'/protocol/TBinaryProtocol.php'</span><span style="color: #006600">;<br />require_once </span><span style="color: #0000CC">$GLOBALS</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'THRIFT_ROOT'</span><span style="color: #006600">&#93;.</span><span style="color: #CC0000">'/transport/TFramedTransport.php'</span><span style="color: #006600">;<br />require_once </span><span style="color: #0000CC">$GLOBALS</span><span style="color: #006600">&#91;</span><span style="color: #CC0000">'THRIFT_ROOT'</span><span style="color: #006600">&#93;.</span><span style="color: #CC0000">'/transport/TBufferedTransport.php'</span><span style="color: #006600">;<br /><br />class </span><span style="color: #0000CC">CassandraDB<br /></span><span style="color: #006600">{<br />&nbsp; </span><span style="color: #FF9900">// Internal variables<br />&nbsp; </span><span style="color: #006600">protected </span><span style="color: #0000CC">$socket</span><span style="color: #006600">;<br />&nbsp; protected </span><span style="color: #0000CC">$client</span><span style="color: #006600">;<br />&nbsp; protected </span><span style="color: #0000CC">$keyspace</span><span style="color: #006600">;<br />&nbsp; protected </span><span style="color: #0000CC">$transport</span><span style="color: #006600">;<br />&nbsp; protected </span><span style="color: #0000CC">$protocol</span><span style="color: #006600">;<br />&nbsp; protected </span><span style="color: #0000CC">$err_str </span><span style="color: #006600">= </span><span style="color: #CC0000">""</span><span style="color: #006600">;<br />&nbsp; protected </span><span style="color: #0000CC">$display_errors </span><span style="color: #006600">= </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />&nbsp; protected </span><span style="color: #0000CC">$consistency </span><span style="color: #006600">= </span><span style="color: #0000CC">1</span><span style="color: #006600">;<br />&nbsp; protected </span><span style="color: #0000CC">$parse_columns </span><span style="color: #006600">= </span><span style="color: #0000CC">1</span><span style="color: #006600">;<br /><br />&nbsp; </span><span style="color: #FF9900">// Functions<br /><br />&nbsp; // Constructor - Connect to Cassandra via Thrift<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">CassandraDB&nbsp; </span><span style="color: #006600">(</span><span style="color: #0000CC">$keyspace</span><span style="color: #006600">, </span><span style="color: #0000CC">$host </span><span style="color: #006600">= </span><span style="color: #CC0000">"127.0.0.1"</span><span style="color: #006600">, </span><span style="color: #0000CC">$port </span><span style="color: #006600">= </span><span style="color: #0000CC">9160</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Initialize<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">err_str </span><span style="color: #006600">= </span><span style="color: #CC0000">''</span><span style="color: #006600">;<br /><br />&nbsp; &nbsp; try<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Store passed 'keyspace' in object<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">keyspace </span><span style="color: #006600">= </span><span style="color: #0000CC">$keyspace</span><span style="color: #006600">;<br /><br />&nbsp; &nbsp;   </span><span style="color: #FF9900">// Make a connection to the Thrift interface to Cassandra<br />&nbsp; &nbsp;   </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">socket </span><span style="color: #006600">= new </span><span style="color: #0000CC">TSocket</span><span style="color: #006600">(</span><span style="color: #0000CC">$host</span><span style="color: #006600">, </span><span style="color: #0000CC">$port</span><span style="color: #006600">);<br />&nbsp; &nbsp;   </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">transport </span><span style="color: #006600">= new </span><span style="color: #0000CC">TFramedTransport</span><span style="color: #006600">(</span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">socket</span><span style="color: #006600">, </span><span style="color: #0000CC">1024</span><span style="color: #006600">, </span><span style="color: #0000CC">1024</span><span style="color: #006600">);<br />&nbsp; &nbsp;   </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">protocol </span><span style="color: #006600">= new </span><span style="color: #0000CC">TBinaryProtocolAccelerated</span><span style="color: #006600">(</span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">transport</span><span style="color: #006600">);<br />&nbsp; &nbsp;   </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">client </span><span style="color: #006600">= new </span><span style="color: #0000CC">CassandraClient</span><span style="color: #006600">(</span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">protocol</span><span style="color: #006600">);<br />&nbsp; &nbsp;   </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">transport</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">open</span><span style="color: #006600">();<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; catch (</span><span style="color: #0000CC">TException $tx</span><span style="color: #006600">)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Error occured<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">err_str </span><span style="color: #006600">= </span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">why</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">Debug</span><span style="color: #006600">(</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">why</span><span style="color: #006600">.</span><span style="color: #CC0000">" "</span><span style="color: #006600">.</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">getMessage</span><span style="color: #006600">());<br />&nbsp; &nbsp; }<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Insert Column into ColumnFamily <br />&nbsp; // (Equivalent to RDBMS Insert record to a table)<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">InsertRecord&nbsp; </span><span style="color: #006600">(</span><span style="color: #0000CC">$table </span><span style="color: #FF9900">/* ColumnFamily */</span><span style="color: #006600">, </span><span style="color: #0000CC">$key </span><span style="color: #FF9900">/* ColumnFamily Key */</span><span style="color: #006600">, </span><span style="color: #0000CC">$record </span><span style="color: #FF9900">/* Columns */</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Initialize<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">err_str </span><span style="color: #006600">= </span><span style="color: #CC0000">''</span><span style="color: #006600">;<br /><br />&nbsp; &nbsp; try<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp;   </span><span style="color: #FF9900">// Timestamp for update<br />&nbsp; &nbsp; &nbsp;   </span><span style="color: #0000CC">$timestamp </span><span style="color: #006600">= </span><span style="color: #0000CC">time</span><span style="color: #006600">();<br /><br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Build batch mutation<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$cfmap </span><span style="color: #006600">= array();<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$cfmap</span><span style="color: #006600">&#91;</span><span style="color: #0000CC">$table</span><span style="color: #006600">&#93; = </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">array_to_supercolumns_or_columns</span><span style="color: #006600">(</span><span style="color: #0000CC">$record</span><span style="color: #006600">, </span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">);<br />&nbsp; <br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Insert<br />&nbsp; &nbsp; &nbsp;   </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">client</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">batch_insert</span><span style="color: #006600">(</span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">keyspace</span><span style="color: #006600">, </span><span style="color: #0000CC">$key</span><span style="color: #006600">, </span><span style="color: #0000CC">$cfmap</span><span style="color: #006600">, </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">consistency</span><span style="color: #006600">);<br /><br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// If we're up to here, all is well<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$result </span><span style="color: #006600">= </span><span style="color: #0000CC">1</span><span style="color: #006600">;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; catch (</span><span style="color: #0000CC">TException $tx</span><span style="color: #006600">)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Error occured<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$result </span><span style="color: #006600">= </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">err_str </span><span style="color: #006600">= </span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">why</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">Debug</span><span style="color: #006600">(</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">why</span><span style="color: #006600">.</span><span style="color: #CC0000">" "</span><span style="color: #006600">.</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">getMessage</span><span style="color: #006600">());<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Return result<br />&nbsp; &nbsp; </span><span style="color: #006600">return </span><span style="color: #0000CC">$result</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Insert SuperColumn into SuperColumnFamily<br />&nbsp; // (Equivalent to RDMBS Insert record to a "nested table")<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">InsertRecordArray&nbsp; </span><span style="color: #006600">(</span><span style="color: #0000CC">$table </span><span style="color: #FF9900">/* SuperColumnFamily */</span><span style="color: #006600">, </span><span style="color: #0000CC">$key_parent </span><span style="color: #FF9900">/* Super CF */</span><span style="color: #006600">, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$record </span><span style="color: #FF9900">/* Columns */</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Initialize<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$err_str </span><span style="color: #006600">= </span><span style="color: #CC0000">''</span><span style="color: #006600">;<br /><br />&nbsp; &nbsp; try<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Timestamp for update<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$timestamp </span><span style="color: #006600">= </span><span style="color: #0000CC">time</span><span style="color: #006600">();<br /><br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Build batch mutation<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$cfmap </span><span style="color: #006600">= array();<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$cfmap</span><span style="color: #006600">&#91;</span><span style="color: #0000CC">$table</span><span style="color: #006600">&#93; = </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">array_to_supercolumns_or_columns</span><span style="color: #006600">(</span><span style="color: #0000CC">$record</span><span style="color: #006600">, </span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">);<br />   <br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Insert<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">client</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">batch_insert</span><span style="color: #006600">(</span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">keyspace</span><span style="color: #006600">, </span><span style="color: #0000CC">$key_parent</span><span style="color: #006600">, </span><span style="color: #0000CC">$cfmap</span><span style="color: #006600">, </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">consistency</span><span style="color: #006600">);<br /><br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// If we're up to here, all is well<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$result </span><span style="color: #006600">= </span><span style="color: #0000CC">1</span><span style="color: #006600">;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; catch (</span><span style="color: #0000CC">TException $tx</span><span style="color: #006600">)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Error occured<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$result </span><span style="color: #006600">= </span><span style="color: #0000CC">0</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">err_str </span><span style="color: #006600">= </span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">why</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">Debug</span><span style="color: #006600">(</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">why</span><span style="color: #006600">.</span><span style="color: #CC0000">" "</span><span style="color: #006600">.</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">getMessage</span><span style="color: #006600">());<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Return result<br />&nbsp; &nbsp; </span><span style="color: #006600">return </span><span style="color: #0000CC">$result</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Get record by key<br />&nbsp;    </span><span style="color: #006600">function </span><span style="color: #0000CC">GetRecordByKey&nbsp; </span><span style="color: #006600">(</span><span style="color: #0000CC">$table </span><span style="color: #FF9900">/* ColumnFamily or SuperColumnFamily */</span><span style="color: #006600">, </span><span style="color: #0000CC">$key</span><span style="color: #006600">, </span><span style="color: #0000CC">$start_from</span><span style="color: #006600">=</span><span style="color: #CC0000">""</span><span style="color: #006600">, </span><span style="color: #0000CC">$end_at</span><span style="color: #006600">=</span><span style="color: #CC0000">""</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Initialize<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$err_str </span><span style="color: #006600">= </span><span style="color: #CC0000">''</span><span style="color: #006600">;<br /><br />&nbsp; &nbsp; try<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; return </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">get</span><span style="color: #006600">(</span><span style="color: #0000CC">$table</span><span style="color: #006600">, </span><span style="color: #0000CC">$key</span><span style="color: #006600">, </span><span style="color: #0000CC">NULL</span><span style="color: #006600">, </span><span style="color: #0000CC">$start_from</span><span style="color: #006600">, </span><span style="color: #0000CC">$end_at</span><span style="color: #006600">);<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; catch (</span><span style="color: #0000CC">TException $tx</span><span style="color: #006600">)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// Error occured<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">err_str </span><span style="color: #006600">= </span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">why</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">Debug</span><span style="color: #006600">(</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">why</span><span style="color: #006600">.</span><span style="color: #CC0000">" "</span><span style="color: #006600">.</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">getMessage</span><span style="color: #006600">());<br />&nbsp; &nbsp; &nbsp; return array();<br />&nbsp; &nbsp; }<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Print debug message<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">Debug&nbsp; &nbsp; &nbsp; </span><span style="color: #006600">(</span><span style="color: #0000CC">$str</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #FF9900">// If verbose is off, we're done<br />&nbsp; &nbsp; </span><span style="color: #006600">if (!</span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">display_errors</span><span style="color: #006600">) return;<br /><br />&nbsp; &nbsp; </span><span style="color: #FF9900">// Print<br />&nbsp; &nbsp; </span><span style="color: #006600">echo </span><span style="color: #0000CC">date</span><span style="color: #006600">(</span><span style="color: #CC0000">"Y-m-d h:i:s"</span><span style="color: #006600">).</span><span style="color: #CC0000">" CassandraDB ERROR: $str\r\n"</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Turn verbose debug on/off (Default is off)<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">SetDisplayErrors</span><span style="color: #006600">(</span><span style="color: #0000CC">$flag</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">display_errors </span><span style="color: #006600">= </span><span style="color: #0000CC">$flag</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Set Consistency level (Default is 1)<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">SetConsistency&nbsp; </span><span style="color: #006600">(</span><span style="color: #0000CC">$consistency</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">consistency </span><span style="color: #006600">= </span><span style="color: #0000CC">$consistency</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Build cf array<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">array_to_supercolumns_or_columns</span><span style="color: #006600">(</span><span style="color: #0000CC">$array</span><span style="color: #006600">, </span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">=</span><span style="color: #0000CC">null</span><span style="color: #006600">) <br />&nbsp; {<br />&nbsp; &nbsp; if(empty(</span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">)) </span><span style="color: #0000CC">$timestamp </span><span style="color: #006600">= </span><span style="color: #0000CC">time</span><span style="color: #006600">();<br /> <br />&nbsp; &nbsp; </span><span style="color: #0000CC">$ret </span><span style="color: #006600">= </span><span style="color: #0000CC">null</span><span style="color: #006600">;<br />&nbsp; &nbsp; foreach(</span><span style="color: #0000CC">$array </span><span style="color: #006600">as </span><span style="color: #0000CC">$name </span><span style="color: #006600">=&gt; </span><span style="color: #0000CC">$value</span><span style="color: #006600">) {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc </span><span style="color: #006600">= new </span><span style="color: #0000CC">cassandra_ColumnOrSuperColumn</span><span style="color: #006600">();<br />&nbsp; &nbsp; &nbsp; if(</span><span style="color: #0000CC">is_array</span><span style="color: #006600">(</span><span style="color: #0000CC">$value</span><span style="color: #006600">)) {<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">super_column </span><span style="color: #006600">= new </span><span style="color: #0000CC">cassandra_SuperColumn</span><span style="color: #006600">();<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">super_column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">name </span><span style="color: #006600">= </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">unparse_column_name</span><span style="color: #006600">(</span><span style="color: #0000CC">$name</span><span style="color: #006600">, </span><span style="color: #0000CC">true</span><span style="color: #006600">);<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">super_column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">columns </span><span style="color: #006600">= </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">array_to_columns</span><span style="color: #006600">(</span><span style="color: #0000CC">$value</span><span style="color: #006600">, </span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">);<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">super_column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">timestamp </span><span style="color: #006600">= </span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; } <br />&nbsp; &nbsp; &nbsp; else <br />&nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc </span><span style="color: #006600">= new </span><span style="color: #0000CC">cassandra_ColumnOrSuperColumn</span><span style="color: #006600">();<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">column </span><span style="color: #006600">= new </span><span style="color: #0000CC">cassandra_Column</span><span style="color: #006600">();<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">name </span><span style="color: #006600">= </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">unparse_column_name</span><span style="color: #006600">(</span><span style="color: #0000CC">$name</span><span style="color: #006600">, </span><span style="color: #0000CC">true</span><span style="color: #006600">);<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">value </span><span style="color: #006600">= </span><span style="color: #0000CC">$value</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$c_or_sc</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">timestamp </span><span style="color: #006600">= </span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$ret</span><span style="color: #006600">&#91;&#93; = </span><span style="color: #0000CC">$c_or_sc</span><span style="color: #006600">;<br />&nbsp; &nbsp; }<br /> <br />&nbsp; &nbsp; return </span><span style="color: #0000CC">$ret</span><span style="color: #006600">;<br />&nbsp; }<br /><br /><br />&nbsp; </span><span style="color: #FF9900">// Parse column names for Cassandra<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">parse_column_name</span><span style="color: #006600">(</span><span style="color: #0000CC">$column_name</span><span style="color: #006600">, </span><span style="color: #0000CC">$is_column</span><span style="color: #006600">=</span><span style="color: #0000CC">true</span><span style="color: #006600">) <br />&nbsp; {<br />&nbsp; &nbsp; if(!</span><span style="color: #0000CC">$column_name</span><span style="color: #006600">) return </span><span style="color: #0000CC">NULL</span><span style="color: #006600">;<br /> <br />&nbsp; &nbsp; return </span><span style="color: #0000CC">$column_name</span><span style="color: #006600">;<br />&nbsp; }<br />&nbsp; <br />&nbsp; </span><span style="color: #FF9900">// Unparse column names for Cassandra<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">unparse_column_name</span><span style="color: #006600">(</span><span style="color: #0000CC">$column_name</span><span style="color: #006600">, </span><span style="color: #0000CC">$is_column</span><span style="color: #006600">=</span><span style="color: #0000CC">true</span><span style="color: #006600">) <br />&nbsp; {<br />&nbsp; &nbsp; if(!</span><span style="color: #0000CC">$column_name</span><span style="color: #006600">) return </span><span style="color: #0000CC">NULL</span><span style="color: #006600">;<br /> <br />&nbsp; &nbsp; return </span><span style="color: #0000CC">$column_name</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Convert supercolumns or columns into an array<br />&nbsp;    </span><span style="color: #006600">function </span><span style="color: #0000CC">supercolumns_or_columns_to_array</span><span style="color: #006600">(</span><span style="color: #0000CC">$array</span><span style="color: #006600">)<br />&nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$ret </span><span style="color: #006600">= </span><span style="color: #0000CC">null</span><span style="color: #006600">;<br />&nbsp; &nbsp; for (</span><span style="color: #0000CC">$i</span><span style="color: #006600">=</span><span style="color: #0000CC">0</span><span style="color: #006600">; </span><span style="color: #0000CC">$i</span><span style="color: #006600">&lt;</span><span style="color: #0000CC">count</span><span style="color: #006600">(</span><span style="color: #0000CC">$array</span><span style="color: #006600">); </span><span style="color: #0000CC">$i</span><span style="color: #006600">++)<br />&nbsp; &nbsp; foreach (</span><span style="color: #0000CC">$array</span><span style="color: #006600">&#91;</span><span style="color: #0000CC">$i</span><span style="color: #006600">&#93; as </span><span style="color: #0000CC">$object</span><span style="color: #006600">)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; if (</span><span style="color: #0000CC">$object</span><span style="color: #006600">)<br />&nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// If supercolumn<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #006600">if (isset(</span><span style="color: #0000CC">$object</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">columns</span><span style="color: #006600">))<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$record </span><span style="color: #006600">= array();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (</span><span style="color: #0000CC">$j</span><span style="color: #006600">=</span><span style="color: #0000CC">0</span><span style="color: #006600">; </span><span style="color: #0000CC">$j</span><span style="color: #006600">&lt;</span><span style="color: #0000CC">count</span><span style="color: #006600">(</span><span style="color: #0000CC">$object</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">columns</span><span style="color: #006600">); </span><span style="color: #0000CC">$j</span><span style="color: #006600">++)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$column </span><span style="color: #006600">= </span><span style="color: #0000CC">$object</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">columns</span><span style="color: #006600">&#91;</span><span style="color: #0000CC">$j</span><span style="color: #006600">&#93;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$record</span><span style="color: #006600">&#91;</span><span style="color: #0000CC">$column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">name</span><span style="color: #006600">&#93; = </span><span style="color: #0000CC">$column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">value</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$ret</span><span style="color: #006600">&#91;</span><span style="color: #0000CC">$object</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">name</span><span style="color: #006600">&#93; = </span><span style="color: #0000CC">$record</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #FF9900">// (Otherwise - not supercolumn)<br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #006600">else<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$ret</span><span style="color: #006600">&#91;</span><span style="color: #0000CC">$object</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">name</span><span style="color: #006600">&#93; = </span><span style="color: #0000CC">$object</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">value</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; return </span><span style="color: #0000CC">$ret</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Get record from Cassandra<br />&nbsp;    </span><span style="color: #006600">function </span><span style="color: #0000CC">get</span><span style="color: #006600">(</span><span style="color: #0000CC">$table</span><span style="color: #006600">, </span><span style="color: #0000CC">$key</span><span style="color: #006600">, </span><span style="color: #0000CC">$super_column</span><span style="color: #006600">=</span><span style="color: #0000CC">NULL</span><span style="color: #006600">, </span><span style="color: #0000CC">$slice_start</span><span style="color: #006600">=</span><span style="color: #CC0000">""</span><span style="color: #006600">, </span><span style="color: #0000CC">$slice_finish</span><span style="color: #006600">=</span><span style="color: #CC0000">""</span><span style="color: #006600">) <br />&nbsp; {<br />&nbsp; &nbsp; try<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$column_parent </span><span style="color: #006600">= new </span><span style="color: #0000CC">cassandra_ColumnParent</span><span style="color: #006600">();<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$column_parent</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">column_family </span><span style="color: #006600">= </span><span style="color: #0000CC">$table</span><span style="color: #006600">;<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$column_parent</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">super_column </span><span style="color: #006600">= </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">unparse_column_name</span><span style="color: #006600">(</span><span style="color: #0000CC">$super_column</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">);<br /> <br />&nbsp; &nbsp; </span><span style="color: #0000CC">$slice_range </span><span style="color: #006600">= new </span><span style="color: #0000CC">cassandra_SliceRange</span><span style="color: #006600">();<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$slice_range</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">start </span><span style="color: #006600">= </span><span style="color: #0000CC">$slice_start</span><span style="color: #006600">;<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$slice_range</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">finish </span><span style="color: #006600">= </span><span style="color: #0000CC">$slice_finish</span><span style="color: #006600">;<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$predicate </span><span style="color: #006600">= new </span><span style="color: #0000CC">cassandra_SlicePredicate</span><span style="color: #006600">();<br />&nbsp; &nbsp; </span><span style="color: #0000CC">$predicate</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">slice_range </span><span style="color: #006600">= </span><span style="color: #0000CC">$slice_range</span><span style="color: #006600">;<br /> <br />&nbsp; &nbsp; </span><span style="color: #0000CC">$resp </span><span style="color: #006600">= </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">client</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">get_slice</span><span style="color: #006600">(</span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">keyspace</span><span style="color: #006600">, </span><span style="color: #0000CC">$key</span><span style="color: #006600">, </span><span style="color: #0000CC">$column_parent</span><span style="color: #006600">, </span><span style="color: #0000CC">$predicate</span><span style="color: #006600">, </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">consistency</span><span style="color: #006600">);<br /><br />&nbsp; &nbsp; return </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">supercolumns_or_columns_to_array</span><span style="color: #006600">(</span><span style="color: #0000CC">$resp</span><span style="color: #006600">);<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; catch (</span><span style="color: #0000CC">TException $tx</span><span style="color: #006600">)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp;   </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">Debug</span><span style="color: #006600">(</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">why</span><span style="color: #006600">.</span><span style="color: #CC0000">" "</span><span style="color: #006600">.</span><span style="color: #0000CC">$tx</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">getMessage</span><span style="color: #006600">());<br />&nbsp; &nbsp; return array();<br />&nbsp; &nbsp; }<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Convert array to columns<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">array_to_columns</span><span style="color: #006600">(</span><span style="color: #0000CC">$array</span><span style="color: #006600">, </span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">=</span><span style="color: #0000CC">null</span><span style="color: #006600">) {<br />&nbsp; &nbsp; if(empty(</span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">)) </span><span style="color: #0000CC">$timestamp </span><span style="color: #006600">= </span><span style="color: #0000CC">time</span><span style="color: #006600">();<br /> <br />&nbsp; &nbsp; </span><span style="color: #0000CC">$ret </span><span style="color: #006600">= </span><span style="color: #0000CC">null</span><span style="color: #006600">;<br />&nbsp; &nbsp; foreach(</span><span style="color: #0000CC">$array </span><span style="color: #006600">as </span><span style="color: #0000CC">$name </span><span style="color: #006600">=&gt; </span><span style="color: #0000CC">$value</span><span style="color: #006600">) {<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$column </span><span style="color: #006600">= new </span><span style="color: #0000CC">cassandra_Column</span><span style="color: #006600">();<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">name </span><span style="color: #006600">= </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">unparse_column_name</span><span style="color: #006600">(</span><span style="color: #0000CC">$name</span><span style="color: #006600">, </span><span style="color: #0000CC">false</span><span style="color: #006600">);<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">value </span><span style="color: #006600">= </span><span style="color: #0000CC">$value</span><span style="color: #006600">;<br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$column</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">timestamp </span><span style="color: #006600">= </span><span style="color: #0000CC">$timestamp</span><span style="color: #006600">;<br /> <br />&nbsp; &nbsp; &nbsp; </span><span style="color: #0000CC">$ret</span><span style="color: #006600">&#91;&#93; = </span><span style="color: #0000CC">$column</span><span style="color: #006600">;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; return </span><span style="color: #0000CC">$ret</span><span style="color: #006600">;<br />&nbsp; }<br /><br />&nbsp; </span><span style="color: #FF9900">// Get error string<br />&nbsp; </span><span style="color: #006600">function </span><span style="color: #0000CC">ErrorStr</span><span style="color: #006600">()<br />&nbsp; {<br />&nbsp; &nbsp; return </span><span style="color: #0000CC">$this</span><span style="color: #006600">-&gt;</span><span style="color: #0000CC">err_str</span><span style="color: #006600">;<br />&nbsp; }<br />}  </span><span style="color: #0000CC"></font></span></div>
<br />
<b>CassandraDB  Todo</b>:<br />
<br />
* Ability to add multiple nodes and try to connect to next-in-line automatically if connection fails<br />
* Implement the remaining get_ functions<br />
* Implement delete<br />
* Implement update vs insert-fail-on-duplicate-key?<br />
* Implement Order By desc<br />
<br />
-<br />
<br />
Recommended Further Reading:<br />
<br />
* <a href="http://www.scribd.com/doc/21962142/Just-Add-an-Index" target="_blank">Cassandra in Production at Digg</a><br />
* <a href="http://arin.me/blog/wtf-is-a-supercolumn-cassandra-data-model" target="_blank">WTF is a SuperColumn?</a><br />
* <a href="http://www.mikeperham.com/2010/03/17/cassandra-internals-reading/" target="_blank">Cassandra Reads - How do they work</a><br />
* <a href="http://www.mikeperham.com/2010/03/13/cassandra-internals-writing/" target="_blank">Cassandra Writes - How do they work</a><br />
* <a href="http://www.facebook.com/video/video.php?v=540974400803" target="_blank">Cassandra Write Properties</a> (Video)<br />
* <a href="http://www.facebook.com/note.php?note_id=24413138919" target="_blank">Engineering notes by Facebook</a><br />
* <a href="http://ria101.wordpress.com/2010/02/22/cassandra-randompartitioner-vs-orderpreservingpartitioner/" target="_blank">RandomPartitioner vs OrderPreservingPartitioner</a>]]></description>

		<wfw:commentRss>http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-1959.html</wfw:commentRss>
		<feedburner:origLink>http://www.softwareprojects.com/resources/programming/t-cassandra-php-wrapper-1959.html</feedburner:origLink>

</item>

</channel></rss>