Beispiel #1
0
/*
 * openFile -- ファイルのオープン
 *
 * 引数:
 *	*fileame
 *
 * 返り値:
 *	オープンしたファイルのFile構造体
 *	オープンに失敗した場合にはNULLを返す
 */
File *openFile(char *filename)
{
  File *file;
  file = malloc(sizeof(File));
  if ( file == NULL){
    return NULL;
  }
  initializeFileModule();
  if( (file -> desc = open(filename , O_RDWR)) == -1 ){
    return NULL;
  }
  strcpy(file -> name , filename);
	

  return file;
}
Beispiel #2
0
/*
 * main -- エントリポイント
 */
int main(int argc, char **argv)
{
    int i, j;

    /* 乱数の初期化 */
    initializeRandomGenerator();

    /*
     * ファイルアクセスモジュールの初期化
     */
    if (initializeFileModule() != OK) {
	fprintf(stderr, "%s: initialization failed.\n", TEST_NAME);
    }

    /*
     * 前回このプログラムを実行したときのテストファイルが
     * 残っている可能性があるので、まず消去する
     */
    deleteFile(TEST_FILE1);
    deleteFile(TEST_FILE2);

    /* FILE_SIZE分のページの内容を乱数で作成 */
    for (i = 0; i < FILE_SIZE; i++) {
	for (j = 0; j < PAGE_SIZE; j++) {
	    /* 0〜9の範囲の乱数を発生しパターンを作る */
	    int x;
	    x = getRandomInteger(0, 9);

	    /* ページに'0'〜'9'の文字を入れる */
	    pagePattern[i][j] = '0' + x;
	}
    }

    /* テストの実行 */
    fprintf(stderr, "%s: test 1: Start\n", TEST_NAME);
    if (test1() == OK) {
	fprintf(stderr, "%s: test 1: OK\n\n", TEST_NAME);
    } else {
	fprintf(stderr, "%s: test 1: NG\n\n", TEST_NAME);
    }

    fprintf(stderr, "%s: test 2: Start\n", TEST_NAME);
    if (test2() == OK) {
	fprintf(stderr, "%s: test 2: OK\n\n", TEST_NAME);
    } else {
	fprintf(stderr, "%s: test 2: NG\n\n", TEST_NAME);
    }

    fprintf(stderr, "%s: test 3: Start\n", TEST_NAME);
    if (test3() == OK) {
	fprintf(stderr, "%s: test 3: OK\n\n", TEST_NAME);
    } else {
	fprintf(stderr, "%s: test 3: NG\n\n", TEST_NAME);
    }

    fprintf(stderr, "%s: test 4: Start\n", TEST_NAME);
    if (test4() == OK) {
	fprintf(stderr, "%s: test 4: OK\n\n", TEST_NAME);
    } else {
	fprintf(stderr, "%s: test 4: NG\n\n", TEST_NAME);
    }

    /*
     * ファイルアクセスモジュールの終了処理
     */
    if (finalizeFileModule() != OK) {
	fprintf(stderr, "%s: finalization failed.\n", TEST_NAME);
    }

    exit(0);
}
Beispiel #3
0
/*
 * main -- データ操作モジュールのテスト
 */
