简体   繁体   中英

Performance tune code in O (n^2) to be more performant

I have this spring boot java controller having code that utilizes the OpenKM document management API to search the document management system for documents and display results using Ajax, HTML, CSS and Jquery datatables on the front-end.

Due to the way the API was written, I cannot get a document object with its metadata in one call but will need to use an output of the first API operation's call as a filter for another API operation method in two nested for loops.

Additionally, I had to iterate the toString method of an API return object to retrieve the metadata information, as they were not accessible through the return object's properties.

The problem is the performance of this code. I would like to see if there is a way to optimize this code.

    // Read the property or metadata to use in constituting the StoredDocument object
    for (QueryResult queryResult : resultSet.getResults()) {
        // Create a locally-scoped List<String>
        List<String> listOfStoredDocumentProperties = new ArrayList<String>();
        Document document = queryResult.getDocument();
        String nodeId = document.getPath();
        // Populate storedDocument object
        storedDocument = new StoredDocument();
        storedDocument.setAuthor(document.getAuthor());
        storedDocument.setCreated(document.getCreated());
        storedDocument.setLastModified(document.getLastModified());
        storedDocument.setPath(document.getPath());
        storedDocument.setPermissions(document.getPermissions());
        storedDocument.setSize(document.getActualVersion().getSize());
        storedDocument.setUuid(document.getUuid());
        storedDocument.setVersionNumber(document.getActualVersion().getName());
        // System.out.println(nodeId);
        try {
            listOfFormElement = okm.getPropertyGroupProperties(nodeId, documentVo.getGroupId());
            int counterForTrackingDocDirectionPos = 0;
            for (FormElement formElement : listOfFormElement) {
                ++counterForTrackingDocDirectionPos;
                if (counterForTrackingDocDirectionPos == 4) {
                    String formElementString = formElement.toString();
                    // System.out.println("formElementString: " + formElementString);
                    System.out.println("name: " + formElement.getName());
                    System.out.println("formElement: " + formElement);
                    String transformedFormElementString = StringUtils.EMPTY;
                    try {
                        transformedFormElementString = formElementString.substring(0, formElementString.indexOf(", selected=true"));
                        // Read the string from a position that is 3 steps before the last position in the string.
                        transformedFormElementString = transformedFormElementString
                                .substring(transformedFormElementString.length() - 3, transformedFormElementString.length()).trim();
                        transformedFormElementString = transformedFormElementString.startsWith("=")
                                ? transformedFormElementString.substring(1, transformedFormElementString.length()) : transformedFormElementString;
                    } catch (Exception ex) {
                        // To catch scenario where formElementString.indexOf(", selected=true") does not find the
                        // specified string. This happens when document direction is not set and therefore is
                        // selected=false for both the options IN and OUT.
                        transformedFormElementString = "NOT SET";
                    }
                    listOfStoredDocumentProperties.add(transformedFormElementString);
                    System.out.println("transformedFormElementString: " + transformedFormElementString);
                } else {
                    String formElementString = formElement.toString();
                    String transformedFormElementString = formElementString.substring(formElementString.indexOf("value="),
                            formElementString.indexOf("data="));
                    // Remove the preceding 'value=' and the last 2 character-constituted string ", "
                    transformedFormElementString = transformedFormElementString.substring(6, transformedFormElementString.length() - 2).trim();
                    listOfStoredDocumentProperties.add(transformedFormElementString);
                }
            }
            storedDocument.setCompanyName(listOfStoredDocumentProperties.get(0));
            storedDocument.setProductLine(listOfStoredDocumentProperties.get(1));
            storedDocument.setSubjectHeading(listOfStoredDocumentProperties.get(2));
            storedDocument.setDocumentDirection(listOfStoredDocumentProperties.get(3));
            storedDocument.setDocumentType(listOfStoredDocumentProperties.get(4));
            storedDocument.setReferenceNumber(listOfStoredDocumentProperties.get(5));
            storedDocument.setDate(ISO8601.parseBasic(listOfStoredDocumentProperties.get(6)).getTime().toString());
            // Add the storedDocument object to the return list
            listOfstoredDocuments.add(storedDocument);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchGroupException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (PathNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DatabaseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknowException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WebserviceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

The solution for it is extending the REST API. In the professional edition, the REST API is extensible with plugins architecture https://docs.openkm.com/kcenter/view/okm-6.4/creating-your-own-rest-plugin-(-extending-rest-api-).html , in the community this option still is not present. The idea is to build a method from server side what provide the exact data what really you need, creating high-level methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM