Beispiel #1
0
// Deletes a range of log entries. The range is inclusive.
func (m *MDBStore) DeleteRange(minIdx, maxIdx uint64) error {
	// Start write txn
	return m.env.Update(func(txn *lmdb.Txn) (err error) {
		txn.RawRead = true
		s := lmdbscan.New(txn, m.dbLogs)
		defer s.Close()
		s.Set(uint64ToBytes(minIdx), nil, lmdb.SetKey)
		for s.Scan() {
			if maxIdx < bytesToUint64(s.Key()) {
				break
			}
			if err := s.Cursor().Del(0); err != nil {
				return err
			}
		}
		return s.Err()
	})
}
Beispiel #2
0
func upgrade1To2LMDB(qs *QuadStore) error {
	fmt.Println("Upgrading v1 to v2...")
	err := qs.env.Update(func(tx *lmdb.Txn) (err error) {
		fmt.Println("Upgrading DB", logDB)

		s := lmdbscan.New(tx, qs.logDBI)
		defer s.Close()

		for s.Scan() {
			var delta graph.Delta
			err := json.Unmarshal(s.Val(), &delta)
			if err != nil {
				return err
			}
			newd := deltaToProto(delta)
			data, err := newd.Marshal()
			if err != nil {
				return err
			}
			err = tx.Put(qs.logDBI, s.Key(), data, 0)
			if err != nil {
				return err
			}
		}

		return s.Err()
	})
	if err != nil {
		return err
	}
	err = qs.env.Update(func(tx *lmdb.Txn) (err error) {
		fmt.Println("Upgrading DB", nodeDB)

		s := lmdbscan.New(tx, qs.nodeDBI)
		defer s.Close()

		for s.Scan() {
			var vd proto.NodeData
			err := json.Unmarshal(s.Val(), &vd)
			if err != nil {
				return err
			}
			data, err := vd.Marshal()
			if err != nil {
				return err
			}
			err = tx.Put(qs.nodeDBI, s.Key(), data, 0)
			if err != nil {
				return err
			}
		}

		return s.Err()
	})
	if err != nil {
		return err
	}

	for _, db := range [4]string{string(spoDB), string(ospDB), string(posDB), string(cpsDB)} {
		err = qs.env.Update(func(tx *lmdb.Txn) (err error) {
			fmt.Println("Upgrading DB", db)
			dbi := qs.dbis[db]
			s := lmdbscan.New(tx, dbi)
			defer s.Close()

			for s.Scan() {
				var h proto.HistoryEntry
				err := json.Unmarshal(s.Val(), &h)
				if err != nil {
					return err
				}
				data, err := h.Marshal()
				if err != nil {
					return err
				}
				err = tx.Put(dbi, s.Key(), data, 0)
				if err != nil {
					return err
				}
			}

			return s.Err()
		})
		if err != nil {
			return err
		}

		return nil
	}
	return nil
}