mirror of
https://github.com/documize/community.git
synced 2025-07-23 15:19:42 +02:00
initial commit
This commit is contained in:
commit
18933c6767
1841 changed files with 810642 additions and 0 deletions
7
vendor/github.com/rs/xid/.travis.yml
generated
vendored
Normal file
7
vendor/github.com/rs/xid/.travis.yml
generated
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
language: go
|
||||
go:
|
||||
- 1.5
|
||||
- tip
|
||||
matrix:
|
||||
allow_failures:
|
||||
- go: tip
|
19
vendor/github.com/rs/xid/LICENSE
generated
vendored
Normal file
19
vendor/github.com/rs/xid/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,19 @@
|
|||
Copyright (c) 2015 Olivier Poitrey <rs@dailymotion.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
65
vendor/github.com/rs/xid/README.md
generated
vendored
Normal file
65
vendor/github.com/rs/xid/README.md
generated
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
# Globally Unique ID Generator
|
||||
|
||||
[](https://godoc.org/github.com/rs/xid) [](https://raw.githubusercontent.com/rs/xid/master/LICENSE) [](https://travis-ci.org/rs/xid) [](http://gocover.io/github.com/rs/xid)
|
||||
|
||||
Package xid is a globally unique id generator suited for web scale
|
||||
|
||||
Xid is using Mongo Object ID algorithm to generate globally unique ids:
|
||||
https://docs.mongodb.org/manual/reference/object-id/
|
||||
|
||||
- 4-byte value representing the seconds since the Unix epoch,
|
||||
- 3-byte machine identifier,
|
||||
- 2-byte process id, and
|
||||
- 3-byte counter, starting with a random value.
|
||||
|
||||
The binary representation of the id is compatible with Mongo 12 bytes Object IDs.
|
||||
The string representation is using URL safe base64 for better space efficiency when
|
||||
stored in that form (16 bytes).
|
||||
|
||||
UUID is 16 bytes (128 bits), snowflake is 8 bytes (64 bits), xid stands in between
|
||||
with 12 bytes with a more compact string representation ready for the web and no
|
||||
required configuration or central generation server.
|
||||
|
||||
Features:
|
||||
|
||||
- Size: 12 bytes (96 bits), smaller than UUID, larger than snowflake
|
||||
- Base64 URL safe encoded by default (16 bytes storage when transported as printable string)
|
||||
- Non configured, you don't need set a unique machine and/or data center id
|
||||
- K-ordered
|
||||
- Embedded time with 1 second precision
|
||||
- Unicity guaranted for 16,777,216 (24 bits) unique ids per second and per host/process
|
||||
|
||||
Best used with [xlog](https://github.com/rs/xlog)'s
|
||||
[RequestIDHandler](https://godoc.org/github.com/rs/xlog#RequestIDHandler).
|
||||
|
||||
References:
|
||||
|
||||
- http://www.slideshare.net/davegardnerisme/unique-id-generation-in-distributed-systems
|
||||
- https://en.wikipedia.org/wiki/Universally_unique_identifier
|
||||
- https://blog.twitter.com/2010/announcing-snowflake
|
||||
|
||||
## Install
|
||||
|
||||
go get github.com/rs/xid
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
guid := xid.New()
|
||||
|
||||
println(guid.String())
|
||||
// Output: TYjhW2D0huQoQS3J
|
||||
```
|
||||
|
||||
Get `xid` embedded info:
|
||||
|
||||
```go
|
||||
guid.Machine()
|
||||
guid.Pid()
|
||||
guid.Time()
|
||||
guid.Counter()
|
||||
```
|
||||
|
||||
## Licenses
|
||||
|
||||
All source code is licensed under the [MIT License](https://raw.github.com/rs/xid/master/LICENSE).
|
172
vendor/github.com/rs/xid/id.go
generated
vendored
Normal file
172
vendor/github.com/rs/xid/id.go
generated
vendored
Normal file
|
@ -0,0 +1,172 @@
|
|||
// Package xid is a globally unique id generator suited for web scale
|
||||
//
|
||||
// Xid is using Mongo Object ID algorithm to generate globally unique ids:
|
||||
// https://docs.mongodb.org/manual/reference/object-id/
|
||||
//
|
||||
// - 4-byte value representing the seconds since the Unix epoch,
|
||||
// - 3-byte machine identifier,
|
||||
// - 2-byte process id, and
|
||||
// - 3-byte counter, starting with a random value.
|
||||
//
|
||||
// The binary representation of the id is compatible with Mongo 12 bytes Object IDs.
|
||||
// The string representation is using URL safe base64 for better space efficiency when
|
||||
// stored in that form (16 bytes).
|
||||
//
|
||||
// UUID is 16 bytes (128 bits), snowflake is 8 bytes (64 bits), xid stands in between
|
||||
// with 12 bytes with a more compact string representation ready for the web and no
|
||||
// required configuration or central generation server.
|
||||
//
|
||||
// Features:
|
||||
//
|
||||
// - Size: 12 bytes (96 bits), smaller than UUID, larger than snowflake
|
||||
// - Base64 URL safe encoded by default (16 bytes storage when transported as printable string)
|
||||
// - Non configured, you don't need set a unique machine and/or data center id
|
||||
// - K-ordered
|
||||
// - Embedded time with 1 second precision
|
||||
// - Unicity guaranted for 16,777,216 (24 bits) unique ids per second and per host/process
|
||||
//
|
||||
// Best used with xlog's RequestIDHandler (https://godoc.org/github.com/rs/xlog#RequestIDHandler).
|
||||
//
|
||||
// References:
|
||||
//
|
||||
// - http://www.slideshare.net/davegardnerisme/unique-id-generation-in-distributed-systems
|
||||
// - https://en.wikipedia.org/wiki/Universally_unique_identifier
|
||||
// - https://blog.twitter.com/2010/announcing-snowflake
|
||||
package xid
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Code inspired from mgo/bson ObjectId
|
||||
|
||||
// ID represents a unique request id
|
||||
type ID [rawLen]byte
|
||||
|
||||
const (
|
||||
encodedLen = 16
|
||||
rawLen = 12
|
||||
)
|
||||
|
||||
// ErrInvalidID is returned when trying to unmarshal an invalid ID
|
||||
var ErrInvalidID = errors.New("invalid ID")
|
||||
|
||||
// objectIDCounter is atomically incremented when generating a new ObjectId
|
||||
// using NewObjectId() function. It's used as a counter part of an id.
|
||||
var objectIDCounter uint32
|
||||
|
||||
// init objectIDCounter to be a random initial value.
|
||||
func init() {
|
||||
b := make([]byte, 3)
|
||||
if _, e := io.ReadFull(rand.Reader, b); e != nil {
|
||||
panic(e)
|
||||
}
|
||||
objectIDCounter = uint32(b[0])<<16 | uint32(b[1])<<8 | uint32(b[2])
|
||||
}
|
||||
|
||||
// machineId stores machine id generated once and used in subsequent calls
|
||||
// to NewObjectId function.
|
||||
var machineID = readMachineID()
|
||||
|
||||
// readMachineId generates machine id and puts it into the machineId global
|
||||
// variable. If this function fails to get the hostname, it will cause
|
||||
// a runtime error.
|
||||
func readMachineID() []byte {
|
||||
id := make([]byte, 3)
|
||||
hostname, err1 := os.Hostname()
|
||||
if err1 != nil {
|
||||
// Fallback to rand number if machine id can't be gathered
|
||||
_, err2 := io.ReadFull(rand.Reader, id)
|
||||
if err2 != nil {
|
||||
panic(fmt.Errorf("cannot get hostname: %v; %v", err1, err2))
|
||||
}
|
||||
return id
|
||||
}
|
||||
hw := md5.New()
|
||||
hw.Write([]byte(hostname))
|
||||
copy(id, hw.Sum(nil))
|
||||
return id
|
||||
}
|
||||
|
||||
// New generates a globaly unique ID
|
||||
func New() ID {
|
||||
var id ID
|
||||
// Timestamp, 4 bytes, big endian
|
||||
binary.BigEndian.PutUint32(id[:], uint32(time.Now().Unix()))
|
||||
// Machine, first 3 bytes of md5(hostname)
|
||||
id[4] = machineID[0]
|
||||
id[5] = machineID[1]
|
||||
id[6] = machineID[2]
|
||||
// Pid, 2 bytes, specs don't specify endianness, but we use big endian.
|
||||
pid := os.Getpid()
|
||||
id[7] = byte(pid >> 8)
|
||||
id[8] = byte(pid)
|
||||
// Increment, 3 bytes, big endian
|
||||
i := atomic.AddUint32(&objectIDCounter, 1)
|
||||
id[9] = byte(i >> 16)
|
||||
id[10] = byte(i >> 8)
|
||||
id[11] = byte(i)
|
||||
return id
|
||||
}
|
||||
|
||||
// String returns a base64 URL safe representation of the id
|
||||
func (id ID) String() string {
|
||||
return base64.URLEncoding.EncodeToString(id[:])
|
||||
}
|
||||
|
||||
// MarshalText implements encoding/text TextMarshaler interface
|
||||
func (id ID) MarshalText() (text []byte, err error) {
|
||||
text = make([]byte, encodedLen)
|
||||
base64.URLEncoding.Encode(text, id[:])
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding/text TextUnmarshaler interface
|
||||
func (id *ID) UnmarshalText(text []byte) error {
|
||||
if len(text) != encodedLen {
|
||||
return ErrInvalidID
|
||||
}
|
||||
b := make([]byte, rawLen)
|
||||
_, err := base64.URLEncoding.Decode(b, text)
|
||||
for i, c := range b {
|
||||
id[i] = c
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Time returns the timestamp part of the id.
|
||||
// It's a runtime error to call this method with an invalid id.
|
||||
func (id ID) Time() time.Time {
|
||||
// First 4 bytes of ObjectId is 32-bit big-endian seconds from epoch.
|
||||
secs := int64(binary.BigEndian.Uint32(id[0:4]))
|
||||
return time.Unix(secs, 0)
|
||||
}
|
||||
|
||||
// Machine returns the 3-byte machine id part of the id.
|
||||
// It's a runtime error to call this method with an invalid id.
|
||||
func (id ID) Machine() []byte {
|
||||
return id[4:7]
|
||||
}
|
||||
|
||||
// Pid returns the process id part of the id.
|
||||
// It's a runtime error to call this method with an invalid id.
|
||||
func (id ID) Pid() uint16 {
|
||||
return binary.BigEndian.Uint16(id[7:9])
|
||||
}
|
||||
|
||||
// Counter returns the incrementing value part of the id.
|
||||
// It's a runtime error to call this method with an invalid id.
|
||||
func (id ID) Counter() int32 {
|
||||
b := id[9:12]
|
||||
// Counter is stored as big-endian 3-byte value
|
||||
return int32(uint32(b[0])<<16 | uint32(b[1])<<8 | uint32(b[2]))
|
||||
}
|
111
vendor/github.com/rs/xid/id_test.go
generated
vendored
Normal file
111
vendor/github.com/rs/xid/id_test.go
generated
vendored
Normal file
|
@ -0,0 +1,111 @@
|
|||
package xid
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type IDParts struct {
|
||||
id ID
|
||||
timestamp int64
|
||||
machine []byte
|
||||
pid uint16
|
||||
counter int32
|
||||
}
|
||||
|
||||
var IDs = []IDParts{
|
||||
IDParts{
|
||||
ID{0x4d, 0x88, 0xe1, 0x5b, 0x60, 0xf4, 0x86, 0xe4, 0x28, 0x41, 0x2d, 0xc9},
|
||||
1300816219,
|
||||
[]byte{0x60, 0xf4, 0x86},
|
||||
0xe428,
|
||||
4271561,
|
||||
},
|
||||
IDParts{
|
||||
ID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
0,
|
||||
[]byte{0x00, 0x00, 0x00},
|
||||
0x0000,
|
||||
0,
|
||||
},
|
||||
IDParts{
|
||||
ID{0x00, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0x00, 0x00, 0x01},
|
||||
0,
|
||||
[]byte{0xaa, 0xbb, 0xcc},
|
||||
0xddee,
|
||||
1,
|
||||
},
|
||||
}
|
||||
|
||||
func TestIDPartsExtraction(t *testing.T) {
|
||||
for i, v := range IDs {
|
||||
assert.Equal(t, v.id.Time(), time.Unix(v.timestamp, 0), "#%d timestamp", i)
|
||||
assert.Equal(t, v.id.Machine(), v.machine, "#%d machine", i)
|
||||
assert.Equal(t, v.id.Pid(), v.pid, "#%d pid", i)
|
||||
assert.Equal(t, v.id.Counter(), v.counter, "#%d counter", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
// Generate 10 ids
|
||||
ids := make([]ID, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
ids[i] = New()
|
||||
}
|
||||
for i := 1; i < 10; i++ {
|
||||
prevID := ids[i-1]
|
||||
id := ids[i]
|
||||
// Test for uniqueness among all other 9 generated ids
|
||||
for j, tid := range ids {
|
||||
if j != i {
|
||||
assert.NotEqual(t, id, tid, "Generated ID is not unique")
|
||||
}
|
||||
}
|
||||
// Check that timestamp was incremented and is within 30 seconds of the previous one
|
||||
secs := id.Time().Sub(prevID.Time()).Seconds()
|
||||
assert.Equal(t, (secs >= 0 && secs <= 30), true, "Wrong timestamp in generated ID")
|
||||
// Check that machine ids are the same
|
||||
assert.Equal(t, id.Machine(), prevID.Machine())
|
||||
// Check that pids are the same
|
||||
assert.Equal(t, id.Pid(), prevID.Pid())
|
||||
// Test for proper increment
|
||||
delta := int(id.Counter() - prevID.Counter())
|
||||
assert.Equal(t, delta, 1, "Wrong increment in generated ID")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIDString(t *testing.T) {
|
||||
id := ID{0x4d, 0x88, 0xe1, 0x5b, 0x60, 0xf4, 0x86, 0xe4, 0x28, 0x41, 0x2d, 0xc9}
|
||||
assert.Equal(t, "TYjhW2D0huQoQS3J", id.String())
|
||||
}
|
||||
|
||||
type jsonType struct {
|
||||
ID *ID
|
||||
}
|
||||
|
||||
func TestIDJSONMarshaling(t *testing.T) {
|
||||
id := ID{0x4d, 0x88, 0xe1, 0x5b, 0x60, 0xf4, 0x86, 0xe4, 0x28, 0x41, 0x2d, 0xc9}
|
||||
v := jsonType{ID: &id}
|
||||
data, err := json.Marshal(&v)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, `{"ID":"TYjhW2D0huQoQS3J"}`, string(data))
|
||||
}
|
||||
|
||||
func TestIDJSONUnmarshaling(t *testing.T) {
|
||||
data := []byte(`{"ID":"TYjhW2D0huQoQS3J"}`)
|
||||
v := jsonType{}
|
||||
err := json.Unmarshal(data, &v)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, ID{0x4d, 0x88, 0xe1, 0x5b, 0x60, 0xf4, 0x86, 0xe4, 0x28, 0x41, 0x2d, 0xc9}, *v.ID)
|
||||
}
|
||||
|
||||
func TestIDJSONUnmarshalingError(t *testing.T) {
|
||||
v := jsonType{}
|
||||
err := json.Unmarshal([]byte(`{"ID":"TYjhW2D0huQoQS"}`), &v)
|
||||
assert.EqualError(t, err, "invalid ID")
|
||||
err = json.Unmarshal([]byte(`{"ID":"TYjhW2D0huQoQS3kdk"}`), &v)
|
||||
assert.EqualError(t, err, "invalid ID")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue