1 ! Copyright (C) 2009 Bruno Deferrari
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types arrays assocs destructors
4 io io.streams.memory kernel libc locals
5 sequences serialize tokyo.alien.tcadb tokyo.alien.tcutil vectors ;
6 IN: tokyo.cabinet.abstract
8 TUPLE: tokyo-abstractdb handle disposed ;
11 : with-memory-reader ( memory quot -- )
12 [ <memory-stream> ] dip with-input-stream* ; inline
14 : memory>object ( memory -- object )
15 [ deserialize ] with-memory-reader ;
18 INSTANCE: tokyo-abstractdb assoc
20 : <tokyo-abstractdb> ( name -- tokyo-abstractdb )
21 tcadbnew [ swap tcadbopen drop ] keep
22 tokyo-abstractdb new [ (>>handle) ] keep ;
24 M: tokyo-abstractdb dispose* [ tcadbdel f ] change-handle drop ;
26 M:: tokyo-abstractdb at* ( key db -- value/f ? )
29 key object>bytes :> kbytes
30 kbytes length :> key-size
31 handle kbytes key-size sizeout tcadbget :> output
33 [ memory>object ] [ tcfree ] bi t
36 M: tokyo-abstractdb assoc-size ( db -- size ) handle>> tcadbrnum ;
38 ! FIXME: make this nicer
39 M:: tokyo-abstractdb >alist ( db -- alist )
42 db assoc-size <vector> :> keys
43 handle tcadbiterinit drop
44 [ handle size-out tcadbiternext dup ] [
45 [ memory>object ] [ tcfree ] bi
48 keys [ dup db at 2array ] { } map-as ;
50 M:: tokyo-abstractdb set-at ( value key db -- )
52 key object>bytes :> kbytes
53 kbytes length :> key-size
54 value object>bytes :> vbytes
55 vbytes length :> value-size
56 handle kbytes key-size vbytes value-size tcadbput drop ;
58 M:: tokyo-abstractdb delete-at ( key db -- )
60 key object>bytes :> kbytes
61 kbytes length :> key-size
62 handle kbytes key-size tcadbout drop ;
64 M: tokyo-abstractdb clear-assoc ( db -- ) handle>> tcadbvanish drop ;
66 M: tokyo-abstractdb equal? assoc= ;
68 M: tokyo-abstractdb hashcode* assoc-hashcode ;