// Decode bytes into the given Go object. func Degobify(byts []byte, param interface{}, debug *gubed.Logger) { var bfr bytes.Buffer dec := gob.NewDecoder(&bfr) err := dec.Decode(¶m) debug.Error(err) return }
func GetKeyType(key interface{}, debug *gubed.Logger) LBTYPE { switch ktype := key.(type) { case uint8: return LBTYPE_UINT8 case uint16: return LBTYPE_UINT16 case uint32: return LBTYPE_UINT32 case uint64: return LBTYPE_UINT64 case int8: return LBTYPE_INT8 case int16: return LBTYPE_INT16 case int32: return LBTYPE_INT32 case int64: return LBTYPE_INT64 case float32: return LBTYPE_FLOAT32 case float64: return LBTYPE_FLOAT64 case complex64: return LBTYPE_COMPLEX64 case complex128: return LBTYPE_COMPLEX128 case CATID_TYPE: return LBTYPE_CATID case string: return LBTYPE_STRING default: debug.Error(FmtErrBadType("Unrecognised key type: %d", ktype)) } return LBTYPE_NIL }
// Encode a Go object into bytes. func Gobify(param interface{}, debug *gubed.Logger) []byte { var bfr bytes.Buffer enc := gob.NewEncoder(&bfr) err := enc.Encode(param) debug.Error(err) return bfr.Bytes() }
// Keys can only be a subset of the LBTYPEs. func MakeKey(kbyts []byte, ktype LBTYPE, debug *gubed.Logger) (interface{}, error) { if IsAllowableKey(ktype) { return MakeTypeFromBytes(kbyts, ktype) } else { err := debug.Error(FmtErrBadType("Bad key type: %d", ktype)) return nil, err } }
// Read the first bytes of the given byte slice to return the LBTYPE. func GetType(x []byte, debug *gubed.Logger) (typ LBTYPE) { if len(x) < LBTYPE_SIZE { debug.Error(FmtErrSliceTooSmall(x, LBTYPE_SIZE)) } bfr := bufio.NewReader(bytes.NewBuffer(x[:LBTYPE_SIZE])) debug.DecodeError(binary.Read(bfr, BIGEND, &typ)) return }
// Map GenericRecord to a new IndexRecord. func (rec *GenericRecord) ToIndexRecord(debug *gubed.Logger) *IndexRecord { irec := NewIndexRecord() irec.ksz = rec.ksz irec.kbyts = rec.kbyts irec.ktype = rec.ktype // Unpack bfr := bufio.NewReader(bytes.NewBuffer(rec.vbyts)) debug.DecodeError(binary.Read(bfr, BIGEND, &irec.vsz)) debug.DecodeError(binary.Read(bfr, BIGEND, &irec.vpos)) return irec }
func (fmap *FieldMap) FromBytes(bfr *bytes.Buffer, debug *gubed.Logger) (err error) { var size LBUINT var label string var vtype LBTYPE var bits []byte for bfr.Len() > 0 { err = debug.DecodeError(binary.Read(bfr, BIGEND, &size)) // label size if err == io.EOF { break } else { if err != nil { return } } bits = make([]byte, int(size)) err = debug.DecodeError(binary.Read(bfr, BIGEND, &bits)) if err == io.EOF { break } else { if err != nil { return } } label = string(bits) // label err = debug.DecodeError(binary.Read(bfr, BIGEND, &size)) // value size if err == io.EOF { break } else { if err != nil { return } } bits = make([]byte, int(size)-LBTYPE_SIZE) err = debug.DecodeError(binary.Read(bfr, BIGEND, &vtype)) // LBTYPE if err == io.EOF { break } else { if err != nil { return } } err = debug.DecodeError(binary.Read(bfr, BIGEND, &bits)) fmap.fields[label] = MakeField(bits, vtype) if err == io.EOF { break } else { if err != nil { return } } } return }
// Map GenericRecord to a new UserPermissionRecord. func (rec *GenericRecord) ToUserPermissionRecord(debug *gubed.Logger) (interface{}, *UserPermissionRecord) { key, err := MakeKey(rec.kbyts, rec.ktype, debug) debug.Error(err) upr := NewUserPermissionRecord() // Unpack var p uint8 = uint8(rec.vbyts[0]) upr.Create = PERMISSION_CREATE == (p & PERMISSION_CREATE) upr.Read = PERMISSION_READ == (p & PERMISSION_READ) upr.Update = PERMISSION_UPDATE == (p & PERMISSION_UPDATE) upr.Delete = PERMISSION_DELETE == (p & PERMISSION_DELETE) return key, upr }
// Map GenericRecord to a new LogRecord. func (rec *GenericRecord) ToLogRecord(debug *gubed.Logger) *LogRecord { lrec := NewLogRecord() lrec.ksz = rec.ksz lrec.vsz = rec.vsz - CRC_SIZE lrec.kbyts = rec.kbyts lrec.ktype = rec.ktype lrec.vtype = rec.vtype // Unpack bfr := bufio.NewReader(bytes.NewBuffer(rec.vbyts)) // Note that the generic vsz includes the LBTYPE prefix lrec.vbyts = make([]byte, int(lrec.vsz)-LBTYPE_SIZE) // must have fixed size debug.DecodeError(binary.Read(bfr, BIGEND, &lrec.vbyts)) debug.DecodeError(binary.Read(bfr, BIGEND, &lrec.crc)) return lrec }
func MakeNode(name string, ntype LBTYPE, debug *gubed.Logger) *Node { switch ntype { case LBTYPE_KIND, LBTYPE_DOC: return &Node{ name: name, ntype: ntype, parents: NewCatalogIdSet(), debug: debug, FieldMap: NewFieldMap(), } default: debug.Error(FmtErrBadType("Bad key type: %d", ntype)) return nil } }
// Read the parent CATID set, which is always the last section of the buffer. func (cidset *CatalogIdSet) FromBytes(bfr *bytes.Buffer, debug *gubed.Logger) (err error) { rem := bfr.Len() % int(CATID_TYPE_SIZE) if rem > 0 { err = debug.Error(FmtErrPartialCATIDSet(bfr.Len(), CATID_TYPE_SIZE)) return } n := bfr.Len() / int(CATID_TYPE_SIZE) var id CATID_TYPE cidset.set = make([]*CatalogId, n) for i := 0; i < n; i++ { err = debug.Error(binary.Read(bfr, BIGEND, &id)) cidset.set[i] = NewCatalogId(id) if err != nil { break } } return }
// Map GenericRecord to a new MasterLogRecord. func (rec *GenericRecord) ToValueLocation(debug *gubed.Logger) (interface{}, *ValueLocation) { key, err := MakeKey(rec.kbyts, rec.ktype, debug) debug.Error(err) vbyts, _ := rec.GetValueAndType(MASTER_RECORD, debug) vloc := NewValueLocation() // Unpack bfr := bufio.NewReader(bytes.NewBuffer(vbyts)) debug.DecodeError(binary.Read(bfr, BIGEND, &vloc.fnum)) debug.DecodeError(binary.Read(bfr, BIGEND, &vloc.vsz)) debug.DecodeError(binary.Read(bfr, BIGEND, &vloc.vpos)) return key, vloc }
// Delete all zapmap records associated with the given logfile number. func (zmap *Zapmap) Purge(fnum LBUINT, debug *gubed.Logger) { debug.Basic("Purge zapmap of logfile %d entries", fnum) for key, zrecs := range zmap.zapmap { var newzrecs []*ZapRecord // Make a new list to replace old for _, zrec := range zrecs { if zrec.fnum != fnum { newzrecs = append(newzrecs, zrec) } else { debug.Fine("Deleting %q%s from zapmap", key, zrec.String()) } } if len(newzrecs) == 0 { zmap.Delete(key) } else { zmap.Put(key, newzrecs) } } return }
// Map GenericRecord to a new ZapRecord list. func (rec *GenericRecord) ToZapRecordList(debug *gubed.Logger) (interface{}, []*ZapRecord) { key, err := MakeKey(rec.kbyts, rec.ktype, debug) debug.Error(err) n := rec.LocationListLength() bfr := bufio.NewReader(bytes.NewBuffer(rec.vbyts)) var zrecs = make([]*ZapRecord, n) for i := 0; i < n; i++ { zrecs[i] = NewZapRecord() debug.DecodeError(binary.Read(bfr, BIGEND, &zrecs[i].fnum)) debug.DecodeError(binary.Read(bfr, BIGEND, &zrecs[i].rsz)) debug.DecodeError(binary.Read(bfr, BIGEND, &zrecs[i].rpos)) } return key, zrecs }
// Read an CATID from a byte slice. func BytesToCatalogId(byts []byte, debug *gubed.Logger) (interface{}, error) { bfr := bytes.NewBuffer(byts) var cid CATID_TYPE err := debug.DecodeError(binary.Read(bfr, BIGEND, &cid)) return cid, err }
func SnipKeyType(key []byte, debug *gubed.Logger) (newkey []byte, ktype LBTYPE) { bfr := bufio.NewReader(bytes.NewBuffer(key[:LBTYPE_SIZE])) debug.DecodeError(binary.Read(bfr, BIGEND, &ktype)) newkey = key[LBTYPE_SIZE:] return }
func ToBytes(val interface{}, vt LBTYPE, debug *gubed.Logger) (byts []byte, err error) { bfr := new(bytes.Buffer) es := "Type mismatch, value is type %T but LBTYPE is %v" switch v := val.(type) { case uint8: if vt != LBTYPE_UINT8 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case uint16: if vt != LBTYPE_UINT16 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case uint32: if vt != LBTYPE_UINT32 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case uint64: if vt != LBTYPE_UINT64 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case int8: if vt != LBTYPE_INT8 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case int16: if vt != LBTYPE_INT16 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case int32: if vt != LBTYPE_INT32 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case int64: if vt != LBTYPE_INT64 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case float32: if vt != LBTYPE_FLOAT32 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case float64: if vt != LBTYPE_FLOAT64 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case complex64: if vt != LBTYPE_COMPLEX64 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case complex128: if vt != LBTYPE_COMPLEX128 { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case CATID_TYPE: if vt != LBTYPE_CATID { return nil, debug.Error(FmtErrBadType(es, v, vt)) } binary.Write(bfr, BIGEND, v) case []byte: if vt != LBTYPE_BYTES { return nil, debug.Error(FmtErrBadType(es, v, vt)) } return v, nil case string: if vt != LBTYPE_STRING && vt != LBTYPE_LOCATION { return nil, debug.Error(FmtErrBadType(es, v, vt)) } return []byte(v), nil } return bfr.Bytes(), nil }