Skip to main content

bitpos.go

bitpos.go - Overview

This file implements the BITPOS command functionality, which finds the position of the first bit set to a specified value (0 or 1) within a byte slice stored in the database. It handles parsing arguments, retrieving data from the store, and performing the bit search operation.

Detailed Documentation

Constants

  • RespNIL: Represents a null response in RESP format ([]byte("$-1\r\n")).
  • RespOK: Represents a successful "OK" response in RESP format ([]byte("+OK\r\n")).
  • RespQueued: Represents a "QUEUED" response in RESP format ([]byte("+QUEUED\r\n")).
  • RespZero: Represents the integer 0 in RESP format ([]byte(":0\r\n")).
  • RespOne: Represents the integer 1 in RESP format ([]byte(":1\r\n")).
  • RespMinusOne: Represents the integer -1 in RESP format ([]byte(":-1\r\n")).
  • RespMinusTwo: Represents the integer -2 in RESP format ([]byte(":-2\r\n")).
  • RespEmptyArray: Represents an empty array in RESP format ([]byte("*0\r\n")).
  • NIL: Represents an empty or null response (RespType = iota).
  • OK: Represents a successful "OK" response.
  • CommandQueued: Represents that a command has been queued for execution.
  • IntegerZero: Represents the integer value zero in RESP format.
  • IntegerOne: Represents the integer value one in RESP format.
  • IntegerNegativeOne: Represents the integer value negative one in RESP format.
  • IntegerNegativeTwo: Represents the integer value negative two in RESP format.
  • EmptyArray: Represents an empty array in RESP format.

Function Encode

  • Purpose: Encodes a given value into a byte slice in RESP format.
  • Parameters:
    • value (interface{}): The value to encode.
    • isSimple (bool): A boolean indicating whether simple encoding should be used (though the implementation doesn't change behavior based on this).
  • Returns: A byte slice representing the encoded value in RESP format ([]byte).

Function evalBITPOS

  • Purpose: Evaluates the BITPOS command, handling argument parsing, data retrieval, and the bit position search.
  • Parameters:
    • args ([]string): A slice of strings representing the command arguments.
    • st (*store.Store): A pointer to the store.Store instance.
  • Returns: A pointer to an EvalResponse struct containing the result or error of the operation (*EvalResponse).

Function parseBitToFind

  • Purpose: Parses the bit to find argument (0 or 1) from a string.
  • Parameters:
    • arg (string): The string representation of the bit to find.
  • Returns:
    • byte: The parsed bit value (0 or 1) as a byte.
    • error: An error if the argument is invalid.

Function parseOptionalParams

  • Purpose: Parses the optional start, end, and range type parameters from the command arguments.
  • Parameters:
    • args ([]string): A slice of strings representing the optional arguments.
    • byteLen (int): The length of the byte slice being searched.
  • Returns:
    • start (int): The start index for the search.
    • end (int): The end index for the search.
    • rangeType (string): The range type (BYTE or BIT).
    • endRangeProvided (bool): Indicates whether the end range was provided.
    • err (error): An error if any of the arguments are invalid.

Function getBitPos

  • Purpose: Performs the bit position search within the byte slice based on the provided parameters.
  • Parameters:
    • byteSlice ([]byte): The byte slice to search within.
    • bitToFind (byte): The bit value to search for (0 or 1).
    • start (int): The start index for the search.
    • end (int): The end index for the search.
    • rangeType (string): The range type (BYTE or BIT).
    • endRangeProvided (bool): Indicates whether the end range was provided.
  • Returns: The position of the first matching bit, or -1 if not found (int).

Function adjustBitPosSearchRange

  • Purpose: Adjusts the start and end indices for the bit position search, handling negative indices and ensuring they are within the valid range.
  • Parameters:
    • start (int): The original start index.
    • end (int): The original end index.
    • byteLen (int): The length of the byte slice (in bytes).
  • Returns:
    • newStart (int): The adjusted start index.
    • newEnd (int): The adjusted end index.

Function getBitPosWithBitRange

  • Purpose: Searches for the bit position within a specified bit range within the byte slice.
  • Parameters:
    • byteSlice ([]byte): The byte slice to search within.
    • bitToFind (byte): The bit value to search for (0 or 1).
    • start (int): The start bit index.
    • end (int): The end bit index.
  • Returns: The position of the first matching bit, or -1 if not found (int).

Code Examples

// Example usage of evalBITPOS (hypothetical, as it depends on the store and argument parsing)
const args = ["mykey", "1", "0", "10", "BYTE"];
// Assuming 'st' is a store.Store instance and "mykey" exists in the store
const response = evalBITPOS(args, st);

if (response.Error) {
console.error("Error:", response.Error);
} else {
console.log("Result:", response.Result); // e.g., Result: ":5\r\n"
}