int main(int argc, char **argv)
{
    char tableName[20];
    TableInfo tableInfo;
    int i;

    /* ファイルモジュールを初期化する */
    if (initializeFileModule() != OK) {
        fprintf(stderr, "Cannot initialize file module.\n");
        exit(1);
    }

    /* データ定義ジュールを初期化する */
    if (initializeDataDefModule() != OK) {
        fprintf(stderr, "Cannot initialize data definition module.\n");
        exit(1);
    }

    /* データ操作ジュールを初期化する */
    if (initializeDataManipModule() != OK) {
        fprintf(stderr, "Cannot initialize data manipulation module.\n");
        exit(1);
    }

    /*
     * このプログラムの前回の実行の時のデータ定義残っている可能性があるので、
     * とりあえず削除する
     */
    dropTable(TABLE_NAME);

    /*
     * 以下のテーブルを作成
     * create table student (
     *   id string,
     *   name string,
     *   age int,
     *   address string
     * )
     */
    strcpy(tableName, TABLE_NAME);
    i = 0;

    strcpy(tableInfo.fieldInfo[i].name, "id");
    tableInfo.fieldInfo[i].dataType = TYPE_VARCHAR;
    i++;

    strcpy(tableInfo.fieldInfo[i].name, "name");
    tableInfo.fieldInfo[i].dataType = TYPE_VARCHAR;
    i++;

    strcpy(tableInfo.fieldInfo[i].name, "age");
    tableInfo.fieldInfo[i].dataType = TYPE_INT;
    i++;

    strcpy(tableInfo.fieldInfo[i].name, "weight");
    tableInfo.fieldInfo[i].dataType = TYPE_DOUBLE;
    i++;

    strcpy(tableInfo.fieldInfo[i].name, "address");
    tableInfo.fieldInfo[i].dataType = TYPE_VARCHAR;
    i++;

    tableInfo.numField = i;

    /* テーブルの作成 */
    if (createTable(tableName, &tableInfo) != OK) {
        /* テーブルの作成に失敗 */
        fprintf(stderr, "Cannot create table.\n");
        exit(1);
    }

    /* 挿入テスト */
    fprintf(stderr, "test1: Start\n\n");
    if (test1() == OK) {
        fprintf(stderr, "test1: OK\n\n");
    } else {
        fprintf(stderr, "test1: NG\n\n");
    }

    /* 検索テスト */
    fprintf(stderr, "test2: Start\n\n");
    if (test2() == OK) {
        fprintf(stderr, "test2: OK\n\n");
    } else {
        fprintf(stderr, "test2: NG\n\n");
    }

    /* 削除テスト */
    fprintf(stderr, "test3: Start\n\n");
    if (test3() == OK) {
        fprintf(stderr, "test3: OK\n\n");
    } else {
        fprintf(stderr, "test3: NG\n\n");
    }

    /* 後始末 */
    dropTable(TABLE_NAME);
    finalizeDataManipModule();
    finalizeDataDefModule();
    finalizeFileModule();
}
Beispiel #4
0
/*
* main -- マイクロDBシステムのエントリポイント
*/
int main()
{
	char input[MAX_INPUT];
	char *token;

	/* ファイルモジュールの初期化 */
	if (initializeFileModule() != OK) {
		fprintf(stderr, "Cannot initialize file module.\n");
		exit(1);
	}

	/* データ定義ジュールの初期化 */
	if (initializeDataDefModule() != OK) {
		fprintf(stderr, "Cannot initialize data definition module.\n");
		exit(1);
	}

	/* データ操作ジュールの初期化 */
	if (initializeDataManipModule() != OK) {
		fprintf(stderr, "Cannot initialize data manipulation module.\n");
		exit(1);
	}

	/* ウェルカムメッセージを出力 */
	printf("マイクロDBMSを起動しました。\n");

	/* 1行ずつ入力を読み込みながら、処理を行う */
	for (;;) {
		/* プロンプトの出力 */
		printf("\nDDLまたはDMLを入力してください。\n");
		printf("> ");

		/* キーボード入力を1行読み込む */
		fgets(input, MAX_INPUT, stdin);

		/* 入力の最後の改行を取り除く */
		if (strchr(input, '\n') != NULL) {
			*(strchr(input, '\n')) = '\0';
		}

		/* 字句解析するために入力文字列を設定する */
		setInputString(input);

		/* 最初のトークンを取り出す */
		token = getNextToken();

		/* 入力が空行だったら、ループの先頭に戻ってやり直し */
		if (token == NULL) {
			continue;
		}

		/* 入力が"quit"だったら、ループを抜けてプログラムを終了させる */
		if (strcmp(token, "quit") == 0) {
			printf("マイクロDBMSを終了します。\n\n");
			break;
		}

		/* 最初のトークンが何かによって、呼び出す関数を決める */
		if (strcmp(token, "create") == 0) {
			callCreateTable();
		}
		else if (strcmp(token, "drop") == 0) {
			callDropTable();
		}
		else if (strcmp(token, "insert") == 0) {
			callInsertRecord();
		}
		else if (strcmp(token, "select") == 0) {
			callSelectRecord();
		}
		else if (strcmp(token, "delete") == 0) {
			callDeleteRecord();
		}
		else if (strcmp(token, "show") == 0){
			showTables();
		}
		else if (strcmp(token, "test1") == 0){
			test1();
		}
		else {
			/* 入力に間違いがあった */
			printf("入力に間違いがあります。:%s\n", token);
			printf("もう一度入力し直してください。\n\n");
		}
	}

	/* 各モジュールの終了処理 */
	finalizeDataManipModule();
	finalizeDataDefModule();
	finalizeFileModule();
}
Beispiel #5
0
/*
 * main -- バッファ管理モジュールのテスト
 */
