Example #1
0
File: hasher.go Project: kego/ke
func (p *PackageHasher) Hash() (uint64, error) {

	p.Version = 5

	bytes, err := json.Marshal(p)
	if err != nil {
		return 0, kerr.Wrap("TGAEJVECIF", err)
	}

	return cityhash.CityHash64(bytes, uint32(len(bytes))), nil

}
Example #2
0
File: parser.go Project: kego/ke
func scanForTypesAndExports(ctx context.Context, env *envctx.Env, cache *sysctx.SysPackageInfo, hash *PackageHasher) error {

	// While we're scanning for types, we should use a custom unpacking env,
	// because the env from the context is the one of the local package.

	files := scanner.ScanDirToFiles(ctx, env.Dir, env.Recursive)
	bytes := scanner.ScanFilesToBytes(ctx, files)
	localContext := envctx.NewContext(ctx, env)
	for b := range bytes {
		if b.Err != nil {
			return kerr.Wrap("JACKALTIGG", b.Err)
		}

		o := &system.ObjectStub{}
		if err := system.Unmarshal(localContext, b.Bytes, o); err != nil {
			return kerr.Wrap("HCYGNBDFFA", err)
		}
		if o.Type == nil {
			return kerr.New("NUKWIHYFMQ", "%s has no type", b.File)
		}
		if o.Id == nil && *o.Type != *system.NewReference("kego.io/system", "package") {
			// we tolerate missing ID only for system:package
			return kerr.New("DLLMKTDYFW", "%s has no id", b.File)
		}
		relativeFile, err := filepath.Rel(env.Dir, b.File)
		if err != nil {
			return kerr.Wrap("AWYRJSCYQS", err)
		}
		switch *o.Type {
		case *system.NewReference("kego.io/system", "type"):
			if err := ProcessTypeFileBytes(ctx, env, relativeFile, b.Bytes, cache, hash); err != nil {
				return kerr.Wrap("IVEFDDSKHE", err)
			}
		case *system.NewReference("kego.io/system", "package"):
			cache.PackageBytes = b.Bytes
			cache.PackageFilename = relativeFile
		default:
			cache.Globals.Set(o.Id.Name, relativeFile)
			if o.Export {
				cache.Exports.Set(o.Id.Name, o.Type.Name, o.Type.Package, b.Bytes)
				if hash != nil {
					hash.Exports[o.Id.Name+" "+o.Type.Name+" "+o.Type.Package] = cityhash.CityHash64(b.Bytes, uint32(len(b.Bytes)))
				}
			}
		}

	}
	return nil
}
Example #3
0
File: hasher.go Project: kego/ke
func (p *NodeHasher) Hash(ctx context.Context) (uint64, error) {

	if version, ok := ctx.Value(nodeHasherVersionKey).(int); ok {
		p.Version = version
	} else {
		p.Version = currentNodeHasherVersion
	}

	bytes, err := json.Marshal(p)
	if err != nil {
		return 0, kerr.Wrap("QYEXVJIEOS", err)
	}

	return cityhash.CityHash64(bytes, uint32(len(bytes))), nil

}
Example #4
0
File: parser.go Project: kego/ke
func ProcessTypeFileBytes(ctx context.Context, env *envctx.Env, filename string, bytes []byte, cache *sysctx.SysPackageInfo, hash *PackageHasher) error {
	t := new(system.Type)
	if err := system.Unmarshal(envctx.NewContext(ctx, env), bytes, t); err != nil {
		return kerr.Wrap("NLRRVIDVWM", err)
	}
	if hash != nil {
		hash.Types[t.Id.Name] = cityhash.CityHash64(bytes, uint32(len(bytes)))
	}
	cache.Types.Set(t.Id.Name, filename, t)
	cache.Files.Set(t.Id.Name, filename, bytes)
	if t.Rule != nil {
		id := system.NewReference(t.Id.Package, fmt.Sprint("@", t.Id.Name))
		if t.Rule.Id != nil && *t.Rule.Id != *id {
			return kerr.New("JKARKEDTIW", "Incorrect id for %v - it should be %v", t.Rule.Id.String(), id.String())
		}
		t.Rule.Id = id

		// Check that the rule embeds system:rule
		found := false
		for _, em := range t.Rule.Embed {
			if *em == *system.NewReference("kego.io/system", "rule") {
				found = true
			}
		}
		if !found {
			return kerr.New("LMALEMKFDI", "%s does not embed system:rule", id.String())
		}

		cache.Types.Set(id.Name, filename, t.Rule)
	} else {
		// If the rule is missing, automatically create a default.
		id := system.NewReference(t.Id.Package, fmt.Sprint("@", t.Id.Name))
		rule := &system.Type{
			Object: &system.Object{
				Description: fmt.Sprintf("Automatically created basic rule for %s", t.Id.Name),
				Type:        system.NewReference("kego.io/system", "type"),
				Id:          id,
			},
			Embed:     []*system.Reference{system.NewReference("kego.io/system", "rule")},
			Native:    system.NewString("object"),
			Interface: false,
		}
		cache.Types.Set(id.Name, filename, rule)
	}

	return nil
}
Example #5
0
func BenchmarkFarm16(b *testing.B) { benchmarkHashn(b, 16, hfarm) }
func BenchmarkFarm40(b *testing.B) { benchmarkHashn(b, 40, hfarm) }
func BenchmarkFarm64(b *testing.B) { benchmarkHashn(b, 64, hfarm) }
func BenchmarkFarm1K(b *testing.B) { benchmarkHashn(b, 1024, hfarm) }
func BenchmarkFarm8K(b *testing.B) { benchmarkHashn(b, 8192, hfarm) }

