コード例 #1
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
	})

}
コード例 #2
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()
	}
}