<commit_before>// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package state

import (
	"fmt"
	"io/ioutil"
	"path/filepath"
	"time"

	"github.com/juju/juju/testcharms"
	"github.com/juju/names"
	jujutxn "github.com/juju/txn"
	txntesting "github.com/juju/txn/testing"
	"github.com/juju/utils/filestorage"
	gc "gopkg.in/check.v1"
	"gopkg.in/juju/charm.v4"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"gopkg.in/mgo.v2/txn"

	"github.com/juju/juju/state/backups"
)

const (
	UnitsC       = unitsC
	ServicesC    = servicesC
	SettingsC    = settingsC
	BackupsMetaC = backupsMetaC
)

var (
	NewMongoConnInfo = newMongoConnInfo

	GetManagedStorage     = (*State).getManagedStorage
	ToolstorageNewStorage = &toolstorageNewStorage
)

var _ filestorage.DocStorage = (*backupsDocStorage)(nil)
var _ filestorage.RawFileStorage = (*backupBlobStorage)(nil)

func newBackupDoc(meta *backups.Metadata) *backupMetaDoc {
	var doc backupMetaDoc
	doc.UpdateFromMetadata(meta)
	return &doc
}

func getBackupDBWrapper(st *State) *backupDBWrapper {
	envUUID := st.EnvironTag().Id()
	db := st.db.Session.DB(backupDB)
	return newBackupDBWrapper(db, BackupsMetaC, envUUID)
}

// NewBackupID creates a new backup ID based on the metadata.
func NewBackupID(meta *backups.Metadata) string {
	doc := newBackupDoc(meta)
	return newBackupID(doc)
}

// GetBackupMetadata pulls the metadata from storage.
func GetBackupMetadata(st *State, id string) (*backups.Metadata, error) {
	db := getBackupDBWrapper(st)
	defer db.Close()
	doc, err := getBackupMetadata(db, id)
	if err != nil {
		return nil, err
	}
	return doc.asMetadata(), nil
}

// AddBackupMetadata pushes the metadata into storage.
func AddBackupMetadata(st *State, meta *backups.Metadata) (string, error) {
	db := getBackupDBWrapper(st)
	defer db.Close()
	doc := newBackupDoc(meta)
	return addBackupMetadata(db, doc)
}

// AddBackupMetadata pushes the metadata into storage, using the given
// backup ID.
func AddBackupMetadataID(st *State, meta *backups.Metadata, id string) error {
	db := getBackupDBWrapper(st)
	defer db.Close()
	doc := newBackupDoc(meta)
	return addBackupMetadataID(db, doc, id)
}

// SetBackupStored stores the time of when the identified backup archive
// file was stored.
func SetBackupStored(st *State, id string, stored time.Time) error {
	db := getBackupDBWrapper(st)
	defer db.Close()
	return setBackupStored(db, id, stored)
}

func SetTestHooks(c *gc.C, st *State, hooks ...jujutxn.TestHook) txntesting.TransactionChecker {
	runner := jujutxn.NewRunner(jujutxn.RunnerParams{Database: st.db})
	st.transactionRunner = runner
	return txntesting.SetTestHooks(c, runner, hooks...)
}

func SetBeforeHooks(c *gc.C, st *State, fs ...func()) txntesting.TransactionChecker {
	runner := jujutxn.NewRunner(jujutxn.RunnerParams{Database: st.db})
	st.transactionRunner = runner
	return txntesting.SetBeforeHooks(c, runner, fs...)
}

func SetAfterHooks(c *gc.C, st *State, fs ...func()) txntesting.TransactionChecker {
	runner := jujutxn.NewRunner(jujutxn.RunnerParams{Database: st.db})<commit_msg>Clarify a few doc comments.<commit_after>// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package state

import (
	"fmt"
	"io/ioutil"
	"path/filepath"
	"time"

	"github.com/juju/juju/testcharms"
	"github.com/juju/names"
	jujutxn "github.com/juju/txn"
	txntesting "github.com/juju/txn/testing"
	"github.com/juju/utils/filestorage"
	gc "gopkg.in/check.v1"
	"gopkg.in/juju/charm.v4"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"gopkg.in/mgo.v2/txn"

	"github.com/juju/juju/state/backups"
)

const (
	UnitsC       = unitsC
	ServicesC    = servicesC
	SettingsC    = settingsC
	BackupsMetaC = backupsMetaC
)

var (
	NewMongoConnInfo = newMongoConnInfo

	GetManagedStorage     = (*State).getManagedStorage
	ToolstorageNewStorage = &toolstorageNewStorage
)

var _ filestorage.DocStorage = (*backupsDocStorage)(nil)
var _ filestorage.RawFileStorage = (*backupBlobStorage)(nil)

func newBackupDoc(meta *backups.Metadata) *backupMetaDoc {
	var doc backupMetaDoc
	doc.UpdateFromMetadata(meta)
	return &doc
}

func getBackupDBWrapper(st *State) *backupDBWrapper {
	envUUID := st.EnvironTag().Id()
	db := st.db.Session.DB(backupDB)
	return newBackupDBWrapper(db, BackupsMetaC, envUUID)
}

// NewBackupID creates a new backup ID based on the metadata.
func NewBackupID(meta *backups.Metadata) string {
	doc := newBackupDoc(meta)
	return newBackupID(doc)
}

// GetBackupMetadata returns the metadata retrieved from storage.
func GetBackupMetadata(st *State, id string) (*backups.Metadata, error) {
	db := getBackupDBWrapper(st)
	defer db.Close()
	doc, err := getBackupMetadata(db, id)
	if err != nil {
		return nil, err
	}
	return doc.asMetadata(), nil
}

// AddBackupMetadata adds the metadata to storage.
func AddBackupMetadata(st *State, meta *backups.Metadata) (string, error) {
	db := getBackupDBWrapper(st)
	defer db.Close()
	doc := newBackupDoc(meta)
	return addBackupMetadata(db, doc)
}

// AddBackupMetadataID adds the metadata to storage, using the given
// backup ID.
func AddBackupMetadataID(st *State, meta *backups.Metadata, id string) error {
	db := getBackupDBWrapper(st)
	defer db.Close()
	doc := newBackupDoc(meta)
	return addBackupMetadataID(db, doc, id)
}

// SetBackupStored stores the time of when the identified backup archive
// file was stored.
func SetBackupStored(st *State, id string, stored time.Time) error {
	db := getBackupDBWrapper(st)
	defer db.Close()
	return setBackupStored(db, id, stored)
}

func SetTestHooks(c *gc.C, st *State, hooks ...jujutxn.TestHook) txntesting.TransactionChecker {
	runner := jujutxn.NewRunner(jujutxn.RunnerParams{Database: st.db})
	st.transactionRunner = runner
	return txntesting.SetTestHooks(c, runner, hooks...)
}

func SetBeforeHooks(c *gc.C, st *State, fs ...func()) txntesting.TransactionChecker {
	runner := jujutxn.NewRunner(jujutxn.RunnerParams{Database: st.db})
	st.transactionRunner = runner
	return txntesting.SetBeforeHooks(c, runner, fs...)
}

func SetAfterHooks(c *gc.C, st *State, fs ...func()) txntesting.TransactionChecker {
	runner := jujutxn.NewRunner(jujutxn.RunnerParams{Database: st.db})
{'fraction_non_alphanumeric': 0.12213617295901903, 'fraction_numerical': 0.00451758631816715, 'mean_word_length': 4.842601319509896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1961276', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2278, 'n_words': 510}