tmqOffset.c 1.6 KB
Newer Older
wmmhello's avatar
wmmhello 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
//
// Created by mingming wanng on 2023/4/7.
//
#include <stdio.h>
#include <stdlib.h>
#include "taoserror.h"
#include "tlog.h"
#include "tmsg.h"

typedef struct {
  int32_t size;
} STqOffsetHead;

int32_t tqOffsetRestoreFromFile(const char* fname) {
  TdFilePtr pFile = taosOpenFile(fname, TD_FILE_READ);
  if (pFile != NULL) {
    STqOffsetHead head = {0};
    int32_t       code;

    while (1) {
      if ((code = taosReadFile(pFile, &head, sizeof(STqOffsetHead))) != sizeof(STqOffsetHead)) {
        if (code == 0) {
          break;
        } else {
          printf("code:%d != 0\n", code);
          return -1;
        }
      }
      int32_t size = htonl(head.size);
      void*   memBuf = taosMemoryCalloc(1, size);
      if (memBuf == NULL) {
        printf("memBuf == NULL\n");
        return -1;
      }
      if ((code = taosReadFile(pFile, memBuf, size)) != size) {
        taosMemoryFree(memBuf);
        printf("code:%d != size:%d\n", code, size);
        return -1;
      }
      STqOffset offset;
      SDecoder  decoder;
      tDecoderInit(&decoder, memBuf, size);
      if (tDecodeSTqOffset(&decoder, &offset) < 0) {
        taosMemoryFree(memBuf);
        tDecoderClear(&decoder);
        printf("tDecodeSTqOffset error\n");
        return -1;
      }

      tDecoderClear(&decoder);
wmmhello's avatar
wmmhello 已提交
51
      printf("subkey:%s, type:%d, uid/version:%"PRId64", ts:%"PRId64"\n",
wmmhello's avatar
wmmhello 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64
             offset.subKey, offset.val.type, offset.val.uid, offset.val.ts);
      taosMemoryFree(memBuf);
    }

    taosCloseFile(&pFile);
  }
  return 0;
}

int main(int argc, char *argv[]) {
  tqOffsetRestoreFromFile("offset-ver0");
  return 0;
}