<commit_before>// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package client

import (
	"fmt"
	"time"

	"v.io/v23"
	"v.io/v23/context"
	"v.io/v23/security"
	"v.io/v23/security/access"
	wire "v.io/v23/services/syncbase"
	"v.io/v23/syncbase"
	"v.io/v23/verror"
	"v.io/x/ref/services/syncbase/longevity_tests/model"
	"v.io/x/ref/services/syncbase/testutil"
)

// CreateDbsAndCollections creates databases and collections according to the
// given models.  It does not fail if any of the databases or collections
// already exist.  If the model contains syncgroups, it will also create or
// join those as well.
func CreateDbsAndCollections(ctx *context.T, sbName string, dbModels model.DatabaseSet) (map[syncbase.Database][]syncbase.Collection, []syncbase.Syncgroup, error) {
	nsRoots := v23.GetNamespace(ctx).Roots()

	service := syncbase.NewService(sbName)
	syncgroups := []syncbase.Syncgroup{}
	dbColsMap := map[syncbase.Database][]syncbase.Collection{}
	for _, dbModel := range dbModels {
		dbPerms := defaultPerms(ctx, wire.AllDatabaseTags)
		if dbModel.Permissions != nil {
			dbPerms = dbModel.Permissions.ToWire("root")
		}
		allowChecker(dbPerms, wire.AllDatabaseTags)

		// Create Database.
		// TODO(nlacasse): Don't create the database unless its blessings match
		// ours.
		db := service.DatabaseForId(dbModel.Id(), nil)
		if err := db.Create(ctx, dbPerms); err != nil && verror.ErrorID(err) != verror.ErrExist.ID {
			return nil, nil, err
		}
		dbColsMap[db] = []syncbase.Collection{}

		// Create collections for database.
		for _, colModel := range dbModel.Collections {
			colPerms := defaultPerms(ctx, wire.AllCollectionTags)
			if colModel.Permissions != nil {
				colPerms = colModel.Permissions.ToWire("root")
			}
			allowChecker(colPerms, wire.AllCollectionTags)

			// TODO(nlacasse): Don't create the collection unless its blessings
			// match ours.
			col := db.CollectionForId(colModel.Id())
			if err := col.Create(ctx, colPerms); err != nil && verror.ErrorID(err) != verror.ErrExist.ID {
				return nil, nil, err
			}
			dbColsMap[db] = append(dbColsMap[db], col)
		}

		// Create or join syncgroups for database.
		for _, sgModel := range dbModel.Syncgroups {
			sg := db.SyncgroupForId(sgModel.Id())
			if sgModel.CreatorDevices.Lookup(sbName) != nil {
				// Create the syncgroup.
				spec := sgModel.Spec("root")
				spec.MountTables = nsRoots

				if len(spec.Perms) == 0 {
					// Syncgroup permissions default to allow anybody.
					spec.Perms = testutil.DefaultPerms(wire.AllSyncgroupTags, "root")
				}
				allowChecker(spec.Perms, wire.AllSyncgroupTags)

				if err := sg.Create(ctx, spec, wire.SyncgroupMemberInfo{}); err != nil && verror.ErrorID(err) != verror.ErrExist.ID {
					return nil, nil, err
				}
				syncgroups = append(syncgroups, sg)
				continue
			}

			if sgModel.JoinerDevices.Lookup(sbName) != nil {
				// Join the syncgroup.  It might not exist at first, so we loop.
				// TODO(nlacasse): Parameterize number of retries.  Exponential
				// backoff?
				var joinErr error
				for i := 0; i < 10; i++ {<commit_msg>TBR: longevity tests: retry syncgroup join forever.<commit_after>// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package client

import (
	"fmt"
	"time"

	"v.io/v23"
	"v.io/v23/context"
	"v.io/v23/security"
	"v.io/v23/security/access"
	wire "v.io/v23/services/syncbase"
	"v.io/v23/syncbase"
	"v.io/v23/verror"
	"v.io/x/ref/services/syncbase/longevity_tests/model"
	"v.io/x/ref/services/syncbase/testutil"
)

// CreateDbsAndCollections creates databases and collections according to the
// given models.  It does not fail if any of the databases or collections
// already exist.  If the model contains syncgroups, it will also create or
// join those as well.
func CreateDbsAndCollections(ctx *context.T, sbName string, dbModels model.DatabaseSet) (map[syncbase.Database][]syncbase.Collection, []syncbase.Syncgroup, error) {
	nsRoots := v23.GetNamespace(ctx).Roots()

	service := syncbase.NewService(sbName)
	syncgroups := []syncbase.Syncgroup{}
	dbColsMap := map[syncbase.Database][]syncbase.Collection{}
	for _, dbModel := range dbModels {
		dbPerms := defaultPerms(ctx, wire.AllDatabaseTags)
		if dbModel.Permissions != nil {
			dbPerms = dbModel.Permissions.ToWire("root")
		}
		allowChecker(dbPerms, wire.AllDatabaseTags)

		// Create Database.
		// TODO(nlacasse): Don't create the database unless its blessings match
		// ours.
		db := service.DatabaseForId(dbModel.Id(), nil)
		if err := db.Create(ctx, dbPerms); err != nil && verror.ErrorID(err) != verror.ErrExist.ID {
			return nil, nil, err
		}
		dbColsMap[db] = []syncbase.Collection{}

		// Create collections for database.
		for _, colModel := range dbModel.Collections {
			colPerms := defaultPerms(ctx, wire.AllCollectionTags)
			if colModel.Permissions != nil {
				colPerms = colModel.Permissions.ToWire("root")
			}
			allowChecker(colPerms, wire.AllCollectionTags)

			// TODO(nlacasse): Don't create the collection unless its blessings
			// match ours.
			col := db.CollectionForId(colModel.Id())
			if err := col.Create(ctx, colPerms); err != nil && verror.ErrorID(err) != verror.ErrExist.ID {
				return nil, nil, err
			}
			dbColsMap[db] = append(dbColsMap[db], col)
		}

		// Create or join syncgroups for database.
		for _, sgModel := range dbModel.Syncgroups {
			sg := db.SyncgroupForId(sgModel.Id())
			if sgModel.CreatorDevices.Lookup(sbName) != nil {
				// Create the syncgroup.
				spec := sgModel.Spec("root")
				spec.MountTables = nsRoots

				if len(spec.Perms) == 0 {
					// Syncgroup permissions default to allow anybody.
					spec.Perms = testutil.DefaultPerms(wire.AllSyncgroupTags, "root")
				}
				allowChecker(spec.Perms, wire.AllSyncgroupTags)

				if err := sg.Create(ctx, spec, wire.SyncgroupMemberInfo{}); err != nil && verror.ErrorID(err) != verror.ErrExist.ID {
					return nil, nil, err
				}
				syncgroups = append(syncgroups, sg)
				continue
			}

			if sgModel.JoinerDevices.Lookup(sbName) != nil {
				// Join the syncgroup.  It might not exist at first, so we loop.
				// TODO(nlacasse): Parameterize number of retries.  Exponential
				// backoff?
				var joinErr error
				for {
{'fraction_non_alphanumeric': 0.12710805746408493, 'fraction_numerical': 0.0070268582136164895, 'mean_word_length': 3.624548736462094, 'pattern_counts': {'":': 0, '<': 4, '<?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': '668579', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2128, 'n_words': 541}