We do handle foreign keys in various ways:

    TUPLE: foo n ;
    TUPLE: bar n foo-id ;

    foo "FOO"
    {
        { "n" "ID" +db-assigned-id+ }
    } define-persistent

    bar "BAR"
    {
        { "n" "ID" +db-assigned-id+ }
        { "foo-id" "FOO_ID" INTEGER { +foreign-id+ foo "ID" } }
    } define-persistent

    foo ensure-table
    bar ensure-table

    foo new insert-tuple
    bar new 1 >>foo-id insert-tuple

You can use foreign keys to enforce not-null:

        { "foo-id" "FOO_ID" INTEGER { +foreign-id+ foo "ID" +not-null+ } }

As well as a "cascade delete":

        { "foo-id" "FOO_ID" INTEGER { +foreign-id+ foo "ID" +on-delete+
+cascade+ } }

If you're looking for a ``bar`` tuple to be populated automatically with a
``foo`` tuple when you do a ``select-tuples``, that doesn't seem to be
supported I think.

I'm a little unsure of your question, is it about duplicative code, or
performance?

If duplicative code, you could do something like this:

    :: copy-all-slots ( from to -- )
        from class-of all-slots [ [ name>> from ] [ offset>> ] bi slot ] {
} map>assoc to set-slots ;

Or perhaps only a few named slots:

    :: copy-slots ( from to names -- )
        names [ [ from get-slot-named ] [ to set-slot-named ] bi ] each ;

I doubt performance would be an issue, but if it is, I'm sure I can have
some suggestions for that too.

Best,
John.


On Fri, Jul 15, 2016 at 3:21 PM, Alexander Ilin <ajs...@yandex.ru> wrote:

> Hello!
>
>   I need some expert advice on an architectural pattern.
>
>   Should I be worried if my code looks like this?
>
> : load-scan ( scan-id/f -- resource-scan/f )
>     [ scan-info new swap >>id select-tuple ] [ f ] if* ;
>
> : load-scans ( resource-data -- resource-data' )
>     [ load-scan ] change-last-path-scan
>     [ load-scan ] change-last-short-scan
>     [ load-scan ] change-last-full-scan ;
>
> : load-meta ( resource-data -- resource-data' )
>     [
>         dup guid>> resource-meta new swap >>guid select-tuple
>         {
>             [ name>> >>name ] [ root>> >>root ]
>             [ desc>> >>desc ] [ guid>> >>guid ]
>             [ last-path-scan>> >>last-path-scan ]
>             [ last-short-scan>> >>last-short-scan ]
>             [ last-full-scan>> >>last-full-scan ]
>         } cleave
>         load-scans
>     ] with-resource-db ;
>
>   The reason for such code organization is that resource-data tuple
> contains *-scan slots, which are pointers to scan-info tuples at run-time.
> But when data are stored into a DB, resource-data is stored in one table,
> and scan-info is in another table, linked by ID field ("foreign key", I
> believe it's called). (The reason for that is there are some other things
> pointing to the scans by ID.)
>
>   Existing persistence mechanism (define-persistent, select-tuple) in
> Factor does not handle foreign keys (does it?), so I have to introduce the
> intermediate tuple called resource-meta, which has the *-scan slots saved
> as INTEGERs:
>
> resource-meta "RESOURCE_META"
> {
>     { "guid" "GUID" VARCHAR +user-assigned-id+ }
>     { "name" "NAME" VARCHAR }
>     { "root" "ROOT" VARCHAR }
>     { "desc" "DESC" VARCHAR }
>     { "last-path-scan" "LAST_PATH_SCAN" INTEGER }
>     { "last-short-scan" "LAST_SHORT_SCAN" INTEGER }
>     { "last-full-scan" "LAST_FULL_SCAN" INTEGER }
> } define-persistent
>
>   When I load the data, I read resource-meta, convert INTEGERs into
> scan-info instances with separate calls to select-tuple (see load-meta
> above).
>
>   I'm worried about all the slot copying I have to perform while saving
> and loading data:
>             [ name>> >>name ] [ root>> >>root ]
>             [ desc>> >>desc ] [ guid>> >>guid ]
>   ... etc.
>
>   Is there a better way?
>
> ---=====---
>  Александр
>
>
> ------------------------------------------------------------------------------
> What NetFlow Analyzer can do for you? Monitors network bandwidth and
> traffic
> patterns at an interface-level. Reveals which users, apps, and protocols
> are
> consuming the most bandwidth. Provides multi-vendor support for NetFlow,
> J-Flow, sFlow and other flows. Make informed decisions using capacity
> planning
> reports.http://sdm.link/zohodev2dev
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to