簡體   English   中英

O(n ^ 2)中的性能調整代碼更具性能

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

我有一個帶有代碼的Spring Boot Java控制器,該控制器利用OpenKM文檔管理API在文檔管理系統中搜索文檔並在前端使用Ajax,HTML,CSS和Jquery數據表顯示結果。

由於API的編寫方式,我無法在一個調用中獲取帶有元數據的文檔對象,但需要在兩個嵌套的for循環中將第一個API操作的調用輸出用作另一個API操作方法的過濾器。

另外,我必須迭代API返回對象的toString方法來檢索元數據信息,因為元數據信息無法通過返回對象的屬性進行訪問。

問題是此代碼的性能。 我想看看是否有一種方法可以優化此代碼。

    // 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();
        }
    }

解決方案是擴展REST API。 在專業版中,REST API可通過插件體系結構https://docs.openkm.com/kcenter/view/okm-6.4/creating-your-own-rest-plugin-(-extending-rest-api-)進行擴展。 .html ,在社區中此選項仍然不存在。 這個想法是從服務器端構建一種方法,該方法提供真正需要的確切數據,從而創建高級方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM