func randURI(base string) rdf.URI { n := rnd.Intn(100) if n > 70 { // 70% using base uri base = "http://" } letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-..///") l := rnd.Intn(100) + 1 r := make([]rune, l) for i := range r { r[i] = letters[rand.Intn(len(letters))] } if r[0] == '/' { r[0] = letters[rand.Intn(len(letters)-3)] } return rdf.NewURI(base + string(r)) }
func (db *DB) decode(b []byte) (rdf.Term, error) { // We control the encoding, so the only way for this method to fail to decode // into a RDF term is if the underlying stoarge has been corrupted on the file system level. if len(b) == 0 { return nil, errors.New("cannot decode empty byte slice into RDF term") } var dt rdf.URI switch b[0] { case 0x00: return rdf.URI(db.base + string(b[1:])), nil case 0x01: return rdf.URI(string(b[1:])), nil case 0x02: return rdf.NewTypedLiteral(string(b[1:]), rdf.XSDstring), nil case 0x03: if len(b) < 2 { return nil, fmt.Errorf("cannot decode as rdf:langString: %v", b) } ll := int(b[1]) if len(b) < ll+2 { return nil, fmt.Errorf("cannot decode as rdf:langString: %v", b) } return rdf.NewLangLiteral(string(b[ll+2:]), string(b[2:2+ll])), nil case 0x04: dt = rdf.XSDboolean case 0x05: dt = rdf.XSDbyte case 0x06: dt = rdf.XSDint case 0x07: dt = rdf.XSDshort case 0x08: dt = rdf.XSDlong case 0x09: dt = rdf.XSDinteger case 0x0A: dt = rdf.XSDunsignedShort case 0x0B: dt = rdf.XSDunsignedInt case 0x0C: dt = rdf.XSDunsignedLong case 0x0D: dt = rdf.XSDunsignedByte case 0x0E: dt = rdf.XSDfloat case 0x0F: dt = rdf.XSDdouble case 0x10: dt = rdf.XSDdateTimeStamp case 0xFF: if len(b) < 2 { return nil, fmt.Errorf("cannot decode as literal: %v", b) } ll := int(b[1]) if len(b) < ll { return nil, fmt.Errorf("cannot decode as literal: %v", b) } return rdf.NewTypedLiteral(string(b[ll+2:]), rdf.NewURI(string(b[2:2+ll]))), nil default: return nil, fmt.Errorf("cannot decode RDF term: %v", b) } return rdf.NewTypedLiteral(string(b[1:]), dt), nil }