/*
 * SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
 * SPDX-License-Identifier: Apache-2.0
 */

package badger

import (
	
	
	
	
)

type valuePointer struct {
	Fid    uint32
	Len    uint32
	Offset uint32
}

const vptrSize = unsafe.Sizeof(valuePointer{})

func ( valuePointer) ( valuePointer) bool {
	if .Fid != .Fid {
		return .Fid < .Fid
	}
	if .Offset != .Offset {
		return .Offset < .Offset
	}
	return .Len < .Len
}

func ( valuePointer) () bool {
	return .Fid == 0 && .Offset == 0 && .Len == 0
}

// Encode encodes Pointer into byte buffer.
func ( valuePointer) () []byte {
	 := make([]byte, vptrSize)
	// Copy over the content from p to b.
	*(*valuePointer)(unsafe.Pointer(&[0])) = 
	return 
}

// Decode decodes the value pointer into the provided byte buffer.
func ( *valuePointer) ( []byte) {
	// Copy over data from b into p. Using *p=unsafe.pointer(...) leads to
	// pointer alignment issues. See https://github.com/dgraph-io/badger/issues/1096
	// and comment https://github.com/dgraph-io/badger/pull/1097#pullrequestreview-307361714
	copy(((*[vptrSize]byte)(unsafe.Pointer())[:]), [:vptrSize])
}

// header is used in value log as a header before Entry.
type header struct {
	klen      uint32
	vlen      uint32
	expiresAt uint64
	meta      byte
	userMeta  byte
}

const (
	// Maximum possible size of the header. The maximum size of header struct will be 18 but the
	// maximum size of varint encoded header will be 22.
	maxHeaderSize = 22
)

// Encode encodes the header into []byte. The provided []byte should be at least 5 bytes. The
// function will panic if out []byte isn't large enough to hold all the values.
// The encoded header looks like
// +------+----------+------------+--------------+-----------+
// | Meta | UserMeta | Key Length | Value Length | ExpiresAt |
// +------+----------+------------+--------------+-----------+
func ( header) ( []byte) int {
	[0], [1] = .meta, .userMeta
	 := 2
	 += binary.PutUvarint([:], uint64(.klen))
	 += binary.PutUvarint([:], uint64(.vlen))
	 += binary.PutUvarint([:], .expiresAt)
	return 
}

// Decode decodes the given header from the provided byte slice.
// Returns the number of bytes read.
func ( *header) ( []byte) int {
	.meta, .userMeta = [0], [1]
	 := 2
	,  := binary.Uvarint([:])
	.klen = uint32()
	 += 
	,  := binary.Uvarint([:])
	.vlen = uint32()
	 += 
	.expiresAt,  = binary.Uvarint([:])
	return  + 
}

// DecodeFrom reads the header from the hashReader.
// Returns the number of bytes read.
func ( *header) ( *hashReader) (int, error) {
	var  error
	.meta,  = .ReadByte()
	if  != nil {
		return 0, 
	}
	.userMeta,  = .ReadByte()
	if  != nil {
		return 0, 
	}
	,  := binary.ReadUvarint()
	if  != nil {
		return 0, 
	}
	.klen = uint32()
	,  := binary.ReadUvarint()
	if  != nil {
		return 0, 
	}
	.vlen = uint32()
	.expiresAt,  = binary.ReadUvarint()
	if  != nil {
		return 0, 
	}
	return .bytesRead, nil
}

// Entry provides Key, Value, UserMeta and ExpiresAt. This struct can be used by
// the user to set data.
type Entry struct {
	Key       []byte
	Value     []byte
	ExpiresAt uint64 // time.Unix
	version   uint64
	offset    uint32 // offset is an internal field.
	UserMeta  byte
	meta      byte

	// Fields maintained internally.
	hlen         int // Length of the header.
	valThreshold int64
}

func ( *Entry) () bool {
	return len(.Key) == 0
}

func ( *Entry) ( int64) int64 {
	if .valThreshold == 0 {
		.valThreshold = 
	}
	 := int64(len(.Key))
	 := int64(len(.Value))
	if  < .valThreshold {
		return  +  + 2 // Meta, UserMeta
	}
	return  + 12 + 2 // 12 for ValuePointer, 2 for metas.
}

func ( *Entry) ( int64) bool {
	if .valThreshold == 0 {
		.valThreshold = 
	}
	return int64(len(.Value)) < .valThreshold
}

//nolint:unused
func ( Entry) ( string) {
	fmt.Printf("%s Key: %s Meta: %d UserMeta: %d Offset: %d len(val)=%d",
		, .Key, .meta, .UserMeta, .offset, len(.Value))
}

// NewEntry creates a new entry with key and value passed in args. This newly created entry can be
// set in a transaction by calling txn.SetEntry(). All other properties of Entry can be set by
// calling WithMeta, WithDiscard, WithTTL methods on it.
// This function uses key and value reference, hence users must
// not modify key and value until the end of transaction.
func (,  []byte) *Entry {
	return &Entry{
		Key:   ,
		Value: ,
	}
}

// WithMeta adds meta data to Entry e. This byte is stored alongside the key
// and can be used as an aid to interpret the value or store other contextual
// bits corresponding to the key-value pair of entry.
func ( *Entry) ( byte) *Entry {
	.UserMeta = 
	return 
}

// WithDiscard adds a marker to Entry e. This means all the previous versions of the key (of the
// Entry) will be eligible for garbage collection.
// This method is only useful if you have set a higher limit for options.NumVersionsToKeep. The
// default setting is 1, in which case, this function doesn't add any more benefit. If however, you
// have a higher setting for NumVersionsToKeep (in Dgraph, we set it to infinity), you can use this
// method to indicate that all the older versions can be discarded and removed during compactions.
func ( *Entry) () *Entry {
	.meta = bitDiscardEarlierVersions
	return 
}

// WithTTL adds time to live duration to Entry e. Entry stored with a TTL would automatically expire
// after the time has elapsed, and will be eligible for garbage collection.
func ( *Entry) ( time.Duration) *Entry {
	.ExpiresAt = uint64(time.Now().Add().Unix())
	return 
}

// withMergeBit sets merge bit in entry's metadata. This
// function is called by MergeOperator's Add method.
func ( *Entry) () *Entry {
	.meta = bitMergeEntry
	return 
}