Esempio n. 1
0
func PersistOneBoltTx(bucket *bolt.Bucket, key string, obj proto.Marshaler) error {
	exBytes, err := obj.Marshal()
	if err != nil { // pragma: nocover -- no idea how to produce this error
		return fmt.Errorf("Could not marshal object")
	}
	return bucket.Put([]byte(key), exBytes)
}
Esempio n. 2
0
// Init initializes this component. It requires an amount and an interval in
// seconds to be present.
func (RateLimitFilter) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
	amount, ok := params["amount"]
	if !ok {
		return errors.New("amount is required")
	}

	if i, err := strconv.Atoi(amount); err != nil || i <= 0 {
		return fmt.Errorf("amount must be a positive number > 0: %s", err)
	}

	interval, ok := params["interval"]
	if !ok {
		return errors.New("interval is required")
	}

	if i, err := strconv.Atoi(interval); err != nil || i <= 0 {
		return fmt.Errorf("interval must be a positive number: %s", err)
	}

	if err := b.Put([]byte(fmt.Sprintf("%s-amount", h.ID)), []byte(amount)); err != nil {
		return err
	}
	if err := b.Put([]byte(fmt.Sprintf("%s-interval", h.ID)), []byte(interval)); err != nil {
		return err
	}
	_, err := b.CreateBucketIfNotExists([]byte("requests"))
	return err
}
Esempio n. 3
0
func GetLink(links *bolt.Bucket, stats *het.CountStats, url *url.URL) (het.Link, error) {
	url.Fragment = ""

	lbytes := links.Get([]byte(url.String()))
	link := het.Link{}
	if lbytes != nil {
		// link already exists, return early
		json.Unmarshal(lbytes, &link)

		// follow redirects in the links bucket
		if link.Redirect {
			return GetLink(links, stats, &link.URL)
		}

		return link, nil
	}

	resp, err := http.Get(url.String())
	if err != nil {
		return link, err
	}

	defer resp.Body.Close()

	finalURL := resp.Request.URL
	finalURL.Fragment = ""

	link = het.Link{
		URL:          *finalURL,
		StatusCode:   resp.StatusCode,
		ContentType:  resp.Header.Get("Content-Type"),
		LastModified: strings.Trim(resp.Header.Get("Last-Modified"), " \t\n"),
	}

	lbytes, err = json.Marshal(&link)
	if err != nil {
		log.Fatal(err)
	}

	links.Put([]byte(finalURL.String()), lbytes)
	stats.LinkCount++

	// redirect link
	if finalURL.String() != url.String() {
		lrbytes, err := json.Marshal(&het.Link{
			URL:      *finalURL,
			Redirect: true,
		})

		if err != nil {
			log.Fatal(err)
		}

		links.Put([]byte(url.String()), lrbytes)
		stats.LinkCount++
	}

	return link, nil

}
Esempio n. 4
0
func AddOutgoingLink(links *bolt.Bucket, parentLink, childLink *het.Link) {
	if parentLink.Outgoing == nil {
		parentLink.Outgoing = make(map[string]bool)
	}

	if childLink.Incomming == nil {
		childLink.Incomming = make(map[string]bool)
	}

	parentLink.Outgoing[childLink.URL.String()] = true
	childLink.Incomming[parentLink.URL.String()] = true

	fmt.Printf("Incomming for %s %d\n", childLink.URL.String(), len(childLink.Incomming))
	fmt.Printf("Outgoing for %s %d\n", parentLink.URL.String(), len(parentLink.Outgoing))

	pbytes, err := json.Marshal(&parentLink)
	if err != nil {
		log.Fatal(err)
	}

	cbytes, err := json.Marshal(&childLink)
	if err != nil {
		log.Fatal(err)
	}

	links.Put([]byte(childLink.URL.String()), cbytes)
	links.Put([]byte(parentLink.URL.String()), pbytes)
}
Esempio n. 5
0
// regularly flush statistics
func FlushStats(stats *bolt.Bucket, countStats *het.CountStats) {
	sbytes, err := json.Marshal(countStats)
	if err != nil {
		log.Fatal(err)
	}
	stats.Put([]byte("count"), sbytes)
}
Esempio n. 6
0
func (db *DB) SaveEntry(entry omron.Entry, b *bolt.Bucket) error {
	encoded, err := json.Marshal(entry)
	if err != nil {
		return err
	}
	return b.Put([]byte(entryKey(entry)), encoded)
}
Esempio n. 7
0
func putChanCommitTxns(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error {
	var bc bytes.Buffer
	if err := writeOutpoint(&bc, channel.ChanID); err != nil {
		return err
	}
	txnsKey := make([]byte, len(commitTxnsKey)+bc.Len())
	copy(txnsKey[:3], commitTxnsKey)
	copy(txnsKey[3:], bc.Bytes())

	var b bytes.Buffer

	if err := channel.OurCommitTx.Serialize(&b); err != nil {
		return err
	}

	if err := wire.WriteVarBytes(&b, 0, channel.OurCommitSig); err != nil {
		return err
	}

	// TODO(roasbeef): should move this into putChanFundingInfo
	scratch := make([]byte, 4)
	byteOrder.PutUint32(scratch, channel.LocalCsvDelay)
	if _, err := b.Write(scratch); err != nil {
		return err
	}
	byteOrder.PutUint32(scratch, channel.RemoteCsvDelay)
	if _, err := b.Write(scratch); err != nil {
		return err
	}

	return nodeChanBucket.Put(txnsKey, b.Bytes())
}
Esempio n. 8
0
// Store a word,word -> word sequence
func storeWords(bucket *bolt.Bucket, word1, word2, word3 string) error {
	key := []byte(word1 + " " + word2)

	// Get value from bucket and decode it
	rawValue := bucket.Get(key)
	var value []string
	if rawValue == nil {
		value = make([]string, 0, 1)
	} else {
		if err := json.Unmarshal(rawValue, &value); err != nil {
			log.Printf("Cannot decode raw value for key '%v': %v; starting new empty key; old value is: %v", string(key), string(rawValue))
			value = make([]string, 0, 1)
		}
	}

	// Add new word to value
	value = append(value, word3)

	// Encode value and store it in bucket
	rawValue, err := json.Marshal(value)
	if err != nil {
		return err
	}

	if err := bucket.Put(key, rawValue); err != nil {
		return err
	}

	// All done
	return nil
}
Esempio n. 9
0
// Save saves an Entry in the database.
func (e *Entry) Save() error {
	if !database.Main.Opened {
		return fmt.Errorf("db must be opened before saving")
	}
	return database.Main.DB.Update(func(tx *bolt.Tx) error {
		var err error
		var b *bolt.Bucket
		var enc []byte
		var id uint64

		if b, err = tx.CreateBucketIfNotExists([]byte(Bucket)); err != nil {
			return fmt.Errorf("Error creating bucket : %s", err)
		}
		if enc, err = e.Encode(); err != nil {
			return fmt.Errorf("Could not encode : %s", err)
		}
		if e.ID == 0 {
			if id, err = b.NextSequence(); err != nil {
				return fmt.Errorf("Could not generate ID : %s", err)
			}
			e.ID = int(id)
		}
		return b.Put([]byte(strconv.Itoa(e.ID)), enc)
	})
}
Esempio n. 10
0
// Allocate returns the next available inode number, and marks it
// used.
//
// Returns OutOfInodes if there are no free inodes.
func Allocate(bucket *bolt.Bucket) (uint64, error) {
	c := bucket.Cursor()
	var i uint64
	k, _ := c.Last()
	if k != nil {
		i = bytesToInode(k)
	}

	// reserve a few inodes for internal use
	if i < tokens.MaxReservedInode {
		i = tokens.MaxReservedInode
	}

	i++

	if i&tokens.InodeKindMask != tokens.InodeKindNormal {
		return 0, OutOfInodes
	}

	var buf [8]byte
	inodeToBytes(i, buf[:])
	err := bucket.Put(buf[:], nil)
	if err != nil {
		return 0, err
	}
	return i, nil
}
Esempio n. 11
0
func putTypedValue(bucket *bolt.Bucket, key string, value string, dataType string) error {

	buf := new(bytes.Buffer)
	shouldWrite := true

	switch dataType {
	case "int":
		i64, err := strconv.ParseInt(value, 10, 64)
		checkError(err)
		binary.Write(buf, binary.BigEndian, i64)

	case "float":
		f64, err := strconv.ParseFloat(value, 64)
		checkError(err)
		binary.Write(buf, binary.BigEndian, f64)

		if math.IsNaN(f64) {
			shouldWrite = false
		}

	default:
		err := bucket.Put([]byte(key), []byte(value))
		return err
	}

	if !shouldWrite {
		return nil
	}

	err := bucket.Put([]byte(key), buf.Bytes())

	return err
}
Esempio n. 12
0
func executeUpdate(stmt *boltq.UpdateStatement, db *bolt.DB) error {
	return db.Update(func(tx *bolt.Tx) error {
		var bucket *bolt.Bucket
		var err error

		for _, name := range stmt.BucketPath {
			log.Debugln("navigating to bucket", name)
			bucket, err = tx.CreateBucketIfNotExists([]byte(name))

			if err != nil {
				return err
			}

			if bucket == nil {
				return fmt.Errorf("cannot find bucket %s", name)
			}
		}

		for k, v := range stmt.Fields {
			log.Debugf("putting %s -> %v", k, v)

			b, err := encode(v)
			if err != nil {
				return err
			}

			if err = bucket.Put([]byte(k), b); err != nil {
				return err
			}
		}

		return nil
	})
}
Esempio n. 13
0
func addCityToIndex(
	bucket *bolt.Bucket, id string, name string, locale string, population uint32,
) error {
	var err error
	var cityName *ds.CityName

	if locale == "" {
		locale = "en"
	}

	cityNameKey := []byte(ds.PrepareCityNameKey(name))
	if conflict := bucket.Get(cityNameKey); conflict != nil {
		cityName, err = ds.CityNameFromString(string(cityNameKey), string(conflict))
		if strconv.Itoa(cityName.CityId) != id {
			cityNameKey = []byte(string(cityNameKey) + "|" + id)
		}
	}

	err = bucket.Put(
		cityNameKey, []byte(
			name+"\t"+id+"\t"+locale+"\t"+strconv.Itoa(int(population)),
		),
	)

	return err
}
Esempio n. 14
0
func putEncodedObject(bucket *bolt.Bucket, key string, value interface{}) error {
	data, err := json.Marshal(value)
	if err != nil {
		return err
	}
	return bucket.Put([]byte(key), data)
}
Esempio n. 15
0
func instrumentsToDB(b *bolt.Bucket) error {
	d, err := ioutil.ReadFile("instruments")
	if err != nil {
		return err
	}

	lines := strings.Split(string(d), "\n")
	lines = lines[:len(lines)-1]
	instruments = make([]Instrument, len(lines))

	for i, line := range lines {
		var buf bytes.Buffer
		enc := gob.NewEncoder(&buf)
		s := strings.Split(line, ",")
		inst := Instrument{ID: i, Name: s[0], Tuned: s[1] == "1"}
		instruments[i] = inst
		err = enc.Encode(inst)
		if err != nil {
			return err
		}
		err = b.Put([]byte(s[0]), buf.Bytes())
		if err != nil {
			return err
		}
	}

	return nil
}
Esempio n. 16
0
func fillBucket(b *bolt.Bucket, prefix []byte) error {
	n := 10 + rand.Intn(50)
	for i := 0; i < n; i++ {
		v := make([]byte, 10*(1+rand.Intn(4)))
		_, err := crypto.Read(v)
		if err != nil {
			return err
		}
		k := append(prefix, []byte(fmt.Sprintf("k%d", i))...)
		if err := b.Put(k, v); err != nil {
			return err
		}
	}
	// limit depth of subbuckets
	s := 2 + rand.Intn(4)
	if len(prefix) > (2*s + 1) {
		return nil
	}
	n = 1 + rand.Intn(3)
	for i := 0; i < n; i++ {
		k := append(prefix, []byte(fmt.Sprintf("b%d", i))...)
		sb, err := b.CreateBucket(k)
		if err != nil {
			return err
		}
		if err := fillBucket(sb, append(k, '.')); err != nil {
			return err
		}
	}
	return nil
}
Esempio n. 17
0
func RepoCreateOrder(order CheckoutCartPlayLoad) interface{} {
	var newOrder interface{}

	GlobalDB.Update(func(tx *bolt.Tx) error {
		orderBucket, err := tx.CreateBucketIfNotExists([]byte(OrderTable))
		if err != nil {
			HandleError(err)
			return err
		}
		var cusOrderBuck *bolt.Bucket
		if order.ChannelAccountId == nil {
			cusOrderBuck, err = orderBucket.CreateBucketIfNotExists([]byte("GUESTUSER"))
		} else {
			cusOrderBuck, err = orderBucket.CreateBucketIfNotExists([]byte(order.ChannelAccountId.(string)))
		}
		if err != nil {
			HandleError(err)
			return err
		}
		newId, _ := orderBucket.NextSequence()
		newOrder = GenerateOrder(order, newId)

		orderBytes, _ := json.Marshal(newOrder)
		cusOrderBuck.Put(TableId(newId).ToBytes(), orderBytes)

		return nil
	})

	return newOrder
}
Esempio n. 18
0
// Init initializes this component. It requires a command to be present.
func (ExecuteAction) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
	command, ok := params["command"]
	if !ok {
		return errors.New("command is required")
	}

	return b.Put([]byte(fmt.Sprintf("%s-command", h.ID)), []byte(command))
}
Esempio n. 19
0
func (p *pluginT) save(store *bolt.Bucket) error {
	data, err := json.Marshal(p)
	if err != nil {
		return fmt.Errorf("Unable to marshal plugin %+v to json - %s", *p, err)
	}

	return store.Put([]byte(p.Name), data)
}
Esempio n. 20
0
// commitSiacoinOutputDiff applies or reverts a SiacoinOutputDiff from within
// a database transaction.
func (cs *ConsensusSet) commitBucketSiacoinOutputDiff(scoBucket *bolt.Bucket, scod modules.SiacoinOutputDiff, dir modules.DiffDirection) error {
	if build.DEBUG && (scoBucket.Get(scod.ID[:]) == nil) != (scod.Direction == dir) {
		panic(errRepeatInsert)
	}
	if scod.Direction == dir {
		return scoBucket.Put(scod.ID[:], encoding.Marshal(scod.SiacoinOutput))
	}
	return scoBucket.Delete(scod.ID[:])
}
Esempio n. 21
0
func put(bucket *bolt.Bucket, id int, node *VPNode) error {
	buffer := new(bytes.Buffer)

	err := binary.Write(buffer, binary.LittleEndian, node)
	if err != nil {
		return err
	}

	return bucket.Put(intToBytes(id), buffer.Bytes())
}
Esempio n. 22
0
func addTranslationsToCountry(
	bucket *bolt.Bucket, id int, translations []string,
) error {
	key := strconv.Itoa(id)
	val := bucket.Get([]byte(key))

	return bucket.Put([]byte(key), []byte(
		string(val)+";"+strings.Join(translations, ";"),
	))
}
Esempio n. 23
0
func bkPutNX(b *bolt.Bucket, k, v []byte) (ok bool, err error) {
	if b.Get(k) != nil {
		ok = false
		return
	}
	if err = b.Put(k, v); err == nil {
		ok = true
	}
	return
}
Esempio n. 24
0
func (store *BoltStore) put(bucket *bolt.Bucket, key string, value interface{}) error {
	var err error
	if is_value_basic(value) {
		if key == "" {
			return fmt.Errorf("key is absent")
		}
		byts, err := store.coder.Encode(value)
		if err != nil {
			return err
		}
		return bucket.Put([]byte(key), byts)
	}

	if key != "" {
		bucket, err = bucket.CreateBucketIfNotExists([]byte(key))
		if err != nil {
			return err
		}
	}

	switch value_type_kind(value) {
	case reflect.Slice:
		vs := reflect.ValueOf(value)
		for i := 0; i < vs.Len(); i++ {
			if err := store.put(bucket, strconv.Itoa(i), vs.Index(i).Interface()); err != nil {
				return err
			}
		}
		return nil
	case reflect.Map:
		vs, err := cast.ToStringMapE(value)
		if err != nil {
			return err
		}
		for k, v := range vs {
			if err := store.put(bucket, k, v); err != nil {
				return err
			}
		}
		return nil
	case reflect.Struct:
		vt := reflect.TypeOf(value)
		vv := reflect.ValueOf(value)
		for i := 0; i < vt.NumField(); i++ {
			vfield := vt.Field(i)
			if tag := vfield.Tag.Get("object"); tag != "" {
				if err := store.put(bucket, tag, vv.Field(i).Interface()); err != nil {
					return err
				}
			}
		}
		return nil
	}
	return fmt.Errorf("unsupport store value type")
}
Esempio n. 25
0
// put gob encodes the value and puts it in the given bucket with the given key.
// This function assumes b was created from a writeable transaction.
func put(b *bolt.Bucket, key []byte, val interface{}) error {
	// we reuse the buffer here since this is, by definition locked by being in
	// a bolt write action.
	defer buf.Reset()
	e := gob.NewEncoder(buf)
	err := e.Encode(val)
	if err != nil {
		return err
	}
	return b.Put(key, buf.Bytes())
}
Esempio n. 26
0
func appendChannelLogEntry(log *bolt.Bucket, delta *ChannelDelta,
	chanPoint *wire.OutPoint) error {

	var b bytes.Buffer
	if err := serializeChannelDelta(&b, delta); err != nil {
		return err
	}

	logEntrykey := makeLogKey(chanPoint, delta.UpdateNum)
	return log.Put(logEntrykey[:], b.Bytes())
}
Esempio n. 27
0
// Init initializes this component. It requires a secret to be present.
func (GithubValidator) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
	secret, ok := params["secret"]
	if !ok {
		return errors.New("secret is required")
	}
	if err := b.Put([]byte(fmt.Sprintf("%s-secret", h.ID)), []byte(secret)); err != nil {
		return err
	}
	_, err := b.CreateBucketIfNotExists([]byte("deliveries"))
	return err
}
Esempio n. 28
0
// Init initializes this component. It requires a Mailgun API key to be
// present.
func (MailgunValidator) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
	apikey, ok := params["apikey"]
	if !ok {
		return errors.New("apikey is required")
	}
	if err := b.Put([]byte(fmt.Sprintf("%s-apikey", h.ID)), []byte(apikey)); err != nil {
		return err
	}
	_, err := b.CreateBucketIfNotExists([]byte("tokens"))
	return err
}
Esempio n. 29
0
// Helpers
func resetTime(b *bolt.Bucket) error {
	err := b.Put([]byte(timestampKey), []byte(time.Now().Format(time.RFC3339)))
	if err != nil {
		return err
	}
	err = b.Put([]byte(countKey), []byte(strconv.Itoa(1)))
	if err != nil {
		return err
	}
	return nil
}
Esempio n. 30
0
// Init initializes this component. It requires a valid url parameter to be
// present.
func (ForwardRequestAction) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
	uri, ok := params["url"]
	if !ok {
		return errors.New("url is required")
	}

	if _, err := url.Parse(uri); err != nil {
		return fmt.Errorf("url is not valid: %s", err)
	}

	return b.Put([]byte(fmt.Sprintf("%s-url", h.ID)), []byte(uri))
}