github.com/braveheart12/insolar-09-08-19@v0.8.7/ledger/storage/jet/jetid.go (about) 1 /* 2 * Copyright 2019 Insolar Technologies 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package jet 18 19 import ( 20 "bytes" 21 "fmt" 22 23 "github.com/insolar/insolar/core" 24 "github.com/ugorji/go/codec" 25 ) 26 27 // ZeroJetID is value of an empty Jet ID 28 var ZeroJetID = *NewID(0, nil) 29 30 // NewID generates RecordID for jet. 31 func NewID(depth uint8, prefix []byte) *core.RecordID { 32 var id core.RecordID 33 copy(id[:core.PulseNumberSize], core.PulseNumberJet.Bytes()) 34 id[core.PulseNumberSize] = depth 35 copy(id[core.PulseNumberSize+1:], prefix) 36 return &id 37 } 38 39 // IDSet is an alias for map[ID]struct{} 40 type IDSet map[core.RecordID]struct{} 41 42 // Has checks if passed id is in IDSet set. 43 func (j IDSet) Has(id core.RecordID) bool { 44 _, ok := j[id] 45 return ok 46 } 47 48 // Bytes serializes pulse. 49 func (j IDSet) Bytes() []byte { 50 var buf bytes.Buffer 51 enc := codec.NewEncoder(&buf, &codec.CborHandle{}) 52 enc.MustEncode(j) 53 return buf.Bytes() 54 } 55 56 // Jet extracts depth and prefix from jet id. 57 func Jet(id core.RecordID) (uint8, []byte) { 58 if id.Pulse() != core.PulseNumberJet { 59 panic(fmt.Sprintf("provided id %b is not a jet id", id)) 60 } 61 return id[core.PulseNumberSize], id[core.PulseNumberSize+1:] 62 } 63 64 func Parent(id core.RecordID) core.RecordID { 65 depth, prefix := Jet(id) 66 if depth == 0 { 67 return id 68 } 69 70 return *NewID(depth-1, ResetBits(prefix, depth-1)) 71 }