~play-developers/play/zdb

« back to all changes in this revision

Viewing changes to src/zdb/core/Store.java

  • Committer: Stéphane
  • Date: 2008-09-17 16:32:58 UTC
  • Revision ID: stephane@sgo-20080917163258-xwgzp1nj2cqszkej
document update + code cleaning

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
import java.util.HashMap;
13
13
import java.util.List;
14
14
import java.util.Map;
15
 
import java.util.TreeMap;
16
15
import java.util.concurrent.TimeUnit;
17
16
import java.util.concurrent.locks.Lock;
18
17
import java.util.concurrent.locks.ReadWriteLock;
31
30
 
32
31
import zdb.client.Document.Index;
33
32
import zdb.exceptions.BucketDoesNotExistException;
34
 
import zdb.exceptions.DocumentAlreadyExistsException;
35
33
import zdb.exceptions.DocumentDoesNotExistException;
36
34
import zdb.exceptions.LockedException;
37
35
import zdb.exceptions.UnexpectedException;
61
59
        return true;
62
60
    }
63
61
 
64
 
    public static void save(String bucket, String id, InputStream data, String mimeType, List<String> indexes, Boolean update) {
 
62
    public static void save(String bucket, String id, InputStream data, String mimeType, List<String> indexes) {
65
63
        assert validID.matcher(id).matches();
66
64
        File bucketDir = getBucket(bucket);
67
65
        String fileName = id;
68
66
        File content = new File(bucketDir, fileName + ".content");
69
67
        File meta = new File(bucketDir, fileName + ".meta");
 
68
        if(!meta.exists() && data == null) { // it is not an update, so data must be given
 
69
                throw new IllegalArgumentException("data can not be null if not an update");
 
70
        }
70
71
        // Meta data
71
72
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
72
73
        try {
88
89
        try {
89
90
            if (lock.tryLock(1, TimeUnit.SECONDS)) {
90
91
                try {
91
 
                    if (update != null) {
92
 
                        if (update) {
93
 
                            if (!meta.exists()) {
94
 
                                throw new DocumentDoesNotExistException(bucket, id);
95
 
                            }
96
 
                        } else {
97
 
                            if (meta.exists()) {
98
 
                                throw new DocumentAlreadyExistsException(bucket, id);
99
 
                            }
100
 
                        }
101
 
                    }
102
92
                    // Add to index
103
93
                    Document document = Indexer.toDocument(id, indexes);
104
94
                    Indexer.storeToIndex(bucketDir, id, document);
 
95
                    
 
96
                    FileOutputStream fos;
 
97
                    
105
98
                    // write content
106
 
                    byte[] buffer = new byte[100 * 1024];
107
 
                    int read = -1;
108
 
                    FileOutputStream fos = new FileOutputStream(content);
109
 
                    while ((read = data.read(buffer)) > 0) {
110
 
                        fos.write(buffer, 0, read);
 
99
                    if(data != null) {
 
100
                            byte[] buffer = new byte[100 * 1024];
 
101
                            int read = -1;
 
102
                            fos = new FileOutputStream(content);
 
103
                            while ((read = data.read(buffer)) > 0) {
 
104
                                fos.write(buffer, 0, read);
 
105
                            }
 
106
                            fos.close();
111
107
                    }
112
 
                    fos.close();
113
108
                    // write meta
114
109
                    fos = new FileOutputStream(meta);
115
110
                    fos.write(metaData);
314
309
                qresult.score = hits.score(i);
315
310
                for (String index : indexes) {
316
311
                    qresult.indexes.add(new zdb.client.Document.Index(index, hits.doc(i).get(index)));
317
 
                    //docIndexes.add(new zdb.client.Document.Index(index, document.get(index)));
318
312
                }
319
 
                //result.put(document.get("_docID"), docIndexes);
320
313
                results.add(qresult);
321
314
            }
322
315
            return results;
351
344
        List<zdb.client.Document.Index> indexes = new ArrayList<zdb.client.Document.Index>();
352
345
        try {
353
346
            Query q = new QueryParser("", new KeywordAnalyzer()).parse("_docID:" + id);
354
 
            File bucketDir = getBucket(bucket);
355
347
            Document doc = Indexer.searchOne(getBucket(bucket), q);
356
348
            List<Field> fields = doc.getFields();
357
349
            for (Field field : fields) {
370
362
        if (!started) {
371
363
            throw new UnexpectedException("ZDB not started");
372
364
        }
373
 
        /*if (!bucket.matches("^[^/_][-_/@a-zA-Z0-9]+[^/]$")) {
374
 
        throw new InvalidNameException("Invalid bucket name : " + bucket);
375
 
        }*/
376
365
        if (buckets.containsKey(bucket)) {
377
366
            return buckets.get(bucket);
378
367
        }