001/* 002 * (C) Copyright 2019 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019package org.nuxeo.ecm.core.transientstore.computation; 020 021import java.io.IOException; 022import java.util.Collections; 023import java.util.List; 024import java.util.Map; 025 026import org.apache.logging.log4j.LogManager; 027import org.apache.logging.log4j.Logger; 028import org.nuxeo.ecm.core.api.Blob; 029import org.nuxeo.ecm.core.api.impl.blob.ByteArrayBlob; 030import org.nuxeo.ecm.core.transientstore.api.TransientStore; 031import org.nuxeo.ecm.core.transientstore.api.TransientStoreService; 032import org.nuxeo.ecm.core.work.BaseOverflowRecordFilter; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * Filter that use a Transient Store to pass big record value. The TTL needs to be configured at the TransientStore 037 * level. 038 * 039 * @since 11.1 040 */ 041public class TransientStoreOverflowRecordFilter extends BaseOverflowRecordFilter { 042 private static final Logger log = LogManager.getLogger(TransientStoreOverflowRecordFilter.class); 043 044 @Override 045 public void init(Map<String, String> options) { 046 super.init(options); 047 // check for ts availability 048 getTransientStore(); 049 } 050 051 protected TransientStore getTransientStore() { 052 return Framework.getService(TransientStoreService.class).getStore(getStoreName()); 053 } 054 055 @Override 056 protected void storeValue(String recordKey, byte[] data) { 057 String key = getPrefixedKey(recordKey); 058 Blob blob = new ByteArrayBlob(data); 059 TransientStore store = getTransientStore(); 060 store.putBlobs(key, Collections.singletonList(blob)); 061 store.setCompleted(key, true); 062 063 } 064 065 @Override 066 protected byte[] fetchValue(String recordKey) { 067 String key = getPrefixedKey(recordKey); 068 List<Blob> blobs = getTransientStore().getBlobs(key); 069 Blob blob = blobs == null || blobs.isEmpty() ? null : blobs.get(0); 070 if (blob == null) { 071 log.error("Blob value not found for record: {}", recordKey); 072 return null; 073 } 074 try { 075 return blob.getByteArray(); 076 } catch (IOException e) { 077 log.error("Cannot get bytes of blob value for record: {}", recordKey, e); 078 return null; 079 } 080 } 081 082}