2 * ============LICENSE_START=======================================================
 
   4 * ================================================================================
 
   5 * Copyright 2019 China Mobile
 
   6 *=================================================================================
 
   7 * Licensed under the Apache License, Version 2.0 (the "License");
 
   8 * you may not use this file except in compliance with the License.
 
   9 * You may obtain a copy of the License at
 
  11 *     http://www.apache.org/licenses/LICENSE-2.0
 
  13 * Unless required by applicable law or agreed to in writing, software
 
  14 * distributed under the License is distributed on an "AS IS" BASIS,
 
  15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16 * See the License for the specific language governing permissions and
 
  17 * limitations under the License.
 
  18 * ============LICENSE_END=========================================================
 
  21 package org.onap.datalake.feeder.service;
 
  23 import java.util.ArrayList;
 
  24 import java.util.List;
 
  26 import javax.annotation.PostConstruct;
 
  27 import javax.annotation.PreDestroy;
 
  29 import org.json.JSONObject;
 
  30 import org.onap.datalake.feeder.config.ApplicationConfiguration;
 
  31 import org.onap.datalake.feeder.domain.Topic;
 
  32 import org.slf4j.Logger;
 
  33 import org.slf4j.LoggerFactory;
 
  35 import org.springframework.beans.factory.annotation.Autowired;
 
  36 import org.springframework.stereotype.Service;
 
  38 import com.couchbase.client.java.Bucket;
 
  39 import com.couchbase.client.java.Cluster;
 
  40 import com.couchbase.client.java.CouchbaseCluster;
 
  41 import com.couchbase.client.java.document.JsonDocument;
 
  42 import com.couchbase.client.java.document.JsonLongDocument;
 
  43 import com.couchbase.client.java.document.json.JsonObject; 
 
  46 import rx.functions.Func1;
 
  49  * Service to use Couchbase
 
  55 public class CouchbaseService {
 
  57         private final Logger log = LoggerFactory.getLogger(this.getClass());
 
  60         private ApplicationConfiguration config;
 
  66         // Initialize Couchbase Connection
 
  67         Cluster cluster = CouchbaseCluster.create(config.getCouchbaseHost());
 
  68         cluster.authenticate(config.getCouchbaseUser(), config.getCouchbasePass());
 
  69         bucket = cluster.openBucket(config.getCouchbaseBucket());
 
  71                 log.info("Connect to Couchbase " + config.getCouchbaseHost());
 
  73         // Create a N1QL Primary Index (but ignore if it exists)
 
  74         bucket.bucketManager().createN1qlPrimaryIndex(true, false);                 
 
  78         public void cleanUp() { 
 
  82         public void saveJsons(Topic topic, List<JSONObject> jsons) { 
 
  83                 List<JsonDocument> documents= new ArrayList<>(jsons.size());
 
  84                 for(JSONObject json : jsons) {
 
  85                         //convert to Couchbase JsonObject from org.json JSONObject
 
  86                         JsonObject jsonObject = JsonObject.fromJson(json.toString());   
 
  88                         long timestamp = jsonObject.getLong("_ts");//this is Kafka time stamp, which is added in StoreService.messageToJson()
 
  91                         int expiry = (int) (timestamp/1000L) + topic.getTtl()*3600*24; //in second
 
  93                         String id = getId(topic.getId());
 
  94                         JsonDocument doc = JsonDocument.create(id, expiry, jsonObject);
 
  97                 saveDocuments(documents);               
 
 101         private String getId(String topicStr) {
 
 102                 //String id = topicStr+":"+timestamp+":"+UUID.randomUUID();
 
 104                 //https://forums.couchbase.com/t/how-to-set-an-auto-increment-id/4892/2
 
 105                 //atomically get the next sequence number:
 
 106                 // increment by 1, initialize at 0 if counter doc not found
 
 107                 //TODO how slow is this compared with above UUID approach?
 
 108                 JsonLongDocument nextIdNumber = bucket.counter(topicStr, 1, 0); //like 12345 
 
 109                 String id = topicStr +":"+ nextIdNumber.content();
 
 114         //https://docs.couchbase.com/java-sdk/2.7/document-operations.html
 
 115         private void saveDocuments(List<JsonDocument> documents) { 
 
 118             .flatMap(new Func1<JsonDocument, Observable<JsonDocument>>() {
 
 120                 public Observable<JsonDocument> call(final JsonDocument docToInsert) {
 
 121                     return bucket.async().insert(docToInsert);