Ejemplo n.º 1
0
TEST(externalSort, testCorrectSorting) {
    int input, output, error;
    if((input = open(inputfile.c_str(), O_RDONLY)) < 0) {
        std::cerr << "cannot open input file: " << strerror(errno) << std::endl;
        return;
    }
    uint64_t input_filesize = fsize(inputfile);
    if((output = open(outputfile.c_str(), O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
        std::cerr << "cannot open output file: " << strerror(errno) << std::endl;
        return;
    } else if((error = posix_fallocate(output, 0, input_filesize))) {
	    std::cout << "error: " << strerror(error) << std::endl;
	    exit(1);
    }
    externalSort(input, input_filesize / sizeof(uint64_t) , output, memoryBuffer);
    
    
    close(input);
    close(output);
	
    std::cout << "starting to check output" << std::endl;
    if((output = open(outputfile.c_str(), O_RDONLY)) < 0) {
        std::cerr << "cannot open output file for checking: " << strerror(errno) << std::endl;
        return;
    }
    uint64_t number = 0, lastnumber = 0;

    while(read(output, &number, sizeof(uint64_t)) > 0) {
        EXPECT_GE(number, lastnumber);
        lastnumber = number;
    }
    close(output);
}
Ejemplo n.º 2
0
// Driver program to test above functions
int main()
{
    int N = 3; // Number of machines
 
    // an array of pointers storing the head nodes of the linked lists
    ListNode *array[N];
 
    // Create a Linked List 30->40->50 for first machine
    array[0] = NULL;
    push (&array[0], 50);
    push (&array[0], 40);
    push (&array[0], 30);
 
    // Create a Linked List 35->45 for second machine
    array[1] = NULL;
    push (&array[1], 45);
    push (&array[1], 35);
 
    // Create Linked List 10->60->70->80 for third machine
    array[2] = NULL;
    push (&array[2], 100);
    push (&array[2], 80);
    push (&array[2], 70);
    push (&array[2], 60);
    push (&array[2], 10);
 
    // Sort all elements
    externalSort( array, N );
 
    return 0;
}
Ejemplo n.º 3
0
/* Compress the custkey in orders. */
bool pageManage::compressCustkey() {
  /* External sort. */
  if (!externalSort())
    return false;

  /* Open the relevant files. */
  FILE* cfileAfterSort = fopen(PATH_OF_CUSTKEY_AFTER_SORT, "rb"),
  *cfileAfterCompress = fopen(PATH_OF_CUSTKEY_AFTER_COMPRESS, "wb");
  if (!cfileAfterSort || !cfileAfterCompress)
    return false;
  int ckeyAfterSort[MAX_ITEM], count = 0, length = 1, pre_ckey = -1;
  item ckeyAfterCompress[MAX_ITEM];

  /* Compress custkey sort. */
  while(!feof(cfileAfterSort)) {
    int num = fread(ckeyAfterSort, sizeof(int), MAX_ITEM, cfileAfterSort);
    for (int i = 0; i < num; ++i) {
      if (ckeyAfterSort[i] == pre_ckey) {
        ++length;
      } else if (pre_ckey != -1) {
        if (count == MAX_ITEM) {
          fwrite(ckeyAfterCompress, sizeof(item), count, cfileAfterCompress);
          count = 0;
        }
        ckeyAfterCompress[count++] = item(pre_ckey, length);
        length = 1;
      }
      pre_ckey = ckeyAfterSort[i];
    }
  }
  if (count != 0)
    fwrite(ckeyAfterCompress, sizeof(item), count, cfileAfterCompress);
  item last_item(pre_ckey, length);
  fwrite(&last_item, sizeof(item), 1, cfileAfterCompress);

  /* Close the relevant files and remove the temporary files. */
  fclose(cfileAfterSort);
  fclose(cfileAfterCompress);
  remove(PATH_OF_CUSTKEY_AFTER_SORT);
  return true;
}
Ejemplo n.º 4
0
int main(int argc, char* argv[]) {
	if (argc < 4) {
		std::cerr << "usage: " << argv[0] << " <inputFile> <outputFile> <memoryBufferInMB>" << std::endl;
		return -1;
	}
	unsigned bufferSize = atoi(argv[3])*1024;
	if (bufferSize < 1024) {
		std::cerr << "invalid memory buffer size (min. 1MB): " << argv[3] << std::endl;
		return -1;
	}
	int fi, fo;
	if ((fi = open(argv[1], O_RDONLY)) < 0) {
		std::cerr << "cannot open input file '" << argv[1] << "': " << strerror(errno) << std::endl;
		return -1;
	}
	struct stat st;
	if(fstat(fi, &st) < 0){
		std::cerr << "fstat for file '" << argv[1] << "' failed: " << strerror(errno) << std::endl;
		return -1;
	}
	long long fileSize = st.st_size;
	if(fileSize%sizeof(uint64_t) != 0){
		std::cerr << "file size has to be a multiple of 64 Bit" << std::endl;
		return -1;
	}
	if ((fo = open(argv[2], O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
		std::cerr << "cannot open output file '" << argv[2] << "': " << strerror(errno) << std::endl;
		return -1;
	}

	externalSort(fi, fileSize/sizeof(uint64_t), fo, bufferSize);

	close(fi);
	close(fo);
	return 0;
}