cache

package
v0.24.2 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2023 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// from https://github.com/ptt/pttbbs/blob/master/include/pttstruct.h
	// commit: 6bdd36898bde207683a441cdffe2981e95de5b20
	SHM_VERSION = 4842

	PRE_ALLOCATED_USERS = 1000
)
View Source
const DUMMY_SHMGV2 = 512 - 6 // 6 defined int32 variables in shmGV2a
View Source
const SHM_RAW_SZ = unsafe.Sizeof(EMPTY_SHM_RAW)

Variables

View Source
var (
	ErrInvalidOp       = errors.New("invalid op")
	ErrShmNotInit      = errors.New("shm not init")
	ErrShmAlreadyInit  = errors.New("shm already init")
	ErrShmVersion      = errors.New("invalid shm version")
	ErrShmSize         = errors.New("invalid shm size")
	ErrShmNotLoaded    = errors.New("shm not loaded")
	ErrAddToUHash      = errors.New("unable to add-to-uhash")
	ErrRemoveFromUHash = errors.New("unable to remove-from-uhash")

	ErrInvalidUID = errors.New("invalid uid")

	ErrStats = errors.New("invalid stats")

	ErrNotFound = errors.New("not found")
	ErrBusy     = errors.New("busy")

	ErrInvalidNumBoards = errors.New("invalid num boards")
)
View Source
var (
	IsTest = false

	TestMutex     sync.Mutex
	TestEachMutex sync.Mutex
)
View Source
var EMPTY_SHM_RAW = SHMRaw{}
View Source
var TestShmKey = types.Key_t(65534)

Functions

func AddCooldownTime added in v0.12.0

func AddCooldownTime(uid ptttype.UID, minutes int) (err error)

func AddPosttimes added in v0.12.0

func AddPosttimes(uid ptttype.UID, times int) (err error)

func AddToUHash

func AddToUHash(uidInCache ptttype.UIDInStore, userID *ptttype.UserID_t) error

AddToUHash

func AddbrdTouchCache added in v0.11.1

func AddbrdTouchCache() (bid ptttype.Bid, err error)

func AttachCheckSHM

func AttachCheckSHM() (err error)

AttachCheckSHM

This is to have attach_check_shm (checking loaded) Should be used after LoadUHash (shmctl init) is done. Should be used only once in the beginning of the program.

func AttachSHM

func AttachSHM() error

AttachSHM

This is to have attach_shm (no checking loaded), not attach_SHM (checking loaded) Should be used after LoadUHash (shmctl init) is done. Should be used only once in the beginning of the program.

func CleanStat

func CleanStat()

func CloseSHM

func CloseSHM() error

Close

XXX [WARNING] know what you are doing before using Close!. This is to be able to close the shared mem for the completeness of the mem-usage. However, in production, we create shm without the need of closing the shm.

We simply use ipcrm to delete the shm if necessary.

Currently used only in test. XXX not doing close shm to prevent opening too many shms in tests.

func CooldownTimeOf added in v0.12.0

func CooldownTimeOf(uid ptttype.UID) (cooldowntime types.Time4)

CooldownTimeOf https://github.com/ptt/pttbbs/blob/master/include/cmbbs.h#L97

func DeUMoney

func DeUMoney(uid ptttype.UID, money int32) (int32, error)

DeUMoney

Add money to uid. (money can be >= 0 or < 0) Get current money and set the money by adding to current-money.

func DoSearchUserRaw

func DoSearchUserRaw(userID *ptttype.UserID_t, rightID *ptttype.UserID_t) (ptttype.UID, error)

func FindBoardAutoCompleteStartIdx added in v0.10.0

func FindBoardAutoCompleteStartIdx(keyword []byte, isAsc bool) (startIdx ptttype.SortIdx, err error)

func FindBoardIdxByClass added in v0.10.0

func FindBoardIdxByClass(cls []byte, boardID *ptttype.BoardID_t, isAsc bool) (idx ptttype.SortIdx, err error)

