001/* 002 * (C) Copyright 2018 Nuxeo (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 * Kevin Leturc <kleturc@nuxeo.com> 018 */ 019package org.nuxeo.ecm.core.bulk; 020 021import static java.nio.charset.StandardCharsets.UTF_8; 022 023import java.util.Arrays; 024import java.util.List; 025 026import org.nuxeo.lib.stream.computation.Record; 027 028/** 029 * Helper class to handle {@link Record} in bulk services. 030 * 031 * @since 10.2 032 */ 033public class BulkRecords { 034 035 protected static final String KEY_SEPARATOR = ":"; 036 037 protected static final String VALUE_SEPARATOR = "_"; 038 039 private BulkRecords() { 040 // no instance allowed 041 } 042 043 /** 044 * @return a new {@link Record} containing document ids respecting bulk format 045 */ 046 public static Record of(String commandId, long currentCount, List<String> documentIds) { 047 String key = commandId + KEY_SEPARATOR + currentCount; 048 String value = String.join(VALUE_SEPARATOR, documentIds); 049 return Record.of(key, value.getBytes(UTF_8)); 050 } 051 052 /** 053 * @return the command id extracted from {@link Record} 054 */ 055 public static String commandIdFrom(Record record) { 056 String key = record.getKey(); 057 return key.split(KEY_SEPARATOR)[0]; 058 } 059 060 public static List<String> docIdsFrom(Record record) { 061 String value = new String(record.getData(), UTF_8); 062 return Arrays.asList(value.split(VALUE_SEPARATOR)); 063 } 064 065}