4. 적용

4.1 POM

4.1.1 ROOT pom.xml

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-couchbase</artifactId>
        <version>1.4.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>com.couchbase.client</groupId>
        <artifactId>java-client</artifactId>
        <version>2.0.2</version>
      </dependency>
    </dependencies>
<dependencyManagement>

<build>
  <pluginManagement>
    <pluginManagement>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>com.rsupport</groupId>
                    <artifactId>couchbase-maven-plugin</artifactId>
                    <versionRange>[1.0-SNAPSHOT,)</versionRange>
                    <goals>
                      <goal>create-bucket</goal>
                      <goal>delete-bucket</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
    </pluginManagement>
  </pluginManagement>
</build>

4.1.2 core pom.xml

  <dependencies>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-couchbase</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>com.rsupport</groupId>
        <artifactId>couchbase-maven-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
        <configuration>
          <host>http://${config.couchbase.host}:8091</host>
          <username>${config.couchbase.username}</username>
          <password>${config.couchbase.password}</password>
        </configuration>
        <executions>
          <execution>
            <id>delete-bucket</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>delete-bucket</goal>
            </goals>
            <configuration>
              <bucketName>${db.name}</bucketName>
              <failOnError>false</failOnError>
              <skip>${project.build.couchbase.skipDeleteBucket}</skip>
            </configuration>
          </execution>
          <execution>
            <id>create-bucket</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>create-bucket</goal>
            </goals>
            <configuration>
              <bucketName>${db.name}</bucketName>
              <bucketType>couchbase</bucketType>
              <authType>sasl</authType>
              <saslPassword>${db.password}</saslPassword>
              <ramQuotaMB>${config.coucbase.ramQuotaMB}</ramQuotaMB>
              <replicaNumber>0</replicaNumber>
              <proxyPort>11211</proxyPort>
              <skip>${project.build.couchbase.skipCreateBucket}</skip>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  <build>

4.2 Source

4.2.1 Create views

  • /src/main/resources/couchbase 폴더에 생성하면 build 시 생성됨
  • Design Document Name으로 폴더 생성

create views

  • map/reduce js 추가 : byProductCodeAndCreatedDate/map.js
    function (doc, meta) {
    if (doc._class == "com.rsupport.core.couchbase.domain.CrashLogDoc") {
      var dateArray = dateToArray(doc.createdDate).slice(0,4);
      emit([doc.productCode, dateArray[0], dateArray[1], dateArray[2], dateArray[3]], null);
    }
    }
    

created views

4.2.2 src/test/java

public class FirstRunTest extends AbstractTests {
  @Autowired
  private CouchbaseViewCreator couchbaseViewCreator;

  @Test
  public void testCreateView() {
    couchbaseViewCreator.createView();
  }
}

4.2.3 Document

@Document
public class CrashLogDoc extends AbstractDoc<String> {
  private static final long serialVersionUID = 6968629530375414549L;

  @Field
  private String productCode;
  @Field
  private String osVersion;
  @Field
  private String appVersion;
  @Field
  private String modelName;
  @Field
  private String description;

  public CrashLogDoc() {
    super(CouchbaseUtils.getDocId(CrashLogDoc.class));
    this.createdDate = new Date();
  }

  @Override
  public String getId() {
    return CouchbaseUtils.getId(CrashLogDoc.class, getDocId());
  }

  @Getter
  public enum View {
    ALL("all"),
    BY_PRODUCT_CODE_AND_CREATED_DATE("byProductCodeAndCreatedDate");
    private String name;
    View(String name) {
      this.name = name;
    }
  }
}

4.2.4 Repository

public interface CrashLogDocRepository extends CouchbaseDocRepository<CrashLogDoc>, CrashLogDocRepositoryCustom {

  @Override
  @View(designDocument="crashLogDoc", viewName="all")
  public Iterable<CrashLogDoc> findAll();

  @View(designDocument="crashLogDoc", viewName="all")
  public long count();

}
public class CrashLogDocRepositoryImpl implements CrashLogDocRepositoryCustom {

  @Inject
  private CouchbaseTemplate couchbaseTemplate;

  @Inject
  private CouchbaseClient couchbaseClient;

  @Override
  public List<CrashLogDoc> findByProductCodeAndCreatedDate(Pageable pageable, CrashLogDoc.Search criteria) {
    Query query = CouchbaseUtils.getPaginationQuery(pageable);
    setupCriteria(query, criteria);
    List<CrashLogDoc> docs = couchbaseTemplate.findByView("crashLogDoc",
        CrashLogDoc.View.BY_PRODUCT_CODE_AND_CREATED_DATE.getName(), query, CrashLogDoc.class);
    return docs;
  }

  private Query setupCriteria(Query query, CrashLogDoc.Search criteria) {
    int[] fromDateArray = CouchbaseUtils.dateToArray(criteria.getFromDate(), 4);
    int[] toDateArray = CouchbaseUtils.dateToArray(criteria.getToDate(), 4);
    ComplexKey startkey = ComplexKey.of(criteria.getProductCode(), fromDateArray[0], fromDateArray[1], fromDateArray[2], 0);
    ComplexKey endkey = ComplexKey.of(criteria.getProductCode(), toDateArray[0], toDateArray[1], toDateArray[2], 23);
    query.setDescending(true);
    query.setRange(endkey, startkey);
    return query;
  }

  @Override
  public long countByProductCodeAndCreatedDate(Search criteria) {
    Query query = new Query();
    setupCriteria(query, criteria);

    return CouchbaseUtils.getCount(couchbaseClient, query, "crashLogDoc",
        CrashLogDoc.View.BY_PRODUCT_CODE_AND_CREATED_DATE.getName());
  }
}