func FindBoardIdxByName added in v0.10.0

func FindBoardIdxByName(boardID *ptttype.BoardID_t, isAsc bool) (idx ptttype.SortIdx, err error)

func GetBCache

func GetBCache(bid ptttype.Bid) (board *ptttype.BoardHeaderRaw, err error)

func GetBTotal added in v0.3.0

func GetBTotal(bid ptttype.Bid) (total int32)

func GetBTotalWithRetry added in v0.10.0

func GetBTotalWithRetry(bid ptttype.Bid) (total int32, err error)

func GetBid added in v0.9.0

func GetBid(boardID *ptttype.BoardID_t) (bid ptttype.Bid, err error)

func GetBottomTotal added in v0.15.0

func GetBottomTotal(bid ptttype.Bid) (total int32)

func GetLastPosttime added in v0.12.0

func GetLastPosttime(bid ptttype.Bid) (lastposttime types.Time4, err error)

func GetUTMPNumber added in v0.19.2

func GetUTMPNumber() (total int32)

func GetUserID

func GetUserID(uid ptttype.UID) (*ptttype.UserID_t, error)

GetUserID

XXX uid = uid-in-cache + 1

func HbflReload

func HbflReload(bidInCache ptttype.BidInStore)

func InitFillUHash

func InitFillUHash(isOnfly bool)

func IsHiddenBoardFriend

func IsHiddenBoardFriend(bidInCache ptttype.BidInStore, uidInCache ptttype.UIDInStore) bool

func LoadUHash

func LoadUHash() (err error)

LoadUHash Load user-hash into SHM.

func MoneyOf

func MoneyOf(uid ptttype.UID) (money int32)

func NHots added in v0.9.0

func NHots() (nhots uint8)

func NewSHM

func NewSHM(key types.Key_t, isUseHugeTlb bool, isCreate bool) error

NewSHM

This is to init SHM with Version and Size checked.

func ParseBMList added in v0.11.1

func ParseBMList(bms *ptttype.BM_t) (uids *[ptttype.MAX_BMs]ptttype.UID)

func PosttimesOf added in v0.12.0

func PosttimesOf(uid ptttype.UID) (posttimes types.Time4)

PosttimesOf https://github.com/ptt/pttbbs/blob/master/include/cmbbs.h#L98

func ReadStat

func ReadStat(stats ptttype.Stat) (uint32, error)

func RemoveFromUHash

func RemoveFromUHash(uidInCache ptttype.UIDInStore) error

RemoveFromUHash

func ResetBoard added in v0.11.1

func ResetBoard(bid ptttype.Bid) (err error)

func ResolveBoardGroup added in v0.17.0

func ResolveBoardGroup(gid ptttype.Bid, bsortBy ptttype.BSortBy) (err error)

func SanitizeBMs added in v0.11.1

func SanitizeBMs(bms *ptttype.BM_t) (parsedBMs *ptttype.BM_t)

func SearchUListPID added in v0.5.0

func SearchUListPID(pid types.Pid_t) (utmpID ptttype.UtmpID, uInfo *ptttype.UserInfoRaw)

func SearchUListUserID

func SearchUListUserID(userID *ptttype.UserID_t) (utmpID ptttype.UtmpID, uInfo *ptttype.UserInfoRaw)

func SearchUserRaw

func SearchUserRaw(userID *ptttype.UserID_t, rightID *ptttype.UserID_t) (uid ptttype.UID, err error)

SearchUser Params

userID: querying user-id.
isReturn: is return the user-id in the shm.

Return

uid:
err:

func SetBTotal added in v0.3.0

func SetBTotal(bid ptttype.Bid) (err error)

SetBTotal

It's possible that we loaded nothing from ReloadBCache in the beginning of the program, and then there are some articles after a while. We need to sync the btotal and lastposttime back to shm.

func SetBoardChildCount added in v0.17.0

func SetBoardChildCount(bid ptttype.Bid, count int32) (err error)

