/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2024 The Fluent Bit Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef FLB_INPUT_CHUNK_H
#define FLB_INPUT_CHUNK_H

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_routes_mask.h>

#include <monkey/mk_core.h>
#include <msgpack.h>

/*
 * This variable defines a 'hint' size for new Chunks created, this
 * value is passed to Chunk I/O.
 */
#define FLB_INPUT_CHUNK_SIZE           262144  /* 256KB (hint) */
/*
 * Defines a maximum size for a Chunk in the file system: note that despite
 * this is considered a limit, a Chunk size might get greater than this.
 */
#define FLB_INPUT_CHUNK_FS_MAX_SIZE   2048000  /* 2MB */

/* Number of bytes reserved for Metadata Header on Chunks */
#define FLB_INPUT_CHUNK_META_HEADER   4

/* Chunks magic bytes (starting from Fluent Bit v1.8.10) */
#define FLB_INPUT_CHUNK_MAGIC_BYTE_0  (unsigned char) 0xF1
#define FLB_INPUT_CHUNK_MAGIC_BYTE_1  (unsigned char) 0x77

/* Chunk types: Log, Metrics and Traces are supported */
#define FLB_INPUT_CHUNK_TYPE_LOGS      0
#define FLB_INPUT_CHUNK_TYPE_METRICS   1
#define FLB_INPUT_CHUNK_TYPE_TRACES    2

#ifdef FLB_HAVE_CHUNK_TRACE
#define FLB_INPUT_CHUNK_HAS_TRACE     1 << 31
#endif /* FLB_HAVE_CHUNK_TRACE */

/* Max length for Tag */
#define FLB_INPUT_CHUNK_TAG_MAX        (65535 - FLB_INPUT_CHUNK_META_HEADER)

struct flb_input_chunk {
    int  event_type;                 /* chunk type: logs, metrics or traces */
    bool fs_counted;
    int  busy;                       /* buffer is being flushed  */
    int  fs_backlog;                 /* chunk originated from fs backlog */
    int  sp_done;                    /* sp already processed this chunk */
#ifdef FLB_HAVE_METRICS
    int  total_records;              /* total records in the chunk */
    int  added_records;              /* recently added records */
#endif
    void *chunk;                    /* context of struct cio_chunk */
    off_t stream_off;               /* stream offset */
    msgpack_packer mp_pck;          /* msgpack packer */
    struct flb_input_instance *in;  /* reference to parent input instance */
    struct flb_task *task;          /* reference to the outgoing task */
#ifdef FLB_HAVE_CHUNK_TRACE
    struct flb_chunk_trace *trace;
#endif /* FLB_HAVE_CHUNK_TRACE */
    uint64_t routes_mask
        [FLB_ROUTES_MASK_ELEMENTS]; /* track the output plugins the chunk routes to */
    struct mk_list _head;
};

struct flb_input_chunk *flb_input_chunk_create(struct flb_input_instance *in, int event_type,
                                               const char *tag, int tag_len);
int flb_input_chunk_destroy(struct flb_input_chunk *ic, int del);
void flb_input_chunk_destroy_all(struct flb_input_instance *in);
int flb_input_chunk_destroy_corrupted(struct flb_input_chunk *ic,
                                      const char *tag_buf, int tag_len,
                                      int del);
int flb_input_chunk_write(void *data, const char *buf, size_t len);
int flb_input_chunk_write_at(void *data, off_t offset,
                             const char *buf, size_t len);
int flb_input_chunk_append_obj(struct flb_input_instance *in,
                               const char *tag, int tag_len,
                               msgpack_object data);
int flb_input_chunk_append_raw(struct flb_input_instance *in,
                               int event_type,
                               size_t records,
                               const char *tag, size_t tag_len,
                               const void *buf, size_t buf_size);

const void *flb_input_chunk_flush(struct flb_input_chunk *ic, size_t *size);
int flb_input_chunk_release_lock(struct flb_input_chunk *ic);
flb_sds_t flb_input_chunk_get_name(struct flb_input_chunk *ic);
int flb_input_chunk_get_event_type(struct flb_input_chunk *ic);

int flb_input_chunk_get_tag(struct flb_input_chunk *ic,
                            const char **tag_buf, int *tag_len);

void flb_input_chunk_ring_buffer_cleanup(struct flb_input_instance *ins);
void flb_input_chunk_ring_buffer_collector(struct flb_config *ctx, void *data);
ssize_t flb_input_chunk_get_size(struct flb_input_chunk *ic);
size_t flb_input_chunk_set_limits(struct flb_input_instance *in);
size_t flb_input_chunk_total_size(struct flb_input_instance *in);
struct flb_input_chunk *flb_input_chunk_map(struct flb_input_instance *in,
                                            int event_type,
                                            void *chunk);
int flb_input_chunk_set_up_down(struct flb_input_chunk *ic);
int flb_input_chunk_set_up(struct flb_input_chunk *ic);
int flb_input_chunk_down(struct flb_input_chunk *ic);
int flb_input_chunk_is_up(struct flb_input_chunk *ic);
void flb_input_chunk_update_output_instances(struct flb_input_chunk *ic,
                                             size_t chunk_size);

#endif
