[Geoserver-devel] what's the expected outcome of getStyleByName(String name)

Hey Justin,

just a question. Working on getting all the jdbc catalog tests to pass
after merging with the latest per workspace settings work, I'm not
sure whether the old implementation of
DefaultCatalogFacade.getStyleByName(String name) is still accurate.

It is like this:
  public StyleInfo getStyleByName(String name) {
        for (Iterator s = styles.iterator(); s.hasNext():wink: {
            StyleInfo style = (StyleInfo) s.next();
            if (name.equals(style.getName())) {
                return ModificationProxy.create(style, StyleInfo.class);
            }
        }
        return null;
    }

Meaning that it'll return the first style with the given name,
regardless of it's workspace.
Is that the intended behaviour, or should it return only a style
that's "global".

TIA,
Gabriel.

--
Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.

Hey Gabriel,

Yeah, I think it would make sense to check that the workspace is null. We could perhaps be lax and actually look first for a global style and if not found use one found within a workspace if there is only one. Does that make sense.

Also layer groups will have this same lookup issue.

So yeah, go ahead and amend the lookup, we’ll see what unit tests fail.

-Justin

On Fri, Apr 13, 2012 at 2:00 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

Hey Justin,

just a question. Working on getting all the jdbc catalog tests to pass
after merging with the latest per workspace settings work, I’m not
sure whether the old implementation of
DefaultCatalogFacade.getStyleByName(String name) is still accurate.

It is like this:
public StyleInfo getStyleByName(String name) {
for (Iterator s = styles.iterator(); s.hasNext():wink: {
StyleInfo style = (StyleInfo) s.next();
if (name.equals(style.getName())) {
return ModificationProxy.create(style, StyleInfo.class);
}
}
return null;
}

Meaning that it’ll return the first style with the given name,
regardless of it’s workspace.
Is that the intended behaviour, or should it return only a style
that’s “global”.

TIA,
Gabriel.


Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.


Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.

On Fri, Apr 13, 2012 at 4:36 PM, Justin Deoliveira <jdeolive@anonymised.com> wrote:

Hey Gabriel,

Yeah, I think it would make sense to check that the workspace is null. We
could perhaps be lax and actually look first for a global style and if not
found use one found within a workspace if there is only one. Does that make
sense.

hmmm.. that last option would be confusing imho. Besides, I don't see
it's usage. In my opinion, for clarity and backwards compatible, it
should be make clear that getStyleByName(String) is a convenient
shortcut for getStyleByName(Workspace, String) with a null workspace
argument?

Also layer groups will have this same lookup issue.

Indeed.

Just found this fixing a side effect of
CatalogImplTest.testAddStyleWithNameConflict(). That test does a
number of checks on a style with a workspace, but the assigned
workspace is not added to the catalog (hence has no id), so the test
works with the default in memory catalog by accident. Fix is to just
call addWorkspace() besides addStyle(). Ok with that too?

So yeah, go ahead and amend the lookup, we'll see what unit tests fail.

Will do.

Thanks for the prompt reply.
Gabriel

-Justin

On Fri, Apr 13, 2012 at 2:00 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

Hey Justin,

just a question. Working on getting all the jdbc catalog tests to pass
after merging with the latest per workspace settings work, I'm not
sure whether the old implementation of
DefaultCatalogFacade.getStyleByName(String name) is still accurate.

It is like this:
public StyleInfo getStyleByName(String name) {
for (Iterator s = styles.iterator(); s.hasNext():wink: {
StyleInfo style = (StyleInfo) s.next();
if (name.equals(style.getName())) {
return ModificationProxy.create(style, StyleInfo.class);
}
}
return null;
}

Meaning that it'll return the first style with the given name,
regardless of it's workspace.
Is that the intended behaviour, or should it return only a style
that's "global".

TIA,
Gabriel.

--
Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.

--
Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.

--
Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.

On Fri, Apr 13, 2012 at 4:27 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

On Fri, Apr 13, 2012 at 4:36 PM, Justin Deoliveira <jdeolive@anonymised.com> wrote:

Hey Gabriel,

Yeah, I think it would make sense to check that the workspace is null. We
could perhaps be lax and actually look first for a global style and if not
found use one found within a workspace if there is only one. Does that make
sense.

hmmm… that last option would be confusing imho. Besides, I don’t see
it’s usage. In my opinion, for clarity and backwards compatible, it
should be make clear that getStyleByName(String) is a convenient
shortcut for getStyleByName(Workspace, String) with a null workspace
argument?

Works for me. I agree the most sensible approach.

Also layer groups will have this same lookup issue.

Indeed.

Just found this fixing a side effect of
CatalogImplTest.testAddStyleWithNameConflict(). That test does a
number of checks on a style with a workspace, but the assigned
workspace is not added to the catalog (hence has no id), so the test
works with the default in memory catalog by accident. Fix is to just
call addWorkspace() besides addStyle(). Ok with that too?

Yup, i imagine there will be a couple of cases like these… feel free to fix as you come across them.

So yeah, go ahead and amend the lookup, we’ll see what unit tests fail.

Will do.

Thanks for the prompt reply.
Gabriel

-Justin

On Fri, Apr 13, 2012 at 2:00 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

Hey Justin,

just a question. Working on getting all the jdbc catalog tests to pass
after merging with the latest per workspace settings work, I’m not
sure whether the old implementation of
DefaultCatalogFacade.getStyleByName(String name) is still accurate.

It is like this:
public StyleInfo getStyleByName(String name) {
for (Iterator s = styles.iterator(); s.hasNext():wink: {
StyleInfo style = (StyleInfo) s.next();
if (name.equals(style.getName())) {
return ModificationProxy.create(style, StyleInfo.class);
}
}
return null;
}

Meaning that it’ll return the first style with the given name,
regardless of it’s workspace.
Is that the intended behaviour, or should it return only a style
that’s “global”.

TIA,
Gabriel.


Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.


Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.


Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.


Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.

The one thing that still bothers me with this API is that
CatalogImpl.getStyleByName(WorkspaceInfo, String) can return one of
four different things:
- the requested style, given a non null workspace argument
- a global style, given a null workspace argument
- a style named the same but belonging to the default workspace, if
none of the above was found.
- if none of the above matches, returns a style from ANY workspace
with that name

My interpretation is this makes style lookup by workspace almost
futil, but I may be missing the use cases that rely on this behavior.
Could you elaborate and tell whether we can make this method contract
stricter? (like in null workspace argument == global style, otherwise
lookup enforced on the argument workspace. Period).

TIA,
Gabriel

On Fri, Apr 13, 2012 at 6:19 PM, Justin Deoliveira <jdeolive@anonymised.com> wrote:

On Fri, Apr 13, 2012 at 4:27 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

On Fri, Apr 13, 2012 at 4:36 PM, Justin Deoliveira <jdeolive@anonymised.com>
wrote:
> Hey Gabriel,
>
> Yeah, I think it would make sense to check that the workspace is null.
> We
> could perhaps be lax and actually look first for a global style and if
> not
> found use one found within a workspace if there is only one. Does that
> make
> sense.
hmmm.. that last option would be confusing imho. Besides, I don't see
it's usage. In my opinion, for clarity and backwards compatible, it
should be make clear that getStyleByName(String) is a convenient
shortcut for getStyleByName(Workspace, String) with a null workspace
argument?

Works for me. I agree the most sensible approach.

>
> Also layer groups will have this same lookup issue.
Indeed.

Just found this fixing a side effect of
CatalogImplTest.testAddStyleWithNameConflict(). That test does a
number of checks on a style with a workspace, but the assigned
workspace is not added to the catalog (hence has no id), so the test
works with the default in memory catalog by accident. Fix is to just
call addWorkspace() besides addStyle(). Ok with that too?

Yup, i imagine there will be a couple of cases like these... feel free to
fix as you come across them.

>
> So yeah, go ahead and amend the lookup, we'll see what unit tests fail.
Will do.

Thanks for the prompt reply.
Gabriel
>
> -Justin
>
> On Fri, Apr 13, 2012 at 2:00 PM, Gabriel Roldan <groldan@anonymised.com>
> wrote:
>>
>> Hey Justin,
>>
>> just a question. Working on getting all the jdbc catalog tests to pass
>> after merging with the latest per workspace settings work, I'm not
>> sure whether the old implementation of
>> DefaultCatalogFacade.getStyleByName(String name) is still accurate.
>>
>> It is like this:
>> public StyleInfo getStyleByName(String name) {
>> for (Iterator s = styles.iterator(); s.hasNext():wink: {
>> StyleInfo style = (StyleInfo) s.next();
>> if (name.equals(style.getName())) {
>> return ModificationProxy.create(style, StyleInfo.class);
>> }
>> }
>> return null;
>> }
>>
>> Meaning that it'll return the first style with the given name,
>> regardless of it's workspace.
>> Is that the intended behaviour, or should it return only a style
>> that's "global".
>>
>> TIA,
>> Gabriel.
>>
>> --
>> Gabriel Roldan
>> OpenGeo - http://opengeo.org
>> Expert service straight from the developers.
>
>
>
>
> --
> Justin Deoliveira
> OpenGeo - http://opengeo.org
> Enterprise support for open source geospatial.
>

--
Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.

--
Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.

--
Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.

It may be a bit too lax… but that is the general idea. We did the same thing for layers so I kind of just followed that template. As you say the issue is null is kind of ambiguous… does it mean global or does it mean any workspace? I sort of opted to make it mean both since we don;t really expose the concept of an ANY workspace. And it only really takes affect in cases where only a single style exists which matches the name.

That said I am fine with making it stricter, as I can’t think of any use case that doesn’t involve going through a virtual service endpoint, and that which the right workspace will be forcibly set anyways. So to be clear you are advocating for removing the last part of the lookup heuristic? Have you tried doing so and see what happens in unit tests?

On Sun, Apr 15, 2012 at 4:44 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

The one thing that still bothers me with this API is that
CatalogImpl.getStyleByName(WorkspaceInfo, String) can return one of
four different things:

  • the requested style, given a non null workspace argument
  • a global style, given a null workspace argument
  • a style named the same but belonging to the default workspace, if
    none of the above was found.
  • if none of the above matches, returns a style from ANY workspace
    with that name

My interpretation is this makes style lookup by workspace almost
futil, but I may be missing the use cases that rely on this behavior.
Could you elaborate and tell whether we can make this method contract
stricter? (like in null workspace argument == global style, otherwise
lookup enforced on the argument workspace. Period).

TIA,
Gabriel

On Fri, Apr 13, 2012 at 6:19 PM, Justin Deoliveira <jdeolive@anonymised.com> wrote:

On Fri, Apr 13, 2012 at 4:27 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

On Fri, Apr 13, 2012 at 4:36 PM, Justin Deoliveira <jdeolive@anonymised.com>
wrote:

Hey Gabriel,

Yeah, I think it would make sense to check that the workspace is null.
We
could perhaps be lax and actually look first for a global style and if
not
found use one found within a workspace if there is only one. Does that
make
sense.
hmmm… that last option would be confusing imho. Besides, I don’t see
it’s usage. In my opinion, for clarity and backwards compatible, it
should be make clear that getStyleByName(String) is a convenient
shortcut for getStyleByName(Workspace, String) with a null workspace
argument?

Works for me. I agree the most sensible approach.

Also layer groups will have this same lookup issue.
Indeed.

Just found this fixing a side effect of
CatalogImplTest.testAddStyleWithNameConflict(). That test does a
number of checks on a style with a workspace, but the assigned
workspace is not added to the catalog (hence has no id), so the test
works with the default in memory catalog by accident. Fix is to just
call addWorkspace() besides addStyle(). Ok with that too?

Yup, i imagine there will be a couple of cases like these… feel free to
fix as you come across them.

So yeah, go ahead and amend the lookup, we’ll see what unit tests fail.
Will do.

Thanks for the prompt reply.
Gabriel

-Justin

On Fri, Apr 13, 2012 at 2:00 PM, Gabriel Roldan <groldan@anonymised.com>
wrote:

Hey Justin,

just a question. Working on getting all the jdbc catalog tests to pass
after merging with the latest per workspace settings work, I’m not
sure whether the old implementation of
DefaultCatalogFacade.getStyleByName(String name) is still accurate.

It is like this:
public StyleInfo getStyleByName(String name) {
for (Iterator s = styles.iterator(); s.hasNext():wink: {
StyleInfo style = (StyleInfo) s.next();
if (name.equals(style.getName())) {
return ModificationProxy.create(style, StyleInfo.class);
}
}
return null;
}

Meaning that it’ll return the first style with the given name,
regardless of it’s workspace.
Is that the intended behaviour, or should it return only a style
that’s “global”.

TIA,
Gabriel.


Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.


Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.


Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.


Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.


Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.


Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.

On Sun, Apr 15, 2012 at 5:55 PM, Justin Deoliveira <jdeolive@anonymised.com> wrote:

It may be a bit too lax... but that is the general idea. We did the same
thing for layers so I kind of just followed that template. As you say the
issue is null is kind of ambiguous... does it mean global or does it mean
any workspace? I sort of opted to make it mean both since we don;t really
expose the concept of an ANY workspace. And it only really takes affect in
cases where only a single style exists which matches the name.

That said I am fine with making it stricter, as I can't think of any use
case that doesn't involve going through a virtual service endpoint, and that
which the right workspace will be forcibly set anyways. So to be clear you
are advocating for removing the last part of the lookup heuristic? Have you
tried doing so and see what happens in unit tests?

Find attached the complete patch with the tests expectations that
needed to be changed.
I think the check for ANY_WORKSPACE in
DefaultCatalogFacade.getStyleByName(Workspace, String) should also be
removed. End goal being having less code that's never gonna be
executed, as you mentioned, because it is there without a use case
that hits it.

Cheers,
Gabriel

--
Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.

(attachments)

getStyleByName.patch (8.63 KB)

On Sun, Apr 15, 2012 at 5:12 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

On Sun, Apr 15, 2012 at 5:55 PM, Justin Deoliveira <jdeolive@anonymised.com> wrote:

It may be a bit too lax… but that is the general idea. We did the same
thing for layers so I kind of just followed that template. As you say the
issue is null is kind of ambiguous… does it mean global or does it mean
any workspace? I sort of opted to make it mean both since we don;t really
expose the concept of an ANY workspace. And it only really takes affect in
cases where only a single style exists which matches the name.

That said I am fine with making it stricter, as I can’t think of any use
case that doesn’t involve going through a virtual service endpoint, and that
which the right workspace will be forcibly set anyways. So to be clear you
are advocating for removing the last part of the lookup heuristic? Have you
tried doing so and see what happens in unit tests?

Find attached the complete patch with the tests expectations that
needed to be changed.
I think the check for ANY_WORKSPACE in
DefaultCatalogFacade.getStyleByName(Workspace, String) should also be
removed. End goal being having less code that’s never gonna be
executed, as you mentioned, because it is there without a use case
that hits it.

Well the fact that test assertions are being changed sort of means there is a use case :slight_smile:

I am always weary of changing this sort of stuff just for the purposes of code cleanup. Can you provide more information about how it makes your life easier to have this removed? Is it strictly just api clarity? Does it make implementing a new catalog easier?

Anyways, not trying to discourage just trying to be careful. As this is generally new code shouldn’t be a big deal though. Just want to do my due diligence :slight_smile:

Cheers,
Gabriel


Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.


Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.

On Sun, Apr 15, 2012 at 6:24 PM, Justin Deoliveira <jdeolive@anonymised.com> wrote:

On Sun, Apr 15, 2012 at 5:12 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

On Sun, Apr 15, 2012 at 5:55 PM, Justin Deoliveira <jdeolive@anonymised.com>
wrote:
> It may be a bit too lax... but that is the general idea. We did the same
> thing for layers so I kind of just followed that template. As you say
> the
> issue is null is kind of ambiguous... does it mean global or does it
> mean
> any workspace? I sort of opted to make it mean both since we don;t
> really
> expose the concept of an ANY workspace. And it only really takes affect
> in
> cases where only a single style exists which matches the name.
>
> That said I am fine with making it stricter, as I can't think of any use
> case that doesn't involve going through a virtual service endpoint, and
> that
> which the right workspace will be forcibly set anyways. So to be clear
> you
> are advocating for removing the last part of the lookup heuristic? Have
> you
> tried doing so and see what happens in unit tests?

Find attached the complete patch with the tests expectations that
needed to be changed.
I think the check for ANY_WORKSPACE in
DefaultCatalogFacade.getStyleByName(Workspace, String) should also be
removed. End goal being having less code that's never gonna be
executed, as you mentioned, because it is there without a use case
that hits it.

Well the fact that test assertions are being changed sort of means there is
a use case :slight_smile:

I am always weary of changing this sort of stuff just for the purposes of
code cleanup. Can you provide more information about how it makes your life
easier to have this removed? Is it strictly just api clarity? Does it make
implementing a new catalog easier?

Anyways, not trying to discourage just trying to be careful. As this is
generally new code shouldn't be a big deal though. Just want to do my due
diligence :slight_smile:

It does make my life easier implementing a new catalog. I'd need to
switch back to the other branch and revert the patch to recall why
exactly, as I'm on master and cherry-picked this one, which I can do.
But in any case, take the following as an example of why I think
enforcing a clear contract on new code is important: running the full
build with the stricter contract revealed the following bug: modifying
a style on a given workspace leads to modifying any style on any
workspace that's named the same. So far that REST config code seems
like the only user of such an expectation, and proved to be a bug (the
other methods in StyleResource do take care of getting the requested
workspace).
So I'm not trying to be an asshole here, and I do realize CatalogImpl
in fact prevents adding two styles with the same name, even if on
different workspaces. But that might change too (how long it's gonna
be until someone complains he can't have styles road and foo:road). In
any case my motivation to pursue this is not anticipating to that
request. It's having a clear contract to attach to when implementing
an alternate catalog facade. Having to put so many if's by looking at
what the default one does instead of having a contract to comply with
is painful, and so many control sentences also smell bad (for example,
as we go down from CatalogImpl to CatalogFacade, it'd be better if
CatalogFacade's method doesn't need to care about whether one input
argument is null, as CatalogImpl already did, which is the case for
ANY_ and NO_WORKSPACE, and that kind of stuff).

diff --git a/src/restconfig/src/main/java/org/geoserver/catalog/rest/StyleResource.java
b/src/restconfig/src/main/java/org/geoserver/catalog/rest/StyleResource.java
index ef6cdf7..ad3e80a 100644
--- a/src/restconfig/src/main/java/org/geoserver/catalog/rest/StyleResource.java
+++ b/src/restconfig/src/main/java/org/geoserver/catalog/rest/StyleResource.java
@@ -197,10 +197,11 @@ public class StyleResource extends
AbstractCatalogResource {
     @Override
     protected void handleObjectPut(Object object) throws Exception {
         String style = getAttribute("style");
-
+ String workspace = getAttribute("workspace");
+
         if ( object instanceof StyleInfo ) {
             StyleInfo s = (StyleInfo) object;
- StyleInfo original = catalog.getStyleByName( style );
+ StyleInfo original = catalog.getStyleByName( workspace, style );

             //ensure no workspace change
             if (s.getWorkspace() != null) {
@@ -218,7 +219,7 @@ public class StyleResource extends AbstractCatalogResource {
              * Force the .sld file to be overriden and it's Style
object cleared from the
              * ResourcePool cache
              */
- StyleInfo s = catalog.getStyleByName( style );
+ StyleInfo s = catalog.getStyleByName( workspace, style );
             catalog.getResourcePool().writeStyle( s, (Style) object, true );
             /*
              * make sure to save the StyleInfo so that the Catalog
issues the notification events

On Sun, Apr 15, 2012 at 6:43 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

Well the fact that test assertions are being changed sort of means there is
a use case :slight_smile:

There is in the test cases. I started asking what the "real world" use
case is. If we are "fixing" the method contract, it makes sense to fix
it's test expectations accordingly (the unit test is checking the
class does "things right", but does it do "the right thing"?). So
yeah, back to the original question, I am not sure if this is "fixing"
at all, cause I'm not sure if there's some higher level use case
depending on it? It looks to me like there isn't, but as didn't design
the API I can't be sure. If the current behavior _is_ needed I'm more
than willing to stop wasting everybody's time (and refrain from
argumenting the API keeps being confusing).

Cheers,
Gabriel.
--
Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.

Cool, thanks for the explanation, you make good points. So yeah, +1 on the change as i can;t think of any higher level use case and it does make the contract more understandable, and easier to implement.

On Sun, Apr 15, 2012 at 6:02 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

On Sun, Apr 15, 2012 at 6:43 PM, Gabriel Roldan <groldan@anonymised.com> wrote:

Well the fact that test assertions are being changed sort of means there is
a use case :slight_smile:

There is in the test cases. I started asking what the “real world” use
case is. If we are “fixing” the method contract, it makes sense to fix
it’s test expectations accordingly (the unit test is checking the
class does “things right”, but does it do “the right thing”?). So
yeah, back to the original question, I am not sure if this is “fixing”
at all, cause I’m not sure if there’s some higher level use case
depending on it? It looks to me like there isn’t, but as didn’t design
the API I can’t be sure. If the current behavior is needed I’m more
than willing to stop wasting everybody’s time (and refrain from
argumenting the API keeps being confusing).

Cheers,
Gabriel.


Gabriel Roldan
OpenGeo - http://opengeo.org
Expert service straight from the developers.


Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.