2012/02/18

使用jquery跨網域存取json資料

jQuery的$.getJSON()能夠跨網域存取資料,是base在browser可以load不同網域的js檔的方式去包裝的。
<script src="http://www.example.com/test.js"></script>

範例

Client端
$.getJSON("http://www.example.com/getMessage.php?jsoncallback=?",
    function(data) {
        alert(data.message);
    });
Request發出時,jsoncallback後面的?會被取代成特定string,server回應的訊息必需包含此string才會被接受。而回傳的data會被轉成object。


Server端
$data = '{"message" : "hello world"}';
echo $_GET['jsoncallback'] . '(' . $data . ')';


參考資料

2011/10/26

AWS Toolkit for Eclipse

AWS Toolkit for Eclipse

安裝時出現下面錯誤訊息:
Cannot complete the install because one or more required items could not be found.
Software currently installed: Amazon SimpleDB Management 1.0.0.v201110211341 (com.amazonaws.eclipse.datatools.enablement.simpledb.feature.feature.group 1.0.0.v201110211341)
Missing requirement: Eclipse Data Tools Platform Amazon SimpleDB UI Plug-in 1.0.0.v201110211341 (com.amazonaws.eclipse.datatools.enablement.simpledb.ui 1.0.0.v201110211341) requires 'bundle org.eclipse.datatools.sqltools.sqlscrapbook 1.0.0' but it could not be found
Cannot satisfy dependency:
From: Amazon SimpleDB Management 1.0.0.v201110211341 (com.amazonaws.eclipse.datatools.enablement.simpledb.feature.feature.group 1.0.0.v201110211341)
To: com.amazonaws.eclipse.datatools.enablement.simpledb.ui [1.0.0.v201110211341]

錯誤訊息是說缺少了 Eclipse Data Tools Platform Amazon SimpleDB UI Plug-in,但我從Eclipse Data Tools Platform (DTP)下載最新的套件還是不足。

最後直接去下載Eclipse IDE for Java EE Developers,這個版本的eclipse內建的plug-in比較完整。
再次安裝aws toolkit就可以成功了!


參考資料:
AWS Developer Forums: Eclipse AWS Toolkit install error. ...
having hard time install AWS toolkit for java and eclipse - HPCS

2011/10/04

Postgresql + pgpool-II

抓一張官方的示意圖:

特色
  • Connection Pooling: 減少建立connection的overhead
  • Load Balance: 依照負載去dispatch
  • Replication: 所有的修改都會replication到底下所有database
  • Parallel Query
不外乎就是要來增加throughput啦。

設定
可以參考官方網站,在這邊以下列的架構為例




pgpool 192.168.0.2
DB1 192.168.0.3
DB2 192.168.0.4


2011/09/26

Postgresql建立索引

有沒有建索引的查詢速度真的差很多,預設是使用binary-tree來實作index,時間複雜度是O(logN)。從N變成logN,是有指數級的差距!

建index的方法可以參考


以下舉幾個例子,假如我們的table叫account,email這個欄位很常用來當查詢條件,那我們會為email建立index:
postgres=> CREATE INDEX account_email_idx ON account (email);
postgres=> \d account;
Indexes:
    "account_pkey" PRIMARY KEY, btree (id)   # Primary key預設就會建立index了
    "account_email_idx" btree (email)

Composite index?
Composite indexes are used when two or more columns are best searched as a unit or if many queries reference only the columns specified in the index.
All the columns in a composite index must be in the same table.
我自己把它解釋成當一個query同時帶多個條件時,composite index可以加快查詢的速度。
postgres=> CREATE INDEX account_composite_idx ON account (name, email);
postgres=> \d account;
Indexes:
    "account_pkey" PRIMARY KEY, btree (id)
    "account_composite_idx" btree (name, email)


除了建索引,定期清理(VACUUM) database 也可以加快存取速度
postgres=> VACUUM VERBOSE ANALYZE;

2011/09/22

Implement tornado session in memcached

示範將tornado的session實作在memcached裡面,我是參考這個blog:
玩蛇记-给tornado加上session支持 - 亚历山大同志 - 博客园

Requirement
  • Tornado 2.0
  • python-libmemcached 0.4
    • Pyrex 0.9.4+
    • libmemcached 0.52

邏輯


流程

2011/09/21

Postgresql 小記

常用指令
# Start
su postgres -c "/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data"

# Stop
su postgres -c "/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data"

# restart
su postgres -c "/usr/local/pgsql/bin/pg_ctl restart -D /usr/local/pgsql/data"

# Init postgres
sudo mkdir /usr/local/pgsql/data
chown postgres:postgres /usr/local/pgsql/data
su postgres -c "/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data"

# Create user
/usr/local/pgsql/bin/createuser -U postgres -P userA

# Drop user
/usr/local/pgsql/bin/dropuser -U postgres userA

# Create db
/usr/local/pgsql/bin/createdb -O userA -U postgres testdb

# Drop db
/usr/local/pgsql/bin/dropdb -U postgres testdb

# Dump db
/usr/local/pgsql/bin/pg_dump -U postgres testdb > dump.sql

# Restore db
/usr/local/pgsql/bin/psql -U postgres -d testdb < dump.sql

# Grant all privileges
/usr/local/pgsql/bin/psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE testdb TO userA;"

# Enter postgresql command line
/usr/local/pgsql/bin/psql -U postgres -d poesgres -h localhost -p 5432


基本設定
  • /usr/local/pgsql/data/postgres.conf
listen_addresses = '192.168.0.3'   # 設定listen IP

  • /usr/local/pgsql/data/pg_hba.conf
host    all             all             127.0.0.1/32              trust
host    all             all             192.168.0.0/16            trust
host    all             all             10.10.1.5/32              password


Log分析
推薦使用pgFouine - a PostgreSQL log analyzer,它可以分析出某段時間內,做了幾次query,處理時間最久的,次數最多的...可以看一下Sample reports長怎樣。


效能測試:
pgbench
使用 pgbench 进行数据库压力测试
pgbench -U postgres -i -s 50 postgres 
pgbench -U postgres -c 100 -t 100 -S postgres


Performance Tuning PostgreSQL