Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "github.com/vporoshok/symbols" ) // User is a domain model type User struct { ID int Name string Labels []string } // UserCache is a persistent cache of users type UserCache struct { data map[int]cachedUser dict symbols.Dictionary // using Dictionary to deduplicate labels } // cachedUser is a special struct to glue symbols with domain model type cachedUser struct { name, labels symbols.Symbol } // Add user to cache func (cache *UserCache) Add(user User) { if _, ok := cache.data[user.ID]; !ok { if cache.data == nil { cache.data = make(map[int]cachedUser) } cache.data[user.ID] = cachedUser{ name: cache.dict.AddString(user.Name), labels: symbols.AddStrings(&cache.dict, user.Labels), } } } // Get user from cache by id func (cache UserCache) Get(id int) (User, bool) { if user, ok := cache.data[id]; ok { return User{ ID: id, Name: cache.dict.GetString(user.name), Labels: symbols.GetStrings(cache.dict, user.labels), }, true } return User{}, false } func main() { cache := new(UserCache) cache.Add(User{ ID: 1, Name: "John", Labels: []string{"developer", "golang", "bicycle"}, }) cache.Add(User{ ID: 2, Name: "Mary", Labels: []string{"developer", "golang", "running"}, }) cache.Add(User{ ID: 3, Name: "Albert", Labels: []string{"manager", "bicycle", "running"}, }) fmt.Println(cache.Get(1)) fmt.Println(cache.Get(2)) fmt.Println(cache.Get(3)) // Dictionary now contain strings // John, Mary, Albert // developer, golang, bicycle, running, manager // and composition of John's, Mary's and Albert's labels as 3-byte strings fmt.Println(cache.dict.Len()) // 11 }
Output: {1 John [developer golang bicycle]} true {2 Mary [developer golang running]} true {3 Albert [manager bicycle running]} true 11
Index ¶
Examples ¶
Constants ¶
View Source
const ( // PageSize 1MB PageSize = 1 << pageSizeLog // LongGuard length of string to store outside of pages and not to check on duplicates LongGuard = 1<<(32-pageSizeLog) - 1 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Dictionary ¶
type Dictionary struct { Store // contains filtered or unexported fields }
Dictionary deduplicate wrapper for Store
Use it on filling Store and use dict.Store in runtime to reduce memory utilization.
func (*Dictionary) AddString ¶
func (dict *Dictionary) AddString(s string) Symbol
AddString check string to duplicate and return existed Symbol or create new
func (*Dictionary) DropIndex ¶
func (dict *Dictionary) DropIndex()
DropIndex clean map to reduce memory utilization
func (Dictionary) State ¶ added in v1.3.0
func (dict Dictionary) State() DictionaryState
State of the dictionary
type DictionaryState ¶ added in v1.3.0
type DictionaryState struct { StoreState ShortIndex, LongIndex int }
DictionaryState static information of the dictionary
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store of strings
func (Store) Dump ¶ added in v1.1.0
func (store Store) Dump() io.ReadCloser
Dump data with comression
type StoreState ¶ added in v1.3.0
type StoreState struct {
Symbols, Pages, LongStrings int
}
StoreState static information of the store
Click to show internal directories.
Click to hide internal directories.