utils

package
v0.0.0-...-a05fd75 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2014 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package utils 是goose中的一个公共包,存放的是公共类型的定义和一些基础工具.

BigFile是逻辑大文件操作类,支持追加写和随机读,利用多个小文件组成的逻辑大文件提供服务 其最大的作用是避免磁盘文件过大超出系统限制

jsonfile提供结构体和磁盘文件的序列化/反序列化函数

type.go定义了goose使用到的类型

Index

Constants

View Source
const (
	GOOSE_MAX_INVLIST_SIZE = 10 * 10000
	GOOSE_MAX_QUERY_TERM   = 32

	GOOSE_DEFAULT_SEARCH_RESULT_CAPACITY = 10000
)

Variables

This section is empty.

Functions

func FileSize

func FileSize(f *os.File) (int64, error)

func GobDecode

func GobDecode(data []byte, to interface{}) error

把二进制流反序列化

func GobEncode

func GobEncode(data interface{}) ([]byte, error)

把数据编码成二进制流,函数内部分配了内存. 经过实验得到,slice编码浪费的空间很大,一个int32类型的slice有这样的实验数据:

lst1 len[0] cap[5] --GobEncode--> buf1 len[18] cap[64]
lst2 len[3] cap[5] --GobEncode--> buf2 len[21] cap[64]
lst3 len[3] cap[10] --GobEncode--> buf3 len[21] cap[64]
lst4 len[64] cap[64] --GobEncode--> buf4 len[82] cap[197]

func IntKindSize

func IntKindSize(n interface{}) (reflect.Kind, int)

反射一个数字的底层类型以及占用内存大小(字节) 字节为0表示该类型不支持

func JsonDecodeFromFile

func JsonDecodeFromFile(v interface{}, fullpath string) error

func JsonEncodeToFile

func JsonEncodeToFile(v interface{}, fullpath string) error

func Ns2Ms

func Ns2Ms(t int64) int64

func StringSignBKDR

func StringSignBKDR(text string) int64

字符串签名为数字,使用BKDRHash算法.

func StringSignMd5

func StringSignMd5(text string) int64

字符串签名为数字,使用md5结果对折.

Types

type BigFile

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

由多个小文件组成的逻辑大文件 支持追加写操作 : 只能把数据追加写到逻辑大文件的末尾,也就是最后一个文件末尾 支持读操作 : 可以读取逻辑大文件的任何一部分,但是不支持跨物理子文件 就是为了goose的data二级索引而生,没啥通用性,就是封装而已

func (*BigFile) Append

func (this *BigFile) Append(buf []byte) (*BigFileIndex, error)

追加数据,返回追加数据的存储信息.不可并发进行写操作.

func (*BigFile) Close

func (this *BigFile) Close()

关闭文件

func (*BigFile) GetStatInfo

func (this *BigFile) GetStatInfo() BigFileStat

获取逻辑文件信息

func (*BigFile) Init

func (this *BigFile) Init(path string, name string, maxFileSz uint32) error

初始化,抛弃已有的任何数据 maxFileSz : 建议物理文件的大小

func (*BigFile) Open

func (this *BigFile) Open(path string, name string) error

打开已存在的大文件,如果不存在,直接返回错误

func (*BigFile) Read

func (this *BigFile) Read(i BigFileIndex, desBuf []byte) error

读取数据,外部需要准备好够存放的desBuf

func (*BigFile) Sync

func (this *BigFile) Sync() error

type BigFileIndex

type BigFileIndex struct {
	FileNo uint8  /// 文件编号
	Offset uint32 /// 数据存储地址偏移量
	Length uint32 /// 数据长度
}

大文件索引

func (*BigFileIndex) Decode

func (this *BigFileIndex) Decode(buf []byte) error

func (*BigFileIndex) Encode

func (this *BigFileIndex) Encode(buf []byte) error

type BigFileStat

type BigFileStat struct {
	FileCnt         uint8  /// 由多少个物理文件组成
	LastFileOffset  uint32 /// 最后一个文件的文件偏移量
	SuggestFileSize uint32 /// 建议的一个物理文件的最大大小
}

记录大逻辑文件的一些必须信息

func (*BigFileStat) Reset

func (this *BigFileStat) Reset()

type BufferIterOnce

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

把一块buf当成一个doc一次返回

func NewBufferIterOnce

func NewBufferIterOnce(buf []byte) *BufferIterOnce

func (*BufferIterOnce) NextDoc

func (this *BufferIterOnce) NextDoc() interface{}

type Data

type Data []byte

全文数据

func NewData

func NewData(arg ...int) Data

NewData(length,capacity)

func (*Data) Len

func (this *Data) Len() int

type FileIter

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

func NewFileIter

func NewFileIter(fh *os.File) *FileIter

