Example #1
0
// createDSCOBucket creates a bucket for the delayed siacoin outputs at the
// input height.
func createDSCOBucket(tx *bolt.Tx, bh types.BlockHeight) {
	bucketID := append(prefixDSCO, encoding.Marshal(bh)...)
	_, err := tx.CreateBucket(bucketID)
	if build.DEBUG && err != nil {
		panic(err)
	}
}
Example #2
0
// Inserts a key into the database.
func simulatePutHandler(tx *bolt.Tx, qdb *QuickDB) {
	var err error
	keys, value := randKeys(), randValue()

	// Retrieve root bucket.
	b := tx.Bucket(keys[0])
	if b == nil {
		b, err = tx.CreateBucket(keys[0])
		if err != nil {
			panic("create bucket: " + err.Error())
		}
	}

	// Create nested buckets, if necessary.
	for _, key := range keys[1 : len(keys)-1] {
		child := b.Bucket(key)
		if child != nil {
			b = child
		} else {
			b, err = b.CreateBucket(key)
			if err != nil {
				panic("create bucket: " + err.Error())
			}
		}
	}

	// Insert into database.
	if err := b.Put(keys[len(keys)-1], value); err != nil {
		panic("put: " + err.Error())
	}

	// Insert into in-memory database.
	qdb.Put(keys, value)
}
Example #3
0
// createDSCOBucket creates a bucket for the delayed siacoin outputs at the
// input height.
func createDSCOBucket(tx *bolt.Tx, bh types.BlockHeight) error {
	bucketID := append(prefix_dsco, encoding.Marshal(bh)...)
	dscoBuckets := tx.Bucket(DSCOBuckets)
	err := dscoBuckets.Put(encoding.Marshal(bh), encoding.Marshal(bucketID))
	if err != nil {
		panic(err)
	}
	_, err = tx.CreateBucket(bucketID)
	return err
}
Example #4
0
func (g *Gadget) createGadget(tx *bolt.Tx) error {
	g.Id = gogadgets.GetUUID()
	b, err := tx.CreateBucket([]byte(g.Id))
	if err != nil {
		return err
	}
	if _, err := b.CreateBucket(_notes); err != nil {
		return err
	}
	_, err = b.CreateBucket(_stats)
	return err
}
Example #5
0
func (bl *boltLoader) saveRedirect(tx *bolt.Tx, redirect Redirect) error {
	bucket, err := tx.CreateBucket([]byte(redirect.Source))
	if err != nil {
		return err
	}

	err = bucket.Put([]byte(redirect.Target), []byte{})
	if err != nil {
		return err
	}

	return nil
}
Example #6
0
// Insert one wikimedia dump file at a time, the db file is named "[a-z]-[az]*", the regexp's are the letter of the 1st and last article in the dump file (get that in the main func)
func BoltInsertArticles(tx *bolt.Tx, articles map[string]PageItems) (err error) {
	for key, _ := range articles {
		b, err := tx.CreateBucket([]byte(key))
		if err != nil {
			return err
		}
		// Puts the name of the articles sections and section content into the bolt bucket, so the TX's bucket name is the article name, and TX's bucket content's key is the article's sections, pagerank, links and so forth (see type article struct for full content).
		for sectionKey, sectionText := range articles[key].Sections {
			if err := b.Put([]byte(sectionKey), []byte(sectionText)); err != nil {
				return err
			}
		}
	}
	return nil
}
Example #7
0
func (bl *boltLoader) savePage(tx *bolt.Tx, page Page) error {
	bucket, err := tx.CreateBucket([]byte(page.Title))
	if err != nil {
		return err
	}

	for _, link := range page.Links {
		err = bucket.Put([]byte(link), []byte{})
		if err != nil {
			return err
		}
	}

	return nil
}
Example #8
0
// refreshDB saves, deletes, and then restores all of the buckets in the
// database. This eliminates bugs during reorgs. The exact source of the bugs
// is unknown, but the problem is that after excessive use by Sia, calling
// Delete on a bucket will occasionally delete many more elements than the
// single element being targeted. It is strongly suspected that this is due to
// an error in the boltdb code, but the source remains unknown for the time
// being. Refreshing the database between blocks has solved the issue for the
// time being - it is currently unknown whether large blocks are able to
// trigger the error, but it is suspected that large blocks are safe.
func refreshDB(tx *bolt.Tx) {
	// Get a list of buckets.
	var bucketNames [][]byte
	err := tx.ForEach(func(bucketName []byte, _ *bolt.Bucket) error {
		bucketNames = append(bucketNames, bucketName)
		return nil
	})
	if err != nil {
		manageErr(tx, err)
	}

	for _, bucketName := range bucketNames {
		var keys [][]byte
		var values [][]byte
		err := tx.Bucket(bucketName).ForEach(func(k, v []byte) error {
			keys = append(keys, k)
			values = append(values, v)
			return nil
		})
		if err != nil {
			manageErr(tx, err)
		}

		err = tx.DeleteBucket(bucketName)
		if err != nil {
			manageErr(tx, err)
		}
		bucket, err := tx.CreateBucket(bucketName)
		if err != nil {
			manageErr(tx, err)
		}
		for i := range keys {
			err := bucket.Put(keys[i], values[i])
			if err != nil {
				manageErr(tx, err)
			}
		}
	}
}
Example #9
0
func Put(tx *bolt.Tx, bucket []byte, keys [][]byte, data []byte) error {
	var err error
	var buf []byte
	var b *bolt.Bucket
	b, err = tx.CreateBucketIfNotExists(bucket)
	if err != nil {
		return e.Forward(err)
	}
	if len(keys) == 0 {
		return e.New("no keys")
	}
	if len(keys) >= 2 {
		for i := 0; i < len(keys)-1; i++ {
			buf = b.Get(keys[i])
			if buf == nil {
				id, err := rand.Uuid()
				if err != nil {
					return e.Forward(err)
				}
				buf = []byte(id)
				err = b.Put(keys[i], buf)
				if err != nil {
					return e.Forward(err)
				}
			}
			b, err = tx.CreateBucket(buf)
			if e.Contains(err, "bucket already exists") {
				b = tx.Bucket(buf)
			} else if err != nil {
				return e.Forward(err)
			}
		}
	}
	err = b.Put(keys[len(keys)-1], data)
	if err != nil {
		return e.Forward(err)
	}
	return nil
}
Example #10
0
// initDatabase is run when the database. This has become the true
// init function for consensus set
func (cs *ConsensusSet) initDB(tx *bolt.Tx) error {
	// Enumerate the database buckets.
	buckets := [][]byte{
		BlockHeight,
		BlockMap,
		BlockPath,
		Consistency,
		SiacoinOutputs,
		FileContracts,
		SiafundOutputs,
		SiafundPool,
	}

	// Create the database buckets.
	for _, bucket := range buckets {
		_, err := tx.CreateBucket(bucket)
		if err != nil {
			return err
		}
	}

	// Place a 'false' in the consistency bucket to indicate that no
	// inconsistencies have been found.
	err := tx.Bucket(Consistency).Put(Consistency, encoding.Marshal(false))
	if err != nil {
		return err
	}

	// Set the block height to -1, so the genesis block is at height 0.
	blockHeight := tx.Bucket(BlockHeight)
	underflow := types.BlockHeight(0)
	err = blockHeight.Put(BlockHeight, encoding.Marshal(underflow-1))
	if err != nil {
		return err
	}

	// Set the siafund pool to 0.
	setSiafundPool(tx, types.NewCurrency64(0))

	// Update the siafund output diffs map for the genesis block on disk. This
	// needs to happen between the database being opened/initilized and the
	// consensus set hash being calculated
	for _, sfod := range cs.blockRoot.SiafundOutputDiffs {
		commitSiafundOutputDiff(tx, sfod, modules.DiffApply)
	}

	// Add the miner payout from the genesis block to the delayed siacoin
	// outputs - unspendable, as the unlock hash is blank.
	createDSCOBucket(tx, types.MaturityDelay)
	addDSCO(tx, types.MaturityDelay, cs.blockRoot.Block.MinerPayoutID(0), types.SiacoinOutput{
		Value:      types.CalculateCoinbase(0),
		UnlockHash: types.UnlockHash{},
	})

	// Add the genesis block to the block strucutres - checksum must be taken
	// after pushing the genesis block into the path.
	pushPath(tx, cs.blockRoot.Block.ID())
	if build.DEBUG {
		cs.blockRoot.ConsensusChecksum = consensusChecksum(tx)
	}
	addBlockMap(tx, &cs.blockRoot)
	return nil
}
Example #11
0
func store(tx *bolt.Tx, img Image, hash []byte) error {
	bimg := tx.Bucket([]byte(images))
	bhits := tx.Bucket([]byte(hits))

	buf, err := encImg(img)
	if err != nil {
		return e.Forward(err)
	}

	rect, err := img.Bounds()
	if err != nil {
		return e.Forward(err)
	}

	width := uint(rect.Max.X - rect.Min.X)
	height := uint(rect.Max.Y - rect.Min.Y)

	bufw := encLength(uint64(width))
	bufh := encLength(uint64(height))

	bucketw := append(hash, bufw...)
	bucketh := append(bucketw, bufh...)

	hitshash := []byte(string(hash) + string(bufw) + string(bufh))
	bufhits := encHits(0)

	err = bhits.Put(hitshash, bufhits)
	if err != nil {
		return e.Forward(err)
	}

	w := bimg.Get(hash)
	if w == nil {
		err = bimg.Put(hash, bufw)
		if err != nil {
			return e.Forward(err)
		}
		bw, err := tx.CreateBucket(bucketw)
		if err != nil {
			return e.Forward(err)
		}
		err = bw.Put(bufw, bufh)
		if err != nil {
			return e.Forward(err)
		}
		bh, err := tx.CreateBucket(bucketh)
		if err != nil {
			return e.Forward(err)
		}
		err = bh.Put(bufh, buf)
		if err != nil {
			return e.Forward(err)
		}
		return nil
	}

	bw := tx.Bucket(append(hash, w...))
	h := bw.Get(bufw)
	if h == nil {
		err = bw.Put(bufw, bufh)
		if err != nil {
			return e.Forward(err)
		}
		bh, err := tx.CreateBucket(bucketh)
		if err != nil {
			return e.Forward(err)
		}
		err = bh.Put(bufh, buf)
		if err != nil {
			return e.Forward(err)
		}
		return nil
	}

	bh := tx.Bucket([]byte(string(hash) + string(w) + string(h)))
	err = bh.Put(bufh, buf)
	if err != nil {
		return e.Forward(err)
	}

	return nil
}