1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package org.opengrok.suggest.popular.impl.chronicle; 24 25 import net.openhft.chronicle.bytes.HeapBytesStore; 26 import net.openhft.chronicle.bytes.RandomDataInput; 27 import net.openhft.chronicle.hash.AbstractData; 28 import net.openhft.chronicle.hash.Data; 29 import net.openhft.chronicle.hash.serialization.DataAccess; 30 import net.openhft.chronicle.wire.WireIn; 31 import net.openhft.chronicle.wire.WireOut; 32 import org.apache.lucene.util.BytesRef; 33 import org.jetbrains.annotations.NotNull; 34 import org.jetbrains.annotations.Nullable; 35 36 /** 37 * {@link BytesRef} data serializer for {@link net.openhft.chronicle.map.ChronicleMap}. 38 * Modified from https://github.com/OpenHFT/Chronicle-Map/blob/master/docs/CM_Tutorial_DataAccess.adoc 39 */ 40 @SuppressWarnings("deprecation") 41 public class BytesRefDataAccess extends AbstractData<BytesRef> implements DataAccess<BytesRef> { 42 43 /** Cache field. */ 44 private transient HeapBytesStore<byte[]> bs; 45 46 /** State field. */ 47 private transient byte[] array; 48 BytesRefDataAccess()49 public BytesRefDataAccess() { 50 initTransients(); 51 } 52 initTransients()53 private void initTransients() { 54 bs = null; 55 } 56 57 @Override bytes()58 public RandomDataInput bytes() { 59 return bs; 60 } 61 62 @Override offset()63 public long offset() { 64 return bs.start(); 65 } 66 67 @Override size()68 public long size() { 69 return bs.capacity(); 70 } 71 72 @Override get()73 public BytesRef get() { 74 return new BytesRef(array); 75 } 76 77 @Override getUsing(@ullable BytesRef using)78 public BytesRef getUsing(@Nullable BytesRef using) { 79 if (using == null) { 80 using = new BytesRef(new byte[array.length]); 81 } else if (using.bytes.length < array.length) { 82 using.bytes = new byte[array.length]; 83 } 84 System.arraycopy(array, 0, using.bytes, 0, array.length); 85 using.offset = 0; 86 using.length = array.length; 87 return using; 88 } 89 90 @Override getData(@otNull BytesRef instance)91 public Data<BytesRef> getData(@NotNull BytesRef instance) { 92 if (instance.bytes.length == instance.length) { 93 array = instance.bytes; 94 } else { 95 array = new byte[instance.length]; 96 System.arraycopy(instance.bytes, instance.offset, array, 0, instance.length); 97 } 98 bs = HeapBytesStore.wrap(array); 99 return this; 100 } 101 102 @Override uninit()103 public void uninit() { 104 array = null; 105 bs = null; 106 } 107 108 @Override copy()109 public DataAccess<BytesRef> copy() { 110 return new BytesRefDataAccess(); 111 } 112 113 @Override writeMarshallable(@otNull WireOut wireOut)114 public void writeMarshallable(@NotNull WireOut wireOut) { 115 // no fields to write 116 } 117 118 @Override readMarshallable(@otNull WireIn wireIn)119 public void readMarshallable(@NotNull WireIn wireIn) { 120 // no fields to read 121 initTransients(); 122 } 123 124 } 125