1 ! Copyright (C) 2009 Bruno Deferrari
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types assocs destructors kernel libc locals
4 sequences serialize tokyo.alien.tcadb ;
5 IN: tokyo.cabinet.abstract
7 TUPLE: tokyo-abstractdb handle disposed ;
9 INSTANCE: tokyo-abstractdb assoc
11 : <tokyo-abstractdb> ( name -- tokyo-abstractdb )
12 tcadbnew [ swap tcadbopen drop ] keep
13 tokyo-abstractdb new [ (>>handle) ] keep ;
15 M: tokyo-abstractdb dispose* [ tcadbdel f ] change-handle drop ;
17 M:: tokyo-abstractdb at* ( key db -- value/f ? )
20 key object>bytes :> kbytes
21 kbytes length :> key-size
22 handle kbytes key-size sizeout tcadbget :> output
24 [ sizeout *int memory>byte-array bytes>object t ] [ drop ] bi
27 M: tokyo-abstractdb assoc-size ( db -- size ) handle>> tcadbrnum ;
30 ! M: tokyo-abstractdb >alist ( db -- alist )
33 M:: tokyo-abstractdb set-at ( value key db -- )
35 key object>bytes :> kbytes
36 kbytes length :> key-size
37 value object>bytes :> vbytes
38 vbytes length :> value-size
39 handle kbytes key-size vbytes value-size tcadbput drop ;
41 M:: tokyo-abstractdb delete-at ( key db -- )
43 key object>bytes :> kbytes
44 kbytes length :> key-size
45 handle kbytes key-size tcadbout drop ;
47 M: tokyo-abstractdb clear-assoc ( db -- ) handle>> tcadbvanish drop ;
49 M: tokyo-abstractdb equal? assoc= ;
51 M: tokyo-abstractdb hashcode* assoc-hashcode ;