var hcfarmhash = func(k []byte) uint64 { return cfarmhash.Hash64(k) }

func BenchmarkCFarmhash8(b *testing.B)  { benchmarkHashn(b, 8, hcfarmhash) }
func BenchmarkCFarmhash16(b *testing.B) { benchmarkHashn(b, 16, hcfarmhash) }
func BenchmarkCFarmhash40(b *testing.B) { benchmarkHashn(b, 40, hcfarmhash) }
func BenchmarkCFarmhash64(b *testing.B) { benchmarkHashn(b, 64, hcfarmhash) }
func BenchmarkCFarmhash1K(b *testing.B) { benchmarkHashn(b, 1024, hcfarmhash) }
func BenchmarkCFarmhash8K(b *testing.B) { benchmarkHashn(b, 8192, hcfarmhash) }

var hcity = func(k []byte) uint64 { return cityhash.CityHash64(k, uint32(len(k))) }

func BenchmarkCity8(b *testing.B)  { benchmarkHashn(b, 8, hcity) }
func BenchmarkCity16(b *testing.B) { benchmarkHashn(b, 16, hcity) }
func BenchmarkCity40(b *testing.B) { benchmarkHashn(b, 40, hcity) }
func BenchmarkCity64(b *testing.B) { benchmarkHashn(b, 64, hcity) }
func BenchmarkCity1K(b *testing.B) { benchmarkHashn(b, 1024, hcity) }
func BenchmarkCity8K(b *testing.B) { benchmarkHashn(b, 8192, hcity) }

var hmetro = func(k []byte) uint64 { return metro.Hash64(k, 0) }

func BenchmarkMetro8(b *testing.B)  { benchmarkHashn(b, 8, hmetro) }
func BenchmarkMetro16(b *testing.B) { benchmarkHashn(b, 16, hmetro) }
func BenchmarkMetro40(b *testing.B) { benchmarkHashn(b, 40, hmetro) }
func BenchmarkMetro64(b *testing.B) { benchmarkHashn(b, 64, hmetro) }
func BenchmarkMetro1K(b *testing.B) { benchmarkHashn(b, 1024, hmetro) }