]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/tokyo/abstractdb/abstractdb.factor
tokyo: Reimplement assoc protocols for remote and abstract db using a functor
[factor.git] / extra / tokyo / abstractdb / abstractdb.factor
index 1433c275e17c4a812dc8bd908a2fe8c014aaa12b..ea6d20fc2d3013a5c14d8ea7682a5fced97c8887 100644 (file)
@@ -1,60 +1,10 @@
 ! Copyright (C) 2009 Bruno Deferrari
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors alien.c-types arrays assocs destructors
-kernel locals sequences serialize vectors
-tokyo.alien.tcadb tokyo.alien.tcutil tokyo.utils ;
+USING: accessors kernel tokyo.alien.tcadb tokyo.assoc-functor ;
 IN: tokyo.abstractdb
 
-TUPLE: tokyo-abstractdb handle disposed ;
-
-INSTANCE: tokyo-abstractdb assoc
+<< "tcadb" "abstractdb" define-tokyo-assoc-api >>
 
 : <tokyo-abstractdb> ( name -- tokyo-abstractdb )
     tcadbnew [ swap tcadbopen drop ] keep
     tokyo-abstractdb new [ (>>handle) ] keep ;
-
-M: tokyo-abstractdb dispose* [ tcadbdel f ] change-handle drop ;
-
-M:: tokyo-abstractdb at* ( key db -- value/f ? )
-    0 <int>          :> sizeout
-    db handle>>      :> handle
-    key object>bytes :> kbytes
-    kbytes length    :> key-size
-    handle kbytes key-size sizeout tcadbget :> output
-    output [
-        [ memory>object ] [ tcfree ] bi t
-    ] [ f f ] if* ;
-
-M: tokyo-abstractdb assoc-size ( db -- size ) handle>> tcadbrnum ;
-
-! FIXME: make this nicer
-M:: tokyo-abstractdb >alist ( db -- alist )
-    db handle>>            :> handle
-    0 <int>                :> size-out
-    db assoc-size <vector> :> keys
-    handle tcadbiterinit drop
-    [ handle size-out tcadbiternext dup ] [
-        [ memory>object ] [ tcfree ] bi
-        keys push
-    ] while drop
-    keys [ dup db at 2array ] { } map-as ;
-
-M:: tokyo-abstractdb set-at ( value key db -- )
-    db handle>>        :> handle
-    key object>bytes   :> kbytes
-    kbytes length      :> key-size
-    value object>bytes :> vbytes
-    vbytes length      :> value-size
-    handle kbytes key-size vbytes value-size tcadbput drop ;
-
-M:: tokyo-abstractdb delete-at ( key db -- )
-    db handle>>      :> handle
-    key object>bytes :> kbytes
-    kbytes length    :> key-size
-    handle kbytes key-size tcadbout drop ;
-
-M: tokyo-abstractdb clear-assoc ( db -- ) handle>> tcadbvanish drop ;
-
-M: tokyo-abstractdb equal? assoc= ;
-
-M: tokyo-abstractdb hashcode* assoc-hashcode ;