allegro cache存储hash table

20 views
Skip to first unread message

Jeova Sanctus Unus

unread,
Feb 21, 2012, 6:41:23 AM2/21/12
to lis...@googlegroups.com
我有个class里的一个slot需要存储hash-table,但是allegro cache 不直接支持这么做。但是我仍想这么做。
看了些文档,试了下,得出以下几点看法,希望能通过和大家交流,共同进步:
1.貌似不怎么能搜索到其他人问的关于allegro cache的问题。

2.我不能使用ac-map来代替hash-table,因为,这个class的对象很多,而文档(acache.pdf)说:Therefore
use maps sparingly (create no more than a few hundred of them).

3.acache支持的类型中不包括hash-table,但是包括non-persistent的object.就是通过encode-object和decode-object来支持存储hash.我还在学习common
lisp,所以对概念不是很清楚。我无法直接(defmethod encode-object ((object
hash-table)))来支持存储hash-table,只能先定义另外一个non-persistent的class,然后写关于这个class的encode-object来间接支持。比如:
(defpackage :foo
(:use :cl :db.ac :alexandria))
(in-package :foo)
(defclass hash ()
((h :initarg :h :accessor h)))

(defclass bar ()
((i :initarg :i :accessor i))
(:metaclass persistent-class))

(defmethod encode-object ((object hash))
(hash-table-plist (h object)))

(defmethod decode-object ((object hash) vals)
(make-instance (class-of object) :h (plist-hash-table vals :test #'equalp)))

大家有什么想法嘛?我感觉我这样做,实在不够酷.谢谢。

刘滔

unread,
Feb 21, 2012, 9:49:42 AM2/21/12
to lis...@googlegroups.com
哪里不够酷了?是说encode和decode两个方法不能处理CL原生的hash-table吗?那样的话试试能不能在用defmethod定义一个和hash-table类型相关联的方法呢?


--
Lisp-cn(Lisp中文用户组)
CLUG http://lisp.org.cn



--
Liutos Love Linux LaTeX Lisp Ling

我的博客,纪念我死去的GAE

Jeova Sanctus Unus

unread,
Feb 21, 2012, 6:39:19 PM2/21/12
to lis...@googlegroups.com

嗯,对,我尝试直接写hash-table的,但是失败了。或许我哪里写错了,今天有机会的话,我再把hash-table的代码贴下。
要不你试试看?

Jeova Sanctus Unus

unread,
Feb 21, 2012, 11:38:19 PM2/21/12
to lis...@googlegroups.com
直接写hash-table的encode-object,代码差不多是这样,(make-instance 'bar :i
(plist-hash-table '(1 2 3 4))) 再 (commit),会提示 hash tables cannot be
written to the database. Consider using ac-map objects for this
purpose

(defpackage :foo
(:use :cl :db.ac :alexandria))
(in-package :foo)

(defclass bar ()
((i :initarg :i :accessor i))
(:metaclass persistent-class))

(defmethod encode-object ((object hash-table))
(hash-table-plist object))

(defmethod decode-object ((object hash-table) vals)
(plist-hash-table vals :test #'equalp))

Jianshi Huang

unread,
Feb 22, 2012, 1:33:55 AM2/22/12
to lis...@googlegroups.com
看 decode-object 的 api,第二个参数是 slot-vals,只能 serialize CLOS object.

况且你的 decode-object 定义得也不对,objects 的 type 应该是 list

如果 object 的 type 是 list,那问题是是否所有的 plist 都要转化为 hash-table?

你想办法定义一个 generic 的 map 吧,或者用现成的。


2012/2/22 Jeova Sanctus Unus <jeova.san...@gmail.com>:

--
黄 澗石 (Jianshi Huang)
http://huangjs.net/

Jeova Sanctus Unus

unread,
Feb 22, 2012, 8:59:49 AM2/22/12
to lis...@googlegroups.com
我觉得decode-object的object的class应该就是hash啊。
在method里插入format可以看到。

(defpackage :foo
(:use :cl :db.ac :alexandria))
(in-package :foo)
(defclass hash ()
((h :initarg :h :accessor h)))

(defclass bar ()
((i :initarg :i :accessor i))
(:metaclass persistent-class))

(defmethod encode-object ((object hash))
(format t "encode:~a~%" object)
(hash-table-plist (h object)))

(defmethod decode-object ((object hash) vals)

(format t "decode:~a ~a~%" object vals)


(make-instance (class-of object) :h (plist-hash-table vals :test #'equalp)))

Jianshi Huang

unread,
Feb 22, 2012, 9:11:30 AM2/22/12
to lis...@googlegroups.com
机器里没 allegrocl,你看看下面的能否用

(defmethod encode-object ((object hash))
(cons 'h (hash-table-plist (h object))))

(defmethod decode-object ((object hash) slot-vals)
(let ((plist (cdr (assoc 'h slot-vals))))
(make-instance 'hash :h (plist-hash-table plist :test #'equalp))))


2012/2/22 Jeova Sanctus Unus <jeova.san...@gmail.com>:
>

> (defmethod encode-object ((object hash))
> (format t "encode:~a~%" object)
> (hash-table-plist (h object)))
>
> (defmethod decode-object ((object hash) vals)
> (format t "decode:~a ~a~%" object vals)
> (make-instance (class-of object) :h (plist-hash-table vals :test #'equalp)))

--

Jeova Sanctus Unus

unread,
Feb 22, 2012, 9:19:01 AM2/22/12
to lis...@googlegroups.com
这得定义一个class吧,名字是hash.
这个和我那个差不多啊。
定义一个新的class(非persistente的),然后在此class的slot里存hash,没问题。
我想知道,如何直接在persisitent-class里的slot存hash

Jianshi Huang

unread,
Feb 22, 2012, 9:48:45 AM2/22/12
to lis...@googlegroups.com
哦,我回的是你直接存储 hash-table 的例子。
Reply all
Reply to author
Forward
0 new messages