Search in sources :

Example 1 with UpdateInfo

use of org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method newIndex.

@Override
public String newIndex(final String name) throws CalFacadeException {
    try {
        final String newName = name + newIndexSuffix();
        targetIndex = newName;
        final IndicesAdminClient idx = getAdminIdx();
        final CreateIndexRequestBuilder cirb = idx.prepareCreate(newName);
        final File f = new File(idxpars.getIndexerConfig());
        final byte[] sbBytes = Streams.copyToByteArray(f);
        cirb.setSource(sbBytes);
        final CreateIndexRequest cir = cirb.request();
        final ActionFuture<CreateIndexResponse> af = idx.create(cir);
        /*resp = */
        af.actionGet();
        index(new UpdateInfo());
        info("Index created: change token set to " + currentChangeToken());
        return newName;
    } catch (final ElasticsearchException ese) {
        // Failed somehow
        error(ese);
        return null;
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        error(t);
        throw new CalFacadeException(t);
    }
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) ElasticsearchException(org.elasticsearch.ElasticsearchException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) File(java.io.File) UpdateInfo(org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 2 with UpdateInfo

use of org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method markUpdated.

private void markUpdated(final String docType) throws CalFacadeException {
    UpdateInfo ui = updateInfo.get(targetIndex);
    if (ui == null) {
        currentChangeToken();
    }
    ui = updateInfo.get(targetIndex);
    if (ui == null) {
        throw new CalFacadeException("Unable to set updateInfo");
    }
    if ((docType == null) || docType.equals(docTypeUnknown)) {
        categories.clear();
        contacts.clear();
        locations.clear();
    } else if (docType.equals(docTypeCategory)) {
        categories.clear();
    } else if (docType.equals(docTypeContact)) {
        contacts.clear();
    } else if (docType.equals(docTypeLocation)) {
        locations.clear();
    }
    ui.setUpdate(true);
}
Also used : UpdateInfo(org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 3 with UpdateInfo

use of org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method currentChangeToken.

@Override
public String currentChangeToken() throws CalFacadeException {
    UpdateInfo ui;
    try {
        final GetRequestBuilder grb = getClient().prepareGet(targetIndex, docTypeUpdateTracker, updateTrackerId).setFields("count", "_timestamp");
        final GetResponse gr = grb.execute().actionGet();
        if (!gr.isExists()) {
            return null;
        }
        final EntityBuilder er = getEntityBuilder(gr.getFields());
        ui = er.makeUpdateInfo();
    } catch (final ElasticsearchException ese) {
        warn("Exception getting UpdateInfo: " + ese.getLocalizedMessage());
        ui = new UpdateInfo();
    }
    synchronized (updateInfo) {
        UpdateInfo tui = updateInfo.get(targetIndex);
        if ((tui != null) && (tui.getCount() >= (0111111111 - 1000))) {
            // Reset before we overflow
            tui = null;
        }
        if ((tui == null) || (!tui.getCount().equals(ui.getCount()))) {
            updateInfo.put(targetIndex, ui);
        } else {
            ui = tui;
        }
    }
    return ui.getChangeToken();
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) UpdateInfo(org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder)

Example 4 with UpdateInfo

use of org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method markTransaction.

@Override
public void markTransaction() throws CalFacadeException {
    final UpdateInfo ui = updateInfo.get(targetIndex);
    if ((ui != null) && !ui.isUpdate()) {
        return;
    }
    try {
        final UpdateRequestBuilder urb = getClient().prepareUpdate(targetIndex, docTypeUpdateTracker, updateTrackerId).setRetryOnConflict(20).setRefresh(true);
        urb.setScript("ctx._source.count += 1", ScriptService.ScriptType.INLINE);
        final UpdateResponse ur = urb.execute().actionGet();
    } catch (final ElasticsearchException ese) {
        warn("Exception updating UpdateInfo: " + ese.getLocalizedMessage());
        index(new UpdateInfo());
    }
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) UpdateInfo(org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo)

Aggregations

UpdateInfo (org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 File (java.io.File)1 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)1 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)1 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)1 GetRequestBuilder (org.elasticsearch.action.get.GetRequestBuilder)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 UpdateRequestBuilder (org.elasticsearch.action.update.UpdateRequestBuilder)1 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)1 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)1