A Go library implementing an FST (finite state transducer)——mark下

简介:

https://github.com/couchbaselabs/vellum

Building an FST

To build an FST, create a new builder using the New() method. This method takes an io.Writer as an argument. As the FST is being built, data will be streamed to the writer as soon as possible. With this builder you MUST insert keys in lexicographic order. Inserting keys out of order will result in an error. After inserting the last key into the builder, you MUST call Close() on the builder. This will flush all remaining data to the underlying writer.

In memory:

  var buf bytes.Buffer
  builder, err := vellum.New(&buf, nil) if err != nil { log.Fatal(err) }

To disk:

  f, err := os.Create("/tmp/vellum.fst") if err != nil { log.Fatal(err) } builder, err := vellum.New(f, nil) if err != nil { log.Fatal(err) }

MUST insert keys in lexicographic order:

err = builder.Insert([]byte("cat"), 1) if err != nil { log.Fatal(err) } err = builder.Insert([]byte("dog"), 2) if err != nil { log.Fatal(err) } err = builder.Insert([]byte("fish"), 3) if err != nil { log.Fatal(err) } err = builder.Close() if err != nil { log.Fatal(err) }

Using an FST

After closing the builder, the data can be used to instantiate an FST. If the data was written to disk, you can use the Open()method to mmap the file. If the data is already in memory, or you wish to load/mmap the data yourself, you can instantiate the FST with the Load() method.

Load in memory:

  fst, err := vellum.Load(buf.Bytes())
  if err != nil { log.Fatal(err) }

Open from disk:

  fst, err := vellum.Open("/tmp/vellum.fst") if err != nil { log.Fatal(err) }

Get key/value:

  val, exists, err = fst.Get([]byte("dog"))
  if err != nil { log.Fatal(err) } if exists { fmt.Printf("contains dog with val: %d\n", val) } else { fmt.Printf("does not contain dog") }

Iterate key/values:

  itr, err := fst.Iterator(startKeyInclusive, endKeyExclusive)
  for err == nil { key, val := itr.Current() fmt.Printf("contains key: %s val: %d", key, val) err = itr.Next() } if err != nil { log.Fatal(err) }

How does the FST get built?

A full example of the implementation is beyond the scope of this README, but let's consider a small example where we want to insert 3 key/value pairs.

First we insert "are" with the value 4.

step1

Next, we insert "ate" with the value 2.

step2

Notice how the values associated with the transitions were adjusted so that by summing them while traversing we still get the expected value.

At this point, we see that state 5 looks like state 3, and state 4 looks like state 2. But, we cannot yet combine them because future inserts could change this.

Now, we insert "see" with value 3. Once it has been added, we now know that states 5 and 4 can longer change. Since they are identical to 3 and 2, we replace them.

step3

Again, we see that states 7 and 8 appear to be identical to 2 and 3.

Having inserted our last key, we call Close() on the builder.

step4

Now, states 7 and 8 can safely be replaced with 2 and 3.

For additional information, see the references at the bottom of this document.















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6806129.html,如需转载请自行联系原作者


目录
打赏
0
0
0
0
64
分享
相关文章
Go REFLECT Library | 05 - reflect.Value 动态修变量值
Go REFLECT Library | 05 - reflect.Value 动态修变量值
Go REFLECT Library | 05 - reflect.Value 动态修变量值
Go REFLECT Library | 06 - reflect.Type 和 reflect.Value 应用
Go REFLECT Library | 06 - reflect.Type 和 reflect.Value 应用
Go REFLECT Library | 02 - 反射的类型 Type
Go REFLECT Library | 02 - 反射的类型 Type
ALiyun LOG Go Consumer Library 快速入门及原理剖析
Aliyun LOG Go Consumer Library是用go语言编写的协同消费库,主要处理多个消费者同时消费logstore时自动分配shard问题。 其中消费组会自动处理shard的负载均衡,消费者failover等逻辑,这部分说明会在本篇下面的篇幅中进行详细的介绍。
11840 1
Go Consumer Library发布
信息摘要: Go SDK提供Consumer Library模式,提供断点续传、多实例负载均衡、弹性伸缩等实时消费能力适用客户: Go语言开发者版本/规格功能: Aliyun LOG Go Consumer Library是用Go语言编写的协同消费库,支持多个消费者同时消费logstore。
1129 0
监控局域网其他电脑:Go 语言迪杰斯特拉算法的高效应用
在信息化时代,监控局域网成为网络管理与安全防护的关键需求。本文探讨了迪杰斯特拉(Dijkstra)算法在监控局域网中的应用,通过计算最短路径优化数据传输和故障检测。文中提供了使用Go语言实现的代码例程,展示了如何高效地进行网络监控,确保局域网的稳定运行和数据安全。迪杰斯特拉算法能减少传输延迟和带宽消耗,及时发现并处理网络故障,适用于复杂网络环境下的管理和维护。
揭秘 Go 语言中空结构体的强大用法
Go 语言中的空结构体 `struct{}` 不包含任何字段,不占用内存空间。它在实际编程中有多种典型用法:1) 结合 map 实现集合(set)类型;2) 与 channel 搭配用于信号通知;3) 申请超大容量的 Slice 和 Array 以节省内存;4) 作为接口实现时明确表示不关注值。此外,需要注意的是,空结构体作为字段时可能会因内存对齐原因占用额外空间。建议将空结构体放在外层结构体的第一个字段以优化内存使用。

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等