Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Friction is a drag.


devel / comp.lang.smalltalk.dolphin / How to copy or 'save as' a record using ReStore

SubjectAuthor
* How to copy or 'save as' a record using ReStoreScott McWilliams
`* Re: How to copy or 'save as' a record using ReStorejohn.a...@gmail.com
 `- Re: How to copy or 'save as' a record using ReStoreScott McWilliams

1
How to copy or 'save as' a record using ReStore

<7358e933-c9fb-490b-8de9-ded396845babn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=263&group=comp.lang.smalltalk.dolphin#263

  copy link   Newsgroups: comp.lang.smalltalk.dolphin
X-Received: by 2002:a05:6214:8d2:b0:649:aa62:1ef0 with SMTP id da18-20020a05621408d200b00649aa621ef0mr88260qvb.2.1694635532851;
Wed, 13 Sep 2023 13:05:32 -0700 (PDT)
X-Received: by 2002:a05:6808:15a1:b0:3a7:b15d:b59d with SMTP id
t33-20020a05680815a100b003a7b15db59dmr1528042oiw.11.1694635532717; Wed, 13
Sep 2023 13:05:32 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Wed, 13 Sep 2023 13:05:32 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=98.194.192.24; posting-account=UYtTcwoAAABQV700HQ2QW4xbagv2F8m2
NNTP-Posting-Host: 98.194.192.24
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7358e933-c9fb-490b-8de9-ded396845babn@googlegroups.com>
Subject: How to copy or 'save as' a record using ReStore
From: scottmcwilliams999@gmail.com (Scott McWilliams)
Injection-Date: Wed, 13 Sep 2023 20:05:32 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 1553
 by: Scott McWilliams - Wed, 13 Sep 2023 20:05 UTC

What is the recommended approach to to copy or 'save as' a record using ReStore? Using #copy or #deepCopy just seems to update the original.

I have tried creating a new, 'empty' object of a class and populating just the fields of interest, which seems to work sometimes, but other times results in either an 'index of bounds' error or fails trying to (re-) insert a duplicate key value in a related table.

Thanks in advance for any and all ideas.

Regards,
Scott

Re: How to copy or 'save as' a record using ReStore

<d8a8b046-348e-4316-bf6c-8ab2c2f4fa10n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=264&group=comp.lang.smalltalk.dolphin#264

  copy link   Newsgroups: comp.lang.smalltalk.dolphin
X-Received: by 2002:a05:622a:1441:b0:40f:ea7a:52a2 with SMTP id v1-20020a05622a144100b0040fea7a52a2mr39694qtx.3.1694794464900;
Fri, 15 Sep 2023 09:14:24 -0700 (PDT)
X-Received: by 2002:a9d:6d92:0:b0:6c0:a3e0:f9e3 with SMTP id
x18-20020a9d6d92000000b006c0a3e0f9e3mr565153otp.5.1694794464549; Fri, 15 Sep
2023 09:14:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Fri, 15 Sep 2023 09:14:24 -0700 (PDT)
In-Reply-To: <7358e933-c9fb-490b-8de9-ded396845babn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=85.203.71.185; posting-account=OQ6sIwoAAAC1iWrFEUhdmRsgEkeDOgOm
NNTP-Posting-Host: 85.203.71.185
References: <7358e933-c9fb-490b-8de9-ded396845babn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d8a8b046-348e-4316-bf6c-8ab2c2f4fa10n@googlegroups.com>
Subject: Re: How to copy or 'save as' a record using ReStore
From: john.aspinall@gmail.com (john.a...@gmail.com)
Injection-Date: Fri, 15 Sep 2023 16:14:24 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 4619
 by: john.a...@gmail.com - Fri, 15 Sep 2023 16:14 UTC

Hi Scott,

When copying objects in Smalltalk you may also need to copy some component objects to ensure the copy has a sensible structure. This is particularly important when copying objects persisted with ReStore (in some cases ReStore will complain about invalid structures) however the principle is the same as for non-persistent objects.

As an example I'll use the classes from the "SSW ReStore Examples" package (see the package comment for more details). Starting with an empty database let's first create a Customer with an Address:

Customer new
firstName: 'John';
surname: 'Smith';
address: (Address new line1: '123 Oxford Street'; yourself);
store.

You should now have a database containing one Customer and one Address:

Customer storedInstances size. "1"
Address storedInstances size. "1"

Now let's try to copy the customer and persist the copy:

customer := Customer storedInstances first.
copy := customer copy.
copy firstName: 'James'; store.

At this point you should receive the error "attempt to assign collection to > 1 persistent object". This is because the default implementation of copy will just return a shallowCopy, so the copy has the exact same 'orders' collection as the original. Sharing persistent collections isn't valid in ReStore so the copy needs its own collection - we do this by adding an implementation of postCopy to Customer:

postCopy

super postCopy.
self orders: OrderedCollection new

We should now be able to create and persist a new copy of the customer:

copy := customer copy.
copy firstName: 'James'; store.

However note that although we've copied the customer the address has not been copied:

Customer storedInstances size. "2"
Address storedInstances size. "1"

