
com.fluxtion.runtime.partition.Partitioner Maven / Gradle / Ivy
/*
* Copyright (C) 2018 2024 gregory higgins.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package com.fluxtion.runtime.partition;
import com.fluxtion.runtime.StaticEventProcessor;
import com.fluxtion.runtime.lifecycle.BatchHandler;
import com.fluxtion.runtime.lifecycle.Lifecycle;
import com.fluxtion.runtime.partition.LambdaReflection.SerializableFunction;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* An StaticEventProcessor partitioner based upon a received event.A partitioner
* creates an instance of an {@link StaticEventProcessor} and dispatches events
* to that instance. Partitioning allows a separate memory context for an
* EventHandler, this can be useful when the structure of processing is repeated
* but the state is different for each instance.
*
* For example monitoring the fuel level on a fleet of cars is the same
* processing for each car, but an individual car will have a unique fuel level.
* In this case the StaticEventProcessor can be partitioned on vehicle
* identification number.
*
*
* The StaticEventProcessor instance will be re-used or a new one created when
* new events are received. The {@link #partition(SerializableFunction)} methods provide
* functions that map keys from an incoming event. the key is used
* manage StaticEventProcessor instances in an underlying map. If no key/value
* mapping is found then a new StaticEventProcessor is created and handles the
* incoming message.
*
*
* New instances are created with s {@link Supplier} factory. Optionally an
* initialiser can be provided that can access the newly created
* StaticEventProcessor before any messages are processed. Using the car/fuel
* analogy the initialiser function may set a reference to a global fuel monitor
* from each newly created car processor.
*
* @param
* @author gregp
*/
public class Partitioner implements StaticEventProcessor, Lifecycle, BatchHandler {
private HashMap class2Function;
private HashMap class2MultiFunction;
private final ByteBuffer buffer;
private final byte[] array;
private static final int DEFAULT_SIZE = 64;
private List charKeyedHandlers;
private HashMap