Can't access objects in dictionary

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't access objects in dictionary

Amir Ansari
I enter the following code in a Workspace:
(the class 'User' is just a very simple class with instance variable 'username' and corresponding accessors)


MagmaRepositoryController create: '/testDB' root: Dictionary new.

initialSession := MagmaSession openLocal: '/testDB'.
initialSession connectAs: 'admin'.
initialSession commit: [ initialSession root at: 'users' put: MagmaPreallocatedDictionary new ].
initialSession disconnect; closeRepository.

newUser := User new username: 'Charlie'; yourself.
newSession := MagmaSession openLocal: '/testDB'.
newSession connectAs: 'charlie'.
newSession commit: [ (newSession root at: 'users') at: newUser put: OrderedCollection new ].
newSession disconnect; closeRepository.

testSession := MagmaSession openLocal: '/testDB'.
testSession connectAs: 'aUser'.
(testSession root at: 'users') size
(testSession root at: 'users') at: newUser


The last two lines indicate the problem: the size of the MagmaPreallocatedDictionary is reported correctly (even when I add more users), but when I try to access any of the entries with the 'User' objects as keys, I get a 'errorKeyNotFound:' error.

Am I doing something wrong...?

Amir
_______________________________________________
Magma mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/magma
Reply | Threaded
Open this post in threaded view
|

Re: Can't access objects in dictionary

fvozzi
Hi Amir,
did you define #= method on User ? 

I guest that It'll look like this:

User >> = otherUser
"Answer whether the receiver is equal to the argument."
self class = otherUser class ifFalse: [^false].
^self name = otherUser name

You need implement the #hash method too:

User >> hash
"Answer a SmallInteger whose value is related to the receiver's identity."
^self name hash

Hope this help.

Facu


On Thu, Dec 30, 2010 at 10:24 AM, Amir Ansari <[hidden email]> wrote:
I enter the following code in a Workspace:
(the class 'User' is just a very simple class with instance variable 'username' and corresponding accessors)


MagmaRepositoryController create: '/testDB' root: Dictionary new.

initialSession := MagmaSession openLocal: '/testDB'.
initialSession connectAs: 'admin'.
initialSession commit: [ initialSession root at: 'users' put: MagmaPreallocatedDictionary new ].
initialSession disconnect; closeRepository.

newUser := User new username: 'Charlie'; yourself.
newSession := MagmaSession openLocal: '/testDB'.
newSession connectAs: 'charlie'.
newSession commit: [ (newSession root at: 'users') at: newUser put: OrderedCollection new ].
newSession disconnect; closeRepository.

testSession := MagmaSession openLocal: '/testDB'.
testSession connectAs: 'aUser'.
(testSession root at: 'users') size
(testSession root at: 'users') at: newUser


The last two lines indicate the problem: the size of the MagmaPreallocatedDictionary is reported correctly (even when I add more users), but when I try to access any of the entries with the 'User' objects as keys, I get a 'errorKeyNotFound:' error.

Am I doing something wrong...?

Amir
_______________________________________________
Magma mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/magma


_______________________________________________
Magma mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/magma
Reply | Threaded
Open this post in threaded view
|

Re: Can't access objects in dictionary

Amir Ansari
In reply to this post by Amir Ansari
Hi Tim

I'm using the User object as a key precisely because it's unique!  Usernames will inevitably overlap.  I considered email addresses, but if a user on the system changes their email address, it's an extra operation to update the Dictionary; with the User object, everything remains the same.

Nevertheless, thanks for your reply! ;-)

Amir


On Thu, 30 Dec 2010 10:22:59 -0800
Tim Ziebart <[hidden email]> wrote:

>     I don't understand what you are trying to accomplish. I would have
> expected the key to be a unique identifier such as username, email etc.  
> Then you would associate with the key a newUser.  So, (newSession root
> at: 'users') at: 'Charlie' put: newUser.
_______________________________________________
Magma mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/magma
Reply | Threaded
Open this post in threaded view
|

Re: Can't access objects in dictionary

Amir Ansari
In reply to this post by fvozzi
Hi Facundo

Thanks for your help.  What I'm trying to understand is how, between different Magma sessions, the Dictionary keys seem to 'lose' their identity.  Obviously, the first thought is that there's something wrong in my code (which I why I posted it all).  Surely it should be simple to check directly whether an object equals a key, without having to define hash methods...?

I ought to point out that I also tested these steps separately without committing in a Magma session, and everything worked OK.

Amir


On Thu, 30 Dec 2010 16:48:32 -0300
Facundo Vozzi <[hidden email]> wrote:

> Hi Amir,
> did you define #= method on User ?
>
> I guest that It'll look like this:
>
> User >> = otherUser
> "Answer whether the receiver is equal to the argument."
>  self class = otherUser class ifFalse: [^false].
> ^self name = otherUser name
>
> You need implement the #hash method too:
>
> User >> hash
> "Answer a SmallInteger whose value is related to the receiver's identity."
>  ^self name hash
>
> Hope this help.
>
> Facu
_______________________________________________
Magma mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/magma
Reply | Threaded
Open this post in threaded view
|

Re: Can't access objects in dictionary

Chris Muller-3
Hi Amir, each session has its *own* view of the persistent object
model.  It's own copy.  It _must_ be that way, or else when one
session changes an object it will appear "changed" for the other
sessions too.

You are perfectly welcome to use identity-based checking in your Magma
programs, as long as you do the check on objects from the same
session.

However, as previously stated, you must ensure that identityHash is
never part of the hash calculation, because the hash determines its
position in the Dictionary, and it will not be the same between
sessions.

HTH,
  Chris

On Fri, Dec 31, 2010 at 7:10 AM, Amir Ansari <[hidden email]> wrote:

> Hi Facundo
>
> Thanks for your help.  What I'm trying to understand is how, between different Magma sessions, the Dictionary keys seem to 'lose' their identity.  Obviously, the first thought is that there's something wrong in my code (which I why I posted it all).  Surely it should be simple to check directly whether an object equals a key, without having to define hash methods...?
>
> I ought to point out that I also tested these steps separately without committing in a Magma session, and everything worked OK.
>
> Amir
>
>
> On Thu, 30 Dec 2010 16:48:32 -0300
> Facundo Vozzi <[hidden email]> wrote:
>
>> Hi Amir,
>> did you define #= method on User ?
>>
>> I guest that It'll look like this:
>>
>> User >> = otherUser
>> "Answer whether the receiver is equal to the argument."
>>  self class = otherUser class ifFalse: [^false].
>> ^self name = otherUser name
>>
>> You need implement the #hash method too:
>>
>> User >> hash
>> "Answer a SmallInteger whose value is related to the receiver's identity."
>>  ^self name hash
>>
>> Hope this help.
>>
>> Facu
> _______________________________________________
> Magma mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/magma
>
_______________________________________________
Magma mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/magma
Reply | Threaded
Open this post in threaded view
|

Re: Can't access objects in dictionary

Amir Ansari
OK, that makes sense!  Thanks for clearing this up; I can stop worrying about it now ;-)

Lastly, thank you (and other contributors) for all your time and effort on Magma - I'm using it in a web app, and it's proven to be a simple, elegant system, deserved of a far wider audience...

Happy 2011!

Amir


On Fri, 31 Dec 2010 10:58:23 -0600
Chris Muller <[hidden email]> wrote:

> Hi Amir, each session has its *own* view of the persistent object
> model.  It's own copy.  It _must_ be that way, or else when one
> session changes an object it will appear "changed" for the other
> sessions too.
>
> You are perfectly welcome to use identity-based checking in your Magma
> programs, as long as you do the check on objects from the same
> session.
>
> However, as previously stated, you must ensure that identityHash is
> never part of the hash calculation, because the hash determines its
> position in the Dictionary, and it will not be the same between
> sessions.
>
> HTH,
>   Chris
_______________________________________________
Magma mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/magma
Reply | Threaded
Open this post in threaded view
|

Re: Can't access objects in dictionary

Amir Ansari
Incidentally, I'm replying to myself here - because this exchange of emails has (half) gone through personal email addresses instead of through the mailing list.  Is there a way to configure the list server to exchange the 'From' field with the 'Cc' field in list emails?

So, when hitting the 'Reply' button in one's email client, the email would go automatically to the mailing list rather than the personal address of the sender...

Amir
_______________________________________________
Magma mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/magma