The single persistent Address is shared by both customers (it's valid to share persistent non-collection objects in ReStore). It may be that this would be a valid situation in some databases, however for a database of customers lets assume we want each customer to have its own independent address, so we should also copy the address as part of the customer's postCopy method:

postCopy

super postCopy.
self orders: OrderedCollection new.
self address: self address copy

If we now create and persist an additional copy we will get both a new customer and a new address:

copy := customer copy.
copy firstName: 'Jack'; store.

Customer storedInstances size. "3"
Address storedInstances size. "2"

Just to reiterate, the important thing is to implement postCopy methods in your persistent classes so that the resulting copies represent a valid structure according to the requirements of your data model.

Hope this helps,

John Aspinall

On Wednesday, September 13, 2023 at 9:05:33 PM UTC+1, Scott McWilliams wrote:
> What is the recommended approach to to copy or 'save as' a record using ReStore? Using #copy or #deepCopy just seems to update the original.
>
> I have tried creating a new, 'empty' object of a class and populating just the fields of interest, which seems to work sometimes, but other times results in either an 'index of bounds' error or fails trying to (re-) insert a duplicate key value in a related table.
>
> Thanks in advance for any and all ideas.
>
> Regards,
> Scott

Re: How to copy or 'save as' a record using ReStore

<7e30c5df-512d-4efa-a840-30049f646785n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=265&group=comp.lang.smalltalk.dolphin#265

  copy link   Newsgroups: comp.lang.smalltalk.dolphin
X-Received: by 2002:a05:6214:4a5a:b0:64a:176:b117 with SMTP id ph26-20020a0562144a5a00b0064a0176b117mr31911qvb.2.1695216655817;
Wed, 20 Sep 2023 06:30:55 -0700 (PDT)
X-Received: by 2002:a05:6830:1cc:b0:6bc:fb26:499e with SMTP id
r12-20020a05683001cc00b006bcfb26499emr839119ota.2.1695216655649; Wed, 20 Sep
2023 06:30:55 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Wed, 20 Sep 2023 06:30:55 -0700 (PDT)
In-Reply-To: <d8a8b046-348e-4316-bf6c-8ab2c2f4fa10n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=98.194.192.24; posting-account=UYtTcwoAAABQV700HQ2QW4xbagv2F8m2
NNTP-Posting-Host: 98.194.192.24
References: <7358e933-c9fb-490b-8de9-ded396845babn@googlegroups.com> <d8a8b046-348e-4316-bf6c-8ab2c2f4fa10n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7e30c5df-512d-4efa-a840-30049f646785n@googlegroups.com>
Subject: Re: How to copy or 'save as' a record using ReStore
From: scottmcwilliams999@gmail.com (Scott McWilliams)
Injection-Date: Wed, 20 Sep 2023 13:30:55 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 5096
 by: Scott McWilliams - Wed, 20 Sep 2023 13:30 UTC

On Friday, September 15, 2023 at 11:14:25 AM UTC-5, john.a...@gmail..com wrote:
> Hi Scott,
>
> When copying objects in Smalltalk you may also need to copy some component objects to ensure the copy has a sensible structure. This is particularly important when copying objects persisted with ReStore (in some cases ReStore will complain about invalid structures) however the principle is the same as for non-persistent objects.
>
> As an example I'll use the classes from the "SSW ReStore Examples" package (see the package comment for more details). Starting with an empty database let's first create a Customer with an Address:
>
> Customer new
> firstName: 'John';
> surname: 'Smith';
> address: (Address new line1: '123 Oxford Street'; yourself);
> store.
>
> You should now have a database containing one Customer and one Address:
>
> Customer storedInstances size. "1"
> Address storedInstances size. "1"
>
> Now let's try to copy the customer and persist the copy:
>
> customer := Customer storedInstances first.
> copy := customer copy.
> copy firstName: 'James'; store.
>
> At this point you should receive the error "attempt to assign collection to > 1 persistent object". This is because the default implementation of copy will just return a shallowCopy, so the copy has the exact same 'orders' collection as the original. Sharing persistent collections isn't valid in ReStore so the copy needs its own collection - we do this by adding an implementation of postCopy to Customer:
>
> postCopy
>
> super postCopy.
> self orders: OrderedCollection new
>
> We should now be able to create and persist a new copy of the customer:
>
> copy := customer copy.
> copy firstName: 'James'; store.
>
> However note that although we've copied the customer the address has not been copied:
>
> Customer storedInstances size. "2"
> Address storedInstances size. "1"
>
> The single persistent Address is shared by both customers (it's valid to share persistent non-collection objects in ReStore). It may be that this would be a valid situation in some databases, however for a database of customers lets assume we want each customer to have its own independent address, so we should also copy the address as part of the customer's postCopy method:
>
> postCopy
>
> super postCopy.
> self orders: OrderedCollection new.
> self address: self address copy
>
> If we now create and persist an additional copy we will get both a new customer and a new address:
>
> copy := customer copy.
> copy firstName: 'Jack'; store.
>
> Customer storedInstances size. "3"
> Address storedInstances size. "2"
>
> Just to reiterate, the important thing is to implement postCopy methods in your persistent classes so that the resulting copies represent a valid structure according to the requirements of your data model.
>
> Hope this helps,
>
> John Aspinall
> On Wednesday, September 13, 2023 at 9:05:33 PM UTC+1, Scott McWilliams wrote:
> > What is the recommended approach to to copy or 'save as' a record using ReStore? Using #copy or #deepCopy just seems to update the original.
> >
> > I have tried creating a new, 'empty' object of a class and populating just the fields of interest, which seems to work sometimes, but other times results in either an 'index of bounds' error or fails trying to (re-) insert a duplicate key value in a related table.
> >
> > Thanks in advance for any and all ideas.
> >
> > Regards,
> > Scott

Thank you John!

Regards,
Scott

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor