ratecache

package
v1.0.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2021 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package ratecache provides functions to efficiently store rate and availability data in a buffer or a file.

Index

Constants

View Source
const AvailMask uint32 = 4026531840

AvailMask masks the lower 28 bits of an uint32

View Source
const FileHeaderSize = 37

FileHeaderSize is the size of the rate file header in bytes

View Source
const FixBlockHeaderSize = 24

FixBlockHeaderSize is the portion of the block header size that does not chane, i.e. without room rate code and acco code

View Source
const FixIdxRecSize = 28

FixIdxRecSize is the portion of the record size in the index file that does not change, i.e. without room rate code and acco code.

View Source
const RateMask uint32 = 268435455

RateMask masks the upper 4 bytes of an uint32 which is used to transport availability

View Source
const Release = "1.0 Beta"

Release version of this Open RateCache implementation

View Source
const Signature = "LOSRATES"

Signature is the signature string for rate files

View Source
const Version = 8

Version is the format version of the rate file

Variables

This section is empty.

Functions

func AddRateBlockToFile

func AddRateBlockToFile(f *os.File, byteStr []byte) (uint32, error)

AddRateBlockToFile adds a rate block to cache file. The block position is determined by the RateBlockCount. Method will return the index, not the count! Do not forget to update the rateBlockCount on your FileHeader object!

func CreateRateBlock

func CreateRateBlock(fhdr *FileHeader, rbhdr *RateBlockHeader) []byte

CreateRateBlock creates an empty rate block for padding.

func DirExists

func DirExists(path string) (bool, error)

DirExists checks if a directory exists.

func DirIsEmpty

func DirIsEmpty(path string) (bool, error)

DirIsEmpty checks if the directory is empty

func DirIsWritable

func DirIsWritable(path string) (bool, error)

DirIsWritable checks if the current user can access and write in the directory

func InitRateFile

func InitRateFile(fhdr *FileHeader, folder string, filename string, blockCount int) (string, error)

InitRateFile creates a new rate file on disc and resets the rateBlockCount of file header object. folder is the folder, where the file is going to live.

func PackRate

func PackRate(rate uint32, avail uint8) []byte

PackRate packs rate and availability into a single uint32 and returns the value as a 4 byte string (big endian) that can be written to the rate cache.

func StrToTime

func StrToTime(s string) (time.Time, error)

StrToTime takes a date string in rate header format and returns a time.Time object.

func TimeToStr

func TimeToStr(t time.Time) string

TimeToStr converts the Time object into a date string as used in the rate file header.

func UnpackRate

func UnpackRate(buf []byte) (uint32, uint8)

UnpackRate takes a 4 byte string, unpacks values for rate and availability and returns them separately.

Types

type AccoRoomRate

type AccoRoomRate struct {
	AccoCode      string   `json:"accoCode"`
	RoomRateCodes []string `json:"roomRateCodes"`
}

AccoRoomRate represents one Accommodation and all possible. RoomRates.

type Ages

type Ages []uint8

Ages represents a specific occupancy (i.e. a specific group of guests) as a slice of ages, e.g. [8, 40, 38] represents an eigth year old child and two adults aged 40 and 38.

func (Ages) MarshalJSON

func (ages Ages) MarshalJSON() ([]byte, error)

func (*Ages) UnmarshalJSON

func (ages *Ages) UnmarshalJSON(b []byte) error

type CacheIndex

type CacheIndex struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

CacheIndex contains a map with the nested map and slice structure for the cache index, protected by a mutex.

func NewCacheIndex

func NewCacheIndex() *CacheIndex

NewCacheIndex returns a pointer to a new CacheIndex instance. Returning a pointer is necessary because returning a copy of a mutex is not safe.

func (*CacheIndex) AddRoomOccIdx

func (idx *CacheIndex) AddRoomOccIdx(accoCode string, roomRateCode string, roomOccIdx RoomOccIdx) error

AddRoomOccIdx adds a new RoomOccIdx to the index.

func (*CacheIndex) Find

func (idx *CacheIndex) Find(searchRq *SearchRq) []IdxResult

func (*CacheIndex) Get

func (idx *CacheIndex) Get(q IndexQuery) (uint32, bool)

func (*CacheIndex) GetAccoCount

func (idx *CacheIndex) GetAccoCount() int

GetAccoCount returns the number of accommodations in the idx.

func (*CacheIndex) GetAccoList

func (idx *CacheIndex) GetAccoList() []string

Get AccoList returns a slice of all accommodations in the idx.

func (*CacheIndex) GetAccommodation

func (idx *CacheIndex) GetAccommodation(AccoCode string) map[string][][]OccupancyItem

func (*CacheIndex) Load

func (idx *CacheIndex) Load(fhdr *FileHeader, filename string) error

Load reads the cache index from a file.

func (*CacheIndex) LoadFromCache

func (idx *CacheIndex) LoadFromCache(filename string) error

func (*CacheIndex) Save

func (idx *CacheIndex) Save(fhdr *FileHeader, filename string) error

Save saves the whole index to a file. Index format is: - AccoCode (length as of FileHeader object) - RoomCode (length as of FileHeader object) - 8 Occupancy items (1 MinAge, 1 MaxAge, 1 Count) - Index (uint16)

type DateRangeAvail

type DateRangeAvail struct {
	FirstCheckIn JSONDate `json:"firstCheckIn"`
	LastCheckIn  JSONDate `json:"lastCheckIn"`
	LengthOfStay uint8    `json:"lengthOfStay"`
	Available    uint8    `json:"available"`
}

DateRangeAvail represents the number of available rooms for a range of check-in dates.

func (*DateRangeAvail) ExplodeAvail

func (dra *DateRangeAvail) ExplodeAvail(cacheDate time.Time, hdrSize int, days uint16) (int, []uint8)

ExplodeAvail is similar to ExplodeRates but returns a slice with the availabilities instead of rates

type DateRangeRate

type DateRangeRate struct {
	FirstCheckIn JSONDate `json:"firstCheckIn"`
	LastCheckIn  JSONDate `json:"lastCheckIn"`
	LengthOfStay uint8    `json:"lengthOfStay"`
	Rate         float32  `json:"rate"`
}

DateRangeRate represents a rate that is valid for various checkin dates.

func (DateRangeRate) ExplodeRate

func (drr DateRangeRate) ExplodeRate(cacheDate time.Time, hdrSize int, days uint16, DecimalPlaces uint8) (int, []uint32)

ExplodeRate returns the exploded rates as a uint32 slice and the offset for the first rate in the room rate block. Check-in dates that are beyond the valid scope of the cache, i.e. the configured check-in dates in the future, will be cut off.

type DateRate

type DateRate struct {
	CheckIn      JSONDate `json:"checkIn"`
	LengthOfStay uint8    `json:"lengthOfStay"`
	Rate         float32  `json:"rate"`
}

DateRate represents a rate or an availability for one specific day.

type FileHeader

type FileHeader struct {
	Signature          string
	Supplier           string
	Version            uint8
	StartDate          time.Time
	Currency           string
	MaxLos             uint8
	Days               uint16
	AccoCodeLength     uint8
	RoomRateCodeLength uint8
	RateBlockCount     uint32
}

FileHeader represents header information from a rate cache file which can be read from and written to a byte string.

func FileHeaderFromByteStr

func FileHeaderFromByteStr(byteStr []byte) (*FileHeader, error)

FileHeaderFromByteStr parse a file header into a FileHeader object.

func NewFileHeader

func NewFileHeader(Supplier string, StartDate time.Time, Currency string, MaxLos uint8, Days uint16, AccoCodeLength uint8, RoomRateCodeLength uint8) (*FileHeader, error)

NewFileHeader returns a pointer to a new FileHeader object.

func (*FileHeader) GetBlockHeaderSize

func (fhdr *FileHeader) GetBlockHeaderSize() int

GetBlockHeaderSize calculates the rate block header size.

func (*FileHeader) GetRateBlockSize

func (fhdr *FileHeader) GetRateBlockSize() int

GetRateBlockSize returns the total size of a rate block including the header

func (*FileHeader) GetRateBlockStart

func (fhdr *FileHeader) GetRateBlockStart(index uint32) int64

GetRateBlockStart returns offset of rate block from its index. First block has index 0.

func (*FileHeader) GetRateInfo

