2012-11-23 43 views
1

目前,你可以發現,看法不存在,當您嘗試嘗試檢索視圖後執行的查詢:如何確定存儲桶中是否存在Couchbase視圖? (.NET API)

var result = _couchClient.GetView<etc>("DocumentName", "ViewNameThatDoesNotExist"); 
result.Count(); // This returns a service exception 

你發現它計數後不存在拋出異常,所以目前我使用try/catch來確定是否有異常。

是否有更優雅的解決方案,不需要執行查詢所需的儘可能多的資源? 似乎沒有像

result.exists() 

或類似的東西。

+0

爲了澄清,執行命令: '變種結果= _couchClient.GetView (「DocumentName」,「ViewNameThatDoesNotExist」);' 將不會產生異常,即使視圖不存在,並且結果不會爲空。只有在對結果執行查詢時纔會出現異常。 – Kyle

回答

1

馬特 - 感謝您分享您的擴展方法!看到這個問題現在已經出現好幾次了,我正在向1.2.1客戶端發佈支持(將於2013年2月5日發佈)。接下來的答案在1.2.0中是無效的,但是可供將來參考。我還將其作爲回答,而不是更好的代碼突出顯示的評論...

在1.2.1中,將有兩種方法檢查不存在的視圖。首先是使用視圖的新的CheckExists()方法。該方法將查詢設計文檔並解析JSON。與Matt的方法類似,但不使用CouchbaseCluster API。第二種方法是捕獲異常,這是有意義的類型化(不再是InvalidOperationException)。這種方法的優點是減少一個HTTP查詢。

var view = client.GetView("designDoc", "viewName") 

try { 
    view.Count(); 
} catch (ViewNotFoundException vnfe) { //extends ViewException 
    ... 
} catch (ViewException ve) { //bad params for example 
    log.Error("Error {0}, Reason {1}", ve.Error, ve.Reason); 
} catch (Exception e) { 
    ... 
} 

的吉拉請求是在http://www.couchbase.com/issues/browse/NCBC-165和該代碼是在審查在http://review.couchbase.org/#/c/24068/1

+0

哇謝謝加入! – Kyle

+0

樂意幫忙!這就是開源的美妙之處。 :) –

0

您可以嘗試使用設計REST API,不知道它是更優雅,見 http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-designdoc-api-retrieving.html

這種方法是一個非常通用的做法。

我不是.Net專家,但在Java SDK中調用getView()方法時,如果視圖沒有退出,則引發異常。也許你有一個類似的方式.Net

+0

REST API是一種替代方法,但後來我失去了對C#API的支持,並且在存在視圖的情況下,它在執行我的查詢中非常有用並且非常有用。 不幸的是,我不能切換到Java,並再次。淨版本,當你調用GetView()方法時,如果視圖不存在,它不會引發異常。 – Kyle

2

我已經創建了一個重載的CouchbaseClient來解決這個問題。它速度快,工作得很好。

編輯:我做了一個小小的改變,我沒有使用string.contains,我使用Json Objects。

public class CouchbaseClientExtended : CouchbaseClient 
{ 
    private string _bucket; 
    private bool _isDevMode; 
    private CouchbaseCluster _cluster; 

    public CouchbaseClientExtended() 
    { 
     LoadBaseConfig("couchbase"); 
    } 

    public CouchbaseClientExtended(CouchbaseClientConfiguration config) : base(config) 
    { 
     LoadBaseConfig(config); 
    } 

    public CouchbaseClientExtended(string sectionName) : base(sectionName) 
    { 
     LoadBaseConfig(sectionName); 
    } 

    public CouchbaseClientExtended(ICouchbaseServerPool pool, CouchbaseClientConfiguration config) : base(pool, config) 
    { 
     LoadBaseConfig(config); 
    } 

    public CouchbaseClientExtended(string bucketName, string bucketPassword) : base(bucketName, bucketPassword) 
    { 
     var config = new CouchbaseClientConfiguration 
     { 
      Bucket = bucketName, 
      BucketPassword = bucketPassword 
     }; 
     LoadBaseConfig(config); 
     _bucket = bucketName; 
    } 

    public CouchbaseClientExtended(CouchbaseClientConfiguration config, string bucketName, string bucketPassword) : base(config, bucketName, bucketPassword) 
    { 
     LoadBaseConfig(config); 
     _bucket = bucketName; 
    } 

    public CouchbaseClientExtended(string sectionName, string bucketName, string bucketPassword) 
     : base(sectionName, bucketName, bucketPassword) 
    { 
     LoadBaseConfig(sectionName); 
     _bucket = bucketName; 
    } 

    private void LoadBaseConfig(string sectionName) 
    { 
     var config = ConfigurationManager.GetSection(sectionName) as CouchbaseClientSection; 
     if (config == null) 
     { 
      throw new NullReferenceException(
       "Couchbase configuration cannot be null. Add it to App.Config file or pass a custom one via parameter."); 
     } 
     _bucket = config.Servers.Bucket; 
     _isDevMode = config.DocumentNameTransformer.Type == typeof (DevelopmentModeNameTransformer); 
     _cluster = new CouchbaseCluster(config); 
    } 

    private void LoadBaseConfig(CouchbaseClientConfiguration config) 
    { 
     _bucket = config.Bucket; 
     _isDevMode = config.DesignDocumentNameTransformer is DevelopmentModeNameTransformer; 
     _cluster = new CouchbaseCluster(config); 
    } 

    public bool StoreJson<T>(StoreMode storeMode, string key, T value) where T : class 
    { 
     return Store(storeMode, key, JsonConvert.SerializeObject(value)); 
    } 

    public T GetJson<T>(string key) where T : class 
    { 
     T obj = null; 
     var json = Get<string>(key); 
     if (json != null) 
     { 
      obj = JsonConvert.DeserializeObject<T>(json); 
     } 
     return obj; 
    } 

    public bool ViewExist(string designName, string viewName) 
    { 
     try 
     { 
      var doc = _cluster.RetrieveDesignDocument(_bucket, designName); 
      if (!VerifyDesignDocumentContainsView(doc, viewName)) 
      { 
       if (_isDevMode) 
       { 
        var devDoc = _cluster.RetrieveDesignDocument(_bucket, "dev_" + designName); 
        return VerifyDesignDocumentContainsView(devDoc, viewName); 
       } 
       return false; 
      } 

      return true; 
     } 
     catch (WebException wex) 
     { 
      if (((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound) 
      { 
       return false; 
      } 
      throw; 
     } 
    } 

    private bool VerifyDesignDocumentContainsView(string doc, string viewName) 
    { 
     if (string.IsNullOrEmpty(doc) || string.IsNullOrEmpty(viewName)) 
     { 
      return false; 
     } 

     JToken token; 
     JObject jObject = JsonConvert.DeserializeObject<JObject>(doc); 
     jObject.TryGetValue("views", out token); 
     if (token != null) 
     { 
      jObject = (JObject)token; 
      return jObject.TryGetValue(viewName, out token); 
     } 
     return false; 
    } 
} 

希望工程的順利嗎:)

+0

哇,真棒男人:) – Kyle

相關問題