func SetBottomTotal added in v0.3.0

func SetBottomTotal(bid ptttype.Bid) error

func SetCooldownTime added in v0.12.0

func SetCooldownTime(uid ptttype.UID, cooldownTime types.Time4) (err error)

func SetIsTest

func SetIsTest()

func SetLastPosttime added in v0.12.0

func SetLastPosttime(bid ptttype.Bid, nowTS types.Time4) (err error)

func SetUMoney

func SetUMoney(uid ptttype.UID, money int32) (int32, error)

SetUMoney

XXX uid-in-cache = uid - 1

func SetUserID

func SetUserID(uid ptttype.UID, userID *ptttype.UserID_t) (err error)

SetUserID

XXX uid = uid-in-cache + 1

func SetUtmpMode added in v0.12.0

func SetUtmpMode(uid ptttype.UID, mode ptttype.UserOpMode) (err error)

SetUtmpMode

XXX skip utmp for now.

func SortBCache added in v0.11.1

func SortBCache()

SortBCache XXX TODO: implement

func StatInc

func StatInc(stats ptttype.Stat) error

func TouchBPostNum added in v0.12.0

func TouchBPostNum(bid ptttype.Bid, delta int32) (err error)

func UnsetIsTest

func UnsetIsTest()

Types

type SHM

type SHM struct {
	Shmid   int
	IsNew   bool
	Shmaddr unsafe.Pointer

	Raw SHMRaw // dummy variable to get the offset and size of the shm-fields.

	Shm *SHMRaw
}
var Shm *SHM

//////// !!! We should have only 1 Shm.

func (*SHM) CheckMaxUser added in v0.5.0

func (s *SHM) CheckMaxUser()

func (*SHM) Close

func (s *SHM) Close() error

Close

XXX [WARNING] know what you are doing before using Close!. This is to be able to close the shared mem for the completeness of the mem-usage. However, in production, we create shm without the need of closing the shm.

We simply use ipcrm to delete the shm if necessary.

Currently used only in test. XXX not doing close shm to prevent opening too many shms in tests.

func (*SHM) GetBNumber

func (s *SHM) GetBNumber() (bnumber int32)

func (*SHM) QsortCmpBoardClass

func (s *SHM) QsortCmpBoardClass()

func (*SHM) QsortCmpBoardName

func (s *SHM) QsortCmpBoardName()

func (*SHM) Reset

func (s *SHM) Reset()

type SHMRaw

