コード例 #1
0
ファイル: boltdb.go プロジェクト: conseweb/factoid
// Return the instance, properly unmarshaled, given the entry in the database, which is
// the hash for the Instance (vv) followed by the source from which to unmarshal (v)
func (d *BoltDB) GetInstance(v []byte) fct.IBlock {

	var vv [32]byte
	copy(vv[:], v[:32])
	v = v[32:]

	var instance fct.IBlock = d.instances[vv]
	if instance == nil {
		vp := fct.NewHash(vv[:])
		fct.Prtln("Object hash: ", vp)
		panic("This should not happen.  Object stored in the database has no IBlock instance")
	}

	r := instance.GetNewInstance()
	if r == nil {
		panic("An IBlock has failed to implement GetNewInstance()")
	}

	datalen, v := binary.BigEndian.Uint32(v[0:4]), v[4:]
	if len(v) != int(datalen) {
		fct.Prtln("Lengths don't match.  Expected ", datalen, " and got ", len(v))
		panic("Data not returned properly")
	}
	err := r.UnmarshalBinary(v)
	if err != nil {
		panic("This should not happen.  IBlock failed to unmarshal.")
	}

	return r
}
コード例 #2
0
ファイル: boltdb.go プロジェクト: conseweb/factoid
func (d *BoltDB) PutRaw(bucket []byte, key []byte, value fct.IBlock) {
	var out bytes.Buffer
	hash := value.GetDBHash()
	out.Write(hash.Bytes())
	data, err := value.MarshalBinary()
	binary.Write(&out, binary.BigEndian, uint32(len(data)))
	out.Write(data)

	if err != nil {
		panic("This should not happen.  Failed to marshal IBlock for BoltDB")
	}
	d.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket(bucket)
		err := b.Put(key, out.Bytes())
		return err
	})

}
コード例 #3
0
ファイル: boltdb_test.go プロジェクト: conseweb/factoid
func Test_bolt_init(t *testing.T) {
	db := new(BoltDB)

	bucketList := make([][]byte, 5, 5)

	bucketList[0] = []byte("one")
	bucketList[1] = []byte("two")
	bucketList[2] = []byte("three")
	bucketList[3] = []byte("four")
	bucketList[4] = []byte("five")

	instances := make(map[[fct.ADDRESS_LENGTH]byte]fct.IBlock)
	{
		var a fct.IBlock
		a = new(fct.Address)
		instances[cp(a.GetDBHash())] = a
		a = new(fct.Hash)
		instances[cp(a.GetDBHash())] = a
		a = new(fct.InAddress)
		instances[cp(a.GetDBHash())] = a
		a = new(fct.OutAddress)
		instances[cp(a.GetDBHash())] = a
		a = new(fct.OutECAddress)
		instances[cp(a.GetDBHash())] = a
		a = new(fct.RCD_1)
		instances[cp(a.GetDBHash())] = a
		a = new(fct.RCD_2)
		instances[cp(a.GetDBHash())] = a
		a = new(fct.Signature)
		instances[cp(a.GetDBHash())] = a
		a = new(fct.Transaction)
		instances[cp(a.GetDBHash())] = a
	}
	db.Init(bucketList, instances)
	a := new(fct.Address)
	a.SetBytes(fct.Sha([]byte("I came, I saw")).Bytes())
	db.Put("one", fct.Sha([]byte("one")), a)
	r := db.Get("one", fct.Sha([]byte("one")))

	if a.IsEqual(r) != nil {
		t.Fail()
	}

	db.DeleteKey([]byte("one"), fct.Sha([]byte("one")).Bytes())
	r = db.Get("one", fct.Sha([]byte("one")))

	if r != nil {
		t.Fail()
	}
}