示例#1
0
func (db *RocksDB) Get(hash data.Hash256) (data.Storer, error) {
	db.mu.Lock()
	cached, ok := db.cache.Get(hash)
	db.mu.Unlock()
	if ok {
		db.hits.Mark(1)
		return cached.(data.Storer), nil
	}
	value, err := db.db.Get(db.ro, hash[:])
	if err != nil {
		return nil, err
	}
	defer value.Free()
	if value.Size() == 0 {
		return nil, storage.ErrNotFound
	}
	node, err := data.ReadPrefix(bytes.NewReader(value.Data()), hash)
	if err != nil {
		return nil, err
	}
	db.misses.Mark(1)
	db.mu.Lock()
	db.cache.Add(hash, node)
	db.mu.Unlock()
	return node, nil
}
示例#2
0
文件: mem.go 项目: askk/ripple
func readInput(mem *MemoryDB, path string) error {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()
	r, err := gzip.NewReader(f)
	if err != nil {
		return err
	}
	defer r.Close()
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		parts := strings.Split(scanner.Text(), ":")
		var nodeid data.Hash256
		if _, err := hex.Decode(nodeid[:], []byte(parts[0])); err != nil {
			return err
		}
		value, err := hex.DecodeString(parts[1])
		if err != nil {
			return err
		}
		node, err := data.ReadPrefix(bytes.NewReader(value), nodeid)
		if err != nil {
			return err
		}
		mem.nodes[nodeid] = node
	}
	return scanner.Err()
}
示例#3
0
文件: explain.go 项目: askk/ripple
func main() {
	if len(os.Args) == 1 {
		showUsage()
	}
	flags.Parse(os.Args[2:])
	matches := argumentRegex.FindStringSubmatch(os.Args[1])
	r, err := websockets.NewRemote(*host)
	checkErr(err)
	glog.Infoln("Connected to: ", *host)
	switch {
	case len(matches) == 0:
		showUsage()
	case len(matches[1]) > 0:
		hash, err := data.NewHash256(matches[1])
		checkErr(err)
		fmt.Println("Getting transaction: ", hash.String())
		result, err := r.Tx(*hash)
		checkErr(err)
		explain(&result.TransactionWithMetaData, terminal.Default)
	case len(matches[2]) > 0:
		seq, err := strconv.ParseUint(matches[2], 10, 32)
		checkErr(err)
		ledger, err := r.Ledger(seq, true)
		checkErr(err)
		fmt.Println("Getting transactions for: ", seq)
		for _, txm := range ledger.Ledger.Transactions {
			explain(txm, terminal.Default)
		}
	case len(matches[3]) > 0:
		account, err := data.NewAccountFromAddress(matches[3])
		checkErr(err)
		fmt.Println("Getting transactions for: ", account.String())
		for txm := range r.AccountTx(*account, *pageSize) {
			explain(txm, terminal.ShowLedgerSequence)
		}
	case len(matches[4]) > 0:
		r := bufio.NewReader(os.Stdin)
		for line, err := r.ReadString('\n'); err == nil; line, err = r.ReadString('\n') {
			// TODO: Accept nodeid:nodedata format
			b, err := hex.DecodeString(line[:len(line)-1])
			checkErr(err)
			var nodeid data.Hash256
			v, err := data.ReadPrefix(bytes.NewReader(b), nodeid)
			checkErr(err)
			terminal.Println(v, terminal.Default)
		}
	}
}