Beispiel #1
0
func BenchmarkVolumeWrite(b *testing.B) {
	var (
		v     *Volume
		err   error
		file  = "./test/testb2"
		ifile = "./test/testb2.idx"
		data  = make([]byte, _16kb) // 16kb
	)
	os.Remove(file)
	os.Remove(ifile)
	defer os.Remove(file)
	defer os.Remove(ifile)
	if _, err = rand.Read(data); err != nil {
		b.Errorf("rand.Read() error(%v)", err)
		b.FailNow()
	}
	if v, err = NewVolume(1, file, ifile, testConf); err != nil {
		b.Errorf("NewVolume() error(%v)", err)
		b.FailNow()
	}
	defer v.Close()
	b.SetParallelism(8)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		var (
			i, j int
			ts   int32
			t    int64
			err1 error
			n    *needle.Needle
			ns   = make([]needle.Needle, 9)
			buf  = make([]byte, 163840) // 16kb
		)
		for i = 0; i < 9; i++ {
			t = mrand.Int63()
			n = &ns[i]
			n.Init(t, 1, data)
			n.Write(buf[ts:])
			ts += n.TotalSize
		}
		for pb.Next() {
			for j = 0; j < 9; j++ {
				t = mrand.Int63()
				n = &ns[j]
				n.Key = t
				binary.BigEndian.PutInt64(buf[n.TotalSize+needle.KeyOffset:], n.Key)
			}
			if err1 = v.Write(ns, buf[:ts]); err1 != nil {
				b.Errorf("Add() error(%v)", err1)
				v.Unlock()
				b.FailNow()
			}
			b.SetBytes(int64(ts))
		}
	})
	os.Remove(file)
	os.Remove(ifile)
}
Beispiel #2
0
func (h httpUploadsHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		i, rn, tn, nn, nb int
		ok                bool
		buf               []byte
		err               error
		vid               int64
		key               int64
		cookie            int64
		size              int64
		str               string
		keys              []string
		cookies           []string
		sr                sizer
		fr                *os.File
		fi                os.FileInfo
		v                 *Volume
		n                 *needle.Needle
		ns                []needle.Needle
		uerr              errors.Error
		file              multipart.File
		fh                *multipart.FileHeader
		fhs               []*multipart.FileHeader
		res               = map[string]interface{}{"ret": errors.RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	// check total content-length
	if size, err = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if size > int64(h.c.NeedleMaxSize*h.c.BatchMaxNum) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	keys = r.MultipartForm.Value["keys"]
	cookies = r.MultipartForm.Value["cookies"]
	if len(keys) != len(cookies) {
		log.Errorf("param length not match, keys: %d, cookies: %d", len(keys), len(cookies))
		res["ret"] = errors.RetParamErr
		return
	}
	fhs = r.MultipartForm.File["file"]
	nn = len(fhs)
	if len(keys) != nn {
		log.Errorf("param length not match, keys: %d, cookies: %d, files: %d", len(keys), len(cookies), len(fhs))
		res["ret"] = errors.RetParamErr
		return
	}
	nb = int(size-1)/(h.c.NeedleMaxSize) + 1
	buf = h.s.Buffer(nb)
	ns = h.s.Needle(nn)
	tn = needle.HeaderSize
	for i, fh = range fhs {
		if key, err = strconv.ParseInt(keys[i], 10, 64); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", keys[i], err)
			err = errors.ErrParam
			break
		}
		if cookie, err = strconv.ParseInt(cookies[i], 10, 32); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", cookies[i], err)
			err = errors.ErrParam
			break
		}
		file, err = fh.Open()
		file.Close()
		if err != nil {
			log.Errorf("fh.Open() error(%v)", err)
			break
		}
		// check size
		if sr, ok = file.(sizer); ok {
			size = sr.Size()
		} else if fr, ok = file.(*os.File); ok {
			if fi, err = fr.Stat(); err != nil {
				break
			}
			size = fi.Size()
		}
		if size > int64(h.c.NeedleMaxSize) {
			err = errors.ErrNeedleTooLarge
			break
		}
		if rn, err = file.Read(buf[tn:]); err != nil {
			log.Errorf("file.Read() error(%v)", err)
			break
		}
		n = &ns[i]
		n.Init(key, int32(cookie), buf[tn:tn+rn])
		n.WriteHeader(buf[tn-needle.HeaderSize:])
		n.WriteFooter(buf[tn+rn:], false)
		tn += int(n.TotalSize) // prev needle + header
	}
	if err == nil {
		if v = h.s.Volumes[int32(vid)]; v != nil {
			err = v.Write(ns, buf[:tn])
		} else {
			err = errors.ErrVolumeNotExist
		}
	}
	h.s.FreeBuffer(nb, buf)
	h.s.FreeNeedle(nn, ns)
	if err != nil {
		if uerr, ok = err.(errors.Error); ok {
			res["ret"] = int(uerr)
		} else {
			res["ret"] = errors.RetInternalErr
		}
	}
	return
}
Beispiel #3
0
func (h httpUploadHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		ok     bool
		rn     int
		vid    int64
		key    int64
		cookie int64
		size   int64
		err    error
		str    string
		buf    []byte
		v      *Volume
		n      *needle.Needle
		ns     []needle.Needle
		file   multipart.File
		sr     sizer
		fr     *os.File
		fi     os.FileInfo
		uerr   errors.Error
		res    = map[string]interface{}{"ret": errors.RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	// check total content-length
	if size, err = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if size > int64(h.c.NeedleMaxSize) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	str = r.FormValue("key")
	if key, err = strconv.ParseInt(str, 10, 64); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	str = r.FormValue("cookie")
	if cookie, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	if file, _, err = r.FormFile("file"); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if sr, ok = file.(sizer); ok {
		size = sr.Size()
	} else if fr, ok = file.(*os.File); ok {
		if fi, err = fr.Stat(); err != nil {
			res["ret"] = errors.RetInternalErr
			return
		}
		size = fi.Size()
	}
	if size > int64(h.c.NeedleMaxSize) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	ns = h.s.Needle(1)
	n = &(ns[0])
	buf = h.s.Buffer(1)
	// fill buf skip needle header offset
	// --------------------
	// header (data) footer
	// --------------------
	rn, err = file.Read(buf[needle.HeaderSize:])
	file.Close()
	if err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	n.Init(key, int32(cookie), buf[needle.HeaderSize:needle.HeaderSize+rn])
	n.WriteHeader(buf)
	n.WriteFooter(buf[needle.HeaderSize+rn:], false)
	if v = h.s.Volumes[int32(vid)]; v != nil {
		err = v.Add(n, buf[:n.TotalSize])
	} else {
		err = errors.ErrVolumeNotExist
	}
	h.s.FreeBuffer(1, buf)
	h.s.FreeNeedle(1, ns)
	if err != nil {
		if uerr, ok = err.(errors.Error); ok {
			res["ret"] = int(uerr)
		} else {
			res["ret"] = errors.RetInternalErr
		}
	}
	return
}