Shared objects and collections

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Shared objects and collections

4D Tech mailing list
I’m retrieving some info from an API that I’d like to cache, and I’m using v17, so I thought Storage might be a nice alternative to a process or interprocess variable. The first obstacle is that I can’t add an object from the API response to a shared collection. I found a KB article with a couple methods to copy a regular object to a shared object, functionality which is inexplicably missing from 4D. However, when adding the shared object to the shared collection, I get the error: “This shared obuject already belongs to another shared group.”

Aside from the fact that I shouldn’t even need the code from the KB, once I’ve copied an object into a shared object, how can I put that shared object into a shared collection? This is something that should "just work”. As it is I guess I’ll go back to a process variable for now.

https://kb.4d.com/assetid=78197

Jim Crate

**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Shared objects and collections

4D Tech mailing list
So what doesn’t work is:

$sharedObj:=New Shared Collection
OB_CopyToSharedObject ($catalogObj;$sharedObj)
Use (Storage.MySharedCollection)
        Storage.MySharedCollection.push($sharedObj)
End use


What does work is:

Storage.MySharedCollection.push(New shared object)
Use (Storage.MySharedCollection[Storage.MySharedCollection.length-1])
        OB_CopyToSharedObject ($catalogObj;Storage.MySharedCollection[Storage.MySharedCollection.length-1])
End use

So it looks like you can’t .push() a shared object onto a shared collection.

Jim Crate


> On Sep 9, 2019, at 8:39 PM, Jim Crate via 4D_Tech <[hidden email]> wrote:
>
> I’m retrieving some info from an API that I’d like to cache, and I’m using v17, so I thought Storage might be a nice alternative to a process or interprocess variable. The first obstacle is that I can’t add an object from the API response to a shared collection. I found a KB article with a couple methods to copy a regular object to a shared object, functionality which is inexplicably missing from 4D. However, when adding the shared object to the shared collection, I get the error: “This shared obuject already belongs to another shared group.”
>
> Aside from the fact that I shouldn’t even need the code from the KB, once I’ve copied an object into a shared object, how can I put that shared object into a shared collection? This is something that should "just work”. As it is I guess I’ll go back to a process variable for now.
>
> https://kb.4d.com/assetid=78197
>
> Jim Crate
>
> **********************************************************************
> 4D Internet Users Group (4D iNUG)
> Archive:  http://lists.4d.com/archives.html
> Options: https://lists.4d.com/mailman/options/4d_tech
> Unsub:  mailto:[hidden email]
> **********************************************************************

**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Shared objects and collections

4D Tech mailing list

> So it looks like you can’t .push() a shared object onto a shared collection.

not quite.

a shared object is either single (solo) or multiple (once belonged to a group)
if an object joins a group, it is free to leave it, but it can't join another group.

i.e. once you divorce your spouse,
you can be single for the rest of your life,
or remarry the same person. (bad analogy)

anyway,

in your case, the problem is that you add $sharedObj to $catalogObj,
then $catalogObj to Storage.

if you reverse the order, that is,
add $catalogObj to Storage first,
then $sharedObj to $catalogObj later,
it should work.

> 2019/09/10 13:13、Jim Crate via 4D_Tech <[hidden email]>のメール:
>
> So what doesn’t work is:
>
> $sharedObj:=New Shared Collection
> OB_CopyToSharedObject ($catalogObj;$sharedObj)
> Use (Storage.MySharedCollection)
> Storage.MySharedCollection.push($sharedObj)
> End use
>
>
> What does work is:
>
> Storage.MySharedCollection.push(New shared object)
> Use (Storage.MySharedCollection[Storage.MySharedCollection.length-1])
> OB_CopyToSharedObject ($catalogObj;Storage.MySharedCollection[Storage.MySharedCollection.length-1])
> End use



**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Shared objects and collections

4D Tech mailing list
On Sep 9, 2019, at 9:55 PM, Keisuke Miyako via 4D_Tech <[hidden email]> wrote:

>
>> So it looks like you can’t .push() a shared object onto a shared collection.
>
> not quite.
>
> a shared object is either single (solo) or multiple (once belonged to a group)
> if an object joins a group, it is free to leave it, but it can't join another group.
> [snip]
> in your case, the problem is that you add $sharedObj to $catalogObj,
> then $catalogObj to Storage.
>
> if you reverse the order, that is,
> add $catalogObj to Storage first,
> then $sharedObj to $catalogObj later,
> it should work.

$catalogObj is a sub-object from the results of an API call with HTTP Get. So it is an existing non-shared object (I.e. not manually created with keys and values by 4D code). I want to add that object to a shared collection so it can be put into Storage.

The only reason $sharedObj exists is because I need to copy all attributes of $catalogObj to $sharedObj so I can add to a shared collection. (Note: I had an error in the first code snippet below; $sharedObj should have been assigned a New Shared Object.)

If I use the code in the reference KB article, copying the non-shared $catalogObj to a new shared object $sharedObj, I can’t add $sharedObj to the shared collection. Presumably I could add that shared object to Storage as a root-level object, but I want a shared collection of those objects.

It looks like collection.push() is actually copying the shared object, and that results in an error because of the shared object's __LockerID property being duplicated. However, we don’t seem to have any other way to add objects to a collection besides .unshift(), which probably does the same as .push(). It seems like .push() should just add the shared object reference to the shared collection, not copy it, especially for shared objects.

If collection.push($myObj) actually copies $myObj, this should be clearly documented. If the general rule is that working with objects is by reference, we need to know when objects are copied instead of referenced.

Jim Crate


