本文整理汇总了Golang中github.com/tchap/go-patricia/patricia.Prefix函数的典型用法代码示例。如果您正苦于以下问题:Golang Prefix函数的具体用法?Golang Prefix怎么用?Golang Prefix使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Prefix函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Subscribe
// Subscribe registers handler to be invoked on any event that matches prefix topicPrefix.
//
// The handlers registered with Subscribe are invoked asynchronously using go when an event
// matching the relevant prefix is received.
func (exchange *Exchange) Subscribe(topicPrefix Topic, handler EventHandler) (Handle, error) {
exchange.cond.L.Lock()
defer exchange.cond.L.Unlock()
if exchange.state != stateRunning {
return 0, ErrInvalidState
}
handle, err := exchange.getHandle(topicPrefix)
if err != nil {
return 0, err
}
record := &handlerRecord{
handle: handle,
handler: handler,
}
if item := exchange.trie.Get(patricia.Prefix(topicPrefix)); item == nil {
records := []*handlerRecord{record}
exchange.trie.Insert(patricia.Prefix(topicPrefix), &records)
} else {
records := item.(*[]*handlerRecord)
*records = append(*records, record)
}
return record.handle, nil
}
示例2: MkPath
// MkPath makes a new directory under the current rootPath. The path variable
// is expected to be relative to the rootPath. The diectory will both be added
// to the internal representation AND be added to the real filesystem. Fails
// if the specified path is a file, returns nil if the specified path already
// exists.
func (fs *TransientFilesystem) MkPath(path string) error {
absPath := realPath(fs, path)
// actually make path
if absPath == fs.RootPath() {
return nil
}
fi, err := os.Stat(absPath)
if err != nil && !os.IsNotExist(err) {
return ErrFileNoExist
}
if fi != nil && !fi.IsDir() {
return ErrFileExists
}
doTrieGet := func() (bool, error) {
fs.lock.RLock()
defer fs.lock.RUnlock()
if fs.trie.Match(trie.Prefix(absPath)) {
if fs.trie.Get(trie.Prefix(absPath)).(os.FileInfo).IsDir() {
return true, nil
} else {
return false, fmt.Errorf("specified path %s exists in memory as a file, but does not exist on disk", path)
}
}
return false, nil
}
existsInMem, err := doTrieGet()
if err != nil {
return err
} else if existsInMem {
return nil
}
if err = os.MkdirAll(absPath, 0755); err != nil {
return err
}
fi, err = os.Stat(absPath)
if err != nil {
return err
}
// insert into trie
insertIntoTrie := func() error {
fs.lock.Lock()
defer fs.lock.Unlock()
if success := fs.trie.Insert(trie.Prefix(absPath), fi); !success {
return fmt.Errorf("path %s already exists in memory", path)
}
return nil
}
if err := insertIntoTrie(); err != nil {
// recover
if rmErr := os.RemoveAll(absPath); rmErr != nil {
// wow this is bad, failure inserting into the trie, AND failure
// cleaning up after ourselves
return fmt.Errorf("failure creating path %s in memory: %s, and failure cleaning up alrready created path %s: %s", path, err.Error(), path, rmErr.Error())
}
return err
}
return nil
}
示例3: main
func main() {
key := flag.String("key", "", "key in dot notation")
version := flag.Bool("v", false, "show version and exit")
flag.Parse()
if *version {
fmt.Println(Version)
os.Exit(0)
}
if *key == "" {
log.Fatal("key is required")
}
var reader *bufio.Reader
if flag.NArg() < 1 {
reader = bufio.NewReader(os.Stdin)
} else {
file, err := os.Open(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader = bufio.NewReader(file)
}
trie := patricia.NewTrie()
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
doc := make(map[string]interface{})
err = json.Unmarshal([]byte(line), &doc)
if err != nil {
log.Fatal(err)
}
val, err := StringValue(*key, doc)
if err != nil {
log.Fatal(err)
}
if trie.Match(patricia.Prefix(val)) {
log.Println("SKIP")
continue
}
b, err := json.Marshal(doc)
if err != nil {
log.Fatal(err)
}
trie.Insert(patricia.Prefix(val), 1)
fmt.Println(string(b))
}
}
示例4: longestMatchingPath
func (lm *LocationMuxer) longestMatchingPath(path string) *types.Location {
pathAsPrefix := patricia.Prefix(path)
var matchSoFar patricia.Prefix
var matchedItem *types.Location
err := lm.locationTrie.Visit(func(prefix patricia.Prefix, item patricia.Item) error {
if len(prefix) > len(pathAsPrefix) {
return patricia.SkipSubtree
} else if len(prefix) > len(matchSoFar) && bytes.EqualFold(prefix, pathAsPrefix[:len(prefix)]) {
exactMatch := len(prefix) == len(pathAsPrefix)
matchedLocation := item.(*types.Location)
if isLocationType(matchedLocation, exact) && !exactMatch {
return nil
}
matchedItem = matchedLocation
matchSoFar = prefix
if exactMatch {
return errExactMatch // not an error, just so the search is canceled
}
}
return nil
})
if err != nil && err != errExactMatch {
panic(err) // an impossible error
}
return matchedItem
}
示例5: Get
// Get retrieves an ID from the TruncIndex. If there are multiple IDs
// with the given prefix, an error is thrown.
func (idx *TruncIndex) Get(s string) (string, error) {
if s == "" {
return "", ErrEmptyPrefix
}
var (
id string
)
subTreeVisitFunc := func(prefix patricia.Prefix, item patricia.Item) error {
if id != "" {
// we haven't found the ID if there are two or more IDs
id = ""
return ErrAmbiguousPrefix
}
id = string(prefix)
return nil
}
idx.RLock()
defer idx.RUnlock()
if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil {
return "", err
}
if id != "" {
return id, nil
}
return "", fmt.Errorf("no such id: %s", s)
}
示例6: MkFile
// MkFile makes a file consisting of the provided content at the given path.
// If the path does not already exist, an error will be returned. If the file
// already exists, an error will be returned.
func (fs *TransientFilesystem) MkFile(path string, content []byte) error {
if !fs.PathExists(filepath.Dir(path)) {
return ErrPathNoExist
} else if fs.PathExists(path) {
return ErrFileExists
}
absPath := realPath(fs, path)
if err := ioutil.WriteFile(absPath, content, 0644); err != nil {
return err
}
doInsertTrie := func() error {
fs.lock.Lock()
defer fs.lock.Unlock()
fi, err := os.Stat(absPath)
if err != nil {
return err
}
if success := fs.trie.Insert(trie.Prefix(absPath), fi); !success {
// path already exists in trie
if err := os.RemoveAll(absPath); err != nil {
// this is bad
return fmt.Errorf("file %s already exists in memory and error removing file %s: %s", path, path, err)
}
// technically this is a little misleading, as it refers to the
// trie, not what's on disk, but c'est la vie
return ErrFileExists
}
return nil
}
if err := doInsertTrie(); err != nil {
return err
}
return nil
}
示例7: RmPath
// RmPath recursively removes a path from the filesystem. The path argument
// can be either a file or directory relative to the rootPath. Returns an
// error if the path does not exist, or if there was an error removing
// part of the path.
func (fs *TransientFilesystem) RmPath(path string) error {
absPath := realPath(fs, path)
switch path {
case "/", "":
return fmt.Errorf("unwilling to delete root filesystem path %s", fs.RootPath())
default:
}
doGet := func() interface{} {
fs.lock.RLock()
defer fs.lock.RUnlock()
return fs.trie.Get(trie.Prefix(absPath))
}
item := doGet()
if item == nil {
return ErrPathNoExist
}
if !item.(os.FileInfo).IsDir() {
return ErrFileExists
}
// Make this a function so we can release the mutex ASAP
doTrieDelete := func() error {
fs.lock.Lock()
defer fs.lock.Unlock()
if deleted := fs.trie.Delete(trie.Prefix(absPath)); !deleted {
return fmt.Errorf("path %s does not exist in memory", path)
}
return nil
}
if err := doTrieDelete(); err != nil {
return err
}
if err := os.RemoveAll(absPath); err != nil {
// put item back in the tree
// eat the error here
fs.trie.Insert(trie.Prefix(absPath), item)
// possibly in an unknown state at this point. whomp whomp.
return err
}
return nil
}
示例8: Delete
// Delete removes an ID from the TruncIndex. If there are multiple IDs
// with the given prefix, an error is thrown.
func (idx *TruncIndex) Delete(id string) error {
idx.Lock()
defer idx.Unlock()
if _, exists := idx.ids[id]; !exists || id == "" {
return fmt.Errorf("no such id: '%s'", id)
}
delete(idx.ids, id)
if deleted := idx.trie.Delete(patricia.Prefix(id)); !deleted {
return fmt.Errorf("no such id: '%s'", id)
}
return nil
}
示例9: LoadDictionaries
func LoadDictionaries() {
var newDictionaryMap = make(map[string]*patricia.Trie)
var itemMap = ImportDictionaries()
numPrefixes := 0
numSuggestions := 0
numDictionaries := 0
for dictionaryName, suggestItems := range itemMap {
numDictionaries++
log.Print("Loading dictionary " + dictionaryName)
// First see if the trie already exists
var trie, ok = newDictionaryMap[dictionaryName]
if !ok {
trie = patricia.NewTrie()
}
// Great, we have a trie, now let's see if prefixes for the
// suggestItems exist in the trie
for _, suggestItem := range suggestItems {
numSuggestions++
//Tokenize the suggested term by whitespace. Each token will become a prefix in the trie
var tokens = strings.Fields(suggestItem.Term)
tokens = append(tokens, suggestItem.Term)
for _, token := range tokens {
numPrefixes++
//TODO: use ascii folding
lowerToken := strings.ToLower(token)
// The values in the trie are sorted sets of SuggestItems
trieItem := trie.Get([]byte(lowerToken))
if trieItem != nil {
suggestItemSet := trieItem.(treeset.Set)
//If the set already exists, add the new suggestion to the set
suggestItemSet.Add(suggestItem)
} else {
// Otherwise create a new set, add the SuggestItem, and insert it into
// the trie using the lowercase token for the prefix
suggestItemSet := treeset.NewWith(models.SuggestItemComparator)
// log.Printf("Inserting suggestion item %s (%s)", lowerToken, suggestItem.Term)
suggestItemSet.Add(suggestItem)
trie.Insert(patricia.Prefix([]byte(lowerToken)), *suggestItemSet)
}
}
}
newDictionaryMap[dictionaryName] = trie
log.Print("Dictionary " + dictionaryName + " loaded")
}
//Atomic swap
DictionaryMap = newDictionaryMap
log.Printf("All dictionaries updated")
}
示例10: NewNode
// NewDefaultRouter creates a very basic WAMP router.
func NewNode() Router {
log.Println("Creating new Rabric Node")
trie := patricia.NewTrie()
r := &Realm{URI: "pd"}
trie.Insert(patricia.Prefix("pd"), r)
return &node{
realms: make(map[URI]Realm),
sessionOpenCallbacks: []func(uint, string){},
sessionCloseCallbacks: []func(uint, string){},
root: trie,
}
}
示例11: Unsubscribe
// Unsubscribe cancels the event handler represented by handle.
func (exchange *Exchange) Unsubscribe(handle Handle) error {
exchange.cond.L.Lock()
defer exchange.cond.L.Unlock()
if exchange.state != stateRunning {
return ErrInvalidState
}
topic, ok := exchange.topicForHandle[handle]
if !ok {
return ErrInvalidHandle
}
delete(exchange.topicForHandle, handle)
item := exchange.trie.Get(patricia.Prefix(topic))
if item == nil {
return ErrInvalidHandle
}
records := item.(*[]*handlerRecord)
if len(*records) == 1 {
if exchange.trie.Delete(patricia.Prefix(topic)) {
return nil
}
} else {
for i, record := range *records {
if record.handle == handle {
*records = append((*records)[:i], (*records)[i+1:]...)
return nil
}
}
}
return ErrInvalidHandle
}
示例12: TestTransientFilesystemerCreate
func (suite *TheSuite) TestTransientFilesystemerCreate(c *C) {
fileMap := suite.mkDirs(c, suite.tmpdir)
fs := suite.mkTFS()
c.Assert(fs.trie.Get(trie.Prefix("aaaaaaaaaaa")), IsNil)
c.Assert(fs.trie.Get(trie.Prefix(fs.rootPath)), NotNil)
c.Assert(fs.trie.Get(trie.Prefix(fileMap["d1"])), NotNil)
c.Assert(fs.trie.Get(trie.Prefix(fileMap["f1"])), NotNil)
c.Assert(fs.trie.Get(trie.Prefix(fileMap["f2"])), NotNil)
c.Assert(fs.trie.Get(trie.Prefix(fileMap["f3"])), NotNil)
}
示例13: addID
func (idx *TruncIndex) addID(id string) error {
if strings.Contains(id, " ") {
return ErrIllegalChar
}
if id == "" {
return ErrEmptyPrefix
}
if _, exists := idx.ids[id]; exists {
return fmt.Errorf("id already exists: '%s'", id)
}
idx.ids[id] = struct{}{}
if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted {
return fmt.Errorf("failed to insert id: %s", id)
}
return nil
}
示例14: addId
func (idx *TruncIndex) addId(id string) error {
if strings.Contains(id, " ") {
return fmt.Errorf("Illegal character: ' '")
}
if id == "" {
return ErrNoID
}
if _, exists := idx.ids[id]; exists {
return fmt.Errorf("Id already exists: '%s'", id)
}
idx.ids[id] = struct{}{}
if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted {
return fmt.Errorf("Failed to insert id: %s", id)
}
return nil
}
示例15: GetFile
// GetFile gets the file at path and returns a FSFiler object, which both
// describes the file and has a pointer to a copy of the file in a byte slice.
// If the files does not exist or path is actually a directory, an error is returned.
func (fs *TransientFilesystem) GetFile(path string) (FSFiler, error) {
absPath := realPath(fs, path)
if !fs.PathExists(path) {
return nil, ErrFileNoExist
} else if fi, err := os.Stat(realPath(fs, path)); err != nil || fi.IsDir() {
if err != nil {
return nil, err
}
return nil, fmt.Errorf("Specified path %s is a directory", realPath(fs, path))
}
item := fs.trie.Get(trie.Prefix(absPath))
if item == nil {
return nil, ErrPathNoExist
}
fi, _ := os.Stat(realPath(fs, path))
return &FSFile{FileInfo: fi, absPath: absPath}, nil
}