func PutTokenInfo(tokenInfo *TokenInfo) error {
	oldTokenInfo, err := GetTokenInfo(tokenInfo.Token, tokenInfo.Client)
	if err != nil {
		return err
	}
	if oldTokenInfo != nil {
		return errors.New("duplicate token")
	}
	err = db.Update(func(tx *bolt.Tx) error {
		var clientBucket *bolt.Bucket
		clientBucket = tx.Bucket([]byte(tokenInfo.Client))
		if clientBucket == nil {
			clientBucket, err = tx.CreateBucket([]byte(tokenInfo.Client))
			if err != nil {
				return err
			}
		}
		tokenBucket, err := clientBucket.CreateBucket([]byte(tokenInfo.Token))
		if err != nil {
			return err
		}
		tokenBucket.Put([]byte("token"), []byte(tokenInfo.Token))
		tokenBucket.Put([]byte("client"), []byte(tokenInfo.Client))
		tokenBucket.Put([]byte("user"), []byte(tokenInfo.User))
		tokenBucket.Put([]byte("scopes"), []byte(tokenInfo.Scopes))
		expireBinary, err := tokenInfo.ExpireTime.MarshalBinary()
		if err != nil {
			return err
		}
		tokenBucket.Put([]byte("expire-time"), expireBinary)
		return nil
	})
	return err
}
Esempio n. 2
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. 3
0
func bucketFor(key Key, b *bolt.Bucket) (*bolt.Bucket, []byte, error) {
	var err error
	elems, last := key.B()
	for _, e := range elems {
		// CreateBucketIfNotExists requires a writeable transaction.
		if new := b.Bucket(e); new != nil {
			b = new
			continue
		}
		if b, err = b.CreateBucket(e); err != nil {
			return b, last, err
		}
	}
	return b, last, nil
}
Esempio n. 4
0
// NewUniqueIndex loads a UniqueIndex
func NewUniqueIndex(parent *bolt.Bucket, indexName []byte) (*UniqueIndex, error) {
	var err error
	b := parent.Bucket(indexName)
	if b == nil {
		if !parent.Writable() {
			return nil, ErrNotFound
		}
		b, err = parent.CreateBucket(indexName)
		if err != nil {
			return nil, err
		}
	}

	return &UniqueIndex{
		IndexBucket: b,
		Parent:      parent,
	}, nil
}
Esempio n. 5
0
File: list.go Progetto: asdine/storm
// NewListIndex loads a ListIndex
func NewListIndex(parent *bolt.Bucket, indexName []byte) (*ListIndex, error) {
	var err error
	b := parent.Bucket(indexName)
	if b == nil {
		if !parent.Writable() {
			return nil, ErrNotFound
		}
		b, err = parent.CreateBucket(indexName)
		if err != nil {
			return nil, err
		}
	}

	ids, err := NewUniqueIndex(b, []byte("storm__ids"))
	if err != nil {
		return nil, err
	}

	return &ListIndex{
		IndexBucket: b,
		Parent:      parent,
		IDs:         ids,
	}, nil
}
Esempio n. 6
0
func newMeta(b *bolt.Bucket, n Node) (*meta, error) {
	m := b.Bucket([]byte(metadataBucket))
	if m != nil {
		name := m.Get([]byte(metaCodec))
		if string(name) != n.Codec().Name() {
			return nil, ErrDifferentCodec
		}
		return &meta{
			node:   n,
			bucket: m,
		}, nil
	}

	m, err := b.CreateBucket([]byte(metadataBucket))
	if err != nil {
		return nil, err
	}

	m.Put([]byte(metaCodec), []byte(n.Codec().Name()))
	return &meta{
		node:   n,
		bucket: m,
	}, nil
}
Esempio n. 7
0
func (this *Tab) process(action string, resultChan chan Result) {
	switch action {

	case "addTab":
		this.Id = getNextid()
		this.LastAccessTime = time.Now()
		this.ETag = fmt.Sprint(time.Now().UnixNano())
		if bookTabs[this.Bookid] == nil {
			log.Fatal("addTab - book tabMap not found for bookid =", this.Bookid)
		}
		bookTabs[this.Bookid][this.Id] = this
		tabPrevids[this.Id] = make(previdMap)   // see previd.go
		dbWrite("saveNewTab", this, resultChan) // save new Tab to db

	case "saveNewTab":
		var err, err1, err2, err3 error
		var k, v []byte
		var bktBook, bktTabs, bktTab *bolt.Bucket

		tx, _ := db.Begin(true)
		bumpSequentialid(tx) // required for all db doc adds

		// --- add new tab to "tabs" bkt inside parent book bkt ---
		bktBook = openBook(tx, this.Bookid)
		bktTabs = openTabs(bktBook)
		k = bs(this.Id)
		v, err1 = json.Marshal(this)
		err2 = bktTabs.Put(k, v)

		// --- create bkt for this tab to hold its notes & Commit ---
		bktBook.CreateBucket(bs("tab_" + this.Id))
		err3 = tx.Commit()
		if err = lib.CheckErrs(errNotFatal, action, err1, err2, err3); err != nil {
			tx.Rollback()
			log.Fatal("Fatal DB Error-creating new tab bkts, Ending Program")
		}

		// --- create "notes","previds" bkts inside new tab's bkt & Commit ---
		tx, _ = db.Begin(true)
		bktBook = openBook(tx, this.Bookid)
		bktTab = openTab(bktBook, this.Id)
		_, err1 = bktTab.CreateBucket(bs("notes"))
		bktTab.CreateBucket(bs("previds"))
		err2 = tx.Commit()
		if err = lib.CheckErrs(errNotFatal, action, err1, err2); err != nil {
			tx.Rollback()
			log.Fatal("Fatal DB Error-creating notes bkt, Ending Program")
		}
		resultChan <- Result{DataOk, this.Id}

	case "changeTab":
		var tab *Tab
		if tab = bookTabs[this.Bookid][this.Id]; tab == nil {
			log.Fatal("changeTab, tab not found (bookid, tabid)", this.Bookid, this.Id)
		}
		tab.TabName = this.TabName
		tab.TabNumber = this.TabNumber
		tab.Hidden = this.Hidden
		tab.LastAccessTime = time.Now()
		tab.ETag = fmt.Sprint(time.Now().UnixNano())
		dbWrite("saveTabChange", this, resultChan) // save Tab to db

	case "saveTabChange":
		db.Update(func(tx *bolt.Tx) error {
			bktBook := openBook(tx, this.Bookid)
			bktTabs := openTabs(bktBook)
			k := bs(this.Id)
			v, err1 := json.Marshal(this)
			err2 := bktTabs.Put(k, v)
			return lib.CheckErrs(errFatal, action, err1, err2)
		})
		resultChan <- Result{DataOk, nil}

	default:
		log.Fatal("data Tab.process() invalid action ", action)
	}
}