What have you found for these years?

2008-08-07

data mapper 的 identity map...(2)

得到答覆了,雖然不是很令人滿意的答案,但是是個合情合理的答案 @_@
簡單地說就是 ruby 的 weak reference 有問題(根據 data mapper team),
所以沒辦法在 identity map 當中使用 weak reference 去 track object.

而因為不能用 weak reference, 就沒辦法讓 gc 正常運作,所以必須捨棄這種作法。
取而代之的是,用一個 block 包住你要的操作,就像 File.open

DataMapper.repository do

  # codes here share an identity map

end

會有這樣的狀況,應該是設計面的問題,要循其本。首先為什麼需要 database?
因為 memory 存不了那麼多東西,(所以又有 memcached... 題外話)
更何況我們需要一些 persistence 的紀錄,不能依靠 memory.
(這樣除了 OO database 外,不免讓人幻想會不會有 OO filesystem?)

然而我們使用 identity map 的意思,其實就是把儲存在 database 的狀況,
改成儲存在 memory 中。也就是說,這本來就是一個獨立的暫時動作,
如果一個 process 只有一個 identity map, 這樣也就失去 database 的意義,
回到原本 memory 解決一切的老路上。(除非 weak reference works)

另一方面,由於 http 是 stateless, 所以 request 與 request 之間也是獨立的。
每一個 request 都應該重新從 database 取出物件,而不是再去 identity map 中撈。
(這樣不同 process 才能 share data)
當 identity map 變得跟 process 無關後,一個 process 就能依序處理 request,
就像 rails 沒有 multi-thread 一樣。

所以我原本的想像,應該比較像這樣:

request do # http request init goes here
  repository do # repository init goes here

    # other codes goes here

  end # repository cleanup goes here
end # http request cleanup goes here

不過這應該不是什麼大問題,自己去包就好了 :D

1 retries:

Plumm said...

> 焦躁到不可思議的地步了
你真的不考慮去看一下醫生嘛 ??

Post a Comment

Note: Only a member of this blog may post a comment.



All texts are licensed under CC Attribution 3.0