func (*FileIter) NextDoc

func (this *FileIter) NextDoc() interface{}

内部进行了一次拷贝返回[]buf

type GooseError

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

GooseError : 简单的错误日志

func NewGooseError

func NewGooseError(w string, e string, a string) *GooseError

func (*GooseError) Error

func (e *GooseError) Error() string

type InIdType

type InIdType uint32

内部id类型

type Index

type Index struct {
	InID   InIdType
	Weight TermWeight
}

索引结构 InID : 在索引库里面的内部ID,每个外部doc分配一个唯一的InID Weight : term在doc中的打分情况

type JsonStatusFile

type JsonStatusFile struct {
	// 要写入磁盘的任意类型
	SelfStatus interface{}
	// 写入磁盘的路径
	StatusFilePath string
}

磁盘json文件的序列化/反序列化小工具. 使用方法:(1)设置正确的statusFilePath;(2).设置selfStatus. 直接调用parseJsonFile和saveJsonFile

func (*JsonStatusFile) ParseJsonFile

func (this *JsonStatusFile) ParseJsonFile() error

从磁盘解析数据

func (*JsonStatusFile) SaveJsonFile

func (this *JsonStatusFile) SaveJsonFile() error

保存数据到磁盘

type MmapFile

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

func (*MmapFile) Close

func (this *MmapFile) Close() error

func (*MmapFile) Flush

func (this *MmapFile) Flush() error

func (*MmapFile) OpenFile

func (this *MmapFile) OpenFile(path string, name string, fsize uint32) error

mmap方式打开文件,文件最大大小为fsize(单位byte) 如果文件已经存在,则直接打开 如果文件不存在,则新建

func (*MmapFile) ReadBytes

func (this *MmapFile) ReadBytes(offset uint32, length uint32) ([]byte, error)

read bytes (reference)

func (*MmapFile) ReadBytesCopy

func (this *MmapFile) ReadBytesCopy(offset int32, length int32) ([]byte, error)

read bytes (copy slice)

func (*MmapFile) ReadNum

func (this *MmapFile) ReadNum(offset uint32, destSz uint32) (uint64, error)

读取offset开始的destSz个字节作为数字返回

func (*MmapFile) ReadUint32

func (this *MmapFile) ReadUint32(offset uint32) (uint32, error)

func (*MmapFile) ReadUint64

func (this *MmapFile) ReadUint64(offset uint32) (uint64, error)

func (*MmapFile) ReadUint8

func (this *MmapFile) ReadUint8(offset uint32) (uint8, error)

对ReadNum的封装,方便外部直接使用

func (*MmapFile) WriteBytes

func (this *MmapFile) WriteBytes(offset uint32, buf []byte, length uint32) error

write max length bytes

func (*MmapFile) WriteNum

func (this *MmapFile) WriteNum(offset uint32, n interface{}) error

从offset位置开始写入数字n,占用空间大小根据n反射类型决定

type OutIdType

type OutIdType uint32

外部id类型

type SearchResult

type SearchResult struct {
	InId   InIdType
	OutId  OutIdType
	Weight TermWeight
}

一个检索结果

type SearchResultList

type SearchResultList []SearchResult

结果拉链

func (SearchResultList) Len

func (s SearchResultList) Len() int

支持sort包的排序

func (SearchResultList) Less

func (s SearchResultList) Less(i, j int) bool

func (SearchResultList) Swap

func (s SearchResultList) Swap(i, j int)

type TermInDoc

type TermInDoc struct {
	// 不存储原始串,存储签名
	Sign TermSign
	// term在doc中的打分,TermWeight在策略中可以自由定制
	Weight TermWeight
}

term在doc中的信息

type TermInQuery

type TermInQuery struct {
	// 不存储原始串,存储签名
	Sign TermSign

	// term在query中的打分,TermWeight在策略中可以自由定制
	Weight TermWeight

	// term在query中的属性信息,在策略中可以自由定制存储
	Attr uint32

	// 是否是可省词
	CanOmit bool

	// 是否忽略位置信息
	SkipOffset bool
}

type TermSign

type TermSign int64

在整个检索系统中,都把term转换为64位签名使用

type TermSignSlice

type TermSignSlice []TermSign

func (TermSignSlice) Len

func (s TermSignSlice) Len() int

支持sort包的排序

func (TermSignSlice) Less

func (s TermSignSlice) Less(i, j int) bool

func (TermSignSlice) Swap

func (s TermSignSlice) Swap(i, j int)

type TermWeight

type TermWeight int32

在整个检索系统中,最多32位表示权重的信息

type Value

type Value []byte

value数据

func NewValue

func NewValue(arg ...int) Value

NewValue(length,capacity)

Jump to

Keyboard shortcuts

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