Gaffer icon indicating copy to clipboard operation
Gaffer copied to clipboard

Operation update for the handling of options map

Open GCHQDev404 opened this issue 3 years ago • 0 comments

Previously there have been bug in FederatedStore regarding options map being shared between operations/stores example see gh-2445. This was fixed with a deep clone of JSONSerialisation which is a little slower. In other locations throughout FederatedStore Map map = nonNull(op.getOptions()) ? op.getOptions : new HashMap is used. This is visually noisy.

The below proposed change would remove future similar bugs being implemented and clean up code within FederatedStore.

Add the following to the OperationTest class.

@Test
    public void shouldReturnClonedOptionsObject() {
        /*
         * could cause of some bugs
         */
        T testObject = getTestObject();
        testObject.addOption("foo", "bar");
        assertEquals(testObject.getOptions(), testObject.getOptions(), "operation isn`t returning consistent options");
        assertNotSame(testObject.getOptions(), testObject.getOptions(), "operation isn't returning cloned options");
    }
    @Test
    public void shouldAddClonedOptionsObject() {
        /*
         * could cause of some bugs
         */
        T testObject = getTestObject();
        HashMap<String, String> externalMap = new HashMap<>();
        externalMap.put("foo", "bar");
        testObject.setOptions(externalMap);
        externalMap.put("X", "Added via an external map");
        assertNull(testObject.getOption("X"), "Options was changed via an externally handled map");
        assertEquals("bar", testObject.getOption("foo"));
    }
    @Test
    public void shouldDeepCloneOptionsInShallowClone() {
        T testObject = getTestObject();
        testObject.addOption("foo", "barr");
        Map<String, String> testObjectOptions = testObject.getOptions();
        Map<String, String> clonedOptions = testObject.shallowClone().getOptions();
        assertNotNull(testObjectOptions);
        assertNotNull(clonedOptions);
        assertNotSame(testObjectOptions, clonedOptions);
        assertEquals(testObjectOptions, clonedOptions);
    }

Change the addOption method in abstract Operation class

default void addOption(final String name, final String value) {
        Map<String, String> options = getOptions();
        if (isNull(options)) {
            options = new HashMap<>();
        }
        options.put(name, value);
        setOptions(options);
    }

Then fix the NUMEROUS errors in all concrete operation classes.

GCHQDev404 avatar Jul 08 '21 11:07 GCHQDev404