forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_database_hi_lo_id_generator.go
46 lines (41 loc) · 1.4 KB
/
multi_database_hi_lo_id_generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package ravendb
import "sync"
// MultiDatabaseHiLoIDGenerator manages per-database HiLoKeyGenerotr
type MultiDatabaseHiLoIDGenerator struct {
store *DocumentStore
conventions *DocumentConventions
// string -> *MultiTypeHiLoIDGenerator
_generators sync.Map
}
// NewMultiDatabaseHiLoIDGenerator creates new MultiDatabaseHiLoKeyGenerator
func NewMultiDatabaseHiLoIDGenerator(store *DocumentStore, conventions *DocumentConventions) *MultiDatabaseHiLoIDGenerator {
return &MultiDatabaseHiLoIDGenerator{
store: store,
conventions: conventions,
}
}
// GenerateDocumentID generates id
func (g *MultiDatabaseHiLoIDGenerator) GenerateDocumentID(dbName string, entity interface{}) (string, error) {
if dbName == "" {
dbName = g.store.database
}
panicIf(dbName == "", "expected non-empty dbName")
generatorI, ok := g._generators.Load(dbName)
var generator *MultiTypeHiLoIDGenerator
if !ok {
generator = NewMultiTypeHiLoIDGenerator(g.store, dbName, g.conventions)
g._generators.Store(dbName, generator)
} else {
generator = generatorI.(*MultiTypeHiLoIDGenerator)
}
return generator.GenerateDocumentID(entity)
}
// ReturnUnusedRange returns unused range for all generators
func (g *MultiDatabaseHiLoIDGenerator) ReturnUnusedRange() {
cb := func(key, value interface{}) bool {
generator := value.(*MultiTypeHiLoIDGenerator)
generator.ReturnUnusedRange()
return true
}
g._generators.Range(cb)
}