From reviews-return-901-archive-asf-public=cust-asf.ponee.io@bahir.apache.org Fri Jan 5 15:50:01 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id 0E8D5180784 for ; Fri, 5 Jan 2018 15:50:01 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id F371E160C27; Fri, 5 Jan 2018 14:50:00 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E6D04160C40 for ; Fri, 5 Jan 2018 15:49:59 +0100 (CET) Received: (qmail 50086 invoked by uid 500); 5 Jan 2018 14:49:59 -0000 Mailing-List: contact reviews-help@bahir.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: reviews@bahir.apache.org Delivered-To: mailing list reviews@bahir.apache.org Received: (qmail 49660 invoked by uid 99); 5 Jan 2018 14:49:58 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 05 Jan 2018 14:49:58 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id AAAA7E0341; Fri, 5 Jan 2018 14:49:58 +0000 (UTC) From: ricellis To: reviews@bahir.apache.org Reply-To: reviews@bahir.apache.org References: In-Reply-To: Subject: [GitHub] bahir pull request #61: [BAHIR-154] Refactor sql-cloudant to use Cloudant's ... Content-Type: text/plain Message-Id: <20180105144958.AAAA7E0341@git1-us-west.apache.org> Date: Fri, 5 Jan 2018 14:49:58 +0000 (UTC) Github user ricellis commented on a diff in the pull request: https://github.com/apache/bahir/pull/61#discussion_r159845874 --- Diff: sql-cloudant/src/main/scala/org/apache/bahir/cloudant/CloudantConfig.scala --- @@ -16,34 +16,127 @@ */ package org.apache.bahir.cloudant -import java.net.URLEncoder +import java.net.{URL, URLEncoder} -import play.api.libs.json.{JsArray, JsObject, Json, JsValue} +import scala.collection.JavaConverters._ +import scala.collection.mutable +import scala.reflect.io.File + +import com.cloudant.client.api.{ClientBuilder, CloudantClient, Database} +import com.cloudant.client.api.model.SearchResult +import com.cloudant.client.api.views._ +import com.cloudant.http.{Http, HttpConnection} +import com.cloudant.http.interceptors.Replay429Interceptor +import com.google.gson.{JsonObject, JsonParser} import org.apache.bahir.cloudant.common._ +import org.apache.bahir.cloudant.common.JsonUtil.JsonConverter /* * Only allow one field pushdown now * as the filter today does not tell how to link the filters out And v.s. Or */ class CloudantConfig(val protocol: String, val host: String, - val dbName: String, val indexName: String, val viewName: String) + val dbName: String, val indexPath: String, val viewPath: String) (implicit val username: String, val password: String, val partitions: Int, val maxInPartition: Int, val minInPartition: Int, val requestTimeout: Long, val bulkSize: Int, val schemaSampleSize: Int, val createDBOnSave: Boolean, val endpoint: String, val useQuery: Boolean = false, val queryLimit: Int) extends Serializable { + @transient private lazy val client: CloudantClient = ClientBuilder + .url(getClientUrl) + .username(username) + .password(password) + .interceptors(Replay429Interceptor.WITH_DEFAULTS) + .build + @transient private lazy val database: Database = client.database(dbName, false) lazy val dbUrl: String = {protocol + "://" + host + "/" + dbName} + lazy val designDoc: String = { + if (viewPath != null && viewPath.nonEmpty) { + viewPath.split("/")(1) + } else { + null + } + } + lazy val searchName: String = { + // verify that the index path matches '_design/ddoc/_search/searchname' + if (indexPath != null && indexPath.nonEmpty && indexPath.matches("\\w+\\/\\w+\\/\\w+\\/\\w+")) { + val splitPath = indexPath.split(File.separator) + // return 'design-doc/search-name' + splitPath(1) + File.separator + splitPath(3) + } else { + null + } + } + lazy val viewName: String = { + if (viewPath != null && viewPath.nonEmpty) { + val splitViewPath = viewPath.split(File.separator) + if(splitViewPath(3).contains("?")) { + splitViewPath(3).substring(0, splitViewPath(3).indexOf("?")) + } else { + splitViewPath(3) + } + } else { + null + } + } val pkField = "_id" val defaultIndex: String = endpoint val default_filter: String = "*:*" - def getDbUrl: String = { - dbUrl + def buildAllDocsRequest(limit: Int, includeDocs: Boolean = true): AllDocsRequestBuilder = { + var allDocsReq = database.getAllDocsRequestBuilder.includeDocs(includeDocs) + if (limit != JsonStoreConfigManager.ALLDOCS_OR_CHANGES_LIMIT) { + allDocsReq = allDocsReq.limit(limit) + } + allDocsReq + } + + def buildViewRequest(limit: Int, includeDocs: Boolean = true): + UnpaginatedRequestBuilder[String, String] = { + val viewReq = database.getViewRequestBuilder(designDoc, viewName) + .newRequest(Key.Type.STRING, classOf[String]) + .includeDocs(includeDocs) + if (limit != JsonStoreConfigManager.ALLDOCS_OR_CHANGES_LIMIT) { + viewReq.limit(limit) + } + viewReq + } + + def buildSearchRequest(limit: Int): SearchResult[JsonObject] = { + val searchReq = database.search(searchName) + if (limit != JsonStoreConfigManager.ALLDOCS_OR_CHANGES_LIMIT) { + searchReq.limit(limit) + } + searchReq.querySearchResult(default_filter, classOf[JsonObject]) + } + + def executeRequest(stringUrl: String, postData: String = null): HttpConnection = { + val url = new URL(stringUrl) + if(postData != null) { + val conn = Http.POST(url, "application/json") + conn.setRequestBody(postData) + conn.requestProperties.put("Accept", "application/json") + conn.requestProperties.put("User-Agent", "spark-cloudant") + client.executeRequest(conn) + } else { + val conn = Http.GET(url) + conn.requestProperties.put("Accept", "application/json") + conn.requestProperties.put("User-Agent", "spark-cloudant") --- End diff -- As above ---