Пример #1
0
/**
 *  @brief: Compute all the permutations of the given string that has all
 *      unique characters.
 *  @param: [in] str: The given string whose permutations will be computed.
 *  @return: StringVect
 *      A list of all the permutations of the given string.
 */
StringVect permutation_str_no_dup(const std::string & str) {
    const int STR_SIZE = static_cast<int>(str.size());

    // Handle special cases.
    if (STR_SIZE == 0) {
        // If str is empty, return an empty set.
        return StringVect();
    } else if (STR_SIZE == 1) {
        // One-char string has only one permutation which is itself.
        return StringVect(1, str);
    }

    // Now the STR_SIZE > 1.

    // Compute the permutations.
    char ch = str[0];
    StringVect sub_perms = permutation_str_no_dup(str.substr(1));

    // Insert ch to every possible position of perms.
    StringVect perms;
    for (size_t i = 0; i < sub_perms.size(); ++i) {
        const std::string & subperm = sub_perms[i];
        for (size_t j = 0; j < subperm.size(); ++j) {
            std::string replica(subperm);
            replica.insert(replica.begin() + j, ch);
            perms.push_back(replica);
        }
        // Finally, we need to insert ch to the end of replica.
        std::string replica(subperm);
        replica.push_back(ch);
        perms.push_back(replica);
    }

    return perms;
}
Пример #2
0
sql_rel *
rel_distribute(mvc *sql, sql_rel *rel) 
{
	rel = distribute(sql, rel);
	rel = replica(sql, rel, NULL);
	return rel_remote_func(sql, rel);
}
    void ReplicaManagerWrapper::GenerateActiveReplicas(
        std::wstring const & replicasDescription,
        __out size_t & replicaCount,
        __out std::vector<ReplicaInformation> & replicaInfos)
    {
        vector<wstring> replicas;
        StringUtility::Split<wstring>(replicasDescription, replicas, L";");
        replicaCount = replicas.size();
        for(size_t i = 0; i < replicaCount; ++i)
        {
            vector<wstring> replicaDetails;
            AddIncarnationIdIfNecessary(replicas[i]);
            StringUtility::Split<wstring>(replicas[i], replicaDetails, L":");

            ReplicationEndpointId endpointUniqueId = ReplicationEndpointId::FromString(replicaDetails[0]);
            int64 lsn = Int64_Parse(replicaDetails[1]);
            wstring endpoint;
            transport_->GeneratePublishEndpoint(endpointUniqueId, endpoint);
            ReplicaInformation replica(
                endpointUniqueId.ReplicaId,
                ::FABRIC_REPLICA_ROLE_ACTIVE_SECONDARY,
                endpoint,
                false,
                lsn,
                lsn);

            replicaInfos.push_back(std::move(replica));
        }
    }
Пример #4
0
/*use to make a database
 * program [absolute name of logical file] [list of replicas]
 */
int main(int argc,char **argv){
   saga::url plf(argv[1]);
   plf.set_host("localhost");
   plf.set_scheme("any");
   saga::logical_file::logical_file lf(plf, saga::logical_file::Create | saga::logical_file::CreateParents | saga::logical_file::ReadWrite);
   for(int x=2;x<argc;x++){
      saga::url replica(argv[x]);
      replica.set_host("localhost");
      replica.set_scheme("any");
      lf.add_location(replica);
   } 
   return 0;
}
    void ReplicaManagerWrapper::AddIdles(wstring const & idleReplicas)
    {
        Trace.WriteInfo(TestReplicaManagerSource, "** AddIdles {0}", idleReplicas);

        ReplicaInformationVector idles;
        vector<wstring> replicas;
        StringUtility::Split<wstring>(idleReplicas, replicas, L";");
        size_t replicaCount = replicas.size();
        
        FABRIC_SEQUENCE_NUMBER replLSN;
        for(size_t i = 0; i < replicaCount; ++i)
        {
            vector<wstring> replicaDetails;
            AddIncarnationIdIfNecessary(replicas[i]);
            StringUtility::Split<wstring>(replicas[i], replicaDetails, L":");

            ReplicationEndpointId endpointUniqueId = ReplicationEndpointId::FromString(replicaDetails[0]);
            wstring endpoint;

            transport_->GeneratePublishEndpoint(endpointUniqueId, endpoint);

            ReplicaInformation replica(
                endpointUniqueId.ReplicaId,
                ::FABRIC_REPLICA_ROLE_IDLE_SECONDARY,
                endpoint,
                false,
                -1,
                -1);
            
            ReplicationSessionSPtr session;
            ErrorCode error = replicaManagerSPtr_->TryAddIdleReplica(replica, session, replLSN);
            VERIFY_IS_TRUE_FMT(error.IsSuccess(), "Added idle \"{0}\"", endpointUniqueId);

            int64 lsn = Int64_Parse(replicaDetails[1]);
            session->UpdateAckProgress(lsn, lsn, L"empty", nullptr);
            for (int count = 1; count < 100; count++)
            {
                if (session->Test_IsReplicationAckProcessingInProgress())
                {
                    Sleep(100);
                }
                else
                {
                    break;
                }
            }
        }
    }
