gmodel

package module
v0.0.0-...-ae5ab06 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 13 Imported by: 0

README

gmodel

一个基于leveldb的内容存储模块,适合在超低配置的服务器上运行具有大量文章数据的网站,比如:新闻、词典等类型的网站。

特点

  • 几乎不占用内存,启动快

  • 支持增删改查等基本操作,支持按分类查找

基本信息:

  • gmodel:包装了下面的文章和分类,对外一般用这个,如果需要防止网站被采集,还需要使用下面的 idmgr 模块

  • article:文章

  • tag:分类

  • kvstore:kv存储组件

  • utils:实用函数

  • idmgr:将整型ID和字符串ID映射,避免通过递增ID就可以遍历网站

  • ttl_checker:生命周期管理,这里每篇文章都可以设置单独的TTL

  • ttl_cache:包装了 kvstore 和 ttl_checker,支持给每一个 key 设置 TTL

  • ttl_idmgr:将整型ID和字符串ID映射,支持给每个映射设置 TTL,避免数据库膨胀

  • remote:提供远端访问的API服务器,方便 website/admin/spider 分离

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetStringKey

func GetStringKey(id uint64) string

将uint64转成string,不足20位时左边补0,作用同 GetStringKey 注意:uint64最大值是 18446744073709551615,长度为 20

func IdToBytes

func IdToBytes(intId uint64) []byte

func RandomBytes

func RandomBytes(minLen, maxLen int) []byte

得到一个长度在区间[minLen, maxLen]内的随机字符数组 一般6位长度就够了:62^6 = 56800235584 = 568亿

Types

type Article

type Article struct {
	Id         uint64   `json:"id"`          // 文章ID,从1开始自增,唯一标识,不允许修改
	TagIds     []uint64 `json:"tag_ids"`     // 文章分类,多个分类ID
	Data       string   `json:"data"`        // 文章数据,由上层解析
	CreateTime int64    `json:"create_time"` // 文章创建的时间戳,单位:秒
	TTL        int64    `json:"ttl"`         // 文章的生命周期,单位:秒
}

type ArticleExpiredCallback

type ArticleExpiredCallback = func(article *Article)

注意:回调中不能修改 article

type ArticleMgr

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

func (*ArticleMgr) Add

func (m *ArticleMgr) Add(article *Article) (uint64, error)

增加文章,返回文章ID

func (*ArticleMgr) Close

func (m *ArticleMgr) Close() error

func (*ArticleMgr) Count

func (m *ArticleMgr) Count() uint64

获取文章数量

func (*ArticleMgr) Delete

func (m *ArticleMgr) Delete(id uint64) error

删除文章

func (*ArticleMgr) GetById

func (m *ArticleMgr) GetById(id uint64) (*Article, error)

获取文章

func (*ArticleMgr) GetMaxId

func (m *ArticleMgr) GetMaxId() uint64

返回当前最大(最新)的文章ID

func (*ArticleMgr) Next

func (m *ArticleMgr) Next(id uint64, n int) []*Article

返回指定文章ID的后N篇文章(不包括当前id) 如果id等于0,则表示获取最旧的N篇文章

func (*ArticleMgr) Open

func (m *ArticleMgr) Open(path string) error

打开数据库文件

func (*ArticleMgr) Prev

func (m *ArticleMgr) Prev(id uint64, n int) []*Article

返回指定文章ID的前N篇文章(不包括当前id) 如果id大于CurrentSequence,则表示获取最新的N篇文章

func (*ArticleMgr) Update

func (m *ArticleMgr) Update(article *Article) error

修改文章

type Config

type Config struct {
	ArticleDBPath          string                 // 文章数据库路径
	TagDBPath              string                 // 文章的分类数据库路径
	IndexDBPath            string                 // 索引数据库路径
	TTLDBPath              string                 // TTL数据库路径,用于存储每篇文章的生命周期
	ArticleExpiredCallback ArticleExpiredCallback // 文章过期的回调
}

type GModel

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

封装 article 和 tag 两个模块,方便外部使用

func (*GModel) AddArticle

func (m *GModel) AddArticle(tags []string, data string, ttl int64) (uint64, error)

增加文章,返回文章ID tags: 文章分类名称,可以为空,tags为空表示该文章属于未分类 data: 文章数据内容,不能为空 ttl: 文章生命周期,单位:秒,大于0才生效,否则其生命周期为永久