>> 2019/09/10 13:13、Jim Crate via 4D_Tech <[hidden email]>のメール:
>>
>> So what doesn’t work is:
>>
>> $sharedObj:=New Shared Object
>> OB_CopyToSharedObject ($catalogObj;$sharedObj)
>> Use (Storage.MySharedCollection)
>> Storage.MySharedCollection.push($sharedObj)
>> End use
>>
>>
>> What does work is:
>>
>> Storage.MySharedCollection.push(New shared object)
>> Use (Storage.MySharedCollection[Storage.MySharedCollection.length-1])
>> OB_CopyToSharedObject ($catalogObj;Storage.MySharedCollection[Storage.MySharedCollection.length-1])
>> End use
>
>
>
> **********************************************************************
> 4D Internet Users Group (4D iNUG)
> Archive:  http://lists.4d.com/archives.html
> Options: https://lists.4d.com/mailman/options/4d_tech
> Unsub:  mailto:[hidden email]
> **********************************************************************

**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Shared objects and collections

4D Tech mailing list
Hi Jim,
I haven't worked with Storage very much because frankly it's a pain and
it's slow. I get why it's a pain and has so much overhead. Every single
object has to have its own lock/unlock ('locker') mechanism and it works
between preemptive processes. So cool for that - no small thing. For me
it's good to keep it as flat as possible and limited to data that's likely
to be actively manipulated in the other processes. I figured this out the
first time I attempted to put a smalish collection of objects into Storage.
Not a good use case, in my opinion.

Otherwise I'm coming back to the liking a single IP object var for storing
essentially read-only data. If you don't have a situation where multiple
processes are likely to be hitting on it, like this sounds, it works fine.
If I need to make the data accessible to a preemptive process write it to
disk and read it into the process. I find TEXT TO DOCUMENT, which is thread
safe, pretty fast.


On Fri, Sep 13, 2019 at 9:33 PM Jim Crate via 4D_Tech <[hidden email]>
wrote:

> On Sep 9, 2019, at 9:55 PM, Keisuke Miyako via 4D_Tech <
> [hidden email]> wrote:
> >
> >> So it looks like you can’t .push() a shared object onto a shared
> collection.
> >
> > not quite.
> >
> > a shared object is either single (solo) or multiple (once belonged to a
> group)
> > if an object joins a group, it is free to leave it, but it can't join
> another group.
> > [snip]
> > in your case, the problem is that you add $sharedObj to $catalogObj,
> > then $catalogObj to Storage.
> >
> > if you reverse the order, that is,
> > add $catalogObj to Storage first,
> > then $sharedObj to $catalogObj later,
> > it should work.
>
> $catalogObj is a sub-object from the results of an API call with HTTP Get.
> So it is an existing non-shared object (I.e. not manually created with keys
> and values by 4D code). I want to add that object to a shared collection so
> it can be put into Storage.
>
> The only reason $sharedObj exists is because I need to copy all attributes
> of $catalogObj to $sharedObj so I can add to a shared collection. (Note: I
> had an error in the first code snippet below; $sharedObj should have been
> assigned a New Shared Object.)
>
> If I use the code in the reference KB article, copying the non-shared
> $catalogObj to a new shared object $sharedObj, I can’t add $sharedObj to
> the shared collection. Presumably I could add that shared object to Storage
> as a root-level object, but I want a shared collection of those objects.
>
> It looks like collection.push() is actually copying the shared object, and
> that results in an error because of the shared object's __LockerID property
> being duplicated. However, we don’t seem to have any other way to add
> objects to a collection besides .unshift(), which probably does the same as
> .push(). It seems like .push() should just add the shared object reference
> to the shared collection, not copy it, especially for shared objects.
>
> If collection.push($myObj) actually copies $myObj, this should be clearly
> documented. If the general rule is that working with objects is by
> reference, we need to know when objects are copied instead of referenced.
>
> Jim Crate
>
>
> >> 2019/09/10 13:13、Jim Crate via 4D_Tech <[hidden email]>のメール:
> >>
> >> So what doesn’t work is:
> >>
> >> $sharedObj:=New Shared Object
> >> OB_CopyToSharedObject ($catalogObj;$sharedObj)
> >> Use (Storage.MySharedCollection)
> >> Storage.MySharedCollection.push($sharedObj)
> >> End use
> >>
> >>
> >> What does work is:
> >>
> >> Storage.MySharedCollection.push(New shared object)
> >> Use (Storage.MySharedCollection[Storage.MySharedCollection.length-1])
> >> OB_CopyToSharedObject
> ($catalogObj;Storage.MySharedCollection[Storage.MySharedCollection.length-1])
> >> End use
> >
> >
> >
> > **********************************************************************
> > 4D Internet Users Group (4D iNUG)
> > Archive:  http://lists.4d.com/archives.html
> > Options: https://lists.4d.com/mailman/options/4d_tech
> > Unsub:  mailto:[hidden email]
> > **********************************************************************
>
> **********************************************************************
> 4D Internet Users Group (4D iNUG)
> Archive:  http://lists.4d.com/archives.html
> Options: https://lists.4d.com/mailman/options/4d_tech
> Unsub:  mailto:[hidden email]
> **********************************************************************



--
Kirk Brooks
San Francisco, CA
=======================

What can be said, can be said clearly,
and what you can’t say, you should shut up about

*Wittgenstein and the Computer *
**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Shared objects and collections

4D Tech mailing list
In reply to this post by 4D Tech mailing list
you can grow a collection implicitly using bracket notation.

quote:

If this element index is beyond the last existing element of the collection,
the collection is automatically resized and all new intermediary elements get the null value

https://doc.4d.com/4Dv17/4D/17.3/Using-object-notation.300-4621561.en.html

2019/09/14 13:31、Jim Crate via 4D_Tech <[hidden email]<mailto:[hidden email]>>のメール:

However, we don’t seem to have any other way to add objects to a collection besides .unshift(), which probably does the same as .push().



**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************