func (fhdr *FileHeader) GetRateInfo(f *os.File, idx uint32, date time.Time, los uint8) (uint32, uint8, error)

GetRateInfo gets one rate/avail from rate cache.

func (*FileHeader) GetRateInfoFromMap

func (fhdr *FileHeader) GetRateInfoFromMap(m mmap.ReaderAt, idx uint32, date time.Time, los uint8) (uint32, uint8, error)

func (*FileHeader) GetRatePos

func (fhdr *FileHeader) GetRatePos(idx uint32, date time.Time, los uint8) (int64, error)

GetRatePos gets position of rate in rate cache

func (*FileHeader) SetRateInfo

func (fhdr *FileHeader) SetRateInfo(f *os.File, idx uint32, date time.Time, los uint8, rate uint32, avail uint8) error

SetRateInfo writes one rate/avail to rate cache.

func (*FileHeader) ToByteStr

func (fhdr *FileHeader) ToByteStr() []byte

ToByteStr creates a file header as byte string from object.

type IdxResult

type IdxResult struct {
	AccoCode string
	Rooms    []RoomIdx
}

A slice of IdexResult is returned by "Find" as a result to the search. This slice is used as input to get the actual rate information from the rate cache.

type IndexQuery

type IndexQuery struct {
	AccoCode     string
	RoomRateCode string
	Occupancy    []OccupancyItem
	OccTotal     uint8
}

func (*IndexQuery) AddOccItem

func (indexQuery *IndexQuery) AddOccItem(MinAge uint8, MaxAge uint8, Count uint8) error

AddOccItem adds an Occupancy item, consisting of MinAge, MaxAge and Count to the requested occupancy. This method should always be used as setter instead of directly appending to the Occupancy attribute.

type JSONDate

type JSONDate time.Time

JSONDate represents date in ISO 8601 date format: YYYY-MM-DD.

func (JSONDate) MarshalJSON

func (jd JSONDate) MarshalJSON() ([]byte, error)

MarshalJSON returns date as ISO 8601 date.

func (*JSONDate) UnmarshalJSON

func (jd *JSONDate) UnmarshalJSON(b []byte) error

UnmarshalJSON takes an ISO 8601 date string and returns a JSONDate object.

type OccupancyItem

type OccupancyItem struct {
	MinAge uint8 `json:"minAge"`
	MaxAge uint8 `json:"maxAge"`
	Count  uint8 `json:"count"`
}

OccupancyItem represents a guest type identified by an age range that can occupy a room. One occupancy is made up of one or more OccupancyItems.

func NewOccupancyItem

func NewOccupancyItem(MinAge uint8, MaxAge uint8, Count uint8) (*OccupancyItem, error)

NewOccupancyItem returns a pointer to a new OccupancyItem object.

func (*OccupancyItem) ToByteStr

func (item *OccupancyItem) ToByteStr() *[]byte

ToByteStr returns a representation of OccupancyItem as byte string which can be written to the rate cache.

type RateBlockHeader

type RateBlockHeader struct {
	// contains filtered or unexported fields
}

RateBlockHeader represents a rate block header information which can be read from and written to byte string.

func NewRateBlockHeader

func NewRateBlockHeader(accoCode string, roomRateCode string) (*RateBlockHeader, error)

NewRateBlockHeader returns a pointer to a new RateBlockHeader object.

func RateBlockHeaderFromByteStr

func RateBlockHeaderFromByteStr(byteStr []byte, AccoCodeLength uint8, RoomRateCodeLength uint8) (*RateBlockHeader, error)

RateBlockHeaderFromByteStr creates a rate block header object from byte string.

func (*RateBlockHeader) AddOccupancyItem

func (rbhdr *RateBlockHeader) AddOccupancyItem(MinAge uint8, MaxAge uint8, Count uint8) error

AddOccupancyItem Adds an occupancy item to the rate block header.

func (*RateBlockHeader) ToByteStr

func (rbhdr *RateBlockHeader) ToByteStr(AccoCodeLength uint8, RoomRateCodeLength uint8) []byte

ToByteStr creates a rate block header as byte string from object.

type RoomIdx

type RoomIdx struct {
	RoomRateCode string
	Index        uint32
}