int main(int argc, char **argv)
{
    File *file[2];
    char page1[PAGE_SIZE], page2[PAGE_SIZE];
    int i;
    
    /* 乱数用関数の初期化 */
    initializeRandomGenerator();
    
    /*
     * ファイルアクセスモジュールの初期化
     */
    if (initializeFileModule() != OK) {
        fprintf(stderr, "%s: initialization failed.\n", TEST_NAME);
    }
    
    /*
     * 前回このプログラムを実行したときのテストファイルが
     * 残っている可能性があるので、まず消去する
     */
    deleteFile(TEST_FILE1);
    deleteFile(TEST_FILE2);
    
    /* 空のファイルを2つ作り、オープンする */
    if (createFile(TEST_FILE1) != OK) {
        fprintf(stderr, "Cannot create file.\n");
        exit(1);
    }
    
    if (createFile(TEST_FILE2) != OK) {
        fprintf(stderr, "Cannot create file.\n");
        exit(1);
    }
    
    if ((file[0] = openFile(TEST_FILE1)) == NULL) {
        fprintf(stderr, "Cannot open file.\n");
        exit(1);
    }
    
    if ((file[1] = openFile(TEST_FILE2)) == NULL) {
        fprintf(stderr, "Cannot open file.\n");
        exit(1);
    }
    
    /*
     * ファイルの先頭から順に、(FILE_SIZE)ページ分のダミーデータを書く
     *
     * それぞれのページの先頭3バイトに、以下のように文字を書く
     * file1:
     *   0ページ目: 000
     *   1ページ目: 001
     *   2ページ目: 002
     *   (以下同様)
     * file2:
     *   0ページ目: 100
     *   1ページ目: 101
     *   2ページ目: 102
     *   (以下同様)
     */
    for (i = 0; i < FILE_SIZE; i++) {
        /* ページに書く内容を用意する */
        memset(page1, 0, PAGE_SIZE);
        memset(page2, 0, PAGE_SIZE);
        sprintf(page1, "00%c", '0' + i);
        sprintf(page2, "10%c", '0' + i);
        
        /* ファイルに書き込む */
        if (writePage(file[0], i, page1) != OK) {
            fprintf(stderr, "Cannot write page.\n");
            exit(1);
        }
        
        if (writePage(file[1], i, page2) != OK) {
            fprintf(stderr, "Cannot write page.\n");
            exit(1);
        }
    }
    
    /* いったんファイルをクローズする */
    closeFile(file[0]);
    closeFile(file[1]);
    
    /*
     * ここまでが下準備。ここからバッファリング機能をテストする
     */
    test1();
    test2();
    
    /*
     * ファイルアクセスモジュールの終了処理
     */
    if (finalizeFileModule() != OK) {
        fprintf(stderr, "%s: finalization failed.\n", TEST_NAME);
    }
    
    /* ファイルを削除する */
    deleteFile(TEST_FILE1);
    deleteFile(TEST_FILE2);
}