The Alchemist ….. a book that connects your senses

My CTO at eSpace, Eng.Hamdy Khalil has suggested that I should read “The Alchemist” by Paulo Coelho, so he just lent it to me and to my surprise it took me no more than 2 hours to finish it. I was just very happy and excited while reading it. Though I didn’t like its ending at all, maybe I kept on raising the expectations while I am reading so I just expected a better wrap up, but generally this book really knew how to talk to my brain and how to touch my heart. you know that feeling when you have nothing to say except “Sigh!”….This is exactly how I felt…

As a result of such a strong impact, I just thought that I really need to keep an eye on the best I read inside this book and this is exactly the purpose of this blog post…Below I will be recalling the best I read inside “The Alchemist” [order has no relevance at all]

  1. To realize one’s destiny is a person’s only obligation…
  2. When each day is the same as the next, it’s because people fail to recognize the good things that happen in their lives every day that the sun rises…
  3. If I can learn to understand the language of silence, I can learn to understand the world…
  4. Never stop dreaming
  5. There is a language in the world that everyone understands, it is the language of enthusiasm, of things accomplished with love and purpose, and as part of a search for something believed and desired.
  6. When you want something, all the universe conspires to help you achieve it…
  7. Everyone has his or her own way of learning things. His way isn’t the same as mine, nor mine as his. But we are both in search of our personal legend and I respect him for that…
  8. If I have to fight and die in the end, then it will be just as good a day to die as any other!
  9. I don’t live in either my past or my future. I’m interested only in the present. If you can concentrate always on the present, you’ll be a happy man. You’ll see there is life in the desert, that there are stars in the heavens, life will be a party for you, a grand festival, because life is the moment we are living right now…
  10. The closer he got to the realization of his dreams, the more difficult things become.
  11. Someone in the world awaits you, whether it’s in  the middle of the desert or in some great city, and when two such people encounter each other, and their eyes meet, the past and the future become unimportant. There is only that moment, and the incredible certainty that everything has been written by one hand only, the hand of God which is the hand that evokes love and creates a twin soul for every person in the world. Without such love, one’s dreams would have no meaning….
  12. The desert fills men’s hearts with visions
  13. The future was already written by God, and what he had written was always for the good of man.
  14. The secret is here in the present. If you pay attention to the present, you can improve upon it. And if you improve on the present, what comes later also be better. Forget about the future and live each day according to the teachings, confidence that God loves you.
  15. Each day, in itself, brings with it an eternity.
  16. To die tomorrow was no worse than dying on any other day. Every day was there to be lived or to mark one’s departure from this world.
  17. “You’re crying?” he asked her ….”I’m a woman of the desert, ” she said, averting her face, “But above all, I’m a woman.” She added.
  18. There is only one way to learn, it’s through action.
  19. “My heart is afraid that it will have to suffer” … “Tell your heart that the fear of suffering is worse than the suffering itself, and that no heart has ever suffered when it goes in search of its dreams, because every second of the search is a second’s encounter with God and with eternity
  20. one dies of thirst just when the palm trees have appeared on the horizon.
  21. Every search begins with beginner’s luck… and every search ends with the victor’s being severely tested.
  22. The darkest hour of the night came just before the dawn.
  23. Don’t give in to your fears, because if you do, you won’t be able to to talk to your heart
  24. Then you’ll die in the midst of trying to realize your personal legend. That’s a lot better than dying like millions of other people, who never knew what their personal legends were
  25. It’s not love to be static like the desert, nor it is love to roam the world like the wind. Love is the force that transforms and improves the Soul of the World.

and many others, but if I kept on recalling each and every sentence that really matters, then I will recall the whole book…try to think while you read each of them, not just think, try to link aspects using your brain, your belief, your sense and your heart…The Alchemist, a book that leaves a foot print.

Playing with MongoMapper and Rails 3.0.7

