示例#1
0
func bmeta_read_upload_file(ell *etransport.Elliptics, file, key string) (err error) {
	data, err := ioutil.ReadFile(file)
	if err != nil {
		log.Fatalf("Could not read file %s: %v", file, err)
	}

	ms, err := ell.MetadataSession()
	if err != nil {
		return
	}
	defer ms.Delete()

	ms.SetNamespace(bucket.BucketNamespace)

	tried := false

	for wr := range ms.WriteData(key, bytes.NewReader(data), 0, 0) {
		tried = true

		if wr.Error() != nil {
			err = wr.Error()

			log.Printf("%s: could not write file %s: %v", key, file, err)
			continue
		}

		fmt.Printf("Successfully uploaded %s into elliptics using key %s\n", file, key)
		return
	}

	if !tried {
		err = fmt.Errorf("%s: could not write file %s: WriteData() returned nothing", key, file)
	}
	return
}
示例#2
0
func ReadBucket(ell *etransport.Elliptics, name string) (bucket *Bucket, err error) {
	ms, err := ell.MetadataSession()
	if err != nil {
		log.Printf("read-bucket: %s: could not create metadata session: %v", name, err)
		return
	}
	defer ms.Delete()

	ms.SetNamespace(BucketNamespace)

	b := NewBucket(name)

	for rd := range ms.ReadData(name, 0, 0) {
		if rd.Error() != nil {
			err = rd.Error()

			log.Printf("read-bucket: %s: could not read bucket metadata: %v", name, err)
			return
		}

		var out []interface{}
		err = msgpack.Unmarshal([]byte(rd.Data()), &out)
		if err != nil {
			log.Printf("read-bucket: %s: could not parse bucket metadata: %v", name, err)
			return
		}

		err = b.Meta.ExtractMsgpack(out)
		if err != nil {
			log.Printf("read-bucket: %s: unsupported msgpack data: %v", name, err)
			return
		}

		bucket = b
		return
	}

	bucket = nil
	err = errors.NewKeyError(name, http.StatusNotFound,
		"read-bucket: could not read bucket data: ReadData() returned nothing")
	return
}
示例#3
0
func WriteBucket(ell *etransport.Elliptics, meta *BucketMsgpack) (bucket *Bucket, err error) {
	ms, err := ell.MetadataSession()
	if err != nil {
		log.Printf("%s: could not create metadata session: %v", meta.Name, err)
		return
	}
	defer ms.Delete()

	ms.SetNamespace(BucketNamespace)

	out, err := meta.PackMsgpack()
	if err != nil {
		log.Printf("%s: could not pack bucket: %v", meta.Name, err)
		return
	}

	data, err := msgpack.Marshal(&out)
	if err != nil {
		log.Printf("%s: could not parse bucket metadata: %v", meta.Name, err)
		return
	}

	for wr := range ms.WriteData(meta.Name, bytes.NewReader(data), 0, 0) {
		if wr.Error() != nil {
			err = wr.Error()

			log.Printf("%s: could not write bucket metadata: %v", meta.Name, err)
			return
		}

		bucket = NewBucket(meta.Name)
		bucket.Meta = *meta

		return
	}

	err = errors.NewKeyError(meta.Name, http.StatusNotFound,
		"could not write bucket metadata: WriteData() returned nothing")
	return
}