Пример #6
0
static sql_rel *
distribute(mvc *sql, sql_rel *rel) 
{
	sql_rel *l = NULL, *r = NULL;
	prop *p, *pl, *pr;

	if (!rel)
		return rel;

	if (rel_is_ref(rel)) {
		if (has_remote_or_replica(rel)) {
			sql_rel *nrel = rel_copy(sql->sa, rel);

			if (nrel && rel->p)
				nrel->p = prop_copy(sql->sa, rel->p);
			rel_destroy(rel);
			rel = nrel;
		} else {
			return rel;
		}
	}

	switch (rel->op) {
	case op_basetable: {
		sql_table *t = rel->l;

		/* set_remote() */
		if (isRemote(t)) {
			char *uri = t->query;

			p = rel->p = prop_create(sql->sa, PROP_REMOTE, rel->p); 
			p->value = uri;
		}
	}
	case op_table:
		break;
	case op_join: 
	case op_left: 
	case op_right: 
	case op_full: 

	case op_apply: 
	case op_semi: 
	case op_anti: 

	case op_union: 
	case op_inter: 
	case op_except: 
		l = rel->l = distribute(sql, rel->l);
		r = rel->r = distribute(sql, rel->r);

		if (l && (pl = find_prop(l->p, PROP_REMOTE)) != NULL &&
		    	   r && (pr = find_prop(r->p, PROP_REMOTE)) == NULL) {
			r = rel->r = distribute(sql, replica(sql, rel->r, pl->value));
		} else if (l && (pl = find_prop(l->p, PROP_REMOTE)) == NULL &&
		    	   r && (pr = find_prop(r->p, PROP_REMOTE)) != NULL) {
			l = rel->l = distribute(sql, replica(sql, rel->l, pr->value));
		}
		if (l && (pl = find_prop(l->p, PROP_REMOTE)) != NULL &&
		    r && (pr = find_prop(r->p, PROP_REMOTE)) != NULL && 
		    strcmp(pl->value, pr->value) == 0) {
			l->p = prop_remove(l->p, pl);
			r->p = prop_remove(r->p, pr);
			pl->p = rel->p;
			rel->p = pl;
		}
		break;
	case op_project:
	case op_select: 
	case op_groupby: 
	case op_topn: 
	case op_sample: 
		rel->l = distribute(sql, rel->l);
		l = rel->l;
		if (l && (p = find_prop(l->p, PROP_REMOTE)) != NULL) {
			l->p = prop_remove(l->p, p);
			p->p = rel->p;
			rel->p = p;
		}
		break;
	case op_ddl: 
		rel->l = distribute(sql, rel->l);
		if (rel->r)
			rel->r = distribute(sql, rel->r);
		break;
	case op_insert:
	case op_update:
	case op_delete:
		rel->r = distribute(sql, rel->r);
		break;
	}
	return rel;
}
Пример #7
0
static sql_rel *
replica(mvc *sql, sql_rel *rel, char *uri) 
{
	if (!rel)
		return rel;

	if (rel_is_ref(rel)) {
		if (has_remote_or_replica(rel)) {
			sql_rel *nrel = rel_copy(sql->sa, rel);

			if (nrel && rel->p)
				nrel->p = prop_copy(sql->sa, rel->p);
			rel_destroy(rel);
			rel = nrel;
		} else {
			return rel;
		}
	}
	switch (rel->op) {
	case op_basetable: {
		sql_table *t = rel->l;

		if (isReplicaTable(t)) {
			node *n;

			if (uri) {
				/* replace by the replica which matches the uri */
				for (n = t->tables.set->h; n; n = n->next) {
					sql_table *p = n->data;
	
					if (isRemote(p) && strcmp(uri, p->query) == 0) {
						rel = rewrite_replica(sql, rel, t, p);
						break;
					}
				}
			} else { /* no match, use first */
				sql_table *p = NULL;

				if (t->tables.set) {
					p = t->tables.set->h->data;
					rel = rewrite_replica(sql, rel, t, p);
				} else {
					rel = NULL;
				}
			}
		}
	}
	case op_table:
		break;
	case op_join: 
	case op_left: 
	case op_right: 
	case op_full: 

	case op_apply: 
	case op_semi: 
	case op_anti: 

	case op_union: 
	case op_inter: 
	case op_except: 
		rel->l = replica(sql, rel->l, uri);
		rel->r = replica(sql, rel->r, uri);
		break;
	case op_project:
	case op_select: 
	case op_groupby: 
	case op_topn: 
	case op_sample: 
		rel->l = replica(sql, rel->l, uri);
		break;
	case op_ddl: 
		rel->l = replica(sql, rel->l, uri);
		if (rel->r)
			rel->r = replica(sql, rel->r, uri);
		break;
	case op_insert:
	case op_update:
	case op_delete:
		rel->r = replica(sql, rel->r, uri);
		break;
	}
	return rel;
}
Пример #8
0
Try<Nothing> Initialize::execute(int argc, char** argv)
{
  flags.setUsageMessage(
      "Usage: " + name() + " [option]\n"
      "\n"
      "This command is used to initialize the log.\n"
      "\n");

  // Configure the tool by parsing command line arguments.
  if (argc > 0 && argv != NULL) {
    Try<Nothing> load = flags.load(None(), argc, argv);
    if (load.isError()) {
      return Error(flags.usage(load.error()));
    }

    if (flags.help) {
      return Error(flags.usage());
    }

    process::initialize();
    logging::initialize(argv[0], flags);
  }

  if (flags.path.isNone()) {
    return Error(flags.usage("Missing required option --path"));
  }

  // Setup the timeout if specified.
  Option<Timeout> timeout = None();
  if (flags.timeout.isSome()) {
    timeout = Timeout::in(flags.timeout.get());
  }

  Replica replica(flags.path.get());

  // Get the current status of the replica.
  Future<Metadata::Status> status = replica.status();
  if (timeout.isSome()) {
    status.await(timeout.get().remaining());
  } else {
    status.await();
  }

  if (status.isPending()) {
    return Error("Timed out while getting replica status");
  } else if (status.isDiscarded()) {
    return Error("Failed to get status of replica (discarded future)");
  } else if (status.isFailed()) {
    return Error(status.failure());
  }

  // We only initialize a log if it is empty.
  if (status.get() != Metadata::EMPTY) {
    return Error("The log is not empty");
  }

  // Update the status of the replica to VOTING.
  Future<bool> update = replica.update(Metadata::VOTING);
  if (timeout.isSome()) {
    update.await(timeout.get().remaining());
  } else {
    update.await();
  }

  if (update.isPending()) {
    return Error("Timed out while setting replica status");
  } else if (update.isDiscarded()) {
    return Error("Failed to set replica status (discarded future)");
  } else if (update.isFailed()) {
    return Error(update.failure());
  }

  return Nothing();
}