func (*GModel) Close

func (m *GModel) Close() error

func (*GModel) DeleteArticle

func (m *GModel) DeleteArticle(articleId uint64) error

删除文章

func (*GModel) GetArticle

func (m *GModel) GetArticle(articleId uint64) (*Article, error)

获取文章

func (*GModel) GetArticleCount

func (m *GModel) GetArticleCount() uint64

返回文章总数

func (*GModel) GetArticleCountByTag

func (m *GModel) GetArticleCountByTag(tagName string) uint64

返回分类下的文章数量

func (*GModel) GetMaxArticleId

func (m *GModel) GetMaxArticleId() uint64

返回当前最大(最新)的文章ID

func (*GModel) GetNextArticles

func (m *GModel) GetNextArticles(articleId uint64, n int) []*Article

获取指定文章的后N篇(不包括当前这篇) 如果 articleId 等于 0,则表示获取最旧的N篇文章(id最小的N篇)

func (*GModel) GetNextArticlesByTag

func (m *GModel) GetNextArticlesByTag(tagName string, articleId uint64, n int) []*Article

获取指定文章的后N篇(不包括当前这篇),保证这N篇文章的分类为tagName tagName为文章分类,如果tag不存在,则返回空数组,如果tag为空,则表示未分类,会返回未分类的文章 articleId为文章ID,如果articleId为0,则返回该分类最旧的N篇文章(id最小的N篇)

func (*GModel) GetNextTags

func (m *GModel) GetNextTags(name string, n int) []*Tag

返回指定分类的后N个分类(不包括当前分类) 如果分类不存在,则表示获取最旧的N个分类

func (*GModel) GetPrevArticles

func (m *GModel) GetPrevArticles(articleId uint64, n int) []*Article

获取指定文章的前N篇(不包括当前这篇) 如果 articleId 大于 最大的文章ID,则表示获取最新的N篇文章(id最大的N篇)

func (*GModel) GetPrevArticlesByTag

func (m *GModel) GetPrevArticlesByTag(tagName string, articleId uint64, n int) []*Article

获取指定文章的前N篇(不包括当前这篇),保证这N篇文章的分类为tagName tagName为文章分类,如果tag不存在,则返回空数组,如果tag为空,则表示未分类,会返回未分类的文章 articleId为文章ID,如果 articleId 大于 最大的文章ID,则返回该分类最新的N篇文章(id最大的N篇)

func (*GModel) GetPrevTags

func (m *GModel) GetPrevTags(name string, n int) []*Tag

返回指定分类的前N个分类(不包括当前分类) 如果分类不存在,则表示获取最新的N个分类

func (*GModel) GetTagById

func (m *GModel) GetTagById(id uint64) (*Tag, error)

根据分类ID获取分类

func (*GModel) GetTagByName

func (m *GModel) GetTagByName(name string) (*Tag, error)

根据分类名称获取分类

func (*GModel) GetTagCount

func (m *GModel) GetTagCount() uint64

返回分类总数

func (*GModel) Open

func (m *GModel) Open(conf *Config) error

func (*GModel) RenameTag

func (m *GModel) RenameTag(oldName, newName string) error

修改分类名称

func (*GModel) UpdateArticle

func (m *GModel) UpdateArticle(articleId uint64, newTags []string, newData string, newTTL int64) error

修改文章 articleId: 待修改的文章ID newTags: 新的分类名称,可以为空,tags为空表示该文章属于未分类 newData: 文章数据内容,不能为空 newTTL: 文章生命周期,单位:秒,大于0才生效,否则其生命周期为永久

type IdMgr

type IdMgr struct {

	// 字符串ID的长度,注意这个是期望值,如果冲突太多,则会自动增加位数
	// 默认为 8
	StringIdLength int
	// contains filtered or unexported fields
}

ID是递增的,所以不需要考虑删除,只管做映射即可 这个类的作用:将整型ID和字符串ID互相映射 只能转换整型ID到字符串ID

func (*IdMgr) AddIntId

func (m *IdMgr) AddIntId(intId uint64) (string, error)

增加一个整型ID 返回对应的字符串ID

func (*IdMgr) Close

func (m *IdMgr) Close() error

func (*IdMgr) Count

func (m *IdMgr) Count() uint64

获取记录的数量

func (*IdMgr) GetIntId

func (m *IdMgr) GetIntId(stringId string) (uint64, bool)

获取字符串ID对应的整型ID

func (*IdMgr) GetStringId

func (m *IdMgr) GetStringId(intId uint64) (string, bool)

获取整型ID对应的字符串ID

func (*IdMgr) Open

func (m *IdMgr) Open(path string) error

打开数据库文件

func (*IdMgr) SetIdMap

func (m *IdMgr) SetIdMap(intId uint64, stringId string) error

保存intId和stringId的映射

type KVStore

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

func (*KVStore) Close

func (kv *KVStore) Close() error

func (*KVStore) Count

func (kv *KVStore) Count() uint64

返回 key 的数量

func (*KVStore) CurrentSequence

func (kv *KVStore) CurrentSequence() uint64

返回当前Sequence

func (*KVStore) Delete

func (kv *KVStore) Delete(key []byte) error

func (*KVStore) Get

func (kv *KVStore) Get(key []byte) ([]byte, error)

func (*KVStore) Has

func (kv *KVStore) Has(key []byte) bool

func (*KVStore) Next

func (kv *KVStore) Next(key []byte, n int) [][]byte

返回指定key后面的n个key(不包括当前key,当前key也可以不存在) 如果当前key为空数组或者nil,表示从头开始遍历 如果当前key为字典序最大,则返回的结果为空;如果当前key为字典序最小,则返回最前的n个key 注意:leveldb 是根据 key 的字典序排序的

func (*KVStore) NextSequence

func (kv *KVStore) NextSequence() (uint64, error)

生成并返回下一个Sequence 注意这是一个读写操作

func (*KVStore) Open

func (kv *KVStore) Open(path string) error

func (*KVStore) Prev

func (kv *KVStore) Prev(key []byte, n int) [][]byte

返回指定key前面的n个key(不包括当前key,当前key也可以不存在) 如果当前key为空数组或者nil,表示从末尾开始遍历 如果当前key为字典序最小,则返回的结果为空;如果当前key为字典序最大,则返回最后的n个key 注意:leveldb 是根据 key 的字典序排序的

func (*KVStore) Put

func (kv *KVStore) Put(key, value []byte) error

type KeyExpiredCallback

type KeyExpiredCallback = func(info *TTLInfo)

注意:回调中不能修改 info

type TTLCache

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

func (*TTLCache) Close

func (c *TTLCache) Close() error

func (*TTLCache) Count

func (c *TTLCache) Count() uint64

func (*TTLCache) Delete

func (c *TTLCache) Delete(key []byte) error

func (*TTLCache) Get

func (c *TTLCache) Get(key []byte) ([]byte, error)

func (*TTLCache) Has

func (c *TTLCache) Has(key []byte) bool

func (*TTLCache) Open

func (c *TTLCache) Open(dataDBPath, ttlDBPath string) error

func (*TTLCache) Put

func (c *TTLCache) Put(key, value []byte, ttl int64) error

ttl: 生命周期,单位:秒,大于0才生效,否则表示永久存储

type TTLChecker

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

func (*TTLChecker) Close

func (c *TTLChecker) Close() error

func (*TTLChecker) Delete

func (c *TTLChecker) Delete(key []byte) error

func (*TTLChecker) GetInfo

func (c *TTLChecker) GetInfo(key []byte) (*TTLInfo, error)

func (*TTLChecker) GetKeyCount

func (c *TTLChecker) GetKeyCount() uint64

func (*TTLChecker) Open

func (c *TTLChecker) Open(path string, callback KeyExpiredCallback) error

func (*TTLChecker) SetTTL

func (c *TTLChecker) SetTTL(key []byte, createTime, ttl int64) error

设置 key 的生命周期(TTL),可以用于新增或者更新,TTL单位:秒 注意:createTime 和 ttl 大于 0 才生效

type TTLIdMgr

type TTLIdMgr struct {

	// 字符串ID的长度,注意这个是期望值,如果冲突太多,则会自动增加位数
	// 默认为 8
	StringIdLength int
	// contains filtered or unexported fields
}

这个类的作用:将整型ID和字符串ID互相映射,允许设置映射记录的有效期,避免数据库膨胀 只能转换整型ID到字符串ID

func (*TTLIdMgr) AddIntId

func (m *TTLIdMgr) AddIntId(intId uint64, ttl int64) (string, error)

