Beispiel #1
0
func NewCookie(kvdb *bolt.DB) (*sessions.CookieStore, error) {
	var secret []byte

	// Create cookie secret if one does not exist.
	err := kvdb.Update(func(tx *bolt.Tx) error {
		bucket, err := tx.CreateBucketIfNotExists([]byte("cookie"))
		if err != nil {
			return err
		}

		secret = bucket.Get([]byte("secret"))
		if len(secret) == 0 {
			secret = []byte(uuid.New())
			err = bucket.Put([]byte("secret"), secret)
		}

		return err
	})

	if err != nil {
		return nil, err
	}

	if len(secret) == 0 {
		return nil, errors.New("Cookie secret cannot be empty.")
	}

	return sessions.NewCookieStore(secret), nil
}
Beispiel #2
0
func addUser(db *bolt.DB, email string, password string) bool {
	check := []byte("")
	hashedPass, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		fmt.Println(err)
	}
	db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("UsersBucket"))
		check = b.Get([]byte(email))
		return nil
	})
	if check == nil {
		db.Update(func(tx *bolt.Tx) error {
			b := tx.Bucket([]byte("UsersBucket"))
			err := b.Put([]byte(email), []byte(hashedPass))
			return err
		})
		if err != nil {
			fmt.Println(err)
		}
		return true
	} else {
		return false
	}
}
Beispiel #3
0
func (l *Layered) updateLayer(db *bolt.DB, layer *Layer, f func(*layerVertex)) error {
	return db.Update(func(tx *bolt.Tx) error {
		bucket := tx.Bucket(boltLayersBucket)
		key := []byte(layer.ID)
		data := bucket.Get(key)
		if len(data) == 0 {
			// This should never happen through this struct
			panic(fmt.Errorf("layer %s not found", layer.ID))
		}

		// Read the vertex, call the function to modify it
		var v layerVertex
		if err := l.structRead(&v, data); err != nil {
			return err
		}
		f(&v)

		// Save the resulting layer data
		data, err := l.structData(&v)
		if err != nil {
			return err
		}
		return bucket.Put(key, data)
	})
}
Beispiel #4
0
func (cmd *BenchCommand) runWritesWithSource(db *bolt.DB, options *BenchOptions, results *BenchResults, keySource func() uint32) error {
	results.WriteOps = options.Iterations

	for i := 0; i < options.Iterations; i += options.BatchSize {
		if err := db.Update(func(tx *bolt.Tx) error {
			b, _ := tx.CreateBucketIfNotExists(benchBucketName)
			b.FillPercent = options.FillPercent

			for j := 0; j < options.BatchSize; j++ {
				key := make([]byte, options.KeySize)
				value := make([]byte, options.ValueSize)

				// Write key as uint32.
				binary.BigEndian.PutUint32(key, keySource())

				// Insert key/value.
				if err := b.Put(key, value); err != nil {
					return err
				}
			}

			return nil
		}); err != nil {
			return err
		}
	}
	return nil
}
Beispiel #5
0
func benchWriteNestedWithSource(db *bolt.DB, options *BenchOptions, results *BenchResults, keySource func() uint32) error {
	results.WriteOps = options.Iterations

	for i := 0; i < options.Iterations; i += options.BatchSize {
		err := db.Update(func(tx *bolt.Tx) error {
			top, _ := tx.CreateBucketIfNotExists(benchBucketName)

			var name = make([]byte, options.KeySize)
			binary.BigEndian.PutUint32(name, keySource())
			b, _ := top.CreateBucketIfNotExists(name)

			for j := 0; j < options.BatchSize; j++ {
				var key = make([]byte, options.KeySize)
				var value = make([]byte, options.ValueSize)
				binary.BigEndian.PutUint32(key, keySource())
				if err := b.Put(key, value); err != nil {
					return err
				}
			}

			return nil
		})
		if err != nil {
			return err
		}
	}
	return nil
}
Beispiel #6
0
func (s *server) dump(db *bolt.DB, changes map[uint64]bool) {
	for k := range changes {
		// marshal
		var rs *RankSet
		s.lock_read(func() {
			rs = s.ranks[k]
		})
		if rs == nil {
			log.Warning("empty rankset:", k)
			continue
		}

		// serialization and save
		bin, err := rs.Marshal()
		if err != nil {
			log.Critical("cannot marshal:", err)
			os.Exit(-1)
		}

		db.Update(func(tx *bolt.Tx) error {
			b := tx.Bucket([]byte(BOLTDB_BUCKET))
			err := b.Put([]byte(fmt.Sprint(k)), bin)
			return err
		})
	}
}
Beispiel #7
0
//Put the key, value pair. index number metadata is prepended to the value
func (b *BoltDB) Put(key string, value []byte, opts *store.WriteOptions) error {
	var (
		dbIndex uint64
		db      *bolt.DB
		err     error
	)
	b.Lock()
	defer b.Unlock()

	dbval := make([]byte, libkvmetadatalen)

	if db, err = b.getDBhandle(); err != nil {
		return err
	}
	defer b.releaseDBhandle()

	err = db.Update(func(tx *bolt.Tx) error {
		bucket, err := tx.CreateBucketIfNotExists(b.boltBucket)
		if err != nil {
			return err
		}

		dbIndex = atomic.AddUint64(&b.dbIndex, 1)
		binary.LittleEndian.PutUint64(dbval, dbIndex)
		dbval = append(dbval, value...)

		err = bucket.Put([]byte(key), dbval)
		if err != nil {
			return err
		}
		return nil
	})
	return err
}
Beispiel #8
0
func SaveBucket(db *bolt.DB, bucketName string, key string, value string) {
	db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(bucketName))
		err := b.Put([]byte(key), []byte(value))
		return err
	})
}
Beispiel #9
0
func (file File) Save(db *bolt.DB) error {
	return db.Update(func(tx *bolt.Tx) error {
		filesBucket := tx.Bucket([]byte("files"))

		fileBucket, err := filesBucket.CreateBucketIfNotExists([]byte(file.FileToken))
		if err != nil {
			return err
		}

		now := time.Now()

		marshaledCreationTime, err := now.MarshalBinary()
		if err != nil {
			return err
		}

		marshaledDeleteDate, err := now.AddDate(0, 0, file.DaysAvailable).MarshalBinary()
		if err != nil {
			return err
		}

		err = fileBucket.Put([]byte("user-token"), []byte(file.UserToken))
		err = fileBucket.Put([]byte("filename"), []byte(file.Filename))
		err = fileBucket.Put([]byte("expires-in"), []byte(strconv.Itoa(file.DaysAvailable)))
		err = fileBucket.Put([]byte("creation-time"), marshaledCreationTime)
		err = fileBucket.Put([]byte("delete-date"), marshaledDeleteDate)
		if err != nil {
			return err
		}
		return nil
	})
}
Beispiel #10
0
func boltPrepareBuckets(db *bolt.DB) error {
	return db.Update(func(tx *bolt.Tx) error {
		var err error
		_, err = tx.CreateBucketIfNotExists([]byte(boltTokenBucket))
		if err != nil {
			return err
		}
		_, err = tx.CreateBucketIfNotExists([]byte(boltItemBucket))
		if err != nil {
			return err
		}
		_, err = tx.CreateBucketIfNotExists([]byte(boltPendingBucket))
		if err != nil {
			return err
		}
		_, err = tx.CreateBucketIfNotExists([]byte(boltCloudBucket))
		if err != nil {
			return err
		}
		_, err = tx.CreateBucketIfNotExists([]byte(boltGatewayBucket))
		if err != nil {
			return err
		}
		_, err = tx.CreateBucketIfNotExists([]byte(boltLocalTokenBucket))
		if err != nil {
			return err
		}
		_, err = tx.CreateBucketIfNotExists([]byte(boltOnboardedAppBucket))
		if err != nil {
			return err
		}
		return nil
	})
}
Beispiel #11
0
func (s *server) dump(db *bolt.DB, changes map[uint64]bool) {
	db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(BOLTDB_BUCKET))
		for k := range changes {
			ep := s.read_ep(k)
			if ep == nil {
				log.Errorf("cannot find endpoint %v", k)
				continue
			}

			// serialization and save
			bin, err := msgpack.Marshal(ep.Read())
			if err != nil {
				log.Critical("cannot marshal:", err)
				continue
			}
			err = b.Put([]byte(fmt.Sprint(k)), bin)
			if err != nil {
				log.Critical(err)
				continue
			}
		}
		return nil
	})
}
Beispiel #12
0
func updateTags(db *bolt.DB, ref string, tags []string, remove bool) error {
	return db.Update(func(tx *bolt.Tx) (err error) {
		bucket := tx.Bucket([]byte(TAG))
		if err != nil {
			return
		}
		current := make([]string, 0)
		value := bucket.Get([]byte(ref))
		if value != nil {
			dec := gob.NewDecoder(bytes.NewBuffer(value))
			err = dec.Decode(&current)
			if err != nil {
				return
			}
		}
		current = applyTags(current, tags, remove)
		var buffer bytes.Buffer
		enc := gob.NewEncoder(&buffer)
		err = enc.Encode(current)
		if err != nil {
			return
		}
		bucket.Put([]byte(ref), buffer.Bytes())
		return
	})

}
Beispiel #13
0
// New creates a wait queue.
// The bucket will be created if it does not exist. Otherwise,
// the bucket will be deleted and created again.
func NewHeap(db *bolt.DB, bucket []byte, bufsize int) (h *Heap, err error) {
	if err = db.Update(func(tx *bolt.Tx) error {
		if tx.Bucket(bucket) != nil {
			if err := tx.DeleteBucket(bucket); err != nil {
				return err
			}
		}
		if _, err := tx.CreateBucketIfNotExists(bucket); err != nil {
			return err
		}
		return nil

	}); err != nil {
		db.Close()
		return
	}
	h = &Heap{
		db:       db,
		count:    0,
		min:      nil,
		writebuf: list.New(),
		bufsize:  bufsize,
		bucket:   bucket,
	}
	return
}
Beispiel #14
0
func updateList(db *bolt.DB, params martini.Params, req *http.Request, r render.Render) {

	var l DistributionList

	body, _ := ioutil.ReadAll(req.Body)
	req.Body.Close()
	err := json.Unmarshal(body, &l)

	if err != nil {
		r.Error(http.StatusBadRequest)
		return
	}

	if params["id"] != l.Id {
		r.Error(http.StatusBadRequest)
		return
	}

	// marshal back out to json to normalize our data
	j, err := json.Marshal(l)

	db.Update(
		func(tx *bolt.Tx) error {
			b := tx.Bucket([]byte(bucketDistributionLists))
			return b.Put([]byte(l.Id), j)
		})

	r.Status(http.StatusOK)
}
Beispiel #15
0
func logAction(db *bolt.DB, action string) {
	dbBucketName := "alphazero"
	dbBucketLogs := fmt.Sprintf("%s.logs", dbBucketName)
	db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(dbBucketLogs))

		id, _ := b.NextSequence()
		l := LogEntry{
			ID:     id,
			When:   strconv.FormatInt(time.Now().Unix(), 10),
			Action: action,
			UUID:   uuid.NewV4().String(),
		}
		buf, err := json.Marshal(l)
		if err != nil {
			return err
		}

		err = b.Put(itob(l.ID), buf)
		if err != nil {
			return err
		}
		return nil
	})
}
Beispiel #16
0
func GetAllReminders(db *bolt.DB) (Reminders, error) {
	var allRems Reminders

	e := db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists(boltBucket)
		if err != nil {
			return err
		}

		return b.ForEach(func(k, v []byte) error {
			var rem Reminder

			if err := json.Unmarshal(v, &rem); err != nil {
				return err
			}

			rem.ID = binary.BigEndian.Uint64(k)
			rem.cancel = make(chan struct{})

			allRems = append(allRems, &rem)

			return nil
		})
	})

	return allRems, e
}
Beispiel #17
0
// *whirrrrrrrrrrr*
func probe(db *bolt.DB, api *tbotapi.TelegramBotAPI, closed chan struct{}, wg *sync.WaitGroup) {
	dbBucketName := "alphazero"
	dbBucketStats := fmt.Sprintf("%s.stats", dbBucketName)
	for {
		select {
		case <-closed:
			wg.Done()
			return
		case val := <-api.Updates:
			typ := val.Message.Type()
			if typ != model.TextType {
				fmt.Fprintf(os.Stderr, "Not handling type: %s\n", typ)
				continue
			}

			response, md := process(db, val)
			db.Update(func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte(dbBucketStats))
				id := []byte("messages.count.received")
				var val []byte
				if val = b.Get(id); val == nil {
					val = []byte("0")
				}
				c, _ := strconv.Atoi(string(val))
				c++
				return b.Put(id, []byte(strconv.Itoa(c)))
			})

			// unsafeval := blackfriday.MarkdownCommon([]byte(*val.Message.Text))
			// unsafe := blackfriday.MarkdownCommon([]byte(response))
			// html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)

			sender := val.Message.Chat
			recipient := model.NewRecipientFromChat(val.Message.Chat)

			om := model.NewOutgoingMessage(
				model.NewChatRecipient(*recipient.ChatID),
				response).SetMarkdown(md)

			var msg *model.MessageResponse
			var err error

			msg, err = api.SendMessageExtended(om)

			if err != nil {
				msg, err = api.SendMessageExtended(om.SetMarkdown(false))
				if err != nil {
					fmt.Fprintf(os.Stderr, "Err: %s\n", err)
					continue
				}
			}

			fmt.Printf(
				"MessageID: %04d, ChatID: %s, Text: %s, IsGroupChat:%t\n",
				msg.Message.ID, sender.String(), response, msg.Message.Chat.IsGroupChat())
		case val := <-api.Errors:
			fmt.Printf("Err: %s\n", val)
		}
	}
}
Beispiel #18
0
func updateDatabase(db *bolt.DB, version string) error {

	items, err := godragon.StaticItems(version)
	if err != nil {
		return err
	}
	var buff bytes.Buffer

	db.Update(func(tx *bolt.Tx) error {
		_, err := tx.CreateBucketIfNotExists([]byte("items"))
		return err
	})

	for i := range items {
		db.Update(func(tx *bolt.Tx) error {
			b := tx.Bucket([]byte("items"))

			enc := gob.NewEncoder(&buff)
			enc.Encode(items[i])
			err := b.Put([]byte(items[i].Name), buff.Bytes())
			buff.Reset()
			return err
		})
	}
	return nil
}
Beispiel #19
0
func InsertData(db *bolt.DB, artist string, track string, cover string) {
	db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket(DATABASE_BUCKET)
		err := b.Put([]byte(artist+" - "+track), []byte(cover))
		return err
	})
}
Beispiel #20
0
func generateSourceFile(db *bolt.DB, w io.Writer, reminders time.Duration, regen bool) error {
	return db.Update(func(tx *bolt.Tx) error {
		bucket, err := tx.CreateBucketIfNotExists(aliasBucket)
		if err != nil {
			return err
		}

		fmt.Fprintln(w, "# This file is automatically generated by a-fiend, do not edit")
		fmt.Fprintf(w, "# UPDATED %s\n", time.Now())
		fmt.Fprintf(w, "# Prints reminders for %s\n", reminders)
		fmt.Fprintln(w, "AFIEND_T=`date +%s`")

		c := bucket.Cursor()
		for k, v := c.First(); k != nil; k, v = c.Next() {
			info := aliasInfo{}
			err = json.Unmarshal(v, &info)
			if err != nil {
				return err
			}

			fmt.Fprintf(w, "alias %s=%s\n", string(k), quoteWith(info.Command, '\'', false))
			cutoff := info.Added.Add(reminders)
			fmt.Fprintf(w, "if [ %d -ge $AFIEND_T ]; then\n", cutoff.Unix())
			fmt.Fprintf(w, "\techo alias %s=%s\n", string(k), quoteWith(info.Command, '\'', false))
			fmt.Fprintln(w, "fi")
		}

		if regen {
			fmt.Fprintf(w, "(a-fiend source -reminders %s > /dev/null 2>&1 &)\n", reminders)
		}

		return nil
	})
}
Beispiel #21
0
// DeleteTree deletes a range of keys with a given prefix
func (b *BoltDB) DeleteTree(keyPrefix string) error {
	var (
		db  *bolt.DB
		err error
	)
	b.Lock()
	defer b.Unlock()

	if db, err = b.getDBhandle(); err != nil {
		return err
	}
	defer b.releaseDBhandle()

	err = db.Update(func(tx *bolt.Tx) error {
		bucket := tx.Bucket(b.boltBucket)
		if bucket == nil {
			return store.ErrKeyNotFound
		}

		cursor := bucket.Cursor()
		prefix := []byte(keyPrefix)

		for key, _ := cursor.Seek(prefix); bytes.HasPrefix(key, prefix); key, _ = cursor.Next() {
			_ = bucket.Delete([]byte(key))
		}
		return nil
	})

	return err
}
Beispiel #22
0
//写入文件
func (s *server) dump(db *bolt.DB, changes map[uint64]bool) {
	//写入已经更改的id
	for k := range changes {
		//读取message
		ep := s.read_ep(k)
		if ep == nil {
			log.Errorf("canot find endpoint %v", k)
			continue
		}

		//序列化数据
		bin, err := msgpack.Marshal(ep.Read())
		if err != nil {
			log.Critical("canot marshal:", err)
			continue
		}

		//更新表
		db.Update(func(tx *bolt.Tx) error {
			//拿到表句柄
			b := tx.Bucket([]byte(BOLTDB_BUCKET))
			//存入数据, k-v形式
			err := b.Put([]byte(fmt.Sprint(k)), bin)
			return err
		})
	}
}
Beispiel #23
0
// newBroadcastsStore creates a new BroadcastAddresses object after doing the
// necessary initialization.
func newBroadcastsStore(db *bolt.DB, username string) (*BroadcastAddresses, error) {

	b := &BroadcastAddresses{
		db:       db,
		username: []byte(username),
		addrs:    make([]bmutil.Address, 0),
	}

	err := db.Update(func(tx *bolt.Tx) error {
		userbucket, err := tx.CreateBucketIfNotExists([]byte(username))
		if err != nil {
			return err
		}

		bucket, err := userbucket.CreateBucketIfNotExists(broadcastAddressesBucket)
		if err != nil {
			return err
		}

		return bucket.ForEach(func(k, _ []byte) error {
			addr, err := bmutil.DecodeAddress(string(k))
			if err != nil {
				return err
			}

			b.addrs = append(b.addrs, *addr)
			return nil
		})
	})
	if err != nil {
		return nil, err
	}

	return b, nil
}
Beispiel #24
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
	})
}
Beispiel #25
0
func writePageAttribute(PageDB *bolt.DB, AttrName, AttrValue string) error {
	UpdateFuncErr := PageDB.Update(func(tx *bolt.Tx) error {
		bucket := tx.Bucket([]byte("PageAttributes"))
		return bucket.Put([]byte(AttrName), []byte(AttrValue))
	})
	return UpdateFuncErr
}
Beispiel #26
0
// AtomicDelete deletes a value at "key" if the key
// has not been modified in the meantime, throws an
// error if this is the case
func (b *BoltDB) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
	var (
		val []byte
		db  *bolt.DB
		err error
	)

	if previous == nil {
		return false, store.ErrPreviousNotSpecified
	}
	if db, err = b.getDBhandle(); err != nil {
		return false, err
	}
	defer b.releaseDBhandle()

	err = db.Update(func(tx *bolt.Tx) error {
		bucket := tx.Bucket(b.boltBucket)
		if bucket == nil {
			return ErrBoltBucketNotFound
		}

		val = bucket.Get([]byte(key))
		dbIndex := binary.LittleEndian.Uint64(val[:libkvmetadatalen])
		if dbIndex != previous.LastIndex {
			return store.ErrKeyModified
		}
		err := bucket.Delete([]byte(key))
		return err
	})
	if err != nil {
		return false, err
	}
	return true, err
}
Beispiel #27
0
// init initializes the database for this layer setup.
func (l *Layered) init(db *bolt.DB) ([]*layerVertex, error) {
	layerVertices := make([]*layerVertex, len(l.Layers))
	for i, layer := range l.Layers {
		var parent *Layer
		if i > 0 {
			parent = l.Layers[i-1]
		}

		layerVertex, err := l.initLayer(db, layer, parent)
		if err != nil {
			return nil, err
		}

		layerVertices[i] = layerVertex
		if parent != nil {
			// We have a prior layer, so setup the edge pointer
			err = db.Update(func(tx *bolt.Tx) error {
				bucket := tx.Bucket(boltEdgesBucket)
				return bucket.Put(
					[]byte(layer.ID),
					[]byte(parent.ID))
			})
			if err != nil {
				return nil, err
			}
		}
	}

	return layerVertices, nil
}
Beispiel #28
0
func (c *Context) SerializeIndex(index *Index) (err error) {
	var data []byte
	var db *bolt.DB

	if data, err = json.Marshal(index); err != nil {
		return
	}

	db, err = c.OpenDB()
	if err != nil {
		return err
	}
	defer db.Close()

	return db.Update(func(tx *bolt.Tx) error {
		bucket, err := tx.CreateBucketIfNotExists(byteify(IndicesKey))
		if err != nil {
			return err
		}
		if bucket == nil {
			return ErrNoSuchDbBucket
		}
		return bucket.Put(byteify(index.FileId), data)
	})
}
Beispiel #29
0
func (l *Layered) deleteLayer(db *bolt.DB, layer *Layer, path string) error {
	if err := os.RemoveAll(path); err != nil {
		return err
	}

	return db.Update(func(tx *bolt.Tx) error {
		// Delete the layer itself
		bucket := tx.Bucket(boltLayersBucket)
		key := []byte(layer.ID)
		if err := bucket.Delete(key); err != nil {
			return err
		}

		// Delete all the edges
		bucket = tx.Bucket(boltEdgesBucket)
		if err := bucket.Delete(key); err != nil {
			return err
		}

		// Find any values
		return bucket.ForEach(func(k, data []byte) error {
			if string(data) == layer.ID {
				return bucket.Delete(k)
			}

			return nil
		})
	})
}
Beispiel #30
0
func CreateStatisticsBucket(db *bolt.DB) error {
	return db.Update(func(tx *bolt.Tx) error {
		tx.DeleteBucket(StatisticsBucketName)
		_, err := tx.CreateBucket(StatisticsBucketName)
		return err
	})
}