Ejemplo n.º 1
0
func (store *RemoteStore) Refresh() (err os.Error) {
	store.root = nil
	store.index = nil

	rootFile, err := store.FOpen(filepath.Join("/", r9p.ROOT_FILE), p.OREAD)
	if err != nil {
		return err
	}

	buffer := bytes.NewBuffer([]byte{})
	chunk := make([]byte, fs.BLOCKSIZE)
	for {
		n, err := rootFile.Read(chunk)
		if err != nil && err != os.EOF {
			return err
		}
		if n == 0 {
			err = nil
			break
		}
		buffer.Write(chunk[0:n])
	}

	decoder := gob.NewDecoder(buffer)

	store.root = &fs.Dir{}
	err = decoder.Decode(store.root)

	return err
}
Ejemplo n.º 2
0
func Open(root string) (*Stash, os.Error) {
	// attempt to open the index in the specified directory
	if meta, err := os.Open(root + metaFile); err == nil {
		// attempt to read in the index
		s, dec := new(Stash), gob.NewDecoder(meta)
		if err = dec.Decode(&s.meta); err == nil {
			// attempt to open the data file
			if s.store, err = os.OpenFile(root+dataFile, os.O_RDWR, 0640); err == nil {
				s.init(root)
				s.idx = meta
				go s.sync()
			} else {
				s = nil
			}
		} else {
			s = nil
		}

		if s == nil {
			meta.Close()
		}

		return s, err
	} else {
		return nil, err
	}

	panic("Dead code reached!")
}
Ejemplo n.º 3
0
func TestRatGobEncoding(t *testing.T) {
	var medium bytes.Buffer
	enc := gob.NewEncoder(&medium)
	dec := gob.NewDecoder(&medium)
	for i, test := range gobEncodingTests {
		for j := 0; j < 4; j++ {
			medium.Reset() // empty buffer for each test case (in case of failures)
			stest := test
			if j&1 != 0 {
				// negative numbers
				stest = "-" + test
			}
			if j%2 != 0 {
				// fractions
				stest = stest + "." + test
			}
			var tx Rat
			tx.SetString(stest)
			if err := enc.Encode(&tx); err != nil {
				t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err)
			}
			var rx Rat
			if err := dec.Decode(&rx); err != nil {
				t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err)
			}
			if rx.Cmp(&tx) != 0 {
				t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx)
			}
		}
	}
}
Ejemplo n.º 4
0
func (s *Stash) Get(key string) (interface{}, os.Error) {
	defer s.Flush()

	// check the hot cache
	if e := s.cache.Get(key); e != nil {
		return e.Value, nil
	}

	// resort to the file
	s.sMutex.Lock()
	defer s.sMutex.Unlock()
	if _, err := s.store.Seek(s.meta.Index.Search(key).Offset, 0); err == nil {
		dec, tmp := gob.NewDecoder(io.LimitReader(s.store, s.meta.Index.PageSize)), &record{}

		// place every key/value pair into the hot cache if it is missing
		s.cMutex.Lock()
		defer s.cMutex.Unlock()
		for err = dec.Decode(tmp); err == nil; err = dec.Decode(tmp) {
			if e := s.cache.check(tmp.Key); e == nil {
				s.cache.Add(&entry{record{tmp.Key, tmp.Value}, time{0, 0}, false, nil})
			}
		}

		// check to see if it got picked up out of the file
		if e := s.cache.Get(key); e != nil {
			return e.Value, nil
		}
	} else {
		return nil, err
	}

	return nil, nil // couldn't find it
}
Ejemplo n.º 5
0
func runFunc(w http.ResponseWriter, req *http.Request) {
	c := appengine.NewContext(req)
	defer req.Body.Close()

	var inv invocation
	if err := gob.NewDecoder(req.Body).Decode(&inv); err != nil {
		c.Errorf("delay: failed decoding task payload: %v", err)
		c.Warningf("delay: dropping task")
		return
	}

	f := funcs[inv.Key]
	if f == nil {
		c.Errorf("delay: no func with key %q found", inv.Key)
		c.Warningf("delay: dropping task")
		return
	}

	// TODO: This is broken for variadic functions.
	in := make([]reflect.Value, f.fv.Type().NumIn())
	in[0] = reflect.ValueOf(c)
	for i := 1; i < len(in); i++ {
		in[i] = reflect.ValueOf(inv.Args[i-1])
	}
	f.fv.Call(in)
}
Ejemplo n.º 6
0
func NewFlatStatCache() *FlatStatCache {
	filename := filepath.Join(osutil.CacheDir(), "camput.statcache")
	fc := &FlatStatCache{
		filename: filename,
		m:        make(map[string]fileInfoPutRes),
		dirty:    make(map[string]fileInfoPutRes),
	}

	if f, err := os.Open(filename); err == nil {
		defer f.Close()
		d := gob.NewDecoder(f)
		for {
			var key string
			var val fileInfoPutRes
			if d.Decode(&key) != nil || d.Decode(&val) != nil {
				break
			}
			val.Pr.Skipped = true
			fc.m[key] = val
			log.Printf("Read %q: %v", key, val)
		}
		log.Printf("Flatcache read %d entries from %s", len(fc.m), filename)
	}
	return fc
}
Ejemplo n.º 7
0
func runFunc(c appengine.Context, w http.ResponseWriter, req *http.Request) {
	defer req.Body.Close()

	var inv invocation
	if err := gob.NewDecoder(req.Body).Decode(&inv); err != nil {
		c.Errorf("delay: failed decoding task payload: %v", err)
		c.Warningf("delay: dropping task")
		return
	}

	f := funcs[inv.Key]
	if f == nil {
		c.Errorf("delay: no func with key %q found", inv.Key)
		c.Warningf("delay: dropping task")
		return
	}

	// TODO: This is broken for variadic functions.
	ft := f.fv.Type()
	in := make([]reflect.Value, ft.NumIn())
	in[0] = reflect.ValueOf(c)
	for i := 1; i < len(in); i++ {
		in[i] = reflect.ValueOf(inv.Args[i-1])
	}
	out := f.fv.Call(in)

	if n := ft.NumOut(); n > 0 && ft.Out(n-1) == osErrorType {
		if errv := out[n-1]; !errv.IsNil() {
			c.Errorf("delay: func failed (will retry): %v", errv.Interface())
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	}
}
Ejemplo n.º 8
0
func RetrieveActDetails(c appengine.Context, actId int) (res *ActDetail, err os.Error) {
	var d DSActDetail
	if itm, err := memcache.Get(c, "actId__"+strconv.Itoa(actId)); err != nil && err != memcache.ErrCacheMiss {
		return nil, err
	} else if err == nil {
		// Cache hit
		buf := bytes.NewBuffer(itm.Value)
		dec := gob.NewDecoder(buf)
		dec.Decode(&d)
	} else {
		// Cache miss
		key := datastore.NewKey(c, "DSActDetail", "", int64(actId), nil)
		if err := datastore.Get(c, key, &d); err == datastore.ErrNoSuchEntity {
			return &ActDetail{ActId: actId}, nil
		} else if err != nil {
			return nil, err
		}
		buf := bytes.NewBufferString("")
		enc := gob.NewEncoder(buf)
		enc.Encode(d)

		itm := &memcache.Item{
			Key:   "actId__" + strconv.Itoa(actId),
			Value: buf.Bytes(),
		}

		err = memcache.Set(c, itm)
		c.Debugf("Request cache to memcache")
	}
	return d.fromDS(), nil
}
Ejemplo n.º 9
0
func decodeGob(value string) (result map[string]interface{}) {
	buffer := bytes.NewBufferString(value)
	decoder := gob.NewDecoder(buffer)
	result = make(map[string]interface{})
	decoder.Decode(&result)
	return result
}
Ejemplo n.º 10
0
func (fr *FileReader) SeekToLatestFile() os.Error {
	fr.lk.Lock()
	defer fr.lk.Unlock()

	// Find the file
	dir, pre := path.Split(fr.prefix)
	if dir == "" {
		dir = "."
	}
	d, err := os.Open(dir, os.O_RDONLY, 0)
	if err != nil {
		return err
	}
	files, err := d.Readdirnames(-1)
	if err != nil {
		return err
	}
	d.Close()
	files = filterAndSort(files, pre)
	if len(files) == 0 {
		return os.EOF
	}

	// Open it
	file := path.Join(dir, files[len(files)-1])
	fr.file, err = os.Open(file, os.O_RDONLY, 0)
	if err != nil {
		fr.file, fr.dec = nil, nil
		return err
	}
	fr.dec = gob.NewDecoder(fr.file)

	return nil
}
Ejemplo n.º 11
0
func recoverLatestDecode(fprefix string, e interface{}) os.Error {
	r, err := MakeFileReader(fprefix)
	if err != nil {
		return err
	}
	err = r.SeekToLatestFile()
	if err != nil {
		return err
	}
	dec := gob.NewDecoder(r)
	// TODO: Deep copy the latest successful read, so the last
	// unsuccessful read does not garble the good data
	ok := false
	for {
		err = dec.Decode(e)
		if err != nil {
			break
		}
		ok = true
	}
	if ok {
		return nil
	}
	return os.EOF
}
Ejemplo n.º 12
0
func decodePassword(b []byte) *passwordhash.PasswordHash {
	hash := new(passwordhash.PasswordHash)
	err := gob.NewDecoder(bytes.NewBuffer(b)).Decode(hash)
	if err == nil {
		return hash
	}
	panic(err)
}
Ejemplo n.º 13
0
func (k *Key) GobDecode(buf []byte) error {
	gk := new(gobKey)
	if err := gob.NewDecoder(bytes.NewBuffer(buf)).Decode(gk); err != nil {
		return err
	}
	*k = *gobKeyToKey(gk)
	return nil
}
Ejemplo n.º 14
0
// deserialize decodes a session value using gob.
func deserialize(value []byte) (SessionData, error) {
	var session SessionData
	b := bytes.NewBuffer(value)
	d := gob.NewDecoder(b)
	if err := d.Decode(&session); err != nil {
		return nil, err
	}
	return session, nil
}
Ejemplo n.º 15
0
// NewClient returns a new Client to handle requests to the
// set of services at the other end of the connection.
func NewClient(conn io.ReadWriteCloser) *Client {
	client := new(Client);
	client.conn = conn;
	client.enc = gob.NewEncoder(conn);
	client.dec = gob.NewDecoder(conn);
	client.pending = make(map[uint64]*Call);
	go client.input();
	return client;
}
Ejemplo n.º 16
0
func oldPollChan(file *os.File) *LunchTracker {
	decode := gob.NewDecoder(file)
	poll := &LunchPoll{}
	err := decode.Decode(poll)
	if err != nil {
		panic(err)
	}
	return pollChan(*poll)
}
Ejemplo n.º 17
0
func newEncodeDecodeCloser(rwc io.ReadWriteCloser) *EncodeDecodeCloser {
	p := prof.NewReadWriteCloser(rwc)
	return &EncodeDecodeCloser{
		p,
		gob.NewEncoder(p),
		gob.NewDecoder(p),
		p,
	}
}
Ejemplo n.º 18
0
func LoadTGene(file io.Reader) (tGene *TGene) {
	tGene = new(TGene)
	dec := gob.NewDecoder(file)
	err := dec.Decode(tGene)
	if err != nil {
		panic(err.String())
	}
	return
}
Ejemplo n.º 19
0
func LatestDecode(fprefix string, e interface{}) os.Error {
	l, err := os.Open(fprefix+"-latest", os.O_RDONLY, 0)
	if err != nil {
		return recoverLatestDecode(fprefix, e)
	}
	dec := gob.NewDecoder(l)
	err = dec.Decode(e)
	l.Close()
	return err
}
Ejemplo n.º 20
0
func (db *FileSystem) load(r io.Reader, ser Serializer) Serial {
	gz, err := gzip.NewReader(r)
	defer gz.Close()
	item := ser.New()
	gdec := gob.NewDecoder(gz)
	err = gdec.Decode(item)
	if err != nil {
		return nil
	}
	return item
}
Ejemplo n.º 21
0
func NewTube(rwc io.ReadWriteCloser, r *bufio.Reader) *Tube {
	if r == nil {
		r = bufio.NewReader(rwc)
	}
	return &Tube{
		ReadWriteCloser: rwc,
		Reader:          r,
		Encoder:         gob.NewEncoder(rwc),
		Decoder:         gob.NewDecoder(r),
	}
}
Ejemplo n.º 22
0
func obtainMetaInfo(fin, fout *os.File) (int64, decodeBook) {
	var meta nMeta
	PanicIf(gob.NewDecoder(fin).Decode(&meta))
	db := make(decodeBook)
	for i, record := range meta.Cb {
		if record.Len != 0 {
			db[record.Code] = &dbRecord{char: byte(i), len: record.Len}
		}
	}
	return meta.Fsize, db
}
Ejemplo n.º 23
0
func tokenFromFile(file string) (*oauth.Token, os.Error) {
	if !*cacheToken {
		return nil, os.NewError("--cachetoken is false")
	}
	f, err := os.Open(file)
	if err != nil {
		return nil, err
	}
	t := new(oauth.Token)
	err = gob.NewDecoder(f).Decode(t)
	return t, err
}
Ejemplo n.º 24
0
func TestGobbable(t *testing.T) {
	tg := treegen.New()
	treeSpec := tg.D("foo",
		tg.D("bar",
			tg.D("aleph",
				tg.F("A", tg.B(42, 65537)),
				tg.F("a", tg.B(42, 65537))),
			tg.D("beth",
				tg.F("B", tg.B(43, 65537)),
				tg.F("b", tg.B(43, 65537))),
			tg.D("jimmy",
				tg.F("G", tg.B(44, 65537)),
				tg.F("g", tg.B(44, 65537)))),
		tg.D("baz",
			tg.D("uno",
				tg.F("1", tg.B(1, 65537)),
				tg.F("I", tg.B(1, 65537))),
			tg.D("dos",
				tg.F("2", tg.B(11, 65537)),
				tg.F("II", tg.B(11, 65537))),
			tg.D("tres",
				tg.F("3", tg.B(111, 65537)),
				tg.F("III", tg.B(111, 65537)))))

	path := treegen.TestTree(t, treeSpec)
	defer os.RemoveAll(path)

	foo, err := IndexDir(filepath.Join(path, "foo"))
	assert.Tf(t, err == nil, "%v", err)

	node, found := foo.Resolve(filepath.Join("bar", "aleph", "A"))
	assert.Equal(t, filepath.Join("bar", "aleph", "A"), RelPath(node))

	bufferEnc := bytes.NewBuffer([]byte{})
	encoder := gob.NewEncoder(bufferEnc)
	err = encoder.Encode(foo)
	assert.Tf(t, err == nil, "%v", err)

	bufferDec := bytes.NewBuffer(bufferEnc.Bytes())
	decoder := gob.NewDecoder(bufferDec)

	decFoo := &Dir{}
	err = decoder.Decode(decFoo)
	assert.Tf(t, err == nil, "%v", err)

	node, found = decFoo.Resolve(filepath.Join("bar", "aleph", "A"))
	assert.T(t, found)
	_, isFile := node.(*File)
	assert.T(t, isFile)

	assert.T(t, node.Parent() != nil)
	assert.Equal(t, filepath.Join("bar", "aleph", "A"), RelPath(node))
}
Ejemplo n.º 25
0
func (server *serverType) input(conn io.ReadWriteCloser) {
	dec := gob.NewDecoder(conn)
	enc := gob.NewEncoder(conn)
	sending := new(sync.Mutex)
	for {
		// Grab the request header.
		req := new(Request)
		err := dec.Decode(req)
		if err != nil {
			if err == os.EOF || err == io.ErrUnexpectedEOF {
				log.Stderr("rpc: ", err)
				break
			}
			s := "rpc: server cannot decode request: " + err.String()
			sendResponse(sending, req, invalidRequest, enc, s)
			continue
		}
		serviceMethod := strings.Split(req.ServiceMethod, ".", 0)
		if len(serviceMethod) != 2 {
			s := "rpc: service/method request ill:formed: " + req.ServiceMethod
			sendResponse(sending, req, invalidRequest, enc, s)
			continue
		}
		// Look up the request.
		server.Lock()
		service, ok := server.serviceMap[serviceMethod[0]]
		server.Unlock()
		if !ok {
			s := "rpc: can't find service " + req.ServiceMethod
			sendResponse(sending, req, invalidRequest, enc, s)
			continue
		}
		mtype, ok := service.method[serviceMethod[1]]
		if !ok {
			s := "rpc: can't find method " + req.ServiceMethod
			sendResponse(sending, req, invalidRequest, enc, s)
			continue
		}
		// Decode the argument value.
		argv := _new(mtype.argType)
		replyv := _new(mtype.replyType)
		err = dec.Decode(argv.Interface())
		if err != nil {
			log.Stderr("rpc: tearing down", serviceMethod[0], "connection:", err)
			sendResponse(sending, req, replyv.Interface(), enc, err.String())
			continue
		}
		go service.call(sending, mtype, req, argv, replyv, enc)
	}
	conn.Close()
}
Ejemplo n.º 26
0
func Load(fname string) (taxonomy, os.Error) {
	fh, err := os.Open(fname)
	if err != nil {
		return nil, err
	}
	t := make(map[string]string)
	dec := gob.NewDecoder(fh)
	err = dec.Decode(&t)
	if err != nil {
		return nil, err
	}
	return t, nil

}
Ejemplo n.º 27
0
func fetchMemcache(c appengine.Context) ([]Status, os.Error) {
	item, memErr := memcache.Get(c, "statuses")
	if memErr != nil {
		c.Errorf("Error fetching item from memcache, %s", memErr)
		return nil, memErr
	}

	var data []byte = item.Value
	buffer := bytes.NewBuffer(data)
	dec := gob.NewDecoder(buffer)

	var statusList []Status
	dec.Decode(&statusList)

	return statusList, nil
}
Ejemplo n.º 28
0
func (s *Swarm) LoadSwarm(filename string, config *Config) {
	f, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	defer func() { f.Close() }()
	d := gob.NewDecoder(f)
	err = d.Decode(s)
	if err != nil {
		panic(err)
	}
	s.config = config
	for i := range s.Particles {
		s.Particles[i].swarm = s
	}
}
Ejemplo n.º 29
0
// Load the gob written by WriteGob. This is 12x as fast as LoadXML(),
// taking less than 1/10 s while LoadXML takes >1 s, so it is preferred.
func LoadGob(filename string) (*Database, os.Error) {
	r, err := os.Open(filename, os.O_RDONLY, 0)
	if r == nil {
		return nil, err
	}

	database := Database{}

	//start := time.Nanoseconds()
	decoder := gob.NewDecoder(r)
	decoder.Decode(&database)
	//took := time.Nanoseconds() - start

	//fmt.Printf("Gob: Loaded %d games in %.4f s\n", len(database.Game), float(took) / 1e9)

	return &database, nil
}
Ejemplo n.º 30
0
func fetchStatusInMemcache(c appengine.Context, id uint64) (Status, os.Error) {
	key := "status" + strconv.Uitoa64(id)
	item, memErr := memcache.Get(c, key)
	if memErr != nil {
		c.Errorf("Error fetching item (%s)from memcache, %s", key, memErr)
		return Status{}, memErr
	}

	var data []byte = item.Value
	buffer := bytes.NewBuffer(data)
	dec := gob.NewDecoder(buffer)

	var status Status
	dec.Decode(&status)

	return status, nil
}