The term NoSQL Databases or Document stores has been popping quite a lot recently…A lot of blog posts, surveys , seminars , technical sessions and others were held trying to list the pros and cons of NoSQL Databases versus Relational Databases wishing to try to know which one is better than the other…Although all these discussions  revealed the features and differences of each one of them but still I never find any final conclusion about which is really better. It really depends much on the context and the problem that you are trying to solve and about the nature of the application you are trying to build. Otherwise why on earth do Relational Databases still exist?!

Actually the reason of my post today, is not to continue on comparing the two big stars. My target today is to give a very slightly brief introduction on what the hell are Document stores [for those who don't know] and to share the thrilling experience I had when I decided to play with MongoMapper [A MongoDB library for Ruby] inside a test Rails 3.0.7 Application and the very simple steps to get this running

Okay, so What does Document store mean? A Document Store or a NoSQL Database is a Computer Program (an Object store) where you can persist your objects as Binary JSON . It doesn’t contain tables or rows like the case in Relational Databases. Instead the Object is just a Document. In SOME scenarios [depending on the context] where you need to scale extensively when this scaling might be very expensive in terms of Relational Databases and degrades performance as well because of the extensive joins and queries that will be done…Storing objects as Documents can be a BLESS! that’s why they are called NoSQL databases because you don’t do SQL queries and joins.

Examples of NoSQL databases are:

After getting a quick overview of Document Oriented Databases, let’s save some objects from a Rails application to MongoDB using MongoMapper library.. [Ubunto 10.10 machine]

  • Installing MongoDB and dependencies

open your terminal and let’s search for the required packages “sudo apt-cache search mongo”

Let’s install the server, client and development libraries “sudo apt-get install mongodb mongodb-clients mongodb-dev mongodb-server”

  • Creating a Rails application skipping ActiveRecord
       rails new mongoTest --skip-active-record
  • Adding the mongo.yml file

# config/mongo.yml
base: &base
adapter: mongodb
database: mongoTest_development
host: localhost

development:
<<: *base

test:
<<: *base
database: mongoTest_test

production:
<<: *base

  • Adding Mongo Initializer to connect to MongoDB

MongoMapper.connection = Mongo::Connection.new(‘localhost’, 27017)
MongoMapper.database = “#myapp-#{Rails.env}”

if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect if forked
end
end

  • Configure your GemFile

Requiring rubygems and mongo before running the gem command will ensure that bson_ext is loaded.

require ‘rubygems’
require ‘mongo’
source ‘http://gemcutter.org&#8217;
source ‘http://rubygems.org&#8217;

gem ‘rails’, ‘3.0.7’
gem ‘mongo_mapper’, ‘0.9.1’

  • Adding the mongo.rake file

Actually Skipping active_record will result in breaking all your tests because rake db:test:prepare won’t be declared, so to fix this let’s add a mongo.rake file under lib/tasks and declare our own test:prepare task which will be empty so far

namespace :db do
namespace :test do
task :prepare do
# Stub out for MongoDB
end
end
end

  • Creating your models

To create a new Model we won’t inherit from ActiveRecord::Base instead we need to include a Module “MongoMapper::Document” For example to create a very simple User Model we can do the following:

# models/user.rb 
class User   
 include MongoMapper::Document       
 key :email,  String   
 key :username,  String   
 key :comment_count, Integer   
 validates_length_of :email, :within => 6..50, :allow_blank => true   
 many :comments 
end 

and to create a Comment model where user has many comments we can do the following: 

#models/comment.rb 
class Comment   
 include MongoMapper::Document  
  key :message, String    
  key :message_html, String    
  key :user_id,    ObjectId  
  key :username,   String   
 
 validates_presence_of :message   
 
 belongs_to :user      
 
 before_create :set_username     
 after_create :increment_comment_count     
 
 private     
 
 def increment_comment_count 
    User.increment(user_id, :comment_count => 1)  
  end    
 

 def set_username     
 self.username = self.user.username    
  end    
end 

No need for any migrations, as you can see the Model file will encapsulate everything you need to 
 describe your Object. Furthermore You can even use  include MongoMapper::EmbeddedDocument instead of Document for example we can embed comments along with the user which will save the comments right inside the user document itself [No JOINS!]. 
  • Creating some Models

open your console and Let’s create a user

rails c

u = User.new

u.email = “email@email.com”

u.username = “newuser”

u.save!

=> [#<User _id: BSON::ObjectId('4df8a861f8f4ea166a000001'), comment_count: 0, created_at: Wed, 15 Jun 2011 12:42:17 UTC +00:00, email: "email@email.com", updated_at: Wed, 15 Jun 2011 12:42:17 UTC +00:00, username: "newuser">]

ruby-1.9.2-p180 :002 > c = Comment.new
=> #<Comment _id: BSON::ObjectId(‘4df8a991f8f4ea1746000002′), created_at: nil, message: nil, message_html: nil, updated_at: nil, user_id: nil, username: nil>
ruby-1.9.2-p180 :003 > c.message = “my first comment”
=> “my first comment”
ruby-1.9.2-p180 :004 > u.comments << c

=> [#<Comment _id: BSON::ObjectId('4df8a991f8f4ea1746000002'), created_at: Wed, 15 Jun 2011 12:46:28 UTC +00:00, message: "my first comment", message_html: nil, updated_at: Wed, 15 Jun 2011 12:46:28 UTC +00:00, user_id: BSON::ObjectId('4df8a861f8f4ea166a000001'), username: "newuser">]

Notice  the fact that we have no joins required us to include the username in the comment as if we are caching the username attribute

  • Playing

MongoMapper offers us very similar interface as AR. Actually you can just call

User.first

User.last

User.all

….etc the same as you do in AR , Also it implements most of the validation which is supplied by AR.

Last but not least…It was really very easy to get it working, no tricks, documentation available, very quick performance and very similar interface to AR so I didn’t have any strange feelings!

References:

http://mongomapper.com/

http://railstips.org/blog/archives/2009/06/27/mongomapper-the-rad-mongo-wrapper/

http://www.mongodb.org/display/DOCS/Rails+3+-+Getting+Started

Setting up the rickrockstar demo application of the Refinery CMS

I was playing for a while with Refinery CMS, and thought about getting its demo application “rickrockstar” running on my Ubunto 10 machine.

I have experienced two issues by following their Getting Started Guide, and i am sharing my own experience in this post

I am using Rails 3.0.3, Ruby 1.9.2 using rvm..

  • First

Guide recommendation: gem install refinerycms –version 0.9.8.9

My Recommendation: gem install refinerycms (latest) 0.9.9.9 on the time of writing this

Reason: I encountered problems like empty migration files that resulted in things like “Unknown relation refinery_settings”.. 0 Tables were created.

  • Second

Don’t forget to: sudo apt-get install libsqlite3-dev before running bundle install in order to be able to build the native extensions of sqlite3

  • Third

It runs perfectly using PostgresSQL, tested using pg gem as well.

gem install refinerycms --version 0.9.8.9

Using rails_indexes to identify foreign keys that need indexes and a ruby 1.9.2 fix

Thanks to My CTO Hamdy Khalil who suggested using rails_indexes to identify the needed database indexes.

If you are using ruby <= 1.8.7 everything will work perfectly with u…
but if you are using ruby 1.9.1 or ruby 1.9.2, u won't be able to run the described rakes in the plug in Read Me, and you get the following error:

rake aborted!
undefined method `to_a’ for “id”:String
/home/rofaida/Aptana Studio 3 Workspace/Dokkani/vendor/plugins/rails_indexes/lib/indexer.rb:216:in `key_exists?’

If we looked at the file indexer.rb in the line 216 we can find the following:
result = (key_columns.to_a – ActiveRecord::Base.connection.indexes(table).map { |i| i.columns }.flatten)
method to_a no longer exist in new ruby versions so let’s replace this with the following and try again:
result = (Array(key_columns) – ActiveRecord::Base.connection.indexes(table).map { |i| i.columns }.flatten)

Now the rake db:index_migration runs successfully and prints a migration file with the required indexes for you to check as follows:

class AddFindsMissingIndexes < ActiveRecord::Migration
def self.up

# These indexes were found by searching for AR::Base finds on your application
# It is strongly recommanded that you will consult a professional DBA about your infrastucture and implemntation before
# changing your database in that matter.
# There is a possibility that some of the indexes offered below is not required and can be removed and not added, if you require
# further assistance with your rails application, database infrastructure or any other problem, visit:
#
# http://www.railsmentors.org
# http://www.railstutor.org
# http://guides.rubyonrails.org

add_index :deals, :id
add_index :deals, :feature
add_index :users, :gender
add_index :users, :email
add_index :images, :id
end

def self.down
remove_index :deals, :id
remove_index :deals, :featured
remove_index :users, :gender
remove_index :users, :email
remove_index :images, :id
end
end

A tale of an “expire_fragment”

I used to think that caching in Rails is a very simple straight forward task. This is because I have the nice well documented Rails Guides and it is an everyday task that of course anyone can do….. poor me didn’t know that expire_fragment method is preparing a party for me!

Everything was working perfectly until I decided to implement my sweeper as an Active record Observer instead of implementing it through controllers. It never expires now, as if it doesn’t even know where to find the sweeper..in other words, as if it is not observing… No errors reported as well, nothing at all, just silent with no expiring taking place.

The first thing that came to my mind, is maybe I didn’t configure the observer correctly, so I made sure the following line of code is written in my application.rb taking into consideration that I am using Rails 3

config.active_record.observers = :deal_sweeper, it is there, let’s try again….and again…….. NOTHING!!…

so what!!, What Am i doing wrong??!! After a lot of More

Asset Packager versus Jammit and some Jammit introductory tips

Today, at eSpace, we needed to try to optimize our latest service Actionha and to implement different techniques in order to decrease the response time as much as we can. As you can see, it contains a bunch of images and a full gallery in the homepage, that’s why we wanted to ensure the best performance for end-users.

Of course caching is one of the most important techniques that we are already implementing for the homepage, but we thought, we need more than just this.

Asset packager and Jammit are two Ruby gems that allow you to compress your javascript files as well your stylesheets into one or more compressed files. this helps much in producion environments because instead of sending down a dozen of JavaScript and CSS files full of formatting and comments, these gems merge and compress JavaScript and CSS down into just one or more files, resulting in increasing speed and saving bandwidth.

Actually Jammit offers much more than asset packager… If you just need the very simply idea of compressing your files, then any of them can do for you. But if you are thinking about much more optimization by using Data URI, MHTML as well as font embedding, then Jammit is definietly your right choice.

Furthermore, Jammit makes it easy to use either the YUI Compressor or the new Google Closure Compiler to compress your assets and in your assets.yml file, you don’t have to specify each file individually.

Moreover, Jammit supports javascript templates as well as pre-gzipping for the most optimized compression level.

Now, I just need to mention some very small tips I faced while configurting Jammit:

- If you are using Rails 2.x, then you can’t use a jammit version upper than 0.4.4, because starting from version 0.5.0, jammit included Rails 3.0 support with no backward comptability with older Rails versions. You will just have a thrown exception saying: undefined flatten method for String
– Write your assets.yml file exactly in the format shown in the Jammit documentation, if you tried to change anything thinking that you just need to follow correct YAML syntax, you won’t have your assets loaded, and you will get erros like “JQuery in undefined…etc”.

until now, Jammit is working like a charm… I will cover Data URI and MHTML in another post…

Searching Solutions

I really enjoyed reading this presentation, where it has good statistics and explanations regarding different searching solutions

Previous Older Entries

Follow

Get every new post delivered to your Inbox.