[Groonga-commit] groonga/groonga at 986c0ee [master] grn_obj_get_disk_usage(): add a new API

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Aug 15 22:00:37 JST 2017


Kouhei Sutou	2017-08-15 22:00:37 +0900 (Tue, 15 Aug 2017)

  New Revision: 986c0eeede4243dc51249e9fc2a1c4f2f27d0222
  https://github.com/groonga/groonga/commit/986c0eeede4243dc51249e9fc2a1c4f2f27d0222

  Message:
    grn_obj_get_disk_usage(): add a new API
    
    TODO:
    
      * Document

  Modified files:
    include/groonga/obj.h
    lib/dat.cpp
    lib/grn_dat.h
    lib/grn_ii.h
    lib/ii.c
    lib/obj.c

  Modified: include/groonga/obj.h (+1 -0)
===================================================================
--- include/groonga/obj.h    2017-08-15 21:59:55 +0900 (cfcb19113)
+++ include/groonga/obj.h    2017-08-15 22:00:37 +0900 (7bc554950)
@@ -74,6 +74,7 @@ GRN_API grn_bool grn_obj_name_is_column(grn_ctx *ctx,
                                         int name_len);
 
 GRN_API grn_bool grn_obj_is_corrupt(grn_ctx *ctx, grn_obj *obj);
+GRN_API size_t grn_obj_get_disk_usage(grn_ctx *ctx, grn_obj *obj);
 
 #ifdef __cplusplus
 }

  Modified: lib/dat.cpp (+30 -0)
===================================================================
--- lib/dat.cpp    2017-08-15 21:59:55 +0900 (baca2b133)
+++ lib/dat.cpp    2017-08-15 22:00:37 +0900 (dee0afc26)
@@ -1299,4 +1299,34 @@ grn_dat_is_corrupt(grn_ctx *ctx, grn_dat *dat)
   return GRN_FALSE;
 }
 
+size_t
+grn_dat_get_disk_usage(grn_ctx *ctx, grn_dat *dat)
+{
+  if (!dat->io) {
+    return 0;
+  }
+
+  {
+    CriticalSection critical_section(&dat->lock);
+    size_t usage;
+
+    usage = grn_io_get_disk_usage(ctx, dat->io);
+
+    if (dat->header->file_id == 0) {
+      return usage;
+    }
+
+    char trie_path[PATH_MAX];
+    grn_dat_generate_trie_path(grn_io_path(dat->io),
+                               trie_path,
+                               dat->header->file_id);
+    struct stat stat;
+    if (::stat(trie_path, &stat) == 0) {
+      usage += stat.st_size;
+    }
+
+    return usage;
+  }
+}
+
 }  // extern "C"

  Modified: lib/grn_dat.h (+3 -1)
===================================================================
--- lib/grn_dat.h    2017-08-15 21:59:55 +0900 (a24c310fe)
+++ lib/grn_dat.h    2017-08-15 22:00:37 +0900 (774c02694)
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 2 -*- */
 /*
-  Copyright(C) 2011-2016 Brazil
+  Copyright(C) 2011-2017 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -88,6 +88,8 @@ grn_rc grn_dat_clear_dirty(grn_ctx *ctx, grn_dat *dat);
 
 grn_bool grn_dat_is_corrupt(grn_ctx *ctx, grn_dat *dat);
 
+size_t grn_dat_get_disk_usage(grn_ctx *ctx, grn_dat *dat);
+
 #ifdef __cplusplus
 }
 #endif

  Modified: lib/grn_ii.h (+1 -0)
===================================================================
--- lib/grn_ii.h    2017-08-15 21:59:55 +0900 (2d7f9ffe7)
+++ lib/grn_ii.h    2017-08-15 22:00:37 +0900 (21f57e2f1)
@@ -122,6 +122,7 @@ int grn_ii_updspec_cmp(grn_ii_updspec *a, grn_ii_updspec *b);
 
 void grn_ii_expire(grn_ctx *ctx, grn_ii *ii);
 grn_rc grn_ii_flush(grn_ctx *ctx, grn_ii *ii);
+size_t grn_ii_get_disk_usage(grn_ctx *ctx, grn_ii *ii);
 
 grn_ii_cursor *grn_ii_cursor_openv1(grn_ii *ii, uint32_t key);
 grn_rc grn_ii_cursor_openv2(grn_ii_cursor **cursors, int ncursors);

  Modified: lib/ii.c (+11 -0)
===================================================================
--- lib/ii.c    2017-08-15 21:59:55 +0900 (3ddf53d66)
+++ lib/ii.c    2017-08-15 22:00:37 +0900 (519466841)
@@ -4516,6 +4516,17 @@ grn_ii_flush(grn_ctx *ctx, grn_ii *ii)
   return rc;
 }
 
+size_t
+grn_ii_get_disk_usage(grn_ctx *ctx, grn_ii *ii)
+{
+  size_t usage;
+
+  usage = grn_io_get_disk_usage(ctx, ii->seg);
+  usage += grn_io_get_disk_usage(ctx, ii->chunk);
+
+  return usage;
+}
+
 #define BIT11_01(x) ((x >> 1) & 0x7ff)
 #define BIT31_12(x) (x >> 12)
 

  Modified: lib/obj.c (+43 -0)
===================================================================
--- lib/obj.c    2017-08-15 21:59:55 +0900 (5a168c58e)
+++ lib/obj.c    2017-08-15 22:00:37 +0900 (a1b2acb23)
@@ -644,3 +644,46 @@ grn_obj_get_io(grn_ctx *ctx, grn_obj *obj)
 
   return io;
 }
+
+size_t
+grn_obj_get_disk_usage(grn_ctx *ctx, grn_obj *obj)
+{
+  size_t usage;
+
+  GRN_API_ENTER;
+
+  if (!obj) {
+    ERR(GRN_INVALID_ARGUMENT, "[object][disk-usage] object must not be NULL");
+    GRN_API_RETURN(0);
+  }
+
+  switch (obj->header.type) {
+  case GRN_DB :
+    {
+      grn_db *db = (grn_db *)obj;
+      usage = grn_obj_get_disk_usage(ctx, db->keys);
+      if (db->specs) {
+        usage += grn_obj_get_disk_usage(ctx, (grn_obj *)(db->specs));
+      }
+      usage += grn_obj_get_disk_usage(ctx, (grn_obj *)(db->config));
+    }
+    break;
+  case GRN_TABLE_DAT_KEY :
+    usage = grn_dat_get_disk_usage(ctx, (grn_dat *)obj);
+    break;
+  case GRN_COLUMN_INDEX :
+    usage = grn_ii_get_disk_usage(ctx, (grn_ii *)obj);
+    break;
+  default :
+    {
+      grn_io *io;
+      io = grn_obj_get_io(ctx, obj);
+      if (io) {
+        usage = grn_io_get_disk_usage(ctx, io);
+      }
+    }
+    break;
+  }
+
+  GRN_API_RETURN(usage);
+}
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index