001/* 002 * JKNIV, whinstone one contract to access your database. 003 * 004 * Copyright (C) 2017, the original author or authors. 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software Foundation, Inc., 018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 019 */ 020package net.sf.jkniv.whinstone.couchdb.commands; 021 022import java.util.List; 023import java.util.Map; 024 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028import net.sf.jkniv.sqlegance.RepositoryException; 029import net.sf.jkniv.sqlegance.Sql; 030import net.sf.jkniv.sqlegance.SqlContext; 031import net.sf.jkniv.whinstone.couchdb.HttpBuilder; 032 033/** 034 * 035 * https://docs.couchdb.org/en/stable/api/database/find.html#db-index 036 * 037 * Mango is a declarative JSON querying language for CouchDB databases. 038 * Mango wraps several index types, starting with the Primary Index out-of-the-box. 039 * Mango indexes, with index type json, are built using MapReduce Views. 040 * 041 * @author Alisson Gomes 042 * @since 0.6.6 043 */ 044public class CouchDbSynchIndexDesign 045{ 046 private final static Logger LOG = LoggerFactory.getLogger(CouchDbSynchIndexDesign.class); 047 private final SqlContext sqlContext; 048 private final HttpBuilder httpBuilder; 049 050 public CouchDbSynchIndexDesign(final HttpBuilder httpBuilder, final SqlContext sqlContext) 051 { 052 this.sqlContext = sqlContext; 053 this.httpBuilder = httpBuilder; 054 } 055 056 public void update() 057 { 058 Map<String, List<Sql>> queries = sqlContext.getPackageStartWith("_design"); 059 int indexes = 0; 060 for (String packet : queries.keySet()) 061 { 062 List<Sql> sqls = queries.get(packet); 063 for (Sql sql : sqls) 064 { 065 String name = null; 066 boolean map = true; 067 if (sql.getName().startsWith("map#") || sql.getName().startsWith("reduce#")) 068 continue; 069 070 else if(!sql.getName().startsWith("index#")) 071 throw new RepositoryException("Cannot build " + packet 072 + " view from docs. The queries must be start with 'map#' | 'reduce#' | 'index#' name"); 073 074 075 name = sql.getName().substring(6);// index# 076 077 DropIndexCommand drop = new DropIndexCommand(httpBuilder, "_design", name); 078 drop.execute(); 079 CreateIndexCommand create = new CreateIndexCommand(httpBuilder, sql.getSql()); 080 create.execute(); 081 indexes++; 082 } 083 } 084 if (indexes > 0) 085 LOG.info("Host [{}] had created {} indexes", httpBuilder.getHostContext(), indexes); 086 } 087}