type SHMRaw struct {
	Version int32 // SHM_VERSION   for verification
	Size    int32 // sizeof(SHM_t) for verification

	/* uhash */
	/* uhash is a userid->uid hash table -- jochang */
	Userid     [ptttype.MAX_USERS]ptttype.UserID_t
	Gap1       ptttype.UserID_t
	NextInHash [ptttype.MAX_USERS]ptttype.UIDInStore
	Gap2       [types.INT32_SZ]byte
	Money      [ptttype.MAX_USERS]int32
	Gap3       [types.INT32_SZ]byte
	// XXX CooldownTime is affected with USE_COOLDOWN
	//     Require to discuss with admins about current settings.
	CooldownTime [ptttype.MAX_USERS]types.Time4
	Gap4         [types.INT32_SZ]byte
	HashHead     [1 << ptttype.HASH_BITS]ptttype.UIDInStore
	Gap5         [types.INT32_SZ]byte
	Number       int32 /* # of users total */
	Loaded       int32 /* .PASSWD has been loaded? */

	/* utmpshm */
	// XXX UserInfoRaw is affected with NOKILLWATERBALL
	//     Require to discuss with admins about current settings.
	UInfo  [ptttype.USHM_SIZE]ptttype.UserInfoRaw
	Gap6   [ptttype.USER_INFO_RAW_SZ]byte
	Sorted [2][ptttype.SORT_BY_MAX][ptttype.USHM_SIZE]ptttype.UtmpID
	/* 第一維double buffer 由currsorted指向目前使用的
	   第二維sort type */
	Gap7          [types.INT32_SZ]byte
	CurrSorted    int32
	UTMPUptime    types.Time4
	UTMPNumber    int32
	UTMPNeedSort  byte
	UTMPBusyState byte

	/* brdshm */
	Gap8    [types.INT32_SZ]byte
	BMCache [ptttype.MAX_BOARD][ptttype.MAX_BMs]ptttype.UID
	Gap9    [types.INT32_SZ]byte
	BCache  [ptttype.MAX_BOARD]ptttype.BoardHeaderRaw
	Gap10   [types.INT32_SZ]byte
	BSorted [ptttype.BSORT_BY_MAX][ptttype.MAX_BOARD]ptttype.BidInStore /* 0: by name 1: by class */ /* 裡頭存的是 bid-1 */
	Gap11   [types.INT32_SZ]byte
	// XXX NHOTs and HBcache are affected with HOTBOARDCACHE
	//     Require to discuss with admins about current settings.
	NHOTs         uint8
	HBcache       [ptttype.HOTBOARDCACHE]ptttype.BidInStore
	Gap12         [types.INT32_SZ]byte
	BusyStateB    [ptttype.MAX_BOARD]types.Time4
	Gap13         [types.INT32_SZ]byte
	Total         [ptttype.MAX_BOARD]int32 /* numbre of all posts */
	Gap14         [types.INT32_SZ]byte
	NBottom       [ptttype.MAX_BOARD]uint8 /* number of bottom */
	Gap15         [types.INT32_SZ]byte
	Hbfl          [ptttype.MAX_BOARD][ptttype.MAX_FRIEND + 1]ptttype.UID /* hidden board friend list, 0: load time, 1-MAX_FRIEND: uid (板友們) (0th: load 的時間. 其他欄位: 用來記錄板友的 uid) (XXX 在之後 Time8 時需要改變 structure) */
	Gap16         [types.INT32_SZ]byte
	LastPostTime  [ptttype.MAX_BOARD]types.Time4
	Gap17         [types.INT32_SZ]byte
	BUptime       types.Time4
	BTouchTime    types.Time4
	BNumber       int32
	BBusyState    int32
	CloseVoteTime types.Time4

	/* pttcache */
	Notes   [ptttype.MAX_ADBANNER][256 * ptttype.MAX_ADBANNER_HEIGHT]byte
	Gap18   [types.INT32_SZ]byte
	TodayIs [ptttype.TODAYISSZ]byte
	// FIXME remove it
	NeverUsedNNotes_ [ptttype.MAX_ADBANNER_SECTION]int32 /* 一節中有幾個 看板 */
	Gap19            [types.INT32_SZ]byte
	// FIXME remove it
	NeverUsedNextRefresh_ [ptttype.MAX_ADBANNER_SECTION]int32 /* 下一次要refresh的 看板 */
	Gap20                 [types.INT32_SZ]byte
	LoginMsg              ptttype.MsgQueueRaw /* 進站水球 */
	LastFilm              int32
	LastUsong             int32
	PUptime               types.Time4
	PTouchTime            types.Time4
	PBusyState            int32

	/* SHM 中的全域變數, 可用 shmctl 設定或顯示. 供動態調整或測試使用 */
	GV2 shmGV2

	/* statistic */
	Statistic [ptttype.STAT_MAX]uint32

	// 從前作為故鄉使用 (fromcache). 現已被 daemon/fromd 取代。
	DeprecatedHomeIp_   [ptttype.MAX_FROM]uint32
	DeprecatedHomeMask_ [ptttype.MAX_FROM]uint32
	DeprecatedHomeDesc_ [ptttype.MAX_FROM][32]byte
	DeprecatedHomeNum_  int32

	MaxUser    int32
	MaxTime    types.Time4
	FUptime    types.Time4
	FTouchTime types.Time4
	FBusyState int32
}

Jump to

Keyboard shortcuts

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