001package com.avaje.ebean.config;
002
003import com.avaje.ebean.annotation.DocStoreMode;
004
005/**
006 * Configuration for the Document store integration (e.g. ElasticSearch).
007 */
008public class DocStoreConfig {
009
010  /**
011   * True when the Document store integration is active/on.
012   */
013  protected boolean active;
014
015  /**
016   * Set to true means Ebean will generate mapping files on startup.
017   */
018  protected boolean generateMapping;
019
020  /**
021   * When true the Document store should drop and re-create document indexes.
022   */
023  protected boolean dropCreate;
024
025  /**
026   * When true the Document store should create any document indexes that don't already exist.
027   */
028  protected boolean create;
029
030  /**
031   * The URL of the Document store server. For example: http://localhost:9200.
032   */
033  protected String url;
034
035  /**
036   * The default mode used by indexes.
037   */
038  protected DocStoreMode persist = DocStoreMode.UPDATE;
039
040  /**
041   * The default batch size to use for the Bulk API calls.
042   */
043  protected int bulkBatchSize = 1000;
044
045  /**
046   * Resource path for the Document store mapping files.
047   */
048  protected String mappingPath;
049
050  /**
051   * Suffix used for mapping files.
052   */
053  protected String mappingSuffix;
054
055  /**
056   * Location of resources that mapping files are generated into.
057   */
058  protected String pathToResources = "src/main/resources";
059
060  /**
061   * Return true if the Document store (ElasticSearch) integration is active.
062   */
063  public boolean isActive() {
064    String systemValue = System.getProperty("ebean.docstore.active");
065    if (systemValue != null) {
066      return Boolean.parseBoolean(systemValue);
067    }
068    return active;
069  }
070
071  /**
072   * Set to true to make the Document store (ElasticSearch) integration active.
073   */
074  public void setActive(boolean active) {
075    this.active = active;
076  }
077
078  /**
079   * Return the URL to the Document store.
080   */
081  public String getUrl() {
082    String systemValue = System.getProperty("ebean.docstore.url");
083    if (systemValue != null) {
084      return systemValue;
085    }
086    return url;
087  }
088
089  /**
090   * Set the URL to the Document store server.
091   * <p>
092   * For a local ElasticSearch server this would be: http://localhost:9200
093   */
094  public void setUrl(String url) {
095    this.url = url;
096  }
097
098  /**
099   * Return true if Ebean should generate mapping files on server startup.
100   */
101  public boolean isGenerateMapping() {
102    String systemValue = System.getProperty("ebean.docstore.generateMapping");
103    if (systemValue != null) {
104      return Boolean.parseBoolean(systemValue);
105    }
106    return generateMapping;
107  }
108
109  /**
110   * Set to true if Ebean should generate mapping files on server startup.
111   */
112  public void setGenerateMapping(boolean generateMapping) {
113    this.generateMapping = generateMapping;
114  }
115
116  /**
117   * Return true if the document store should recreate mapped indexes.
118   */
119  public boolean isDropCreate() {
120    String systemValue = System.getProperty("ebean.docstore.dropCreate");
121    if (systemValue != null) {
122      return Boolean.parseBoolean(systemValue);
123    }
124    return dropCreate;
125  }
126
127  /**
128   * Set to true if the document store should recreate mapped indexes.
129   */
130  public void setDropCreate(boolean dropCreate) {
131    this.dropCreate = dropCreate;
132  }
133
134  /**
135   * Create true if the document store should create mapped indexes that don't yet exist.
136   * This is only used if dropCreate is false.
137   */
138  public boolean isCreate() {
139    String systemValue = System.getProperty("ebean.docstore.create");
140    if (systemValue != null) {
141      return Boolean.parseBoolean(systemValue);
142    }
143    return create;
144  }
145
146  /**
147   * Set to true if the document store should create mapped indexes that don't yet exist.
148   * This is only used if dropCreate is false.
149   */
150  public void setCreate(boolean create) {
151    this.create = create;
152  }
153
154  /**
155   * Return the default batch size to use for calls to the Bulk API.
156   */
157  public int getBulkBatchSize() {
158    return bulkBatchSize;
159  }
160
161  /**
162   * Set the default batch size to use for calls to the Bulk API.
163   * <p>
164   * The batch size can be set on a transaction via {@link com.avaje.ebean.Transaction#setDocStoreBatchSize(int)}.
165   * </p>
166   */
167  public void setBulkBatchSize(int bulkBatchSize) {
168    this.bulkBatchSize = bulkBatchSize;
169  }
170
171  /**
172   * Return the mapping path.
173   */
174  public String getMappingPath() {
175    return mappingPath;
176  }
177
178  /**
179   * Set the mapping path.
180   */
181  public void setMappingPath(String mappingPath) {
182    this.mappingPath = mappingPath;
183  }
184
185  /**
186   * Return the mapping suffix.
187   */
188  public String getMappingSuffix() {
189    return mappingSuffix;
190  }
191
192  /**
193   * Set the mapping suffix.
194   */
195  public void setMappingSuffix(String mappingSuffix) {
196    this.mappingSuffix = mappingSuffix;
197  }
198
199  /**
200   * Return the relative file system path to resources when generating mapping files.
201   */
202  public String getPathToResources() {
203    return pathToResources;
204  }
205
206  /**
207   * Set the relative file system path to resources when generating mapping files.
208   */
209  public void setPathToResources(String pathToResources) {
210    this.pathToResources = pathToResources;
211  }
212
213  /**
214   * Return the default behavior for when Insert, Update and Delete events occur on beans that have an associated
215   * Document store.
216   */
217  public DocStoreMode getPersist() {
218    return persist;
219  }
220
221  /**
222   * Set the default behavior for when Insert, Update and Delete events occur on beans that have an associated
223   * Document store.
224   * <ul>
225   * <li>DocStoreEvent.UPDATE - build and send message to Bulk API</li>
226   * <li>DocStoreEvent.QUEUE - add an entry with the index type and id only into a queue for later processing</li>
227   * <li>DocStoreEvent.IGNORE - ignore. Most likely used when some scheduled batch job handles updating the index</li>
228   * </ul>
229   * <p>
230   * You might choose to use QUEUE if that particular index data is updating very frequently or the cost of indexing
231   * is expensive.  Setting it to QUEUE can mean many changes can be batched together potentially coalescing multiple
232   * updates for an index entry into a single update.
233   * </p>
234   * <p>
235   * You might choose to use IGNORE when you have your own external process for updating the indexes. In this case
236   * you don't want Ebean to do anything when the data changes.
237   * </p>
238   */
239  public void setPersist(DocStoreMode persist) {
240    this.persist = persist;
241  }
242
243  /**
244   * Load settings specified in properties files.
245   */
246  public void loadSettings(PropertiesWrapper properties) {
247
248    active = properties.getBoolean("docstore.active", active);
249    url = properties.get("docstore.url", url);
250    persist = properties.getEnum(DocStoreMode.class, "docstore.persist", persist);
251    bulkBatchSize = properties.getInt("docstore.bulkBatchSize", bulkBatchSize);
252    generateMapping = properties.getBoolean("docstore.generateMapping", generateMapping);
253    dropCreate = properties.getBoolean("docstore.dropCreate", dropCreate);
254    create = properties.getBoolean("docstore.create", create);
255    mappingPath = properties.get("docstore.mappingPath", mappingPath);
256    mappingSuffix = properties.get("docstore.mappingSuffix", mappingSuffix);
257    pathToResources = properties.get("docstore.pathToResources", pathToResources);
258  }
259}