All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javanut.pronghorn.util.TrieCollector Maven / Gradle / Ivy

Go to download

Ring buffer based queuing utility for applications that require high performance and/or a small footprint. Well suited for embedded and stream based processing.

There is a newer version: 1.1.27
Show newest version
package com.javanut.pronghorn.util;

import com.javanut.pronghorn.pipe.Pipe;
import com.javanut.pronghorn.pipe.PipeConfig;
import com.javanut.pronghorn.pipe.RawDataSchema;

public class TrieCollector {

    private final TrieParser trie;
    private final TrieParserReader reader;
    private int squenceCount = 0;
    
    private final static int MAX_TEXT_LENGTH = 1024;
    private final Pipe pipe = new Pipe(new PipeConfig(RawDataSchema.instance,3,MAX_TEXT_LENGTH));
    private final int rawChunkSize = Pipe.sizeOf(pipe, RawDataSchema.MSG_CHUNKEDSTREAM_1);
    
    public TrieCollector(int size) {
        trie = new TrieParser(size, 2, false, false);
        reader = new TrieParserReader();
        pipe.initBuffers();        
    }
    
    public int valueOf(byte[] data, int offset, int length, int mask) {        
        int value = (int)TrieParserReader.query(reader, trie, data, offset, length, mask, -1);
        if (value>0) {
            return value;
        } else {
            trie.setValue(data, offset, length, mask, ++squenceCount);
            return squenceCount;
        } 
    }
    
    public int valueOfUTF8(CharSequence cs) {
        
        pipe.reset();
        Pipe.addMsgIdx(pipe, RawDataSchema.MSG_CHUNKEDSTREAM_1);
        
        int origPos = Pipe.getWorkingBlobHeadPosition(pipe);
        int len = Pipe.copyUTF8ToByte(cs, 0, cs.length(), pipe);
        Pipe.addBytePosAndLen(pipe, origPos, len);        
        Pipe.publishWrites(pipe);
        Pipe.confirmLowLevelWrite(pipe, rawChunkSize);
        
        Pipe.takeMsgIdx(pipe);
        Pipe.confirmLowLevelRead(pipe, rawChunkSize);
       
        int meta = Pipe.takeByteArrayMetaData(pipe);
        int result = valueOf(Pipe.byteBackingArray(meta, pipe),Pipe.bytePosition(meta, pipe, Pipe.takeByteArrayLength(pipe)),Pipe.takeByteArrayLength(pipe),Pipe.blobMask(pipe));
        
        Pipe.releaseReadLock(pipe);
        return result;
    }
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy