Wednesday, April 9, 2014

Update your OpenSSL on Mac Mavericks

Before beginning check your current version of OpenSSL by opening a terminal and typing:

openssl version
You should get back the following if you have not updated OpenSSL since installing Mavericks:
OpenSSL 0.9.8y 5 Feb 2013
STEP 1: Download the Source Code from the OpenSSL website (http://www.openssl.org/source/). STEP 2: In your Terminal cd to ~/Downloads:
cd ~/Downloads
STEP 3: Configure the build by typing
./configure darwin64-x86_64-cc
STEP 4: Type
make
STEP 5 (make sure): Test that everything was created correctly:
make test
STEP 6: Install the newly built OpenSSL:
sudo make install
(Type password when prompted) STEP 7: cd to the directory where the old openssl is installed:
cd /usr/bin
STEP 8: Rename the old openssl file to another name so that you can go back to it if you need to:
sudo mv openssl opensslOld
STEP 9: Create a link to the new openssl file
sudo ln -s /usr/local/ssl/bin/openssl openssl
STEP 10: Test that everything worked by typing:
openssl version
If it all worked you should now see OpenSSL 1.0.0c 2 Dec 2010

Monday, April 7, 2014

NodeJs, Mongoose with preexisting data

First we assume there is a collection which is called test in db.test

First we have to create connection

var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
then open the connection and create Schema to match the existing collection's schema, and put the existing collection as the third params in mongoose.model(), like this: mongoose.model('User', userSchema, 'test');
db.once('open', function callback() {
    var userSchema = new mongoose.Schema({
        _id: mongoose.Schema.ObjectId,
        a: Number
    });
    var User = mongoose.model('User', userSchema, 'test');
    User.find(function(err, users){
        if(err) return console.err(err);
        console.log(users);
    })

})
And it is working great.

Sunday, April 6, 2014

Fix for homebrew permission denied issues

1) installing and fixing node.JS
brew install node

~ chrisfu$ brew install node

==> Downloading http://nodejs.org/dist/v0.10.4/node-v0.10.4.tar.gz

Already downloaded: /Library/Caches/Homebrew/node-0.10.4.tar.gz

==> ./configure --prefix=/usr/local/Cellar/node/0.10.4

==> make install

Warning: Could not link node. Unlinking...

Error: The `brew link` step did not complete successfully

The formula built, but is not symlinked into /usr/local

You can try again using `brew link node'

==> Summary

/usr/local/Cellar/node/0.10.4: 951 files, 14M, built in 73 seconds

2) brew link node
~ chrisfu$ brew link node

Linking /usr/local/Cellar/node/0.10.4... Warning: Could not link node. Unlinking...


Error: Could not symlink file: /usr/local/Cellar/node/0.10.4/lib/dtrace/node.d

Target /usr/local/lib/dtrace/node.d already exists. You may need to delete it.

To force the link and delete this file, do:

  brew link --overwrite formula_name

To list all files that would be deleted:

  brew link --overwrite --dry-run formula_name

3) brew link --overwrite node
Linking /usr/local/Cellar/node/0.10.4... Warning: Could not link node. Unlinking...


Error: Permission denied - /usr/local/lib/dtrace/node.d

4) sudo chown -R whoami /usr/local as sudoing homebrew is a bad idea (and doesn't work either) we have to reset the permissions within /usr/local
~ chrisfu$ sudo chown -R `whoami` /usr/local

Password:

~ chrisfu$ brew link --overwrite node

Linking /usr/local/Cellar/node/0.10.4... 5 symlinks created

~ chrisfu$ sudo chown -R `whoami` /usr/local is the key step 5) STEPS brew update brew upgrade brew cleanup brew install node brew link --overwrite node sudo chown -R whoami /usr/local

OC Memory Management

Reference counting

the counting number means the number of users who is using it.









1. each object has its own retain counting.
2. reference counting is 1 when object is created.
3. sending retain message, object's reference counting add 1.
4. sending release message, object's reference counting minus 1.
5. dealloc is revoked when object's reference counting is 0.

example:

Person *person = [[Person alloc] init];



NSLog(@"%ld", [person retainCount])& // 1


[person retain] ;


NSLog(@"%ld", [person retainCount])& // 2


[person retain] ;


NSLog(@"%ld", [person retainCount])& // 3


[person release] ;


NSLog(@"%ld", [person retainCount])& // 2


[person release] ;


NSLog(@"%ld", [person retainCount])& // 1


[person release] ;


NSLog(@"%ld", [person retainCount])& // 0