增加一个整型ID,ttl秒后会自动删除该记录(ttl大于0才有效,否则永不删除) 返回对应的字符串ID

func (*TTLIdMgr) Close

func (m *TTLIdMgr) Close() error

func (*TTLIdMgr) Count

func (m *TTLIdMgr) Count() uint64

获取记录的数量

func (*TTLIdMgr) GetIntId

func (m *TTLIdMgr) GetIntId(stringId string) (uint64, bool)

获取字符串ID对应的整型ID

func (*TTLIdMgr) GetStringId

func (m *TTLIdMgr) GetStringId(intId uint64) (string, bool)

获取整型ID对应的字符串ID

func (*TTLIdMgr) Open

func (m *TTLIdMgr) Open(path string) error

打开数据库文件

func (*TTLIdMgr) SetIdMap

func (m *TTLIdMgr) SetIdMap(intId uint64, stringId string, ttl int64) error

保存intId和stringId的映射

type TTLInfo

type TTLInfo struct {
	Key         []byte `json:"key"`          // 唯一Key
	CreateTime  int64  `json:"create-time"`  // 创建时间戳,单位:秒
	TTL         int64  `json:"ttl"`          // 生命周期,单位:秒
	ExpiredTime int64  `json:"expired-time"` // 过期时间戳,时间单位:秒,该值等于 CreateTime + TTL
}

func (*TTLInfo) Less

func (info *TTLInfo) Less(other interface{}) bool

type Tag

type Tag struct {
	Id           uint64 `json:"id"`            // 分类ID,从1开始自增,唯一标识,不允许修改
	Name         string `json:"name"`          // 分类名称,唯一标识,允许修改
	ArticleCount uint64 `json:"article_count"` // 该分类下的文章数量
}

type TagMgr

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

分类管理器

func (*TagMgr) Add

func (m *TagMgr) Add(name string) (uint64, error)

增加分类,返回分类ID 如果分类已经存在,则返回分类ID

func (*TagMgr) AddArticleCountForId

func (m *TagMgr) AddArticleCountForId(id uint64, count int64) (uint64, error)

增加(或减少)指定分类下的文章数量 返回更新后该分类下的文章数量

func (*TagMgr) AddArticleCountForName

func (m *TagMgr) AddArticleCountForName(name string, count int64) (uint64, error)

增加(或减少)指定分类下的文章数量 返回更新后该分类下的文章数量

func (*TagMgr) Close

func (m *TagMgr) Close() error

func (*TagMgr) Count

func (m *TagMgr) Count() uint64

获取分类数量

func (*TagMgr) DeleteById

func (m *TagMgr) DeleteById(id uint64) error

删除分类

func (*TagMgr) DeleteByName

func (m *TagMgr) DeleteByName(name string) error

删除分类

func (*TagMgr) GetArticleCountById

func (m *TagMgr) GetArticleCountById(id uint64) uint64

获取指定分类下的文章数量

func (*TagMgr) GetArticleCountByName

func (m *TagMgr) GetArticleCountByName(name string) uint64

获取指定分类下的文章数量

func (*TagMgr) GetById

func (m *TagMgr) GetById(id uint64) (*Tag, error)

根据ID获取分类

func (*TagMgr) GetByName

func (m *TagMgr) GetByName(name string) (*Tag, error)

根据名字获取分类

func (*TagMgr) Next

func (m *TagMgr) Next(id uint64, n int) []*Tag

返回指定分类ID的后N个分类(不包括当前id) 如果id等于0,则表示获取最旧的N个分类

func (*TagMgr) NextByName

func (m *TagMgr) NextByName(name string, n int) []*Tag

返回指定分类的后N个分类(不包括当前分类) 如果分类不存在,则表示获取最旧的N个分类

func (*TagMgr) Open

func (m *TagMgr) Open(path string) error

打开数据库文件

func (*TagMgr) Prev

func (m *TagMgr) Prev(id uint64, n int) []*Tag

返回指定分类ID的前N个分类(不包括当前id) 如果id大于CurrentSequence,则表示获取最新的N个分类

func (*TagMgr) PrevByName

func (m *TagMgr) PrevByName(name string, n int) []*Tag

返回指定分类的前N个分类(不包括当前分类) 如果分类不存在,则表示获取最新的N个分类

func (*TagMgr) Rename

func (m *TagMgr) Rename(oldName string, newName string) error

修改分类名字

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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