RoomIdx is part of IdxResult and contains a room rate code and the corresponding index of the room rate block

type RoomOccIdx

type RoomOccIdx struct {
	Occupancy []OccupancyItem
	Total     uint8
	Idx       uint32
}

RoomOccIdx is one possible occupancy for a room or room rate. idx points to the rate block in the cache file.

func (*RoomOccIdx) AddOccItem

func (roomOccIdx *RoomOccIdx) AddOccItem(MinAge uint8, MaxAge uint8, Count uint8) error

AddOccItem adds one occupuncy item to the occupancy.

func (*RoomOccIdx) AppendToIdxFile

func (roomOccIdx *RoomOccIdx) AppendToIdxFile(fhdr FileHeader, filename string, accoCode string, roomRateCode string) error

AppendToIdxFile appends a new index entry to the index file without having to re-write the whole index on disk

func (*RoomOccIdx) Match

func (roomOccIdx *RoomOccIdx) Match(guests []uint8) bool

Match matches a specific group of guests, represented by a slice of ages against a room occupancy and returns true if the group matches the occupancy requirements.

func (*RoomOccIdx) ToByteStr

func (roomOccIdx *RoomOccIdx) ToByteStr() *[]byte

ToByteStr returns a byte string representation of RoomOccIdx which can be written to the rate cache.

type RoomRates

type RoomRates struct {
	AccoCode       string `json:"accommodationCode"`
	RoomRateCode   string `json:"roomRateCode"`
	Occupancy      []OccupancyItem
	Rates          []DateRangeRate  `json:"rates"`
	Availabilities []DateRangeAvail `json:"availabilities"`
}

RoomRates represents partially or completely the rates and availabilities for a room.

func (*RoomRates) AddAvail

func (roomRates *RoomRates) AddAvail(FirstCheckIn time.Time, LastCheckIn time.Time, LengthOfStay uint8, Available uint8) error

AddAvail adds a DateRangeAvail to RoomRates.Rates.

func (*RoomRates) AddRate

func (roomRates *RoomRates) AddRate(FirstCheckIn time.Time, LastCheckIn time.Time, LengthOfStay uint8, Rate float32) error

AddRate adds a DateRangeRate to RoomRates.Rates.

func (*RoomRates) Validate

func (roomRates *RoomRates) Validate() []string

type SearchRq

type SearchRq struct {
	CheckIn         JSONDate       `json:"checkIn"`
	FirstCheckIn    JSONDate       `json:"firstCheckIn"`
	LastCheckIn     JSONDate       `json:"lastCheckIn"`
	LengthOfStay    uint8          `json:"lengthOfStay"`
	MinLengthOfStay uint8          `json:"minLengthOfStay"`
	MaxLengthOfStay uint8          `json:"maxLengthOfStay"`
	Occupancy       Ages           `json:"occupancy"`
	Accommodations  []AccoRoomRate `json:"accommodations"`
}

SearchRq transports a set of search parameters.

func (*SearchRq) Validate

func (searchRq *SearchRq) Validate() ([]string, error)

Validate checks the request for valid entries and returns a list of message strings each of which represents a validation error. The error return value does not refer to semantic validation errors! In order to check, if the request is semantically correct and can be processed check if first return value == 0. If CheckIn is set then First and Last are meant to be ignored; if LengthOfStay is set then Min and Max are meant to be ignored.

type SearchRs

type SearchRs struct {
	CheckIn      JSONDate             `json:"checkIn"`
	LengthOfStay uint8                `json:"lengthOfStay"`
	Options      []SearchRsAccoOption `json:"options"`
}

SearchRs transports a search result.

type SearchRsAccoOption

type SearchRsAccoOption struct {
	AccoCode string `json:"accoCode"`
	Rooms    []SearchRsRoomOption
}

SearchRsAccoOption groups accommodation with different rooms for one specific combination of check-in and los.

type SearchRsRoomOption

type SearchRsRoomOption struct {
	RoomRateCode string  `json:"roomRateCode"`
	Rate         float64 `json:"rate"`
	Availability uint8   `json:"availability"`
}

SearchRsRoomOption represents one room with the corresponding rate and availability for one specific los and stay

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL