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 }
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!") }
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) } } } }
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 }
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) }
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 }
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 } } }
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 }
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 }
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 }
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 }
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) }
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 }
// 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 }
// 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; }
func oldPollChan(file *os.File) *LunchTracker { decode := gob.NewDecoder(file) poll := &LunchPoll{} err := decode.Decode(poll) if err != nil { panic(err) } return pollChan(*poll) }
func newEncodeDecodeCloser(rwc io.ReadWriteCloser) *EncodeDecodeCloser { p := prof.NewReadWriteCloser(rwc) return &EncodeDecodeCloser{ p, gob.NewEncoder(p), gob.NewDecoder(p), p, } }
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 }
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 }
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 }
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), } }
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 }
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 }
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)) }
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() }
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 }
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 }
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 } }
// 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 }
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 }