MongoDB ObjectID for AS3

I’ve been working with MongoDB as of late, and finding the offerings for an ActionScript 3.0 driver to be rather lacking.

I started with ActionMongo, and quickly found out that it has no support for much of anything except the find() command.  I then found my way to MongoAS3, which is a HUGE improvement over the barely-started functionality within ActionMongo.  (Credit where credit’s due, Omar Gonzales built MongoAS3 off of Claudio Alberto Andreoni’s first steps with ActionMongo.)

However, I found a missing piece in MongoAS3 that was holding me back: it doesn’t provide any way to generate a new ObjectID.  This is a problem if, for example, you want to insert a new document.  Since Mongo doesn’t provide a response on a successful insert, this means that a followup query is required to get the ObjectID assigned by the server to the newly-inserted document.  Not ideal.

So, I added functionality to ObjectId.as (which Omar included from Claudio’s original, as part of ActionBSON) to generate a new ObjectID per the ObjectID spec.  The one deviation from the spec comes from the difficulty in getting the process ID from within a flash runtime (requires launching a NativeProcess, which can only be done from an AIR application with the extendedDesktop profile); for this, we’re just using a random.

I also added the ability to extract the timestamp from the ObjectID, which could prove useful.

Hope it’s useful to some of you…I’ve submitted it to Claudio and Omar for inclusion into their distros.  Code after the break, or on Github. (I need a better Gist plugin…)


/*
 * Copyright (c) 2010 Claudio Alberto Andreoni.
 * Modifications by Eric Socolofsky: http://transmote.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

package org.serialization.bson {
	import flash.net.NetworkInfo;
	import flash.net.NetworkInterface;
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	public class ObjectID {
		private static var incrementer : uint = Math.random() * uint.MAX_VALUE;
		private static var machineAndProcessID : ByteArray = null;

		// BSON is little-endian
		private var rep : ByteArray;
		private var time : uint;

		/**
		 * @brief Create a new ObjectID
		 * @param bytearray A little-endian, 12-byte ByteArray containing the ID
		 */
		public function ObjectID( bytearray : ByteArray = null ) :void {
			if (bytearray == null) {
				bytearray = generateObjectID();
			}
			setFromBytes( bytearray );
		}



		/**
		 * @brief Set the value of this ObjectID
		 * @param bytearray A little-endian, 12-byte ByteArray containing the ID
		 */
		public function setFromBytes( bytearray : ByteArray ) :void {
			rep = new ByteArray();
			for ( var i : int = 0; i < 12; ++i ) {
				rep[i] = bytearray.readByte();
			}
			
			time = rep.readUnsignedInt();
			rep.position = 0;
		}



		/**
		 * @brief Get the value of this ObjectID
		 * @return A little-endian, 12-byte ByteArray containing the ID
		 */
		public function getAsBytes() : ByteArray {
			return rep;
		}

		public function toString() : String {
			var str:String = "";
			for ( var i : int = 0; i < 12; ++i ) {
				str += rep[i].toString( 16 );
			}
			return str;
		}
		
		public function toDate() : Date {
			return new Date( time * 1000 );
		}
		
		private function generateObjectID() :ByteArray {
			// from: http://www.mongodb.org/display/DOCS/Object+IDs
			
			// 4-byte timestamp
			time = new Date().getTime();
			var timeBytes:ByteArray = new ByteArray();
			timeBytes.endian = Endian.BIG_ENDIAN;
			timeBytes.writeInt( time );
			timeBytes.length = 4;	// truncate as needed
			
			// 3-byte machine id + 2-byte process id
			if ( machineAndProcessID == null ) {
				generateMachineID();
			}
			
			// 3-byte increment
			var incBytes:ByteArray = new ByteArray();
			incBytes.endian = Endian.BIG_ENDIAN;
			incBytes.writeUnsignedInt( incrementer++ );
			incBytes.length = 3;		// truncate as needed
			
			var idBytes:ByteArray = new ByteArray();
			idBytes.writeBytes( timeBytes );
			idBytes.writeBytes( machineAndProcessID );
			idBytes.writeBytes( incBytes );
			
			idBytes.position = 0;
			return idBytes;
		}
		
		private function generateMachineID() :void {
			machineAndProcessID = new ByteArray();
			machineAndProcessID.endian = Endian.LITTLE_ENDIAN;
			
			var useRandom:Boolean = true;
			for each ( var i : NetworkInterface in NetworkInfo.networkInfo.findInterfaces() ) {
				if ( i.hardwareAddress ) {
					machineAndProcessID.writeUTFBytes( i.hardwareAddress );
					useRandom = false;
					break;
				}
			}
			
			if ( useRandom ) {
				// if no NetworkInterfaces with valid hardware addresses found, use random
				var randomMachineID : uint = Math.floor( Math.random() * uint.MAX_VALUE );
				machineAndProcessID.writeUnsignedInt( randomMachineID );
			}
			
			machineAndProcessID.length = 3;		// truncate as needed
			
			// not possible to get process id from flash without launching a NativeProcess,
			// which requires AIR application with extendedDesktop profile.
			// so, use a random.
			var processID:uint = Math.floor( Math.random() * uint.MAX_VALUE );
			machineAndProcessID.writeUnsignedInt( processID );
			
			machineAndProcessID.length = 5;		// truncate as needed
		}
	}
}

4 Comments

  1. jmc says:

    Thank’s for this usefull code… But this don’t work in FlexBuilder when building an browser embedded (not air) application. Because : flash.net.NetworkInfo et flash.net.NetworkInterface don’t exists.
    Have you a workaround for this ?
    Thank’s…

  2. ericsoco says:

    @jmc — you’re right, and omar noticed this too after i issued a pull request for this fork.
    i’m working with AIR, and neglected to notice that my solution is AIR-specific.

    i believe omar is working on a non-AIR-specific solution to generating the machine+process ID portion of an ObjectID, not sure which route he’s taking. in the meantime, you’ll have to edit out the NetworkInfo and NetworkInterface parts and use the block within if ( useRandom ) { } instead.

  3. jmc says:

    Hello,

    I’ve integrate your code in a new full MongoDB driver for FLEX and AS3. It supports safe mode, full aggregations functions (group, MapReduce, …), socket pooling with databases.
    So you are a contributor to this new driver…. I’ve put a reference to your blog. Let me know, if it is usefull for you.
    You can find this new driver here : http://code.google.com/p/jmcnet-full-mongo-flex-driver/

    Thank’s

  4. ericsoco says:

    wow, that’s great. i’ll take a look when i’m able, though that may be a while as i’ve moved on a bit from this. but great to see someone picking this up! (no progress from omar on mongoAS3 lately…)

Leave a Reply

Additional comments powered by BackType