aboutsummaryrefslogtreecommitdiff
path: root/weed/sequence/snowflake_sequencer.go
diff options
context:
space:
mode:
authorhilimd <68371223+hilimd@users.noreply.github.com>2021-04-06 13:39:26 +0800
committerGitHub <noreply@github.com>2021-04-06 13:39:26 +0800
commit8693cdacae5b4201ccd259382a02392250b0890e (patch)
treeb565af4380b0f59a61b1d3c9fad8f04cbb802e7c /weed/sequence/snowflake_sequencer.go
parent17d02264f33f501e124060ade7b0b39e687aaa3d (diff)
parenta37eca78cd680858d021cd864766b0847328a8d7 (diff)
downloadseaweedfs-8693cdacae5b4201ccd259382a02392250b0890e.tar.xz
seaweedfs-8693cdacae5b4201ccd259382a02392250b0890e.zip
Merge pull request #75 from chrislusf/master
sync
Diffstat (limited to 'weed/sequence/snowflake_sequencer.go')
-rw-r--r--weed/sequence/snowflake_sequencer.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/weed/sequence/snowflake_sequencer.go b/weed/sequence/snowflake_sequencer.go
new file mode 100644
index 000000000..300449fa0
--- /dev/null
+++ b/weed/sequence/snowflake_sequencer.go
@@ -0,0 +1,46 @@
+package sequence
+
+import (
+ "fmt"
+ "hash/fnv"
+
+ "github.com/bwmarrin/snowflake"
+ "github.com/chrislusf/seaweedfs/weed/glog"
+)
+
+// a simple snowflake Sequencer
+type SnowflakeSequencer struct {
+ node *snowflake.Node
+}
+
+func NewSnowflakeSequencer(nodeid string) (*SnowflakeSequencer, error) {
+ nodeid_hash := hash(nodeid) & 0x3ff
+ glog.V(0).Infof("use snowflake seq id generator, nodeid:%s hex_of_nodeid: %x", nodeid, nodeid_hash)
+ node, err := snowflake.NewNode(int64(nodeid_hash))
+ if err != nil {
+ fmt.Println(err)
+ return nil, err
+ }
+
+ sequencer := &SnowflakeSequencer{node: node}
+ return sequencer, nil
+}
+
+func hash(s string) uint32 {
+ h := fnv.New32a()
+ h.Write([]byte(s))
+ return h.Sum32()
+}
+
+func (m *SnowflakeSequencer) NextFileId(count uint64) uint64 {
+ return uint64(m.node.Generate().Int64())
+}
+
+// ignore setmax as we are snowflake
+func (m *SnowflakeSequencer) SetMax(seenValue uint64) {
+}
+
+// return a new id as no Peek is stored
+func (m *SnowflakeSequencer) Peek() uint64 {
+ return uint64(m.node.Generate().Int64())
+}